chain_pulse_monitor.rs

Overview

This file implements the ChainPulseMonitor, a module responsible for monitoring the activity and health of threads involved in block processing within a blockchain system. It handles timeouts and deadlines for thread operations, detects stalled block producer threads, and interacts with the system authority to respond appropriately to these conditions. The monitor listens for chain pulse events such as block finalizations or thread startups and maintains internal deadlines per thread to track progress.

The core functionality revolves around managing deadlines for thread operations, identifying stalled threads, and triggering authority actions to maintain consensus and block production liveness.


Structs and Types

Deadline

#[derive(TypedBuilder, Clone, Getters)]
pub struct Deadline {
    block_height: Option<BlockHeight>,
    timestamp: Instant,
}

ChainPulseMonitor

pub struct ChainPulseMonitor {
    _handler: JoinHandle<()>,
    monitor: Sender<ChainPulseEvent>,
    stalled_threads: Arc<Mutex<HashSet<ThreadIdentifier>>>,
}

Functions

move_deadline

fn move_deadline(
    deadlines: &mut HashMap<ThreadIdentifier, Deadline>,
    thread_identifier: &ThreadIdentifier,
    next_deadline: Deadline,
)

Main Implementation Detail: bind

pub fn bind(authority: Arc<Mutex<Authority>>) -> ChainPulseMonitor

Event Types Interaction

The monitor reacts to variants of ChainPulseEvent (imported from chain_pulse::events), such as:

These events notify about block processing stages or thread lifecycle changes and influence the deadlines and stalled threads tracking.


Thread Safety and Synchronization


Interaction with Other System Components


Visual Diagram: ChainPulseMonitor Structure and Workflow

flowchart TD
A[ChainPulseMonitor]
A -->|spawns| B[Monitor Thread]
B -->|receives| C["ChainPulseEvent Channel (rx)"]
B -->|updates| D[Deadlines HashMap]
B -->|updates| E[BlockCandidates HashMap]
B -->|updates| F[StalledThreads Set]
F -->|shared| G[Arc<Mutex<HashSet<ThreadIdentifier>>]
B -->|invokes| H[Authority GuardedMut]
C -->|receives events| I[BlockFinalized]
C -->|receives events| J[BlockPrefinalized]
C -->|receives events| K[StartThread]
C -->|receives events| L[BlockApplied]
H -->|handles| M[BlockProducerStalled]
B -->|checks| N[SHUTDOWN_FLAG]

References to Related Topics