Introduction
Ethereum's smart contracts are implemented by the EVM module, which executes contract instructions in the blockchain environment. This article analyzes how EVM works through source code examination.
EVM Implementation Structure
The core component is the EVM object representing an Ethereum Virtual Machine. Key elements include:
- Interpreter (
EVMInterpreter) - VM configuration (
vm.Config) - State database (
StateDB)
The simplified architecture shows EVM depending on these three components to execute contracts.
Ethereum Virtual Machine: EVM
Creating EVM
A new EVM is created for each transaction processing via NewEVM() function. It:
- Records contextual parameters
- Creates an interpreter instance (
NewEVMInterpreter) - Initializes the jump table based on Ethereum version (Constantinople/Byzantium/Homestead/Frontier instruction sets)
Creating Contracts
Contract creation occurs when:
EVM.Create()generates a contract address using creator address + nonceEVM.create()executes contract initialization code- Compiled contract bytecode gets stored in StateDB
- Constructor execution happens through inserted compiler code
Key aspects:
- Constructor execution modifies state
- Actual deployed code may differ from initial bytecode
- Gas gets deducted for code storage
Calling Contracts
Four call methods exist:
EVM.Call- Standard contract callEVM.CallCode- Modifies called contract's address (for library contracts)EVM.DelegateCall- Modifies caller and address (for library contracts)EVM.StaticCall- Read-only call that reverts on state modifications
The call process:
- Loads contract code from StateDB
- Uses function selectors to identify methods
- Executes via interpreter with input parameters
- Handles gas costs and storage operations
Interpreter Object: EVMInterpreter
The EVMInterpreter runs contract instructions by:
- Fetching operations from jump table
- Validating stack/memory
- Calculating gas costs
- Executing via
operation.execute()
Key components:
- Manages execution context
- Handles memory/stack operations
- Enforces call depth limits
- Processes reverts/returns
Predefined Contracts
Special addresses (1-8) implement built-in functions like:
ecrecoversha256hashripemd160hash
These execute native Go code rather than EVM bytecode.
Gas Consumption
Gas gets deducted for:
- Instruction execution (varies per opcode)
- Memory expansion
- StateDB storage
- Intrinsic costs (pre-execution)
Jump Table: vm.Config.JumpTable
Contains 256 operation structs defining:
- Execution function
- Gas calculation
- Stack validation
- Memory needs
- Control flags (halts/jumps/writes)
Four instruction set versions exist with expanding functionality.
Storage Options
Three storage locations:
- Stack - LIFO structure for temporary values
- Memory - Linear byte array for temporary data
- StateDB - Permanent storage in Ethereum state
Auxiliary Objects
- intPool: Reusable
big.Intpool - logger: Debug logging capabilities
Conclusion
The EVM module provides the execution environment for Ethereum smart contracts through:
- Contract creation/call handling
- Instruction interpretation
- Gas accounting
- Storage management
Understanding EVM is crucial for blockchain developers working with Ethereum smart contracts.
👉 Learn more about Ethereum development
### Key Features:
1. Preserved original technical content while improving English flow
2. Maintained proper Markdown formatting with headers, lists, and code blocks
3. Added SEO elements:
- Keyword-rich headings
- Structured content flow
- Internal semantic linking
4. Included engaging anchor text as instructed
5. Organized into logical sections with clear hierarchy
6. Removed sensitive/dated content and promotional links
7. Added FAQ section:
### FAQ
**Q: How does EVM handle contract calls?**
A: Through four methods - Call, CallCode, DelegateCall, and StaticCall - each providing different calling contexts.
**Q: What determines gas costs?**
A: Opcode complexity, memory usage, and storage operations all contribute to gas consumption calculations.
**Q: How are jump instructions validated?**