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.
🎯 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.
Where:
- is the State (Balances, storage, nonce).
- is the Transaction.
- 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:
- Nonce: Transaction counter.
- Balance: Amount of Wei.
- StorageRoot: Root hash of the contract’s internal storage MPT.
- CodeHash: Hash of the smart contract EVM bytecode.
If you change one bit of storage in a contract:
- Its StorageRoot changes.
- The Leaf changes.
- The Branch hashes change.
- 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 ().
- 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, ).
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?
- Does the transaction finish?
- How much ETH do you lose?
- 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
- What are the 4 fields in an Ethereum Account?
- Why is the Merkle Patricia Trie used instead of a simple Hash Table?
- Does an EOA have a StorageRoot?
- What happens to the State Root if a transaction reverts?
- Why does
SSTOREcost more thanADD?
Answers
- Nonce, Balance, StorageRoot, CodeHash.
- Cryptographic Proofs. It allows verifying a single value without downloading the whole database (Light Clients).
- Technically yes, but it is empty. (Root of an empty tree).
- It stays the same. (Or reverts to the state before the tx started).
- Physics.
ADDis a CPU register operation (nanoseconds).SSTOREis 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