Purpose and Scope
This document describes the state management system in StableNet that maintains and manages the global state, including all account balances, nonces, contract code, and contract storage.The state management layer provides a caching interface on top of a Merkle Patricia Trie structure, tracks state transitions that occur during transaction execution, and commits state changes at the block level. Related documents:
- Transaction execution and state transitions: Transaction Processing
- Trie database schema: Storage Architecture
- State usage during block execution: Blockchain Management
StateDB Architecture
StateDB is the core interface responsible for accessing and modifying state. It is defined in core/state/statedb.go, provides an in-memory caching layer on top of the account trie, and manages the full lifecycle of state objects representing individual accounts.
Key components:
- trie: the account trie that stores account state
- snaps: snapshot trees for fast state access (optional)
- stateObjects: in-memory cache of account objects (
map[common.Address]*stateObject) - journal: a journal that records state changes for rollback
- originalRoot: the state root at the start of the current block execution
State Objects
AstateObject is an internal structure that represents the state of a single account, including balance, nonce, code, and storage.It is cached in memory and clearly distinguishes between modified (dirty) state and committed state.
StateAccount Structure
Each account’s persistent data is stored in theStateAccount structure defined in core/types/state_account.go:
| Field | Type | Description |
|---|---|---|
Nonce | uint64 | Transaction counter |
Balance | *uint256.Int | Account balance in Wei |
Root | common.Hash | Storage trie root |
CodeHash | []byte | Contract code hash |
Extra | uint64 | StableNet extension field (account policy flags) |
Extra field is a StableNet extension used to represent account policy state defined by the governance layer.
State Object Lifecycle
State objects are managed through the following stages:- Create / Load:
getStateObject()retrieves the object from cache or loads it from the trie or snapshot if absent - Modify: balance, nonce, and storage values are updated in memory during transaction execution
- Finalize: on
Finalise(), dirty state is moved to the pending state - Commit: on
Commit(), changes are written to the trie and persisted to the database
Trie Structure
State is managed using a hierarchical Merkle Patricia Trie structure:- Account Trie: a single trie storing the state of all accounts, keyed by address hash
- Storage Trie: one per contract account, storing contract variable values
| Operation | StateDB Method | Description |
|---|---|---|
| Load account | getStateObject() | Load account state |
| Update account | updateStateObject() | Update account state |
| Delete account | deleteStateObject() | Remove account |
| Load storage | GetState() | Read storage slot |
| Update storage | SetState() | Update storage slot |
Caching Layers
To reduce the cost of state access, a multi-level caching architecture is used:-
StateDB Cache
KeepsstateObjectinstances in memory. It managesdirtyStoragemodified in the current transaction,pendingStoragefinalized but not yet committed, andoriginStoragefor change tracking. -
Snapshot Layer
Provides account and storage data in a flat key–value structure, bypassing trie traversal. This significantly improves read performance and is generated and updated asynchronously. -
Trie Node Cache
Managed by the trie database, separating recently accessed clean nodes and modified (dirty) nodes not yet written to disk. -
Disk Database
The final persistent storage, holding encoded trie nodes and state data.
Journaling and Snapshots
The journal system records all state changes sequentially, enabling transaction-level rollback.If an error occurs during execution, all state changes made by that transaction can be precisely reverted.
Snapshots and Reverts
Snapshot(): creates a rollback checkpoint for the current state and returns a journal indexRevertToSnapshot(): restores all changes made after the specified snapshot in reverse order
- Balance, nonce, and code updates
- Storage slot updates
selfdestructflags- Access list changes
- Log additions
State Commit
State commits are performed at the block level and persist changes to permanent storage.Commit Flow
- Move dirty storage to pending storage
- Clean up empty accounts (EIP-161)
- Called repeatedly after transaction execution
- Compute roots of all storage tries
- Update the account trie
- Return the current state root, used to compute the receipt root
- Commit account and storage tries to disk
- Update the trie database
- Commit snapshots
- Return the final state root
State Change Tracking
With the path-based state scheme (PBSS), the following tracking maps are maintained for efficient state management and pruning:- accounts / storages: accounts and storage entries modified in the current block
- accountsOrigin / storagesOrigin: original values before modification
- stateObjectsDestruct: information about deleted accounts
Database Integration
StateDB integrates with the trie database through theDatabase interface defined in core/state/database.go.Both hash-based and path-based (PBSS) state schemes are supported.
Database Configuration
| Setting | Default | Description |
|---|---|---|
StateScheme | HashScheme | State storage scheme |
TrieCleanLimit | 256 MB | Clean cache size |
TrieDirtyLimit | 256 MB | Dirty cache size |
SnapshotLimit | 256 MB | Snapshot cache size |
Preimages | false | Whether to store trie key preimages |
StateHistory | 0 | Number of blocks to retain state history |
OpenTrie(root)OpenStorageTrie(stateRoot, addrHash, root, trie)ContractCode(addrHash, codeHash)TrieDB()
State Prefetching
State prefetching is an optimization technique that preloads required trie nodes during block execution.It is started with
StartPrefetcher() and stopped with StopPrefetcher().
The prefetcher predicts which accounts and storage slots transactions will access and loads them into cache in advance, reducing disk I/O wait time and improving block processing performance.
Performance Metrics
StateDB collects the following metrics to monitor state management performance:- Account and storage read/write/commit times
- Snapshot lookup and commit times
- Trie database commit times
- Number of modified accounts and storage slots

