snarkvm-ledger crate is the central blockchain state manager in SnarkVM. It maintains the entire chain state, processes blocks and transactions, and provides querying capabilities.
Architecture
The ledger is composed of several key modules:- Block: Block, Header, Transaction, and Transition types
- Store: Persistent storage layer with consensus store
- Query: Query trait for state access
- Authority: Block authority (Beacon or Quorum)
- Committee: Validator committee management
- Narwhal: Consensus protocol implementation
- Puzzle: Proof-of-work puzzle for coinbase
Core Type: Ledger
TheLedger<N, C> type is the main entry point for blockchain state management.
Type Parameters
N: Network- The network type (e.g., MainnetV0)C: ConsensusStorage<N>- The storage backend implementation
Inner Structure
The ledger maintains:- VM state: The underlying virtual machine with program execution
- Genesis block: The initial block of the chain
- Current committee: The active validator committee
- Current block: Latest added block (cached)
- Current epoch hash: Hash for the current epoch
- Committee cache: LRU cache of recent committees (size: 16)
- Epoch provers cache: Solution counts per prover in current epoch
Loading the Ledger
Ledger::load
Loads the ledger from storage with integrity checks.
genesis_block- The genesis block to verify againststorage_mode- Storage configuration (see Storage Modes)
Result<Ledger<N, C>>- The loaded ledger or an error
- Initializes the consensus store with the given storage mode
- Creates a new VM from the store
- Verifies the genesis block hash matches storage
- Spot-checks up to 10 random blocks for integrity
- Loads the current committee, block, and epoch state
Ledger::load_unchecked
Loads the ledger without performing integrity checks.
Advancing the Ledger
advance_to_next_block
Adds the next block to the ledger.
block- The next block to add (must be height + 1)
- Acquires write lock on
current_block - Validates block height is sequential
- Calls
VM::add_next_blockto update storage - Updates current block, committee, and epoch state
- Clears or updates epoch provers cache
prepare_advance_to_next_quorum_block
Prepares a candidate quorum block using a committed subdag.
subdag- The committed subdag from consensustransmissions- Solutions and transactions from the subdagrng- Random number generator
advance_to_next_block
prepare_advance_to_next_beacon_block
Prepares a candidate beacon block (for testing).
Querying State
The ledger provides extensive query methods:Latest State
Historical Access
Storage Modes
The ledger supports multiple storage backends configured viaStorageMode:
Development(id): In-memory storage, useful for testing. Theidallows multiple independent ledgers.Production: Persistent RocksDB storage in the default data directory.Custom(path): Persistent RocksDB storage at a custom path.
Transaction Creation
The ledger provides convenience methods for creating common transactions:create_deploy
Creates a program deployment transaction.
create_transfer
Creates a private-to-private transfer transaction.
VM Access
Access the underlying VM for program operations:Caching
The ledger maintains several caches for performance:- Committee Cache: LRU cache of 16 recent committees by round
- Epoch Provers Cache: Tracks solution counts per prover in the current epoch
- Block Cache: Optional block cache in the block store (configurable size)
Database Operations
Backup (RocksDB only)
Create a checkpoint of the ledger database:Block Tree Caching (RocksDB only)
Cache the block tree to disk for faster startup:Next Steps
- Block Types - Block, Header, Transaction, and Transition structures
- Storage - Storage layer and consensus store
- Query Operations - Query trait and state access