EVM state manager
What is it?
The state manager consists of a Besu node with Shomei plugin and a Shomei node that uses Sparse Merkle trees to represent state.
"State" refers to the data stored on the blockchain at any given point in time. To update state is to update the record of the contents of every account whose contents have changed.
What does it do?
The state manager maintains Linea's state representation optimized for proof generation:
- Tracks state changes block by block
- Generates Merkle proofs for state transitions
- Provides state snapshots for recovery and initialization
- Serves
linea_getProofRPC endpoints
The state manager is the part of the execution client responsible for updating the state of the network globally, and the state of every account individually. It tracks the current state of user accounts, smart contracts, and balances on the Linea Network. The state manager also audits the "read" access made in the EVM, meaning it monitors, verifies, and logs all operations where the EVM needs to read data from the blockchain state.
The main task of the state manager is to receive blocks that have been executed by the sequencer and use the trace data from their execution to update the state of the network.
How does it do it?
Linea uses two data structure types to manage state:
- A Merkle-Patricia Trie to record the world state, maintain consensus, and process blocks. This mirrors how consensus and state are managed on Ethereum Mainnet.
- A variant of a regular Merkle tree called a sparse Merkle tree (SMT), which is used to more efficiently track, manage, and update storage slots representing accounts.
It then passes this updated network state information to the prover in the form of Merkle proofs for submission to Ethereum Mainnet (L1).
Below, we'll explain the element of Linea's state management in greater detail, focussing on the SMT configuration that sets Linea apart.
Merkle trees
The Merkle tree and its variations are commonly used across EVM chains to store and retrieve data about the state of every account on the blockchain.
A Merkle tree is comprised of 'nodes' that branch off from each other. At the base is the 'root', or state root, from which branches stem, and leaves stem from the branches.
Each node, regardless of type, is represented by a cryptographic hash which encodes data about its properties — for example, the contents of your account. Each hash encodes the hashes of its child nodes. Taken to its full extent, this cascading system means the root encodes data of the state of every single account on the blockchain.
Cryptographic hashes are deterministic, which means you can reverse the hash function to get the data which it encoded. If you have the hash of the root—the only node without a parent—you can theoretically derive from it the data of any node in the entire tree.
As a layer 2 (L2) network, Linea is in the business of making transacting faster and more efficient. Linea implements a sparse Merkle tree to track account state and generate and store proofs, and unlock greater efficiency when compared to standard Merkle trees, which require recomputation for every block, leading to excessive computational demands.
Sparse Merkle trees
Linea's state management uses sparse Merkle trees to minimize computation and contribute to the blockchain's efficiency.
A sparse Merkle tree is a variation of a standard Merkle tree where not all leaf nodes are filled with data; instead, data is only stored in nodes where it's needed. It is a complete tree of fixed depth, meaning that all branches of the tree have the same length—i.e. the same number of leaves.
At initialization—the beginning of the chain's history—all leaf nodes are set to a default value, which is typically a hash of a specific value, such as zero. Because all leaf nodes have the same hash value, the parent nodes and higher-level nodes also have the same hash value. A node whose hash is the default value for its level is therefore considered to represent an empty subtree.
In the example above, the children of node A (leaves) contain null values, which means node A does too. Node B, meanwhile, reflects that its children also contain values.
With this construction, we do not need to keep track of every individual node's hash. Instead, we can assume hashes that reflect the default value are empty, and the subtree or node that lies further down the chain of nodes can be disregarded; we only need to pay attention to the ones that correspond to non-empty subtrees.
Cryptographic accumulator
In this context, we can consider Linea's sparse Merkle tree as a type of "cryptographic accumulator". A cryptographic accumulator is a type of cryptographic primitive encoding a collection of items into very short strings and allowing read/write operations to be proven. Merkle trees and sparse Merkle trees are elementary examples of accumulators but there are others with more powerful capabilities.
Linea's state manager uses an extended version of a sparse Merkle tree that enables it to prove all CRUD (create, read, update, delete) operations for a key-addressed database. As an outline, the construction uses a sparse Merkle tree to store the nodes of a sorted doubly-linked list that encodes all the non-zero items of the state.
Linea's state manager uses the accumulator to track the account trie of Linea but also the storage of every contract separately.
The leaves of the tree have the following structure: prev || next || hKey || hVal.
hKey and hVal are the hashes of the key and the value of the stored state entry, respectively.
prev and next are pointers storing the position of the leaves whose hKeys are immediately
lower and higher, respectively, following lexicographic order. The first two leaves of the SMT are
called the head and the tail, and are special in that they do not encode a stored tuple. The head is
the lowest possible hKey, while the tail is the highest possible hKey. They are therefore
situated at the beginning and the end of the linked list, respectively. Starting from the head, we
can access the SMT leaf stored at head.next to get the lowest "actually stored" item. Further
incrementing the next value will give us the second-lowest stored item and so on. Repeating the
process walks us through the entire set of stored items before we end up at the tail node, marking
the final step.
Leaves can also be referred to as storage slots, in that they contain data about the contents of the account in question.