ConsensusStorage Trait
TheConsensusStorage trait defines the interface for storage backends.
Storage Components
The consensus storage is divided into four specialized stores:- FinalizeStore: Manages finalize state (mappings, committee, etc.)
- BlockStore: Stores blocks, headers, and state roots
- TransactionStore: Indexes transactions by various keys
- TransitionStore: Indexes transitions, inputs, and outputs
ConsensusStore
TheConsensusStore wraps a ConsensusStorage implementation.
Opening a Store
Accessing Substores
Storage Modes
TheStorageMode 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
u16parameter allows multiple independent stores
- 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\
- Linux:
- Optimized for performance and durability
- Validator nodes
- Full nodes
- Production deployments
Custom Mode
- Uses RocksDB at a custom path
- Full control over storage location
- Useful for multi-instance setups
- 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
finish_atomic is called.
Checking Atomic State
true if an atomic batch is currently in progress.
Checkpointing
atomic_rewind.
Clearing Checkpoints
atomic_rewind will rewind to the previous checkpoint (if any).
Rewinding
Aborting
Finishing
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.
- 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.
FinalizeMode::RealRun: Commits changes to storageFinalizeMode::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 byIndexMap.
- Fast read/write operations
- No disk I/O
- Data lost on restart
- Used for Development mode
RocksDBStorage
Persistent storage backed by RocksDB.- Persistent across restarts
- Optimized for SSD storage
- Supports atomic operations via write batches
- Used for Production and Custom modes
- 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
- Ledger Overview - Main ledger operations
- Block Types - Block and transaction structures
- Query Operations - State querying interface