Skip to main content
The storage layer provides persistent state management for the ledger, supporting both in-memory and disk-based storage backends.

ConsensusStorage Trait

The ConsensusStorage trait defines the interface for storage backends.

Storage Components

The consensus storage is divided into four specialized stores:
  1. FinalizeStore: Manages finalize state (mappings, committee, etc.)
  2. BlockStore: Stores blocks, headers, and state roots
  3. TransactionStore: Indexes transactions by various keys
  4. TransitionStore: Indexes transitions, inputs, and outputs

ConsensusStore

The ConsensusStore wraps a ConsensusStorage implementation.

Opening a Store

Example:

Accessing Substores

Storage Modes

The StorageMode enum from aleo-std configures storage behavior.

Development Mode

  • Uses in-memory storage (no disk persistence)
  • Fast setup and teardown
  • Ideal for testing and development
  • The u16 parameter allows multiple independent stores
Use cases:
  • Unit tests
  • Integration tests
  • Temporary chains for testing

Production Mode

  • Uses RocksDB for persistent storage
  • Stores data in the default platform-specific directory:
    • Linux: ~/.aleo/storage/
    • macOS: ~/Library/Application Support/Aleo/storage/
    • Windows: %APPDATA%\Aleo\storage\
  • Optimized for performance and durability
Use cases:
  • Validator nodes
  • Full nodes
  • Production deployments

Custom Mode

  • Uses RocksDB at a custom path
  • Full control over storage location
  • Useful for multi-instance setups
Use cases:
  • Multiple nodes on same machine
  • Custom data directories
  • Containerized deployments

Atomic Operations

The consensus store supports atomic batch operations for consistency.

Starting an Atomic Batch

Begins an atomic write batch across all substores. All subsequent write operations are buffered until finish_atomic is called.

Checking Atomic State

Returns true if an atomic batch is currently in progress.

Checkpointing

Creates a checkpoint within the current atomic batch. You can rewind to the most recent checkpoint using atomic_rewind.

Clearing Checkpoints

Removes the most recent checkpoint. After this, atomic_rewind will rewind to the previous checkpoint (if any).

Rewinding

Reverts all operations since the last checkpoint. The atomic batch remains in progress.

Aborting

Discards all buffered operations and exits atomic mode. No changes are written to storage.

Finishing

Commits all buffered operations to storage atomically. Either all operations succeed, or all are rolled back.

Atomic Batch Example

Atomic Batch Macros

The store provides helper macros for common atomic patterns.

atomic_batch_scope!

Executes a block of operations atomically, handling nested atomic scopes.
Behavior:
  • If not in an atomic batch, starts one and commits on success
  • If already in an atomic batch, creates a checkpoint
  • On error, rewinds or aborts depending on nesting level
  • Returns the result of the closure

atomic_finalize!

Executes finalize operations with support for real and dry runs.
Modes:
  • FinalizeMode::RealRun: Commits changes to storage
  • FinalizeMode::DryRun: Discards changes (for speculation)

FinalizeStore

Manages on-chain state including programs, mappings, and the committee.

Key Components

BlockStore

Stores blocks and manages the block tree.

Key Operations

Block Tree

The block store maintains a Merkle tree of all blocks for efficient state proofs.

TransactionStore

Indexes transactions for efficient lookup.

TransitionStore

Indexes transitions and their inputs/outputs.

Storage Implementations

SnarkVM provides two built-in storage implementations:

MemoryStorage

In-memory storage backed by IndexMap.
Characteristics:
  • Fast read/write operations
  • No disk I/O
  • Data lost on restart
  • Used for Development mode

RocksDBStorage

Persistent storage backed by RocksDB.
Characteristics:
  • Persistent across restarts
  • Optimized for SSD storage
  • Supports atomic operations via write batches
  • Used for Production and Custom modes
RocksDB Features:
  • Point lookups via Bloom filters
  • Range scans via LSM tree
  • Compression (Snappy by default)
  • Background compaction
  • Write-ahead log for durability

Performance Considerations

Block Cache

The block store supports an optional LRU cache for frequently accessed blocks:

Batch Operations

For bulk operations, always use atomic batches to minimize write amplification:

Index Selection

Choose the most efficient index for your query:

Example: Full Storage Workflow

Next Steps