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 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} bytes; in reality, memory expands throughout the execution of a message.

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.

Last updated