Skip to content
Tutorials Center
My learning

Topics

Lesson 29 of 41 ยท Using Blockchains in Practice

Ethereum in Practice

How Ethereum works day to day: accounts and nonces, the EVM and gas metering, EIP-1559 fees, blocks and logs, and why rollups exist, with live Python models.

Bitcoin is built to move and store value with a deliberately small script language. Ethereum was built as a shared, programmable computer: every node keeps the same state (accounts, balances and contract storage), and every transaction is a request to change that state by running code. You have already modeled the idea in Building a Toy Smart Contract Engine. This lesson shows how the real network organizes it: accounts, the virtual machine, gas, fees, blocks and logs.

As in the Bitcoin lesson, the numbers here are protocol rules. Fee levels change all the time, so the examples use made-up prices to show the formulas.

Accounts: two kinds

Ethereum uses the account model from UTXO vs Account Model. The global state maps each 20-byte address to an account with four fields: nonce, balance, a hash of its storage, and a hash of its code.

Externally owned account (EOA) Contract account
Controlled by A private key (a person's wallet) Its own code
Can start a transaction Yes No, only react to calls
Code and storage None Yes
Nonce counts Transactions it has sent Contracts it has created

Only EOAs can initiate a transaction, because only a private key can sign one. A contract runs when an EOA (or another contract) calls it.

flowchart LR
    E["EOA<br/>(your wallet)"] -->|"signed transaction"| C1["Contract A"]
    C1 -->|"internal call"| C2["Contract B"]
    C2 -->|"internal call"| C3["Token contract"]

A transaction always starts at an EOA; contracts can then call other contracts within the same transaction.

Nonces keep transactions ordered and unique

Each account has a nonce, the number of transactions it has sent. A transaction must carry exactly the account's next nonce. A reused nonce cannot be replayed, a nonce in the future waits until the gap is filled, and sending two transactions with the same nonce means only one can win (which is also how you replace or cancel a stuck one by paying a higher fee).

Python Nonces in action

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The EVM and gas

The Ethereum Virtual Machine (EVM) executes contract code. It is a stack machine with 256-bit words, and its instructions (opcodes) are simple: PUSH, ADD, MUL, SLOAD (read storage), SSTORE (write storage), CALL, LOG and so on. Every node runs the same code on the same input and must reach the same result, which is why the EVM is fully deterministic.

Anyone can submit code, including an infinite loop. The defense is gas: each opcode has a price in gas, a transaction declares a gas limit, and execution stops with an error when the gas runs out. A few representative costs:

Operation Gas (approximate)
Plain ETH transfer (the base cost of any transaction) 21,000
ADD 3
MUL 5
Reading a cold storage slot (SLOAD) 2,100
Writing a new non-zero storage slot (SSTORE) about 20,000 and up
Each non-zero / zero byte of transaction data 16 / 4

Storage writes are expensive because every node must keep that data for years, while arithmetic is nearly free. Gas is the reason why optimized contracts avoid unnecessary storage.

The model below is a tiny EVM-like interpreter with simplified costs. Out-of-gas reverts all state changes but still charges for the gas used, because the nodes did the work.

Python A mini EVM with gas metering

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The first run succeeds and uses 21,000 + 3 + 3 + 3 + 3 + 5 + 3 + 20,000 = 41,020 gas; the second runs out of gas at the storage write, so storage stays empty, yet the sender still pays for all 30,000 gas it allowed.

Fees after EIP-1559

You pay for gas in gwei (1 gwei = 10^-9 ETH = 10^9 wei). Since the 2021 London upgrade (EIP-1559), a fee has two parts:

  • The base fee per gas is set by the protocol for each block. It is burned (destroyed), not paid to anyone.
  • The priority fee (tip) per gas goes to the validator who includes the transaction.

You set a maxFeePerGas (the most you will pay in total per gas) and a maxPriorityFeePerGas (your tip cap). The effective tip is the smaller of your tip cap and maxFee - baseFee. You pay gas_used ร— (baseFee + effective tip); unused gas is not charged.

flowchart LR
    P["Fee paid<br/>gas used x (base fee + tip)"] --> B["Base fee part<br/>burned"]
    P --> T["Tip part<br/>to the validator"]

The base fee disappears from supply; only the tip rewards the block producer.

Python Fee calculator

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Why the base fee moves

The block gas limit is the maximum gas a block may use, and the target is half of it. If a block is fuller than the target, the next base fee rises by up to 12.5 percent; if it is emptier, it falls by up to 12.5 percent. Demand spikes therefore push the price up quickly, and it comes back down when demand fades.

Python Base fee dynamics over 20 blocks

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Blocks and logs

Since the Merge in 2022, Ethereum uses proof of stake (Proof of Stake): time is divided into 12-second slots, and a validator is chosen to propose the block for each slot. A block header commits to three Merkle-Patricia trees (a Merkle tree variant, see Merkle Trees and Proofs of Inclusion): the state root (all accounts after the block), the transactions root and the receipts root. Because the state root is in the header, a light client can verify any balance with a proof.

Each executed transaction produces a receipt with its status, gas used and logs. A log is created by the LOG opcodes, which is what Solidity's emit compiles to. It has up to four topics (indexed fields, with the first topic identifying the event type) and a data field. Logs are much cheaper than storage and cannot be read by contracts, but off-chain tools can search them quickly, which is why indexers rely on them (dApps, Oracles and Wallet Integration). The standard ERC-20 Transfer event, for instance, uses the same first topic on every chain and token, so one filter can find all token transfers.

Scaling with rollups

Every node re-executing every transaction limits throughput. Rollups execute transactions elsewhere in bulk and post compressed data or proofs back to Ethereum, which acts as the settlement and data layer. Since the 2024 upgrade known as Dencun, rollups can use cheaper temporary data space called blobs, which lowers their costs. Rollups are the subject of Scaling: Layer 2, Rollups and Sharding.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Which kind of account can start a transaction on Ethereum?

    A transaction needs a signature, and only an EOA has a private key. Contracts act only when called.
  2. 2. A transaction arrives with a nonce lower than the sender's current nonce. What happens?

    Nonces make each transaction unique and ordered, which prevents replaying an old signed transaction.
  3. 3. Why are storage writes so much more expensive than additions?

    Gas prices reflect the burden on the network. A new storage slot adds permanent state for all nodes to carry.
  4. 4. Under EIP-1559, where does the base fee go?

    The base fee is destroyed. Only the priority fee (tip) is paid to the validator who includes the transaction.
  5. 5. What happens when a transaction runs out of gas halfway through?

    Nodes did the work of executing it, so the fee is charged, but the failed transaction leaves no state changes.

Summary

  • Ethereum keeps a global state of accounts: EOAs controlled by keys and contract accounts controlled by code.
  • Each account has a nonce that orders transactions and blocks replay.
  • The EVM is a deterministic 256-bit stack machine; gas meters every opcode, and storage writes cost the most.
  • A transaction pays gas used times (base fee plus tip); the base fee is burned and adjusts by up to 12.5 percent per block toward a half-full target.
  • Block headers commit to state, transactions and receipts roots; logs are cheap, searchable records of events.
  • Rollups move execution off the base layer and post data back to Ethereum.

Next: Scaling: Layer 2, Rollups and Sharding.