mod.rs

Overview

This file defines core functionality for a network communication proxy and node system focused on gossip protocol-based peer-to-peer messaging. It provides abstractions to start and manage nodes and proxies that communicate over a network transport layer, handle configuration updates, multiplex incoming and outgoing messages, and maintain node state and statistics. The file integrates with components responsible for gossip-based membership and messaging, network transport, and Pub/Sub mechanisms.

Key responsibilities include:


Functions

make_addr(group: usize, index: usize) -> SocketAddr

Generates a localhost socket address based on a group and index. The port number is computed as 11000 + group * 10 + index.

Usage:

let addr = make_addr(1, 0); // returns SocketAddr for 127.0.0.1:11010

node_addr(group: usize) -> SocketAddr

Returns the socket address for a node in the given group. Calls make_addr with index 0.

gossip_addr(group: usize) -> SocketAddr

Returns the socket address for gossip communication in the given group. Calls make_addr with index 1.

init_logs()

Initializes logging for the module using tracing_subscriber. Logs are written to a file named test.log located relative to the Cargo project directory. This function ensures that logging is initialized only once using a OnceCell.


Structs and Enums

NoChannelMetrics

A no-op implementation of the InstrumentedChannelMetrics trait. It ignores channel metrics reporting.

Proxy<Transport>

Represents a proxy node that manages gossip communication and message multiplexing over a transport.

Fields:

Methods:

Drop Implementation

Sends a shutdown signal when the proxy is dropped.


NodeConfig

Configuration for a node including network and gossip settings.

Fields:

Methods:


Node<Transport>

Represents a running node participating in the network and gossip protocol.

Fields:

Methods:

Drop Implementation

Sends a shutdown signal when the node is dropped.


NodeState

Tracks statistics and state related to message transmission and acknowledgements.

Fields:

Default Implementation

Initializes counters to zero and the unconfirmed map to empty.


Message

An enum representing messages exchanged between nodes.

Variants:

Trait Implementations:


NodeChannels

Holds communication channels for a node:


Async Functions

async fn split_config(shutdown_rx: watch::Receiver<bool>, config_rx: watch::Receiver<NodeConfig>, network_config_tx: watch::Sender<NetworkConfig>, gossip_config_tx: watch::Sender<GossipConfig>)

Listens for changes on the node configuration watch channel and splits updates into separate network and gossip configuration watch channels. Terminates on shutdown signal.


Implementation Details and Algorithms


Interactions with Other System Components


Mermaid Diagram

classDiagram
class Proxy {
+shutdown_tx
+config_tx
+watch_gossip_config_tx
+transport
+chitchat
+chitchat_handle
+gossip_rest_handle
+start()
+message_multiplexor()
+print_stat()
}
class Node {
+shutdown_tx
+config_tx
+watch_gossip_config_tx
+transport
+chitchat
+state
+chitchat_handle
+gossip_rest_handle
+start()
+print_stat()
}
class NodeState {
+data_sent
+data_received
+ack_sent
+ack_received
+unconfirmed
}
class NodeConfig {
+node_id
+network
+advertise_addr
+gossip
+new()
}
class Message {
<<enum>>
+Data
+Ack
}
class NodeChannels {
+direct_tx
+broadcast_tx
+incoming_rx
+peers_rx
}
Proxy --> Transport
Node --> Transport
Node *-- NodeState
Node o-- NodeChannels
Proxy ..> NodeConfig
Node ..> NodeConfig