load_saved_blocks.rs

Overview

This file defines functionality for loading saved blockchain blocks from persistent storage into memory, categorizing them by thread identifier and their finalization status. It provides an implementation of the SavedBlocksLoader trait for the repository struct RepositoryImpl, allowing the system to reconstruct the in-memory representation of blocks after a restart or system recovery. The blocks are loaded along with their associated block states from disk, filtered based on finalization and recency criteria, and organized into a map keyed by thread identifiers.

The logic ensures that only relevant blocks are retained in memory, avoiding stale finalized blocks that fall outside of a configurable cache window. This supports efficient block management and retrieval in the broader blockchain system.


Detailed Description

Trait: SavedBlocksLoader

Defines the interface for loading saved blocks into memory.

Method: load_saved_blocks

fn load_saved_blocks(
    &mut self,
    block_state_repository: &BlockStateRepository,
) -> anyhow::Result<
    HashMap<ThreadIdentifier, Vec<(BlockState, Arc<Envelope<GoshBLS, AckiNackiBlock>>)>>,
>;
let mut repo = RepositoryImpl::new(...);
let block_state_repo = BlockStateRepository::new(...);
let saved_blocks = repo.load_saved_blocks(&block_state_repo)?;
for (thread_id, blocks) in saved_blocks {
    println!("Thread {:?} has {} restored blocks", thread_id, blocks.len());
}

Implementation: SavedBlocksLoader for RepositoryImpl

The core logic resides in the load_saved_blocks method implementation for RepositoryImpl.

Key Implementation Details:


Interaction with Other Components

This file thus plays a critical role in the persistence layer of the blockchain system, bridging between the on-disk representation of blocks and their in-memory runtime state.


Mermaid Diagram

flowchart TD
A[RepositoryImpl] -->|calls| B["load_saved_blocks()"]
B --> C["get_blocks_dir_path()"]
B --> D["read_dir(blocks_dir)"]
D --> E[for each file]
E --> F[parse BlockIdentifier]
F --> G["load_block(blocks_dir, block_id)"]
G --> H["block_state_repository.get(block_id)"]
H --> I{block_state finalized?}
I -->|Yes| J{block valid and recent?}
J -->|Yes| K["finalized_blocks_mut().store()"]
J -->|No| L[skip block]
I -->|No| M{block_state has thread_id?}
M -->|Yes| N[add to result map]
M -->|No| L

This flowchart illustrates the flow of the load_saved_blocks method within RepositoryImpl, showing how blocks are loaded, filtered, and categorized before being stored or returned.