zebra_rpc/methods/types/
peer_info.rs1use derive_getters::Getters;
4use derive_new::new;
5use zebra_network::{types::MetaAddr, PeerSocketAddr};
6
7#[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize, Getters, new)]
9#[allow(clippy::too_many_arguments)]
10pub struct PeerInfo {
11 #[getter(copy)]
13 pub(crate) addr: PeerSocketAddr,
14
15 pub(crate) services: String,
17
18 pub(crate) lastrecv: u32,
20
21 pub(crate) inbound: bool,
23
24 pub(crate) banscore: u32,
26
27 pub(crate) subver: String,
29
30 pub(crate) version: u32,
32
33 pub(crate) connection_state: String,
35
36 #[serde(skip_serializing_if = "Option::is_none")]
38 pub(crate) pingtime: Option<f64>,
39
40 #[serde(skip_serializing_if = "Option::is_none")]
42 pub(crate) pingwait: Option<f64>,
43}
44
45pub type GetPeerInfoResponse = Vec<PeerInfo>;
47
48impl PeerInfo {
49 pub fn from_meta_addr(meta_addr: MetaAddr, banscore: u32) -> Self {
56 let services = meta_addr
57 .services()
58 .map(|s| format!("{:016x}", s.bits()))
59 .unwrap_or_else(|| "0000000000000000".to_string());
60
61 let lastrecv = meta_addr.last_seen().map(|t| t.timestamp()).unwrap_or(0);
62
63 let connection_state = meta_addr.last_connection_state().to_string();
64
65 let subver = meta_addr.user_agent().unwrap_or("").to_string();
66
67 let version = meta_addr.negotiated_version().map(|v| v.0).unwrap_or(0);
68
69 Self {
70 addr: meta_addr.addr(),
71 services,
72 lastrecv,
73 inbound: meta_addr.is_inbound(),
74 banscore,
75 subver,
76 version,
77 connection_state,
78 pingtime: meta_addr.rtt().map(|d| d.as_secs_f64()),
79 pingwait: meta_addr.ping_sent_at().map(|t| t.elapsed().as_secs_f64()),
80 }
81 }
82}
83
84impl Default for PeerInfo {
85 fn default() -> Self {
86 Self {
87 addr: PeerSocketAddr::unspecified(),
88 services: "0000000000000000".to_string(),
89 lastrecv: 0,
90 inbound: false,
91 banscore: 0,
92 subver: String::new(),
93 version: 0,
94 connection_state: String::new(),
95 pingtime: None,
96 pingwait: None,
97 }
98 }
99}