mod.rs

Overview

This file defines the core Node struct and its initialization logic, which serves as a central component for managing blockchain node operations in a multi-threaded, distributed environment. The Node struct encapsulates essential services and state management mechanisms required for block production, validation, synchronization, and network communication within the node's thread context. It integrates with cryptographic components, networking channels, storage, and consensus-related services to facilitate the node's participation in the blockchain network.

Struct: Node<TStateSyncService, TRandomGenerator>

The Node struct is generic over two types:

Fields

Usage

The Node struct represents an active participant in the blockchain network, managing block production, validation, and synchronization within a dedicated thread. It interacts extensively with services such as StateSyncService for state synchronization, BlockProcessorService for processing incoming blocks, and ProducerService for block production. Networking channels facilitate message exchange, while cryptographic keys and attestations ensure security and consensus.

Method: new

pub fn new(
    shared_services: SharedServices,
    state_sync_service: TStateSyncService,
    production_process: TVMBlockProducerProcess,
    repository: RepositoryImpl,
    network_rx: XInstrumentedReceiver<(NetworkMessage, SocketAddr)>,
    network_broadcast_tx: NetBroadcastSender<NetworkMessage>,
    network_direct_tx: NetDirectSender<NodeIdentifier, NetworkMessage>,
    raw_block_tx: InstrumentedSender<(NodeIdentifier, Vec<u8>)>,
    bls_keys_map: Arc<Mutex<HashMap<PubKey, (Secret, RndSeed)>>>,
    config: Config,
    block_keeper_rng: TRandomGenerator,
    producer_election_rng: TRandomGenerator,
    thread_id: ThreadIdentifier,
    feedback_sender: InstrumentedSender<ExtMsgFeedbackList>,
    _update_producer_group: bool,
    block_state_repository: BlockStateRepository,
    block_processor_service: BlockProcessorService,
    attestations_target_service: AttestationTargetsService,
    validation_service: ValidationServiceInterface,
    skipped_attestation_ids: Arc<Mutex<HashSet<BlockIdentifier>>>,
    metrics: Option<BlockProductionMetrics>,
    self_tx: XInstrumentedSender<(NetworkMessage, SocketAddr)>,
    external_messages: ExternalMessagesThreadState,
    message_db: MessageDurableStorage,
    last_block_attestations: Arc<Mutex<CollectedAttestations>>,
    bp_production_count: Arc<AtomicI32>,
    blk_req_tx: Sender<BlockRequestParams>,
    attestation_send_service: AttestationSendServiceHandler,
    ext_msg_receiver: JoinHandle<()>,
    authority_state: Arc<Mutex<Authority>>,
    unprocessed_blocks_cache: UnfinalizedCandidateBlockCollection,
    stop_result_tx: Sender<()>,
    stalled_threads: Arc<Mutex<HashSet<ThreadIdentifier>>>,
    chain_pulse_monitor: Sender<ChainPulseEvent>,
    authority_handler: JoinHandle<()>,
    self_authority_tx: XInstrumentedSender<(NetworkMessage, SocketAddr)>,
    save_optimistic_service_sender: InstrumentedSender<Arc<OptimisticStateImpl>>,
) -> Self

Parameters

Returns

Description

The new method initializes the Node with all its dependencies and starts critical internal services and threads, including:

The method ensures thread-safe setup of state and communication channels and integrates the various subsystems required for node operation.

Usage Example

let node = Node::new(
    shared_services,
    state_sync_service,
    production_process,
    repository,
    network_rx,
    network_broadcast_tx,
    network_direct_tx,
    raw_block_tx,
    bls_keys_map,
    config,
    block_keeper_rng,
    producer_election_rng,
    thread_id,
    feedback_sender,
    false,
    block_state_repository,
    block_processor_service,
    attestations_target_service,
    validation_service,
    skipped_attestation_ids,
    Some(metrics),
    self_tx,
    external_messages,
    message_db,
    last_block_attestations,
    bp_production_count,
    blk_req_tx,
    attestation_send_service,
    ext_msg_receiver,
    authority_state,
    unprocessed_blocks_cache,
    stop_result_tx,
    stalled_threads,
    chain_pulse_monitor,
    authority_handler,
    self_authority_tx,
    save_optimistic_service_sender,
);

Implementation Details and Algorithms

Interactions with Other System Components

Visual Diagram

classDiagram
class Node {
-repository: RepositoryImpl
-shared_services: SharedServices
-state_sync_service: TStateSyncService
-network_rx: XInstrumentedReceiver
-network_broadcast_tx: NetBroadcastSender
-network_direct_tx: NetDirectSender
-raw_block_tx: InstrumentedSender
-bls_keys_map: Arc<Mutex<HashMap>>
-last_block_attestations: Arc<Mutex<CollectedAttestations>>
-received_acks: Arc<Mutex<Vec<Envelope<AckData>>>>
-sent_acks: BTreeMap<BlockSeqNo, Envelope<AckData>>
-received_nacks: Arc<Mutex<Vec<Envelope<NackData>>>>
-config: Config
-received_attestations: BTreeMap<BlockSeqNo, HashMap<BlockIdentifier, HashSet<SignerIndex>>>
-block_keeper_rng: TRandomGenerator
-producer_election_rng: TRandomGenerator
-attestations_to_send: BTreeMap<BlockSeqNo, Vec<Envelope<AttestationData>>>
-ack_cache: BTreeMap<BlockSeqNo, Vec<Envelope<AckData>>>
-nack_cache: BTreeMap<BlockSeqNo, Vec<Envelope<NackData>>>
-thread_id: ThreadIdentifier
-is_spawned_from_node_sync: bool
-block_state_repository: BlockStateRepository
-block_processor_service: BlockProcessorService
-attestation_send_service: AttestationSendServiceHandler
-validation_service: ValidationServiceInterface
-skipped_attestation_ids: Arc<Mutex<HashSet<BlockIdentifier>>>
-message_db: MessageDurableStorage
-last_broadcasted_produced_candidate_block_time: Instant
-finalization_loop: JoinHandle
-producer_service: ProducerService
-metrics: Option<BlockProductionMetrics>
-external_messages: ExternalMessagesThreadState
-is_state_sync_requested: Arc<Mutex<Option<BlockSeqNo>>>
-blk_req_tx: Sender<BlockRequestParams>
-ext_msg_receiver: JoinHandle
-authority_state: Arc<Mutex<Authority>>
-unprocessed_blocks_cache: UnfinalizedCandidateBlockCollection
-stop_result_tx: Sender
-stalled_threads: Arc<Mutex<HashSet<ThreadIdentifier>>>
-last_synced_state: Option<(BlockIdentifier, BlockSeqNo, HashMap<ThreadIdentifier, BlockIdentifier>)>
-chain_pulse_monitor: Sender<ChainPulseEvent>
-authority_handler: JoinHandle
+new()
}
Node --> "1" RepositoryImpl
Node --> "1" SharedServices
Node --> "1" TStateSyncService
Node --> "1" BlockProcessorService
Node --> "1" AttestationSendServiceHandler
Node --> "1" ValidationServiceInterface
Node --> "1" BlockStateRepository
Node --> "1" ProducerService
Node --> "1" Authority
Node --> "1" UnfinalizedCandidateBlockCollection
Node --> "1" ExternalMessagesThreadState
Node --> "many" NetworkMessage
Node --> "many" Envelope
Node --> "many" BlockIdentifier
Node --> "many" BlockSeqNo

This diagram shows the primary properties and key relationships of the Node struct with core services and data types it depends on or manages.


References