The Physics of Ethereum: State, Tries, and Gas

Forget 'World Computer.' Ethereum is a deterministic state machine secured by a Merkle Patricia Trie. Learn how it actually works.

Beginner 50 min read Expert Version →

🎯 What You'll Learn

  • Define Ethereum as a Transaction-Based System Model
  • Deconstruct the Merkle Patricia Trie
  • Explain Gas as a solution to the Halting Problem
  • Differentiate between Storage Root and State Root
  • Trace a State Transition Function (STF)

📚 Prerequisites

Before this lesson, you should understand:

Introduction

Marketing calls Ethereum a “World Computer.” Computer Science calls it a Quasi-Turing Complete State Machine.

“Quasi” because it is bounded by Gas. “State Machine” because it moves from one valid state to another via atomic transactions.

σt+1Υ(σt,T)\sigma_{t+1} \equiv \Upsilon(\sigma_t, T)

Where:

  • σ\sigma is the State (Balances, storage, nonce).
  • TT is the Transaction.
  • Υ\Upsilon is the Ethereum Virtual Machine (EVM).

In this lesson, we stop looking at the “Front-end” of Ethereum and look at the “Hard Drive.”


The Hard Drive: Merkle Patricia Trie

How do you store the balances of 200 million accounts so that any change changes the Global Root Hash, but you don’t have to re-hash the whole database?

Enter the Merkle Patricia Trie (MPT).

The Structure

It is a cryptographic tree where the Path is the address, and the Leaf is the account data.

The Leaf contains exactly 4 fields:

  1. Nonce: Transaction counter.
  2. Balance: Amount of Wei.
  3. StorageRoot: Root hash of the contract’s internal storage MPT.
  4. CodeHash: Hash of the smart contract EVM bytecode.

If you change one bit of storage in a contract:

  1. Its StorageRoot changes.
  2. The Leaf changes.
  3. The Branch hashes change.
  4. The State Root of the block changes.

This is why “Light Clients” work. They verify the Root, not the Data.


The Engine: Gas & The Halting Problem

Alan Turing proved you cannot determine if a program will run forever just by looking at the code (The Halting Problem).

So how do we run untrusted code on a global network without someone writing while(true) and crashing every node?

Solution: Metered Execution (Gas). Every opcode costs a specific amount of physical resource units.

  • ADD: 3 Gas (Cheap CPU cycle).
  • SSTORE: 20,000 Gas (Expensive Disk I/O).

If you run out of Gas, the EVM throws an Out of Gas exception. The state reverts, but you still pay the fees. The miner did the work; they keep the money.


The Account Model: EOA vs. Contract

Ethereum treats humans and robots differently (for now).

1. EOA (Externally Owned Account)

  • Controlled by: A Private Key (kk).
  • Code: None.
  • Privilege: Can initiate transactions.

2. Contract Account

  • Controlled by: Its own code.
  • Code: Yes (Immutable).
  • Privilege: Cannot initiate transactions. Can only react to calls.

Note on Account Abstraction (ERC-4337): This upgrade blurs the line, allowing Contracts to pay their own gas and initiate logic via “UserOperations,” effectively making smart contracts first-class citizens.


Interactive: Reading State

Let’s look at raw state using cast (CLI tool).

# Get the balance (in wei)
cast balance 0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045
> 402301048593920199234

# Get the code hash (Is it a contract?)
cast code 0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045
> 0x (Empty = EOA)

# Get the storage root (Slot 0 of a contact)
cast storage 0xC02aa... (WETH) 0
> 0x...

Practice Exercises

Exercise 1: State Root Calculation (Conceptual)

Scenario: You change the balance of one account in a tree of 1 million accounts. Task: How many hashes does the node need to re-calculate? (Hint: It’s logarithmic, O(logn)O(\log n)).

Exercise 2: The Infinite Loop (Intermediate)

Scenario: You deploy a contract with function loop() { while(true) {} }. Task: You call it with 5 Million Gas. What happens?

  1. Does the transaction finish?
  2. How much ETH do you lose?
  3. What is the state of the contract after?

Exercise 3: Storage Root (Advanced)

Scenario: Two contracts have the exact same code (CodeHash). Task: Can they have different StorageRoots? Why?


Knowledge Check

  1. What are the 4 fields in an Ethereum Account?
  2. Why is the Merkle Patricia Trie used instead of a simple Hash Table?
  3. Does an EOA have a StorageRoot?
  4. What happens to the State Root if a transaction reverts?
  5. Why does SSTORE cost more than ADD?
Answers
  1. Nonce, Balance, StorageRoot, CodeHash.
  2. Cryptographic Proofs. It allows verifying a single value without downloading the whole database (Light Clients).
  3. Technically yes, but it is empty. (Root of an empty tree).
  4. It stays the same. (Or reverts to the state before the tx started).
  5. Physics. ADD is a CPU register operation (nanoseconds). SSTORE is a hard drive write (milliseconds) + blockchain bloat (forever).

Summary

  • Ethereum is a State Machine. Transactions move it from State A to State B.
  • Tries enforce Truth. The State Root summarizes the entire accounting ledger.
  • Gas enforces Time. It prevents infinite loops and spam.

Questions about this lesson? Working on related infrastructure?

Let's discuss