mainline/rpc/
info.rs

1use std::net::SocketAddrV4;
2
3use crate::Id;
4
5use super::Rpc;
6
7/// Information and statistics about this mainline node.
8#[derive(Debug, Clone)]
9pub struct Info {
10    id: Id,
11    local_addr: SocketAddrV4,
12    public_address: Option<SocketAddrV4>,
13    firewalled: bool,
14    dht_size_estimate: (usize, f64),
15    server_mode: bool,
16}
17
18impl Info {
19    /// This Node's [Id]
20    pub fn id(&self) -> &Id {
21        &self.id
22    }
23    /// Local UDP Ipv4 socket address that this node is listening on.
24    pub fn local_addr(&self) -> SocketAddrV4 {
25        self.local_addr
26    }
27    /// Returns the best guess for this node's Public address.
28    ///
29    /// If [crate::DhtBuilder::public_ip] was set, this is what will be returned
30    /// (plus the local port), otherwise it will rely on consensus from
31    /// responding nodes voting on our public IP and port.
32    pub fn public_address(&self) -> Option<SocketAddrV4> {
33        self.public_address
34    }
35    /// Returns `true` if we can't confirm that [Self::public_address] is publicly addressable.
36    ///
37    /// If this node is firewalled, it won't switch to server mode if it is in adaptive mode,
38    /// but if [crate::DhtBuilder::server_mode] was set to true, then whether or not this node is firewalled
39    /// won't matter.
40    pub fn firewalled(&self) -> bool {
41        self.firewalled
42    }
43
44    /// Returns whether or not this node is running in server mode.
45    pub fn server_mode(&self) -> bool {
46        self.server_mode
47    }
48
49    /// Returns:
50    ///  1. Normal Dht size estimate based on all closer `nodes` in query responses.
51    ///  2. Standard deviaiton as a function of the number of samples used in this estimate.
52    ///
53    /// [Read more](https://github.com/pubky/mainline/blob/main/docs/dht_size_estimate.md)
54    pub fn dht_size_estimate(&self) -> (usize, f64) {
55        self.dht_size_estimate
56    }
57}
58
59impl From<&Rpc> for Info {
60    fn from(rpc: &Rpc) -> Self {
61        Self {
62            id: *rpc.id(),
63            local_addr: rpc.local_addr(),
64            dht_size_estimate: rpc.dht_size_estimate(),
65            public_address: rpc.public_address(),
66            firewalled: rpc.firewalled(),
67            server_mode: rpc.server_mode(),
68        }
69    }
70}