Back to glossary

Ethereum Virtual Machine (EVM)

Table of Contents

What is the EVM?

The Ethereum Virtual Machine (EVM) is the stack-based computation engine that processes smart contracts and state changes in the Ethereum network. The EVM is Turing-complete, meaning it can perform any computational operation given sufficient resources. It functions as a decentralized computer that executes bytecode across thousands of distributed nodes, maintaining a consistent state across the entire system.

What is EVM compatibility?

EVM compatibility means a blockchain can run the Ethereum Virtual Machine, execute Ethereum smart contracts, and support Ethereum applications with minimal code modifications. This allows for easier development and deployment of decentralized applications (dApps) across other blockchain networks.

Why is EVM compatibility important?

EVM compatibility connects blockchain networks through shared standards, allowing them to use the same tools, applications, and development practices. Many blockchain platforms have adopted EVM compatibility to work with Ethereum's existing tools and applications, benefiting users and developers.

Users can:

  • Use familiar tools like MetaMask on EVM-compatible networks.
  • Use the same Ethereum address across all EVM chains.
  • Interact with popular protocols like Uniswap on multiple EVM chains.
  • Bridge assets between EVM-compatible networks.

Developers can:

  • Deploy the same smart contracts across EVM chains.
  • Use consistent development tools and practices (such as Foundry, Truffle and Hardhat).
  • Reach users on all EVM-compatible networks.

EVM-compatible platforms include both Layer 2 solutions and L1 blockchains.

Examples of EVM-compatible L2 and L1 solutions

  • Polygon
  • Optimism
  • Arbitrum

Among base layer (L1) EVM-compatible blockchains:

  • Avalanche
  • Binance Smart Chain

How the EVM works

Bytecode execution

Smart contracts must be converted to a format the EVM can understand: bytecode. This conversion process, called compilation, transforms code written in languages like Solidity into simple instructions (opcodes) that the EVM executes. Each node in the Ethereum network runs this bytecode with uniform execution, ensuring consistent results across the network, with each operation consuming a specific amount of gas.

Gas mechanism

The EVM uses gas to measure computational effort and prevent infinite loops. Every operation costs a specific amount of gas, which must be paid in ETH (in wei). This mechanism:

  • Prevents spam and denial-of-service attacks.
  • Compensates nodes for computation by distributing gas fees to validators who process transactions and maintain the network.
  • Halts execution when gas runs out: if a contract enters an infinite loop, it will consume gas until reaching the gas limit, triggering an "out of gas" error and ensuring all programs eventually terminate.

State management

The EVM updates the state of all accounts and smart contracts on Ethereum. This includes:

  • Account balances and nonces
  • Contract code
  • Contract storage
  • Transaction data
  • Block information and chain state

Key components of the EVM

Data storage locations

The EVM utilizes several data storage locations, each serving different purposes:

Read-write locations:

  1. Stack: Temporary storage for small local values.
  2. Memory: Non-persistent memory that lasts during contract execution, byte-addressable and expandable. 
  3. Storage: Persistent storage that remains between transactions, organized as a key-value store where both keys and values are 256-bit words. Contract state variables are stored here.
  4. Transient storage: Temporary key-value store that persists only for the duration of a transaction. Like persistent storage, it uses 256-bit words for both keys and values, but is discarded after the transaction completes.

Read-only locations:

  1. Calldata: Contains function parameters and external call data. The first 4 bytes represent the function selector (method identifier), followed by the encoded parameters. 
  2. Bytecode: The compiled smart contract code itself.

All these data locations use 32-byte (256-bit) words, chosen to optimize Ethereum's cryptographic operations. Gas costs vary significantly among these types, with persistent storage being the most expensive and stack operations being the least.

Opcodes

Opcodes are the fundamental instructions that the EVM reads and executes. Common categories include:

  • Arithmetic operations (ADD, SUB, MUL)
  • Logical operations (AND, OR, NOT)
  • Storage operations (SLOAD, SSTORE)
  • Flow and system operations (JUMP, CALL)
  • Environmental information (CALLER, CALLVALUE)

Smart contract deployment

Smart contracts can be written in high-level EVM-compatible languages, primarily Solidity and Vyper. More specialized languages like Yul exist for low-level optimization. 

When deploying a smart contract:

  1. Contract code is compiled to bytecode
  2. Bytecode is sent in a transaction
  3. The EVM allocates storage and assigns a contract address
  4. The contract becomes accessible on the network

Related Terms

No items found.