network_message.rs

Overview

This file defines the core message types and data structures used for managing consensus rounds and authority switching in a distributed network protocol. It primarily handles the communication and data involved in transitioning block production authority between nodes during consensus rounds, including success, failure, and rejection scenarios. The message structures encapsulate cryptographic proofs, attestations, and locking mechanisms crucial for the protocol's correctness and safety.

The file contains data types representing lock states, round requests, round results (success/failure), rejection messages, and the enumeration of all possible authority switch messages exchanged between nodes. These messages facilitate coordination and verification of block production and finalization in the consensus process.

Data Structures and Enumerations

Lock

Represents the locked state for a particular round in the consensus protocol.

Fields

Usage

Lock is used to represent and propagate the locked state across nodes. The lock ensures that all nodes agree on a particular block and round, helping to prevent conflicting blocks being finalized. The handling of invalid blocks using nack_bad_block supports network robustness by tracking and excluding known bad blocks.

Implementation Details


NextRound

Represents a request message to initiate or continue a consensus round.

Fields

Usage

Sent by nodes to signal the start or continuation of a round, including the current lock and relevant attestations. This message is essential for nodes to synchronize their view of the blockchain state and locked rounds.


NextRoundSuccess

Indicates a successful consensus round with a proposed block.

Fields

Usage

Conveys that a consensus round has completed successfully with a block proposed and validated by a quorum of nodes.


NextRoundFailed

Represents a failed consensus round, including the best known block and proofs.

Fields

Usage

Used when a round fails to produce a final block but nodes still share information about the best known block and proof of efforts made.


NextRoundReject

Sent to reject a NextRound request due to a prefinalized block existing at the requested height.

Fields

Usage

Prevents unnecessary processing of rounds for blocks that have already been prefinalized, maintaining protocol efficiency.


AuthoritySwitch (Enum)

Enumerates all possible messages related to switching block production authority.

Variants

Usage

This enum serves as the comprehensive message type for all authority switch communication scenarios in the protocol.


Important Implementation Details


Interactions with Other Components


Usage Examples

Creating a Lock

use std::collections::HashSet;
use crate::types::{BlockIdentifier, BlockHeight, BlockRound};
use crate::node::NodeIdentifier;

let lock = Lock::builder()
    .parent_block(parent_block_id)
    .height(current_height)
    .next_auth_node_id(next_node_id)
    .locked_round(current_round)
    .locked_block(Some(locked_block_id))
    .nack_bad_block(HashSet::new())
    .build();

Sending a NextRound Request

let next_round_msg = NextRound::builder()
    .lock(lock_envelope)
    .locked_block_attestation(Some(attestation_envelope))
    .attestations_for_ancestors(vec![/* additional attestations */])
    .build();

let authority_switch_request = AuthoritySwitch::Request(next_round_msg);

Handling a Successful Round

match received_message {
    AuthoritySwitch::Switched(success_envelope) => {
        let success = success_envelope.content();
        // Process the proposed block and aggregated attestations
    }
    _ => {}
}

Mermaid Diagram: Structure of network_message.rs

classDiagram
class Lock {
-parent_block: BlockIdentifier
-height: BlockHeight
-next_auth_node_id: NodeIdentifier
-locked_round: BlockRound
-locked_block: Option<BlockIdentifier>
-nack_bad_block: HashSet<BlockIdentifier>
}
class NextRound {
-lock: Envelope<GoshBLS, Lock>
-locked_block_attestation: Option<Envelope<GoshBLS, AttestationData>>
-attestations_for_ancestors: Vec<Envelope<GoshBLS, AttestationData>>
}
class NextRoundSuccess {
-node_identifier: NodeIdentifier
-round: BlockRound
-block_height: BlockHeight
-proposed_block: NetBlock
-attestations_aggregated: Option<Envelope<GoshBLS, AttestationData>>
-requests_aggregated: Vec<Envelope<GoshBLS, Lock>>
}
class NextRoundFailed {
-node_identifier: NodeIdentifier
-round: BlockRound
-block_height: BlockHeight
-proposed_block: NetBlock
-attestations_aggregated: Option<Envelope<GoshBLS, AttestationData>>
-requests_aggregated: Vec<Envelope<GoshBLS, Lock>>
}
class NextRoundReject {
-thread_identifier: ThreadIdentifier
-prefinalized_block: NetBlock
-proof_of_prefinalization: Envelope<GoshBLS, AttestationData>
}
class AuthoritySwitch {
<<enumeration>>
Request
Reject
RejectTooOld
Switched
Failed
}
AuthoritySwitch --> NextRound : Request
AuthoritySwitch --> NextRoundReject : Reject
AuthoritySwitch --> ThreadIdentifier : RejectTooOld
AuthoritySwitch --> NextRoundSuccess : Switched
AuthoritySwitch --> NextRoundFailed : Failed
NextRoundSuccess --> NetBlock
NextRoundFailed --> NetBlock
NextRoundReject --> NetBlock
Lock <.. Envelope
NextRound <.. Envelope
NextRoundSuccess <.. Envelope
NextRoundFailed <.. Envelope
NextRoundReject <.. Envelope

This diagram illustrates the main data structures and their relationships, highlighting the central role of the AuthoritySwitch enum in message handling. The use of Envelope for cryptographic wrapping is indicated via dependencies. The diagram provides a high-level view of message types involved in consensus round management.