mod.rs

Overview

This file implements core functionality for monitoring and managing a set of blockchain nodes (peers) and their associated accounts. It provides traits for accessing blockchain data, an asynchronous watcher function to continuously update node subscriptions and peer information, and utility functions to process and format peer data. The main purpose is to maintain an up-to-date view of the blockchain network topology with respect to active peers and their reachable addresses.

The file heavily relies on data structures such as sets and maps to track peers and their connection endpoints. It interacts with blockchain account abstractions and a node database module, facilitating dynamic subscription updates for network communication layers.

Modules and Imports

Traits

BkSetProvider

pub trait BkSetProvider {
    fn get_bk_set(&self) -> Vec<UInt256>;
}

AccountProvider

pub trait AccountProvider {
    fn get_account(&self, id: &UInt256) -> Option<Account>;
}

Asynchronous Functions

watch_blockchain

pub async fn watch_blockchain<PeerId, B, A>(
    bk_set_provider: B,
    account_provider: A,
    self_peer_id: PeerId,
    subscribe_tx: tokio::sync::watch::Sender<Vec<Vec<SocketAddr>>>,
    peers_tx: tokio::sync::watch::Sender<HashMap<PeerId, SocketAddr>>,
) where
    B: BkSetProvider + Send + Sync + 'static,
    A: AccountProvider + Send + Sync + 'static,
    PeerId: Display + Clone + Hash + Eq + From<UInt256>,
let (subscribe_tx, mut subscribe_rx) = tokio::sync::watch::channel(Vec::new());
let (peers_tx, mut peers_rx) = tokio::sync::watch::channel(HashMap::new());

tokio::spawn(async move {
    watch_blockchain(bk_set_provider, account_provider, self_peer_id, subscribe_tx, peers_tx).await;
});

refresh

async fn refresh<PeerId, B, A>(
    self_peer_id: &PeerId,
    bk_set_provider: &B,
    account_provider: &A,
    _old_subscribe: Vec<Vec<SocketAddr>>,
    old_peers: HashMap<PeerId, SocketAddr>,
) -> (Vec<Vec<SocketAddr>>, HashMap<PeerId, SocketAddr>)
where
    B: BkSetProvider + Send + Sync + 'static,
    A: AccountProvider + Send + Sync + 'static,
    PeerId: Display + Clone + Hash + Eq + From<UInt256>,

Helper Functions

peer_subscribe

fn peer_subscribe(peer_addr: Option<SocketAddr>, proxies: HashSet<SocketAddr>) -> Vec<SocketAddr>

peers_info

fn peers_info<PeerId>(nodes: &HashMap<PeerId, SocketAddr>) -> String
where
    PeerId: Display,

Interactions with Other Parts of the System

Implementation Details and Algorithms

Visual Diagram

flowchart TD
A[watch_blockchain] -->|calls periodically| B[refresh]
B --> C[collect_bk_set]
B --> D[peer_subscribe]
B --> E[peers_info]
A -->|sends updates| F["subscribe_tx (watch channel)"]
A -->|sends updates| G["peers_tx (watch channel)"]
C -->|uses| H[AccountProvider]
C -->|uses| I[BkSetProvider]
D --> J[Vec<SocketAddr>]
E --> K[String summary]