Skip to main content

zebra_rpc/methods/types/
peer_info.rs

1//! An array of [`PeerInfo`] is the output of the `getpeerinfo` RPC method.
2
3use derive_getters::Getters;
4use derive_new::new;
5use zebra_network::{types::MetaAddr, PeerSocketAddr};
6
7/// Item of the `getpeerinfo` response
8#[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize, Getters, new)]
9pub struct PeerInfo {
10    /// The IP address and port of the peer
11    #[getter(copy)]
12    pub(crate) addr: PeerSocketAddr,
13
14    /// Inbound (true) or Outbound (false)
15    pub(crate) inbound: bool,
16
17    /// The round-trip ping time in seconds.
18    #[serde(skip_serializing_if = "Option::is_none")]
19    pub(crate) pingtime: Option<f64>,
20
21    /// The wait time on a ping response in seconds.
22    #[serde(skip_serializing_if = "Option::is_none")]
23    pub(crate) pingwait: Option<f64>,
24}
25
26/// Response type for the `getpeerinfo` RPC method.
27pub type GetPeerInfoResponse = Vec<PeerInfo>;
28
29impl From<MetaAddr> for PeerInfo {
30    fn from(meta_addr: MetaAddr) -> Self {
31        Self {
32            addr: meta_addr.addr(),
33            inbound: meta_addr.is_inbound(),
34            pingtime: meta_addr.rtt().map(|d| d.as_secs_f64()),
35            pingwait: meta_addr.ping_sent_at().map(|t| t.elapsed().as_secs_f64()),
36        }
37    }
38}
39
40impl Default for PeerInfo {
41    fn default() -> Self {
42        Self {
43            addr: PeerSocketAddr::unspecified(),
44            inbound: false,
45            pingtime: None,
46            pingwait: None,
47        }
48    }
49}