Understanding the Ethereum Virtual Machine (EVM)
The Ethereum Virtual Machine (EVM) serves as the execution engine for all transactions on the Ethereum blockchain. Here's how it works:
Transaction Processing:
- Transactions are converted into Message objects for EVM execution
- Simple transfers directly modify account balances in StateDB
- Smart contract interactions trigger bytecode execution through EVM interpreters
Key Components:
- StateDB: Maintains global state including account balances
- Bytecode: Compiled smart contract instructions
- Gas: Computational cost metric for operations
Intrinsic Gas: The Base Transaction Cost
Every transaction incurs a base gas cost calculated as:
| Transaction Type | Gas Cost |
|---|---|
| Simple transfer (no payload) | 21,000 |
| Data-carrying transaction | Variable |
Data payload costs:
- 4 gas per zero byte
- 68 gas per non-zero byte
๐ Learn smart contract optimization techniques to reduce gas costs by minimizing non-zero bytes.
Contract Execution Workflow
1. Contract Object Creation
- Message objects transform into Contract instances
- Loads bytecode from StateDB using contract address
- Sets gas limit based on block GasLimit configuration
2. Interpreter Execution
The EVM's stack-based architecture utilizes:
| Component | Function | Limits |
|---|---|---|
| PC | Tracks current instruction | - |
| Stack | 256-bit values | Max 1024 items |
| Memory | Temporary storage | Expandable |
| Gas Pool | Available computation budget | Depletes per operation |
Execution Flow:
- Fetch OpCode (1-byte instruction)
- Retrieve operation from JumpTable
- Calculate gas cost
- Execute instruction (stack/memory/state operations)
Smart Contract Function Calls
Function Identification
Input data contains:
- 4-byte function signature (Keccak hash prefix)
- Parameter data
Example: 0x87db03b7... identifies add(uint256) with parameter 1
Data Loading Instructions
| Instruction | Purpose |
|---|---|
| CALLDATALOAD | Loads input to stack |
| CALLDATACOPY | Copies input to memory |
| CODECOPY | Copies contract code to memory |
| EXTCODECOPY | Copies external contract code |
Contract-to-Contract Interactions
Four call methods exist for inter-contract communication:
| Method | Context Preservation | Storage Modified |
|---|---|---|
| CALL | No | Callee's |
| CALLCODE | Partial | Caller's |
| DELEGATECALL | Full | Caller's |
| STATICCALL | Full | None (read-only) |
๐ Compare call methods in detail
Contract Creation Process
Address Generation:
- Formula:
Keccak(RLP(caller_address, nonce))[12:] - Creates 20-byte contract address
- Formula:
State Initialization:
- Creates stateObject for new address
- Stores immutable bytecode
- Initializes modifiable storage trie
Gas Calculation Mechanics
Gas costs follow Ethereum Yellow Paper specifications, implemented in:
core/vm/gas.gocore/vm/gas_table.go
FAQ: Common EVM Questions
Why does EVM use 256-bit integers?
The 256-bit architecture allows efficient cryptographic operations and future-proofing for complex computations.
How are gas prices determined?
Gas prices reflect computational complexity, storage costs, and network demand. Each opcode has fixed gas costs defined in the protocol.
What happens when a contract runs out of gas?
The transaction reverts with an "Out of Gas" error, but the sender still pays for consumed computation.
Can smart contracts call external APIs?
While EVM cannot directly call external APIs, oracle services can provide external data to contracts.
Key Takeaways
- EVM enables Turing-complete computation on Ethereum
- Gas system prevents resource abuse
- Multiple call methods enable flexible contract interactions