node.rs

Overview

This file defines the GossipPeer struct and associated functionality for representing and managing gossip protocol peers within a distributed system. A gossip peer encapsulates information about a network node such as its identity, network addresses, optional proxy addresses, API sockets, and cryptographic signatures used for authentication and integrity verification.

The primary responsibilities include:

This file plays a critical role in secure peer discovery and communication within the gossip protocol layer of the system.


Entities

GossipPeer<PeerId>

Represents a peer in the gossip network identified by a generic PeerId. Holds network addresses, optional API sockets, and an optional cryptographic signature.

Properties

Methods

pub fn new(...) -> anyhow::Result<Self>

Creates a new GossipPeer instance from the provided parameters. Optionally signs the peer's serialized data if signing keys are provided.

fn values(&self) -> Vec<(&'static str, String)>

Serializes the peer's fields into a vector of key-value string pairs, suitable for storage or transmission.

pub fn try_from_values<'kv>(values: impl Iterator<Item = (&'kv str, &'kv str)>) -> Option<Self>

Attempts to reconstruct a GossipPeer instance from an iterator over key-value string pairs.

pub fn try_get_from(node_state: &NodeState) -> Option<Self>

Utility to attempt constructing a GossipPeer directly from a NodeState instance by extracting its key-value pairs.

pub fn set_to(&self, node_state: &mut NodeState)

Writes the peer's key-value pairs into a mutable NodeState instance.

impl<PeerId: Display> Display for GossipPeer<PeerId>

Implements the standard Display trait, enabling formatted string representation of GossipPeer.


Free Functions

fn bytes_to_sign<'kv>(key_values: impl Iterator<Item = (&'kv str, &'kv str)>) -> Vec<u8>

Constructs the canonical byte sequence used for signing or verifying a peer's state.

pub fn sign_gossip_node(node_state: &mut NodeState, key: transport_layer::SigningKey)

Signs the key-value pairs stored in a NodeState and writes the signature back into the state.

fn parse_value<T: FromStr>(name: &str, value: &str) -> Option<T>

Attempts to parse a value string into the specified type, logging a warning on failure.

fn pubkey_signature_to_string(pubkey: &transport_layer::VerifyingKey, signature: &transport_layer::Signature) -> String

Encodes the public key and signature bytes into a Base64 string.

fn parse_pubkey_signature(s: &str) -> anyhow::Result<(transport_layer::VerifyingKey, transport_layer::Signature)>

Decodes a Base64-encoded public key and signature string back into their binary forms.


Implementation Details and Algorithms


Interaction with Other Parts of the System


Visual Diagram

classDiagram
class GossipPeer {
+id: PeerId
+advertise_addr: SocketAddr
+proxies: Vec<SocketAddr>
+bm_api_socket: Option<SocketAddr>
+bk_api_socket: Option<SocketAddr>
+pubkey_signature: Option<(VerifyingKey, Signature)>
+new()
+values()
+try_from_values()
+try_get_from()
+set_to()
+fmt()
}
class NodeState {
<<external>>
+key_values()
+set()
}
class transport_layer::SigningKey {
<<external>>
+sign()
+verifying_key()
}
class transport_layer::VerifyingKey {
<<external>>
+verify()
+as_bytes()
}
class transport_layer::Signature {
<<external>>
+to_bytes()
}
GossipPeer ..> "1" NodeState : reads/writes
GossipPeer ..> transport_layer::SigningKey : uses for signing
GossipPeer ..> transport_layer::VerifyingKey : verifies signatures
GossipPeer ..> transport_layer::Signature : holds signature

Testing


Key Constants

Constant

Description

ADVERTISE_ADDR_KEY

Key for advertised address ("node_advertise_addr")

BK_API_SOCKET_KEY

Key for bk API socket address

BM_API_SOCKET_KEY

Key for bm API socket address

ID_KEY

Key for peer ID ("node_id")

PROXIES_KEY

Key for proxy addresses ("node_proxies")

PUBKEY_SIGNATURE_KEY

Key for public key signature ("pubkey_signature")


References