CS4998: Blockchain Development Textbook
  • CS4998: Blockchain Development
  • Prerequisites
  • Introduction
    • Blockchain Theory
      • Bitcoin and the UTXO Model
      • Ethereum and the State-Based Model
    • Remix - A First Glance
    • Hello World!
      • Solidity File Structure
      • Primitive Values & Types
      • Contract Structure
      • Functions
      • Data Structures
      • Summary & Exercises
    • Hello World! Pt. 2
      • Control Flow
      • Interfaces and Inheritance
      • Constructors
      • Contract Interactions
      • Modifiers
      • Dynamic Arrays and Strings
        • Dynamic Arrays
        • Strings
      • Errors
      • Events
      • Units and Global Variables
      • Default Functions
  • Local Development
    • Node Providers
    • Interacting With On-Chain Contracts
    • Migrating to Foundry & VS Code
      • The Basics of Forge
      • Installing and Using Dependencies
      • Cast
      • Anvil
  • Understanding the EVM
    • The Ethereum Virtual Machine
      • A First Look at Computers
      • The Turing Machine
      • EVM Data Structures
      • Operation Codes (Opcodes)
      • Gas
      • Contract Compilation
      • Contract Runtime
    • Gas Optimizations
  • Yul & Advanced EVM Topics
    • Yul
    • Metamorphism
    • Bitwise Manipulations
  • Correctness
    • Security
    • Types of Testing
  • ERC Standards
    • Why ERCs?
    • ERC20
    • ERC721
    • ERC777
    • ERC1155
  • Frequently Used Smart Contracts
    • OpenZeppelin
    • Uniswap
    • Multisignature Contracts
    • AAVE/Compound
  • MEV & Advanced Blockchain Theory
    • Consensus Mechanisms vs Sybil Resistance Mechanisms
    • Maximal Extractable Value (MEV)
    • Looking Past The EVM
  • Etcetera
    • Developer Practices
    • Spring 2023 Past Resources
Powered by GitBook
On this page
  • Stack
  • Memory
  • Storage
  • Contract Code
  • Program Counter
  • Gas Counter
  1. Understanding the EVM
  2. The Ethereum Virtual Machine

EVM Data Structures

What the EVM Stores

Having viewed both the Cuzco Model and the Turing Machine, we have finally arrived at the main focus of this chapter - learning about the architecture of the EVM. We begin by focusing on the components that define the state of the EVM at the time of execution (i.e. at the time of executing a transaction).

In this section, we will go over the following data structures:

  • Stack

  • Memory

  • Storage

  • Contract Code

  • Program Counter

  • Gas Counter

Bit Terminology

The following terminology is useful when reading about the EVM:

  • 1 byte = 8 bits

  • 32 bytes = 256 bits

  • 1 word = 32 bytes

Stack

Like the stack we previously saw in the Cuzco Model, the stack that the EVM uses is a last-in, first-out data structure. Each stack element is a 32-byte integer and the stack has a maximum depth of 1024 elements.

The stack is local relative to a message; whenever the EVM switches to a different message, the stack does not persist.

Memory

Memory is local relative to a message; whenever the EVM switches to a different message, the stack does not persist.

Storage

Storage is where the state variables of a smart contract lives. For a given smart contract, its account storage is a mapping from 32-byte integers to 32-byte integers. EOAs, meanwhile, do not have any type of storage mapping.

What differentiates storage from memory and the stack is that storage is persistent - an account's storage mapping and any respective modifications to it persist throughout messages, transactions, and, in general, state transitions of the blockchain.

Contract Code

Like all programs, there needs to be a set of instructions that the EVM needs to execute. The contract code (also known as read-only memory (ROM or contract bytecode), is the set of instructions that the EVM executes when said contract is called. EOAs do not have any code associated with it.

As suggested by the term ROM, a smart contract's code is immutable - once initialized, it cannot be changed.

Program Counter

The program counter (PC) tells the EVM what line of the contract bytecode to execute. We will focus more on the PC when we discuss operation codes

Gas Counter

The gas counter tracks how much gas the current context of the EVM has left. If there is not enough gas, the transaction will revert.

PreviousThe Turing MachineNextOperation Codes (Opcodes)

Last updated 1 year ago

Memory in the EVM is a byte-addressed data structure where each memory slot is initialized to 0. In theory, memory has a maximum size of 22562^{256}2256 bytes; in reality, memory expands throughout the execution of a message.