Chenyo's org-static-blog

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).
?url=%2Fcontent%2Fdevelopers%2Fdocs%2Fevm%2Fevm.png&w=1920&q=75
Figure 1: The EVM structure

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

  1. An user (externally owned accounts, EOA) signs a transaction, including the sender, receiver (the contract address), Ether value, Gas limit and Gas price.
  2. The transaction is broadcast to the Ethereum network.
  3. Once a validator receives the transaction, it first performs sanity check, e.g., signature validation, balance check.
  4. Upon passing the validation, a transaction is included in a block and executed.
    1. Initialization: PC set to the start of the contract code; Gas limit; empty stack, memory; contract state trie loaded to the storage.
    2. Execution: locally executes each bytecode and modifies stack (PUSH), memory (MSTORE) and storage (SSTORE); modifies the global state tree (CALL).
    3. Abortion: if the gas is used up, all state changes during the execution are reverted.
  5. After the execution is finished, the validator assembles the block and proposes the new block.
  6. 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.
Tags: evm