State Trie
The Ethereum State Trie is a data structure used by Ethereum to store and retrieve the current state of the blockchain. It is implemented as a Patricia Trie, similar to the one used for the Ethereum Merkle Patricia Tree, but with some differences in the way it is used.
The State Trie is used to store the current state of all accounts on the blockchain, including account balances, contract code, and contract storage. Each node in the State Trie represents a key-value pair, where the key is the hash of the account address and the value is the account state information.
The State Trie is updated with each new block that is added to the blockchain. When a new block is added, the state changes caused by the transactions in the block are applied to the State Trie, resulting in a new state.
To search for a value in the State Trie, one must follow the path in the trie corresponding to the hash of the account address being searched. Once the correct node is reached, the value associated with that node can be retrieved.
In Ethereum, the State Trie is implemented as a database stored on disk, with the nodes of the trie stored as key-value pairs in the database. This allows for efficient retrieval of the current state of the blockchain, even as it grows larger over time.
Example
In this example, we have a root node with two branches: one pointing to a node with the key aabbcc
, and another pointing to a node with the key eeff11
.
The node with key aabbcc
has two child nodes: one with the key 000000
, and another with the key 112233
. The node with key eeff11
has one child node with the key 445566
.
Each node in the trie represents a key-value pair, where the key is the 20-byte hash of the account address (in hexadecimal), and the value is the account state (nonce, balance, and storage root hash).
Last updated