inner_loop.rs

Overview

This file implements the core block validation loop for a blockchain node, processing incoming blocks in a continuous manner. It consumes blocks from a message queue, buffers them, filters out blocks that are already finalized or invalidated, and proceeds to verify the remaining blocks using a comprehensive validation algorithm. Validated blocks trigger acknowledgment messages, while invalid blocks are handled by branch invalidation and negative acknowledgment dispatch.

The main functionality is encapsulated in the inner_loop function, which coordinates the interaction between block state management, blockchain configuration, verification services, and messaging subsystems. This file plays a critical role in maintaining the integrity and correctness of the blockchain by ensuring only valid blocks are accepted and propagated.


Functions and Methods

read_into_buffer

fn read_into_buffer(
    rx: &mut InstrumentedReceiver<(BlockState, Envelope<GoshBLS, AckiNackiBlock>)>,
    buffer: &mut VecDeque<(BlockState, Envelope<GoshBLS, AckiNackiBlock>)>,
) -> bool

Reads all available messages from the receiver rx and appends them to the provided buffer. It continuously tries to receive blocks without blocking and finally blocks only if the buffer is empty. If the channel is disconnected, it returns false to signal termination.


inner_loop

pub(super) fn inner_loop(
    mut rx: InstrumentedReceiver<(BlockState, Envelope<GoshBLS, AckiNackiBlock>)>,
    block_state_repo: BlockStateRepository,
    repository: RepositoryImpl,
    blockchain_config: Arc<BlockchainConfig>,
    node_config: Config,
    mut shared_services: SharedServices,
    send: AckiNackiSend,
    metrics: Option<BlockProductionMetrics>,
    wasm_cache: WasmNodeCache,
    message_db: MessageDurableStorage,
    authority: Arc<Mutex<Authority>>,
)

The primary loop function that continuously processes incoming blocks for validation.


Implementation Details and Algorithms


Interactions with Other System Components


Mermaid Diagram

flowchart TD
A[InstrumentedReceiver] -->|read_into_buffer| B["Buffer (VecDeque)"]
B --> C{Filter Blocks}
C -->|Validated or Finalized| B
C -->|To Validate| D[Validation Loop]
D --> E[Check Parent Block Applied]
E --> F[Retrieve Optimistic State]
F --> G[Get Cross-Thread Reference Data]
G --> H[verify_block Function]
H --> I{VerificationResult}
I -->|ValidBlock| J[Send Ack]
I -->|BadBlock| K[Invalidate Branch & Send Nack]
I -->|TooComplexExecution| L[Log Warning]
J & K & L --> M[Update BlockState Validation Status]
M --> B
M -->|Shutdown Flag| N[Exit Loop]

This diagram illustrates the main workflow inside the inner_loop function, showcasing how blocks are read, filtered, validated, and how results are communicated back to the system. It highlights interaction with the buffer, verification process, state updates, and shutdown handling.