mod.rs

Overview

This file implements the core logic for the block finalization process within the blockchain system. It continuously monitors unprocessed candidate blocks, attempts to finalize eligible blocks based on their attestations and parent-child relationships, and updates the system state accordingly. The finalization process ensures that blocks are confirmed and finalized in a consistent order, updating relevant metrics, notifying other system components, and broadcasting finalized blocks.

The primary functionality revolves around the finalization_loop function, which runs continuously, and helper functions such as try_finalize that apply the finalization logic to individual blocks, and on_block_finalized which performs all necessary state updates and side-effects once a block is finalized.

This file interacts closely with:

The overall design follows an event-driven approach, waiting on notifications of new candidate blocks and processing them in a loop until shutdown conditions are met.


Functions and Their Details

finalization_loop

pub fn finalization_loop(
    mut repository: RepositoryImpl,
    block_state_repository: BlockStateRepository,
    mut shared_services: SharedServices,
    mut raw_block_tx: InstrumentedSender<(NodeIdentifier, Vec<u8>)>,
    state_sync_service: impl StateSyncService<Repository = RepositoryImpl>,
    metrics: Option<BlockProductionMetrics>,
    _message_db: MessageDurableStorage,
    node_id: &NodeIdentifier,
    authority: Arc<Mutex<Authority>>,
    unprocessed_blocks_cache: UnfinalizedCandidateBlockCollection,
    last_block_attestations: Arc<Mutex<CollectedAttestations>>,
    chain_pulse_monitor: Sender<ChainPulseEvent>,
    thread_identifier: ThreadIdentifier,
)

Description

Runs the continuous block finalization loop for a specific thread within the blockchain. It listens for updates to the unprocessed blocks cache, attempts to finalize blocks in order, and updates the repository and related services accordingly.

Parameters

Behavior

Usage Example

finalization_loop(
    repository_impl,
    block_state_repo,
    shared_services,
    raw_block_sender,
    state_sync_service_instance,
    Some(block_metrics),
    message_storage,
    &node_identifier,
    authority_arc_mutex,
    unprocessed_blocks_collection,
    last_block_attestations_arc_mutex,
    chain_pulse_event_sender,
    thread_id,
);

try_finalize

fn try_finalize(
    block_state: &BlockState,
    repository: &mut RepositoryImpl,
    block_state_repository: &BlockStateRepository,
    shared_services: &mut SharedServices,
    raw_block_tx: &mut InstrumentedSender<(NodeIdentifier, Vec<u8>)>,
    metrics: &Option<BlockProductionMetrics>,
    node_id: &NodeIdentifier,
    authority: Arc<Mutex<Authority>>,
    state_sync_service: Arc<impl StateSyncService<Repository = RepositoryImpl>>,
    last_block_attestations: Arc<Mutex<CollectedAttestations>>,
    unprocessed_blocks_cache: &UnfinalizedCandidateBlockCollection,
    chain_pulse_monitor: &Sender<ChainPulseEvent>,
) -> anyhow::Result<Option<u64>>

Description

Attempts to finalize a single block if it meets the criteria such as having enough attestations and finalized parent blocks. Performs necessary state updates, notifications, and metrics reporting upon successful finalization.

Parameters

Return Value

Important Implementation Details

Usage Example

let new_cutoff = try_finalize(
    &block_state,
    &mut repository,
    &block_state_repo,
    &mut shared_services,
    &mut raw_block_sender,
    &Some(metrics),
    &node_id,
    authority.clone(),
    state_sync_service.clone(),
    last_block_attestations.clone(),
    &unprocessed_blocks_cache,
    &chain_pulse_monitor,
)?;

on_block_finalized

pub fn on_block_finalized(
    shared_services: &mut SharedServices,
    block: &Envelope<GoshBLS, AckiNackiBlock>,
    repository: &mut RepositoryImpl,
    block_state_repository: &BlockStateRepository,
    raw_block_tx: &mut InstrumentedSender<(NodeIdentifier, Vec<u8>)>,
    state_sync_service: Arc<impl StateSyncService<Repository = RepositoryImpl>>,
    last_block_attestations: Arc<Mutex<CollectedAttestations>>,
) -> anyhow::Result<()>

Description

Performs all necessary operations once a block is finalized:

Parameters

Return Value

Important Implementation Details

Usage Example

on_block_finalized(
    &mut shared_services,
    &finalized_block,
    &mut repository,
    &block_state_repo,
    &mut raw_block_sender,
    state_sync_service.clone(),
    last_block_attestations.clone(),
)?;

Important Implementation Details and Algorithms


Interactions with Other System Components


Mermaid Diagram: Function Relationship Flowchart

flowchart TD
FL(finalization_loop)
TF(try_finalize)
OF(on_block_finalized)
FL -->|iterates over blocks| TF
TF -->|finalizes block| OF
OF -->|updates repository, broadcasts| FL
FL -->|checks shutdown flags| SHUTDOWN[Shutdown Flag Check]
TF -->|checks block state and parent| BS[Block State Repository]
OF -->|marks block finalized| REPO[RepositoryImpl]
OF -->|updates shared services| SHARED[SharedServices]
OF -->|sends via channel| BROADCAST[raw_block_tx]
TF -->|sends finalization events| PULSE[ChainPulseEvent Sender]

This diagram illustrates the flow of the finalization process:


Summary of Key Types Used

For further details on these types, refer to their respective modules and documentation.