chain_tracker.rs

Overview

The chain_tracker.rs file provides functionality to identify and analyze gaps in blockchain data sequences, specifically focusing on missing parent blocks within chains. This is crucial for maintaining the integrity of blockchains by detecting incomplete or invalid chains due to missing blocks. The core operations include finding all gaps in a chain of unfinalized blocks and determining the shortest gap that might fix all chains starting from a specified minimum sequence number.

This file interacts primarily with data structures representing blocks and their states — such as BlockState, BlockStateRepository, and UnfinalizedBlocksSnapshot — and uses metrics reporting via BlockProductionMetrics. These components are part of the broader blockchain node and types management subsystems.


Functions

_find_shortest_gap

pub fn _find_shortest_gap(
    thread_identifier: &ThreadIdentifier,
    unprocessed_blocks_cache: &UnfinalizedBlocksSnapshot,
    block_state_repository: &BlockStateRepository,
    min_seq_no: BlockSeqNo,
    metrics: Option<BlockProductionMetrics>,
) -> Option<BlockSeqNo>

Description

Finds the potentially shortest gap in the block sequence that may fix all chains. It analyzes gaps in the block sequence after the given min_seq_no and returns the smallest sequence number representing a missing block that could restore chain continuity.

Parameters

Returns

Usage Example

let shortest_gap = _find_shortest_gap(
    &thread_id,
    &unfinalized_blocks_snapshot,
    &block_state_repo,
    last_finalized_seq_no + 1,
    Some(metrics_collector),
);
if let Some(gap_seq_no) = shortest_gap {
    println!("Shortest missing gap found at sequence number: {}", gap_seq_no);
}

Implementation Details


_find_all_gaps

pub fn _find_all_gaps(
    thread_identifier: &ThreadIdentifier,
    unprocessed_blocks_cache: &UnfinalizedBlocksSnapshot,
    block_state_repository: &BlockStateRepository,
    metrics: Option<BlockProductionMetrics>,
) -> Vec<BlockState>

Description

Identifies all gaps within the specified blockchain thread by finding blocks that have missing parent blocks. Returns a list of BlockState instances representing blocks whose parents are not stored or finalized.

Parameters

Returns

Usage Example

let gaps = _find_all_gaps(
    &thread_id,
    &unfinalized_blocks_snapshot,
    &block_state_repo,
    None,
);
for gap_block in gaps {
    println!("Block with missing parent: {:?}", gap_block);
}

Implementation Details


Important Implementation Details


Interaction with Other System Components


Visual Diagram

flowchart TD
A[Start: _find_shortest_gap] --> B[_find_all_gaps]
B --> C[Filter gaps by thread & missing parent]
C --> D[List of BlockState gaps]
D --> E[Iterate gaps]
E --> F{Is seq_no > min_seq_no?}
F -- Yes --> G[Update shortest gap if smaller]
F -- No --> E
G --> E
E --> H[Return shortest gap or None]
subgraph _find_all_gaps
direction TB
A1[Input: UnfinalizedBlocksSnapshot] --> A2[Iterate blocks]
A2 --> A3[Filter by thread]
A3 --> A4[Check parent block existence]
A4 --> A5[Check parent block is stored?]
A5 -- No --> A6[Collect block as gap]
A5 -- Yes --> A2
A6 --> A2
A2 --> Result[List of gaps]
end

The diagram illustrates the workflow of the two primary functions: _find_shortest_gap calls _find_all_gaps to gather all gaps and then filters for the shortest gap sequence number greater than the specified minimum. The _find_all_gaps process filters blocks by thread, verifies parent block existence, and collects blocks with missing or unstored parents.


References