send.rs

Overview

This file implements network communication functions and methods related to sending and broadcasting critical blockchain synchronization messages within a node. It primarily deals with requesting blocks, broadcasting node joining events, synchronization finalization, and acknowledgments among nodes in a distributed network. The interactions involve direct messaging to specific nodes or broadcasting messages to all nodes in the network.

The file defines standalone functions for sending specific types of messages and provides implementations of these functionalities as methods on the generic Node struct. The Node struct methods utilize various network channels (NetDirectSender and NetBroadcastSender) to communicate with other nodes. Error handling is performed carefully to handle shutdown scenarios gracefully.


Functions

send_blocks_range_request

pub fn send_blocks_range_request(
    network_direct_tx: &NetDirectSender<NodeIdentifier, NetworkMessage>,
    destination_node_id: NodeIdentifier,
    requester: NodeIdentifier,
    thread_id: ThreadIdentifier,
    inclusive_from: BlockSeqNo,
    exclusive_to: BlockSeqNo,
    at_least_n_blocks: Option<usize>,
) -> anyhow::Result<()>

Sends a direct network message to a specific node requesting a range of blocks in a given thread.

send_blocks_range_request(
    &network_direct_tx,
    destination_node_id,
    requester_node_id,
    thread_id,
    100,
    200,
    Some(50),
)?;

This function logs the request details and sends a NetworkMessage::BlockRequest containing the range and requester information.


broadcast_node_joining

pub(crate) fn broadcast_node_joining(
    network_broadcast_tx: &NetBroadcastSender<NetworkMessage>,
    metrics: Option<&BlockProductionMetrics>,
    node_id: NodeIdentifier,
    thread_id: ThreadIdentifier,
) -> anyhow::Result<()>

Broadcasts a message to all nodes indicating that a new node is joining a specific thread.

broadcast_node_joining(
    &network_broadcast_tx,
    Some(&metrics),
    joining_node_id,
    thread_id,
)?;

It logs the broadcast event and reports metrics if provided.


Node Struct Methods

The methods defined on the generic Node<TStateSyncService, TRandomGenerator> struct provide convenient wrappers and additional logic for sending and broadcasting network messages related to block synchronization and node coordination.

Generic Constraints


broadcast_node_joining

pub(crate) fn broadcast_node_joining(&self) -> anyhow::Result<()>

Broadcasts this node's joining event using the node's internal broadcast sender and metrics.

node.broadcast_node_joining()?;

This method calls the standalone broadcast_node_joining function with the node's context.


send_block_request

pub(crate) fn send_block_request(
    &self,
    node_id: NodeIdentifier,
    included_from: BlockSeqNo,
    excluded_to: BlockSeqNo,
    at_least_n_blocks: Option<usize>,
) -> anyhow::Result<()>

Requests a range of blocks from a specified node. Reports metrics for the number of blocks requested.

node.send_block_request(node_id, 50, 100, Some(10))?;

broadcast_sync_finalized

pub(crate) fn broadcast_sync_finalized(
    &self,
    block_identifier: BlockIdentifier,
    block_seq_no: BlockSeqNo,
    shared_res_address: HashMap<ThreadIdentifier, BlockIdentifier>,
) -> anyhow::Result<()>

Broadcasts a signed SyncFinalized message indicating finalized state synchronization of a block.

node.broadcast_sync_finalized(block_id, seq_no, shared_res_map)?;

send_sync_from

pub(crate) fn send_sync_from(
    &self,
    node_id: NodeIdentifier,
    from_seq_no: BlockSeqNo,
) -> anyhow::Result<()>

Sends a direct SyncFrom message to a node, indicating a request to synchronize starting from a specific block sequence number.

node.send_sync_from(peer_node_id, 123)?;

broadcast_ack

pub(crate) fn broadcast_ack(
    &self,
    ack: <Self as NodeAssociatedTypes>::Ack,
) -> anyhow::Result<()>

Broadcasts an acknowledgment (Ack) message to all nodes in the network.

node.broadcast_ack(ack_message)?;

Important Implementation Details


Interactions With Other System Components


Diagram: Structure and Workflow of send.rs

flowchart TD
A[send_blocks_range_request]
B["broadcast_node_joining (fn)"]
C[Node Struct]
C1["broadcast_node_joining (method)"]
C2[send_block_request]
C3[broadcast_sync_finalized]
C4[send_sync_from]
C5[broadcast_ack]
A -->|uses| NetworkDirectSender
B -->|uses| NetworkBroadcastSender
C1 --> B
C2 --> A
C3 -->|uses| BlockStateRepository
C3 -->|uses| BLS Keys
C3 -->|uses| NetworkBroadcastSender
C4 -->|uses| NetworkDirectSender
C5 -->|uses| NetworkBroadcastSender

This flowchart illustrates the relationship between standalone functions and the corresponding methods in the Node struct, showing their dependencies on network senders and other components. The Node methods wrap the standalone functions and add additional logic such as metrics reporting and cryptographic signing.