find_last_prefinalized.rs

Overview

This file provides functionality to identify the last prefinalized block within a particular thread in a blockchain-like data structure. It operates by traversing from the last finalized block of a thread and iteratively finding the next block that is prefinalized, ensuring the correct sequence of blocks is maintained. The file primarily exposes two functions:

These functions work with BlockState, BlockStateRepository, and RepositoryImpl abstractions to access and query block states and their relationships.


Functions

find_next_prefinalized

pub(super) fn find_next_prefinalized(
    block_state: &BlockState,
    thread_identifier: &ThreadIdentifier,
    block_state_repository: &BlockStateRepository,
) -> Option<BlockState>

Description

This function attempts to find the next prefinalized block among the children of the given block_state for the specified thread_identifier. It returns the child block that is either finalized or prefinalized but not invalidated.

Parameters

Returns

Behavior and Implementation Details

Usage Example

let next_block = find_next_prefinalized(&current_block_state, &thread_id, &block_state_repo);
if let Some(block) = next_block {
    // process the next prefinalized or finalized block
}

find_last_prefinalized

pub fn find_last_prefinalized(
    thread_identifier: &ThreadIdentifier,
    block_repository: &RepositoryImpl,
    block_state_repository: &BlockStateRepository,
) -> anyhow::Result<BlockState>

Description

Finds the last prefinalized block for a given thread by starting from the last finalized block and iteratively traversing to the next prefinalized block until no further prefinalized block exists.

Parameters

Returns

Behavior and Implementation Details

Usage Example

match find_last_prefinalized(&thread_id, &block_repo, &block_state_repo) {
    Ok(last_prefinalized_block) => {
        // use the last prefinalized block
    }
    Err(e) => {
        // handle error
    }
}

Important Implementation Details and Algorithms


Interaction with Other Components

This file effectively bridges the querying of block states and the navigation logic needed for identifying the last prefinalized block in a thread, enabling higher-level components to operate on finalized and prefinalized block sequences.


Structure Flowchart

flowchart TD
A[find_last_prefinalized] --> B[select_thread_last_finalized_block]
B -->|Some| C[Get last finalized block state]
B -->|None| D[Get root block state and safety check]
C --> E[Loop: find_next_prefinalized]
D --> E
E --> F{find_next_prefinalized returns Some?}
F -->|Yes| G[Update cursor to next prefinalized block]
G --> E
F -->|No| H[Return cursor as last prefinalized block]
subgraph find_next_prefinalized
I[Get known children] --> J[Iterate children]
J --> K{Child is finalized?}
K -->|Yes| L[Return child]
K -->|No| M{Child is prefinalized and not invalidated?}
M -->|Yes| N[Set candidate]
M -->|No| J
J --> O[Return candidate or None]
end
E --> find_next_prefinalized