Ethereum Source Code Analysis: EVM

·

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:

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:

  1. Records contextual parameters
  2. Creates an interpreter instance (NewEVMInterpreter)
  3. Initializes the jump table based on Ethereum version (Constantinople/Byzantium/Homestead/Frontier instruction sets)

Creating Contracts

Contract creation occurs when:

  1. EVM.Create() generates a contract address using creator address + nonce
  2. EVM.create() executes contract initialization code
  3. Compiled contract bytecode gets stored in StateDB
  4. Constructor execution happens through inserted compiler code

Key aspects:

Calling Contracts

Four call methods exist:

  1. EVM.Call - Standard contract call
  2. EVM.CallCode - Modifies called contract's address (for library contracts)
  3. EVM.DelegateCall - Modifies caller and address (for library contracts)
  4. EVM.StaticCall - Read-only call that reverts on state modifications

The call process:

  1. Loads contract code from StateDB
  2. Uses function selectors to identify methods
  3. Executes via interpreter with input parameters
  4. Handles gas costs and storage operations

Interpreter Object: EVMInterpreter

The EVMInterpreter runs contract instructions by:

  1. Fetching operations from jump table
  2. Validating stack/memory
  3. Calculating gas costs
  4. Executing via operation.execute()

Key components:

Predefined Contracts

Special addresses (1-8) implement built-in functions like:

Gas Consumption

Gas gets deducted for:

Jump Table: vm.Config.JumpTable

Contains 256 operation structs defining:

Four instruction set versions exist with expanding functionality.

Storage Options

Three storage locations:

  1. Stack - LIFO structure for temporary values
  2. Memory - Linear byte array for temporary data
  3. StateDB - Permanent storage in Ethereum state

Auxiliary Objects

Conclusion

The EVM module provides the execution environment for Ethereum smart contracts through:

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?**