The EVM
Suggest an editEvery Ethereum contract you'll ever write runs on the same virtual machine, the Ethereum Virtual Machine, or EVM. It's a deterministic, stack-based computer that exists only as a specification. Every Ethereum node implements it, and every node implementation must produce the same output on the same input.
Every node executes every block and compares the result
Every full node on Ethereum runs every transaction through its EVM. When a new block arrives, the node takes each transaction in order, finds the contract being called, loads that contract's bytecode and storage, and executes the bytecode in its EVM. The result is a set of state changes: balances moved, storage slots written, events emitted, possibly new contracts created.
After applying every transaction in the block, the node computes a hash of the new state, called the state root, and checks whether that root matches the one the block producer included in the block header. If it matches, the node accepts the block. If not, the node rejects it.
If your node and my node disagree about what the EVM did with a particular transaction, our state roots after the block will be different, and we'll end up on different chains. So the EVM has to be deterministic: the same code with the same input and the same state must give the same result on every machine.
Determinism rules out anything whose result could differ between nodes, such as an HTTP request or a read of the system clock. The lesson on determinism went through the list. The EVM has access only to data the protocol explicitly provides: the transaction's input, the contract's storage, the current block's metadata (block.timestamp, block.number, block.coinbase, etc.), and the chain's state at the time the block is being executed.
The EVM is a specification that every client implements
The EVM is not a physical chip. It's a specification of how a particular kind of computer behaves. Every Ethereum client, the software a node runs, includes its own implementation of the EVM in its source code. If two clients disagree about the output of an EVM execution, one of them has a bug.
The specification is open and has been adopted by dozens of other chains. The same bytecode you deploy on Ethereum will run on these EVM-compatible chains, sometimes with small differences in opcode behavior or gas pricing.
Code is a sequence of opcodes that work on a stack
The EVM is stack-based. It has a stack that holds values, and most operations work by pushing values onto the stack, popping them off, and pushing results back on. There are no general-purpose registers.
Each stack slot holds a 256-bit word, or 32 bytes. This is the EVM's native size for everything. A boolean is a 32-byte word with 1 or 0 in the last byte. An address is a 32-byte word with the 20-byte address padded with zeros. Math on smaller integers still computes with all 256 bits, and the high bits are thrown away if they're not needed. The 256-bit size was chosen to match what cryptographic operations like Keccak hashing produce. The price of one word size for everything is that a smaller type is not cheaper to compute with. It saves gas only when several small values are packed into one storage slot, as the lesson on booleans and integers shows.
The code itself is bytecode: a sequence of single-byte instructions called opcodes. Each one tells the EVM to do one specific thing: ADD pops two values from the stack and pushes their sum, MSTORE writes a word to memory, SLOAD reads a value from contract storage, CALL invokes another contract. The Solidity compiler turns your source code into a sequence of these opcodes, which the EVM then walks one at a time.
To make this concrete, consider one line of Solidity:
uint256 c = a + b;If a and b are local variables, the compiler emits something close to this bytecode sequence:
PUSH1 0x02 // push the value of a (say, 2) onto the stack
PUSH1 0x03 // push the value of b (say, 3) onto the stack
ADD // pop the top two values, push their sum (5)
// c is the 5 now on top of the stackOne line of source became three opcodes, with a total gas cost of about 9. By default Solidity 0.8 also wraps the ADD in an overflow check, which the booleans and integers lesson covers, so the real sequence is longer. The stack started empty and ended with one value on it. Every Solidity statement decomposes into a small handful of stack operations that push, pop, and combine values.
Every opcode has a gas cost, and the EVM itself increments a running counter as it walks through them. When the next opcode would push the counter past the transaction's gas limit, execution halts and the transaction reverts.
Storage is permanent, the other three data areas last one call
During execution, data lives in one of four places. Each has different cost, lifetime, and access rules.
The stack is where the EVM does its work, as in the example above. Solidity manages the stack for you implicitly. You don't write stack operations by hand unless you drop into inline assembly.
Memory is a contiguous, byte-addressable buffer that lives only for the duration of a single call. When a transaction triggers a contract, memory starts empty. When the call ends, memory is gone. Solidity uses memory to hold the runtime representations of structs, arrays, and strings that don't need to persist. Memory is cheap for small amounts and gets expensive as you use more of it, because the protocol charges quadratically as it grows.
Storage is the contract's persistent state. It survives between calls and between blocks, unless someone explicitly overwrites it or the contract is destroyed. Storage is a mapping from 256-bit keys to 256-bit values, owned by the contract. Every state variable you declare in Solidity goes here unless you say otherwise. Storage is by a wide margin the most expensive place to put data, because every full node has to hold it. The lesson on contracts and gas has the numbers.
Calldata is the input data sent with the transaction. It's read-only from inside the contract: the contract can look at it but can't modify it. Calldata is the cheapest place to read function arguments from, which is why Solidity functions that take large inputs like long arrays usually take them as calldata rather than copying them into memory first.
Each call gets a fresh context, and a revert restores the state
Every time a contract is called, the EVM creates a fresh execution context: a new empty stack, a new empty memory buffer, and the call's arguments in calldata. The context also includes a small set of values that describe the call itself. Solidity exposes three of them: msg.sender is the address that made this call, msg.value is the ETH attached to the call, and msg.data is the raw calldata bytes. These exist for the lifetime of one call. If contract A calls contract B, B sees A as msg.sender, not the EOA that started the transaction. If B then calls C, C sees B as its msg.sender.
Storage works differently from the other three data areas. Each contract has its own storage, and the EVM only lets the currently executing code read or write that contract's storage. When A calls B, the executing code is B's, so storage operations touch B's slots. When B returns and execution resumes in A, storage operations touch A's slots again. The other three areas, stack and memory and calldata, are local to a single call and get destroyed when the call ends.
Reverts work at this same call-boundary level. When a contract starts executing, the EVM takes a logical snapshot of the chain state. If the call completes normally, the state changes are committed. If anything in the call reverts, the EVM restores the snapshot, discarding every state change made during that call. The atomicity property from the previous lesson rests on this snapshot-and-restore mechanism, which is built into the EVM itself.
Ask where each variable lives and who pays for it
For every variable in a contract, ask where it lives and who pays for its life. A value on the stack or in memory is gone when the call ends. A state variable sits in storage on every full node until someone overwrites it, and whoever sends a transaction that writes it pays for that, at the highest gas cost of the four data areas.