mod.rs

Overview

This file implements the core logic for the ChainPulse component, which orchestrates the periodic processing and evaluation of blockchain state updates related to block attestations and candidate blocks within a specific thread of a blockchain network. The ChainPulse structure encapsulates two main submodules: pulse_attestations and pulse_candidates, which handle attestation management and candidate block processing, respectively.

The file defines configuration settings via the ChainPulseSettings builder, allowing for fine-grained control over timing, retries, and block gap parameters crucial for consensus and block finalization workflows.

Detailed Explanation of Components

ChainPulseSettings Struct

A typed builder struct that configures parameters required to instantiate a ChainPulse object. This struct includes:

Usage Example

let chain_pulse = ChainPulse::builder()
    .node_id(my_node_id)
    .thread_identifier(my_thread_id)
    .direct_send_tx(my_direct_sender)
    .broadcast_send_tx(my_broadcast_sender)
    .retry_request_missing_block_timeout(Duration::from_secs(30))
    .resend_timeout_blocks_stuck(Duration::from_secs(60))
    .resend_timeout_blocks_stuck_extra_offset_per_candidate(Duration::from_secs(10))
    .missing_blocks_were_requested(Arc::new(AtomicBool::new(false)))
    .finalization_stopped_time_trigger(Duration::from_secs(120))
    .finalization_has_not_started_time_trigger(Duration::from_secs(300))
    .block_gap(my_block_gap)
    .build();

ChainPulse Struct

The main struct managing the chain pulse process. It contains:

impl From<ChainPulseSettings> for ChainPulse

This conversion consumes ChainPulseSettings and initializes the attestations and candidates submodules accordingly by passing relevant configuration parameters.

impl ChainPulse

fn builder() -> ChainPulseBuilder

Returns a builder instance (ChainPulseSettings::builder()) for constructing a ChainPulse object with custom settings.

fn pulse(&mut self, last_finalized_block: &BlockState) -> anyhow::Result<()>

Triggers a pulse operation based on the new finalized block. The method performs:

Parameters:

Returns:

Usage Example:

chain_pulse.pulse(&new_finalized_block)?;

fn evaluate(&mut self, candidates: &UnfinalizedBlocksSnapshot, block_state_repository: &BlockStateRepository, block_repository: &RepositoryImpl) -> anyhow::Result<()>

Evaluates the current state of unfinalized blocks and updates pulse submodules accordingly:

Parameters:

Returns:

Usage Example:

chain_pulse.evaluate(&unfinalized_snapshot, &block_state_repo, &block_repo)?;

Implementation Details and Algorithms

Interaction with Other System Components

Mermaid Diagram

classDiagram
class ChainPulseSettings {
- node_id: NodeIdentifier
- thread_identifier: ThreadIdentifier
- direct_send_tx: NetDirectSender
- broadcast_send_tx: NetBroadcastSender
- trigger_attestation_resend_by_time: Option<Duration>
- trigger_attestation_resend_by_chain_length: Option<usize>
- retry_request_missing_block_timeout: Duration
- resend_timeout_blocks_stuck: Duration
- resend_timeout_blocks_stuck_extra_offset_per_candidate: Duration
- missing_blocks_were_requested: Arc<AtomicBool>
- trigger_increase_block_gap: Option<Duration>
- finalization_stopped_time_trigger: Duration
- finalization_has_not_started_time_trigger: Duration
- block_gap: BlockGap
- last_finalized_block: Option<BlockState>
+ build()
}
class ChainPulse {
- attestations: PulseAttestations
- candidates: PulseCandidateBlocks
- last_finalized_block: Option<BlockState>
+ builder()
+ pulse()
+ evaluate()
}
ChainPulseSettings --> ChainPulse : from(settings)
ChainPulse *-- PulseAttestations : contains
ChainPulse *-- PulseCandidateBlocks : contains

This diagram illustrates the primary data structures, their key fields, and the relationships between ChainPulseSettings, ChainPulse, and its submodules. The builder pattern constructs ChainPulse from ChainPulseSettings, while ChainPulse composes PulseAttestations and PulseCandidateBlocks components.