mod.rs

Overview

This file implements the AuthoritySwitchService, a core service responsible for handling the AuthoritySwitchProtocol network messages within the consensus or authority switch mechanism. It processes different variants of the AuthoritySwitch enum, including requests for switching authority, switches success notifications, rejections, and failure cases. The service interacts with various components such as the network layer, block state repository, unprocessed blocks cache, and chain pulse monitor to coordinate authority transitions in a thread-specific manner.

The service maintains synchronization state, verifies signatures on blocks and attestations, handles broadcasting and direct messaging, and manages the lifecycle of authority switching rounds. It is designed to run in a loop, continuously receiving and processing incoming network messages related to authority switching.


Structures

AuthoritySwitchService

A service struct responsible for processing authority switch protocol messages.

Fields


Methods

fn run(&mut self) -> anyhow::Result<()>

Starts the main processing loop of the AuthoritySwitchService. It continuously listens for incoming messages on rx and handles them according to their variant.

Behavior

Usage Example

let mut service = AuthoritySwitchService::builder()
    // initialize builder fields
    .build();
service.run()?;

fn on_authority_switch_success(&mut self, next_round_success: Envelope<GoshBLS, NextRoundSuccess>) -> anyhow::Result<()>

Processes a successful authority switch notification.

Parameters

Processing Steps

Return

Usage Example

let success_msg: Envelope<GoshBLS, NextRoundSuccess> = ...;
service.on_authority_switch_success(success_msg)?;

Important Implementation Details


Interactions with Other Components


Visual Diagram

classDiagram
class AuthoritySwitchService {
+self_addr: SocketAddr
+rx: XInstrumentedReceiver
+self_node_tx: XInstrumentedSender
+network_direct_tx: NetDirectSender
+thread_id: ThreadIdentifier
+unprocessed_blocks_cache: UnfinalizedCandidateBlockCollection
+thread_authority: Arc<Mutex<ThreadAuthority>>
+network_broadcast_tx: NetBroadcastSender
+block_state_repository: BlockStateRepository
+chain_pulse_monitor: Sender<ChainPulseEvent>
+sync_timeout_duration: Duration
+run()
-on_authority_switch_success()
}
AuthoritySwitchService --> "1" XInstrumentedReceiver : rx
AuthoritySwitchService --> "1" XInstrumentedSender : self_node_tx
AuthoritySwitchService --> "1" NetDirectSender : network_direct_tx
AuthoritySwitchService --> "1" NetBroadcastSender : network_broadcast_tx
AuthoritySwitchService --> "1" Arc~Mutex~ThreadAuthority~ : thread_authority
AuthoritySwitchService --> "1" BlockStateRepository : block_state_repository
AuthoritySwitchService --> "1" UnfinalizedCandidateBlockCollection : unprocessed_blocks_cache
AuthoritySwitchService --> "1" Sender~ChainPulseEvent~ : chain_pulse_monitor

Message Handling Workflow

flowchart TD
Start[Start Receiving Messages]
ReceiveMsg[Receive NetworkMessage from rx]
CheckMsgType{Is AuthoritySwitchProtocol?}
HandleRequest[Handle AuthoritySwitch::Request]
HandleSwitched[Handle AuthoritySwitch::Switched]
HandleReject[Handle AuthoritySwitch::Reject]
HandleRejectTooOld[Handle AuthoritySwitch::RejectTooOld]
HandleFailed[Handle AuthoritySwitch::Failed]
UnexpectedMsg[Unexpected Message - Panic]
LoopBack[Back to Receiving Messages]
Error[Error / Disconnected]
Start --> ReceiveMsg --> CheckMsgType
CheckMsgType -- Yes -->|Match variant| HandleRequest
CheckMsgType -- Yes -->|Match variant| HandleSwitched
CheckMsgType -- Yes -->|Match variant| HandleReject
CheckMsgType -- Yes -->|Match variant| HandleRejectTooOld
CheckMsgType -- Yes -->|Match variant| HandleFailed
CheckMsgType -- No --> UnexpectedMsg
HandleRequest --> LoopBack
HandleSwitched --> LoopBack
HandleReject --> LoopBack
HandleRejectTooOld --> LoopBack
HandleFailed --> LoopBack
UnexpectedMsg --> LoopBack
ReceiveMsg -- Err --> Error