Skip to main content
The 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

The Ledger<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.
Parameters:
  • genesis_block - The genesis block to verify against
  • storage_mode - Storage configuration (see Storage Modes)
Returns:
  • Result<Ledger<N, C>> - The loaded ledger or an error
Behavior:
  1. Initializes the consensus store with the given storage mode
  2. Creates a new VM from the store
  3. Verifies the genesis block hash matches storage
  4. Spot-checks up to 10 random blocks for integrity
  5. Loads the current committee, block, and epoch state
Example:

Ledger::load_unchecked

Loads the ledger without performing integrity checks.
This method skips the random block verification and is faster for trusted environments.

Advancing the Ledger

advance_to_next_block

Adds the next block to the ledger.
Thread Safety: This method is atomic and thread-safe. Only one advancement can occur at a time, and no reads execute during advancement. Parameters:
  • block - The next block to add (must be height + 1)
Side Effects:
  1. Acquires write lock on current_block
  2. Validates block height is sequential
  3. Calls VM::add_next_block to update storage
  4. Updates current block, committee, and epoch state
  5. Clears or updates epoch provers cache
Example:

prepare_advance_to_next_quorum_block

Prepares a candidate quorum block using a committed subdag.
Parameters:
  • subdag - The committed subdag from consensus
  • transmissions - Solutions and transactions from the subdag
  • rng - Random number generator
Returns: A candidate block that can be passed to advance_to_next_block

prepare_advance_to_next_beacon_block

Prepares a candidate beacon block (for testing).
Note: Beacon blocks are only used for testing. Production uses quorum blocks.

Querying State

The ledger provides extensive query methods:

Latest State

Historical Access

Storage Modes

The ledger supports multiple storage backends configured via StorageMode:
Modes:
  • Development(id): In-memory storage, useful for testing. The id allows 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:
Checkpoints use hard links and can serve as incremental backups or full rollback points.

Block Tree Caching (RocksDB only)

Cache the block tree to disk for faster startup:
The block tree is automatically cached on ledger drop for clean shutdowns.

Next Steps