The Ethereum Virtual Machine (EVM) is the core computational engine powering the Ethereum blockchain. It is a decentralized, Turing-complete virtual machine that executes smart contracts and processes transactions, enabling the network to function as a global, trustless computing platform.
This article explores the fundamental design principles, architecture, and operational mechanics of the EVM, providing a clear and structured overview for developers, researchers, and blockchain enthusiasts.
Core Concepts of the EVM
Ether and Gas
Ether (ETH) serves as the native cryptocurrency of the Ethereum network. It acts not only as a digital currency but also as a resource to measure and compensate for computational effort. Gas is the unit that measures the amount of computational work required to execute operations, such as transactions or smart contract functions. All transactions and computations consume gas, which is paid for in Ether.
World State
The world state represents the global state of the Ethereum blockchain. It is a mapping between account addresses and their respective states. This data is stored in a modified Merkle Patricia Trie (MPT), which allows for efficient and secure verification of state transitions.
Accounts
There are two types of accounts in Ethereum:
- Externally Owned Accounts (EOAs): Controlled by private keys, these accounts can initiate transactions.
- Contract Accounts: Controlled by their contract code and triggered by transactions or messages. They have associated storage and executable code.
Each account state includes:
- Nonce: A counter that ensures transaction order.
- Balance: The amount of Ether held by the account.
- Storage Root: The root hash of the account's storage trie.
- Code Hash: The hash of the account's EVM bytecode.
Data Structures
Merkle Patricia Trie (MPT)
The MPT is a combination of a Merkle tree and a Patricia trie. It provides a cryptographically authenticated data structure used to store all account states and transaction data. This structure ensures data integrity and enables efficient proofs.
Recursive Length Prefix (RLP)
RLP is the primary encoding method used to serialize objects in Ethereum. It encodes nested arrays of binary data efficiently, ensuring consistent representation of data across the network.
Transaction Execution
Transactions are the fundamental units of operation in Ethereum. They are cryptographically signed instructions sent from an account to the EVM, triggering state changes.
Transaction Structure
A typical transaction includes:
- Nonce: Sequence number issued by the sender.
- Gas Price: Price per unit of gas (in Wei).
- Gas Limit: Maximum gas allocated for the transaction.
- To: Recipient address (zero address for contract creation).
- Value: Amount of Ether to transfer.
- Data: Input data for message calls or contract initialization code.
- v, r, s: Components of the ECDSA signature.
Transaction Fees
Transaction fees consist of:
- Base Fee: A mandatory fee burned by the network.
- Priority Fee: An incentive for miners to include the transaction.
- Execution Fee: Cost of computational steps and memory expansion.
Gas refunds are provided for clearing storage slots, encouraging efficient resource usage.
Pre-Execution Checks
Before execution, transactions must pass validity checks:
- Correct RLP encoding.
- Valid signature.
- Matching nonce.
- Sufficient gas limit.
- Adequate balance for gas costs.
Execution Process
Transaction execution involves:
- Initialization: Loading transaction parameters and environment context.
- Message Call: If the transaction invokes a contract, a message call is executed.
- Gas Accounting: Deducting gas for each operation.
- State Transition: Modifying account balances, storage, and creating logs.
During execution, temporary state changes occur. If the transaction fails, all changes are reverted, ensuring atomicity.
Substate and Logs
Execution generates substate information:
- Self-Destruct Set: Accounts scheduled for deletion.
- Logs: Indexable event records for off-chain tracking.
- Touched Accounts: Accounts interacted with during execution.
- Refunds: Gas refunds for storage operations.
Logs are stored in transaction receipts, which include Bloom filters for efficient querying.
Message Calls
Message calls occur when a contract invokes another contract or external account. They are similar to transactions but are executed internally. Each call has its own execution environment, including:
- Sender: The account initiating the call.
- Value: Ether transferred with the call.
- Input Data: Function parameters.
- Gas: Allocated gas for the call.
Calls can be nested, with a stack depth limit to prevent excessive recursion.
Precompiled Contracts
Ethereum includes several precompiled contracts for common cryptographic operations, such as elliptic curve operations and hash functions. These are optimized for performance and are integral to the network's functionality.
👉 Explore advanced Ethereum development tools
Execution Termination
Transactions can terminate normally or abnormally.
Normal Termination
- RETURN: Returns output data and terminates successfully.
- REVERT: Reverts state changes but returns data and refunds gas.
- STOP: Halts execution without output.
- SELFDESTRUCT: Deletes the contract and transfers remaining Ether.
Abnormal Termination
Caused by:
- Out-of-gas errors.
- Invalid instructions.
- Stack overflows.
- Invalid jump destinations.
- State modification in static calls.
Abnormal termination triggers a full state revert.
Block Formation and Validation
Blocks are batches of transactions validated by network nodes. The block formation process includes:
- Uncle Validation: Validating and including uncle blocks (stale blocks) for security and fairness.
- Transaction Validation: Ensuring all transactions are valid and gas usage matches the header.
- Reward Distribution: Allocating block rewards and uncle incentives.
- State Validation: Verifying the final state root and nonce.
The blockchain is a tree of blocks, with the longest valid chain representing the canonical state.
Frequently Asked Questions
What is the EVM?
The Ethereum Virtual Machine is a decentralized computer that executes smart contracts. It processes transactions and maintains the state of the Ethereum blockchain, ensuring trustless and deterministic execution.
How does gas work in Ethereum?
Gas measures computational effort. Users pay gas fees to execute transactions or contracts. Fees are priced in Ether and compensate miners for resource usage. Complex operations cost more gas.
What is the difference between a transaction and a message call?
Transactions are signed instructions from external accounts, while message calls are internal invocations between contracts. Both can transfer Ether and trigger state changes.
Why are some transactions reverted?
Transactions revert if execution fails due to errors like out-of-gas exceptions or invalid operations. Reverted transactions cancel all state changes but still consume gas for executed steps.
How does the EVM ensure security?
The EVM uses gas limits, stack depth limits, and static checks to prevent abuse. All execution is sandboxed and deterministic, ensuring consistent outcomes across all nodes.
What are precompiled contracts?
Precompiled contracts are built-in functions optimized for common cryptographic tasks. They reduce gas costs and improve performance for essential operations like hashing and signature verification.
Summary
The Ethereum Virtual Machine is a sophisticated runtime environment that enables decentralized applications and smart contracts. Its design emphasizes security, determinism, and efficiency, leveraging concepts like gas accounting, state trees, and atomic transactions. Understanding the EVM's principles is essential for developers building on Ethereum and for users interacting with the network.
For further learning, consult the Ethereum Yellowpaper, community resources, and developer documentation. Practical experience with tools like Remix IDE can deepen your understanding of EVM operations and bytecode execution.