30 Jun 2024
Ethereum Virtual Machine
This is a personal note of EVM, resources are from:
1. Terminology
- EVM: a decentralized virtual environment that executes code consistently and securely across all Ethereum nodes.
- Gas: used to measure the computational effort required to execute smart contracts.
- Ether(ETH): the native cryptocurrency in Ethereum; used to incentivize computation.
- Wei: the smallest subdenomination of Ether; 1 Ether = \(10^{18}\) Wei.
- State: A modified Merkle Patricia Trie to keep all accounts linked by hashes and reducible to a single root hash stored on the blockchain.
- State transition function:
Y(S, T)=S'
: produces a deterministic new valid state (S'
) given an old valid state (S
) new set of valid transactions (T
) - Transactions: signed instructions from accounts, includes
- a contraction creation to create a new contract account containing compiled contract bytecode, or
- a message call to a contract to execute the bytecode.
- Proof of work: a spam deterrence mechanism; demonstrate the potential for a basic data channel to carry a strong economic signal without relying on trust.
- Fork: a disagreement between nodes as to which root-to-leaf path down the block tree is the best blockchain.
- Chain Id (\(\beta\)): distinguish between diverged blockchains (EIP-155).
1.1. Motivation
- To facilitate transactions between individuals who would otherwise have no means to trust one another.
- To enforce a rich and unambiguous agreement autonomously (crypto-lay system).
1.2. Blockchain paradigm
\begin{aligned}
\sigma_{t+1} & \equiv \Pi\left(\boldsymbol{\sigma}_t, B\right) \\
B & \equiv\left(\ldots,\left(T_0, T_1, \ldots\right), \ldots\right) \\
\Pi(\boldsymbol{\sigma}, B) & \equiv \Upsilon\left(\Upsilon\left(\boldsymbol{\sigma}, T_0\right), T_1\right) \ldots
\end{aligned}
- \(\sigma\): a valid state between two transactions.
- \(B\): a block including a series of transactions.
- \(\Upsilon\): the Ethereum state transition function.
- \(\Pi\): the block-level state transition function.
1.3. Ethereum Transaction execution
- An user (externally owned accounts, EOA) signs a transaction, including the sender, receiver (the contract address), Ether value, Gas limit and Gas price.
- The transaction is broadcast to the Ethereum network.
- Once a validator receives the transaction, it first performs sanity check, e.g., signature validation, balance check.
- Upon passing the validation, a transaction is included in a block and executed.
- Initialization: PC set to the start of the contract code; Gas limit; empty stack, memory; contract state trie loaded to the storage.
- Execution: locally executes each bytecode and modifies stack (
PUSH
), memory (MSTORE
) and storage (SSTORE
); modifies the global state tree (CALL
). - Abortion: if the gas is used up, all state changes during the execution are reverted.
- After the execution is finished, the validator assembles the block and proposes the new block.
- If a consensus is reached, the block is appended to the blockchain, and other nodes verify the block and update their global states accordingly.
1.4. World state \(\sigma\)
- A mapping between 160-bit addresses and account states, maintained in a modified Merkle Patricia tree (MPT), serialized as RLP, stored in a off-chain database backend.
- MPT benefits: the root node depends on all internal data; allows any previous state with known root hash to be recalled as the tree is immutable.
- Merkle Patricia Trie representation of state data across blocks
1.4.1. The account state \(\sigma[a]\)
nonce
: the number of transactions the address has sent, or the number of contracts the address has made.balance
: the number of Wei owned by the address.storageRoot
: a 256-bit hash of the root node of a MPT which encodes the account storage, i.e., the contract storage.codeHash
: the hash of the EVM code, i.e., the contract bytecode, which is executed if the address receives a message call.