Introduction to the Ethereum Virtual Machine (EVM)
What is the EVM?
The Ethereum Virtual Machine (EVM) is a stack-based computational engine that executes smart contract bytecode on the Ethereum blockchain. It processes instructions (opcodes) sequentially, managing state changes, gas fees, and contract interactions.
Key Features:
- Stack-based architecture: Operands are stored and retrieved via a last-in-first-out (LIFO) stack.
- Deterministic execution: Ensures identical results across all nodes.
- Gas mechanism: Charges fees for computational work to prevent spam.
Smart Contracts Explained
A smart contract is a self-executing program composed of opcodes (e.g., PUSH1, JUMP). When deployed, these contracts run on the EVM, enabling decentralized applications (dApps).
Critical Notes:
- Failed executions revert state changes (except gas consumption).
REVERTopcode refunds unused gas, unlike other failures.
EVM Execution Environment
Core Components
- Code: Immutable bytecode stored on-chain. Accessed via
CODESIZE/CODECOPY. - Program Counter (PC): Tracks the next instruction. Modified by
JUMP/JUMPI. - Stack: Holds 32-byte values for opcode inputs/outputs (max 1024 entries). Managed by
DUP1,SWAP1, etc. - Memory: Volatile byte array for runtime data (cleared post-execution).
- Storage: Persistent key-value store (
SLOAD/SSTORE). - Calldata: Immutable input data (
CALLDATALOAD). - Return Data: Output from calls (
RETURNDATACOPY).
Gas Costs and Optimization
Fee Structure
Base Costs:
- Intrinsic gas: 21,000 gas per transaction (+32,000 for contract creation).
- Calldata: 4 gas per zero byte; 16 gas per non-zero byte.
Dynamic Costs:
- Memory expansion: Quadratic pricing based on accessed offsets.
- Access lists: Cold vs. warm storage slots impact costs post-Berlin hardfork.
Memory Expansion Formula
memory_cost = (memory_size_word² / 512) + (3 × memory_size_word)(Where memory_size_word = (byte_size + 31) / 32)
Advanced Topics
Gas Refunds
Post-London Hardfork:
- Max refund capped at 20% of total gas.
- Only
SSTORE(notSELFDESTRUCT) triggers refunds.
Access Lists
- Warm/Cold States: Addresses/slots accessed repeatedly cost less.
- Pre-warmed: Includes
tx.sender,tx.to, and precompile addresses.
FAQ Section
1. How does the EVM handle transaction reversions?
- Answer: Reverted transactions undo state changes but consume gas (except for
REVERTopcode refunds).
2. What’s the difference between memory and storage?
- Answer: Memory is temporary and cheaper; storage is persistent and costly.
3. Why do gas fees vary?
- Answer: Fees depend on opcode complexity, memory usage, and network demand.
4. How can I reduce gas costs?
- Answer: Optimize calldata, use warm access slots, and minimize storage writes.
👉 Explore EVM opcodes in-depth
👉 Master gas optimization techniques
For further reading, consult the official EVM documentation.