lumina_node_wasm/wrapper/
libp2p.rs

1use libp2p::swarm::{
2    ConnectionCounters as SwarmConnectionCounters, NetworkInfo as SwarmNetworkInfo,
3};
4use serde::{Deserialize, Serialize};
5use wasm_bindgen::prelude::*;
6
7#[wasm_bindgen(inspectable)]
8#[derive(Debug, Serialize, Deserialize)]
9/// Information about the connections
10pub struct NetworkInfoSnapshot {
11    /// The number of connected peers, i.e. peers with whom at least one established connection exists.
12    pub num_peers: usize,
13    /// Gets counters for ongoing network connections.
14    pub connection_counters: ConnectionCountersSnapshot,
15}
16
17/// Network connection information
18#[wasm_bindgen(inspectable)]
19#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
20pub struct ConnectionCountersSnapshot {
21    /// The total number of connections, both pending and established.
22    pub num_connections: u32,
23    /// The total number of pending connections, both incoming and outgoing.
24    pub num_pending: u32,
25    /// The total number of pending connections, both incoming and outgoing.
26    pub num_pending_incoming: u32,
27    /// The number of outgoing connections being established.
28    pub num_pending_outgoing: u32,
29    /// The number of outgoing connections being established.
30    pub num_established: u32,
31    /// The number of established incoming connections.
32    pub num_established_incoming: u32,
33    /// The number of established outgoing connections.
34    pub num_established_outgoing: u32,
35}
36
37impl From<SwarmNetworkInfo> for NetworkInfoSnapshot {
38    fn from(info: SwarmNetworkInfo) -> Self {
39        Self {
40            num_peers: info.num_peers(),
41            connection_counters: ConnectionCountersSnapshot::from(info.connection_counters()),
42        }
43    }
44}
45
46impl From<&SwarmConnectionCounters> for ConnectionCountersSnapshot {
47    fn from(counters: &SwarmConnectionCounters) -> Self {
48        Self {
49            num_connections: counters.num_connections(),
50            num_pending: counters.num_pending(),
51            num_pending_incoming: counters.num_pending_incoming(),
52            num_pending_outgoing: counters.num_pending_outgoing(),
53            num_established: counters.num_established(),
54            num_established_incoming: counters.num_established_incoming(),
55            num_established_outgoing: counters.num_established_outgoing(),
56        }
57    }
58}