Ethereum Virtual Machine: A Deep Dive into EVM Mechanics

ยท

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:

  1. 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
  2. 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 TypeGas Cost
Simple transfer (no payload)21,000
Data-carrying transactionVariable

Data payload costs:

๐Ÿ‘‰ Learn smart contract optimization techniques to reduce gas costs by minimizing non-zero bytes.

Contract Execution Workflow

1. Contract Object Creation

2. Interpreter Execution

The EVM's stack-based architecture utilizes:

ComponentFunctionLimits
PCTracks current instruction-
Stack256-bit valuesMax 1024 items
MemoryTemporary storageExpandable
Gas PoolAvailable computation budgetDepletes per operation

Execution Flow:

  1. Fetch OpCode (1-byte instruction)
  2. Retrieve operation from JumpTable
  3. Calculate gas cost
  4. Execute instruction (stack/memory/state operations)

Smart Contract Function Calls

Function Identification

Example:
0x87db03b7... identifies add(uint256) with parameter 1

Data Loading Instructions

InstructionPurpose
CALLDATALOADLoads input to stack
CALLDATACOPYCopies input to memory
CODECOPYCopies contract code to memory
EXTCODECOPYCopies external contract code

Contract-to-Contract Interactions

Four call methods exist for inter-contract communication:

MethodContext PreservationStorage Modified
CALLNoCallee's
CALLCODEPartialCaller's
DELEGATECALLFullCaller's
STATICCALLFullNone (read-only)

๐Ÿ‘‰ Compare call methods in detail

Contract Creation Process

  1. Address Generation:

    • Formula: Keccak(RLP(caller_address, nonce))[12:]
    • Creates 20-byte contract address
  2. 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:

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