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 From<MetaAddr> for PeerInfo {
49 fn from(meta_addr: MetaAddr) -> Self {
50 let services = meta_addr
51 .services()
52 .map(|s| format!("{:016x}", s.bits()))
53 .unwrap_or_else(|| "0000000000000000".to_string());
54
55 let lastrecv = meta_addr.last_seen().map(|t| t.timestamp()).unwrap_or(0);
56
57 let connection_state = meta_addr.last_connection_state().to_string();
58
59 let subver = meta_addr.user_agent().unwrap_or("").to_string();
60
61 let version = meta_addr.negotiated_version().map(|v| v.0).unwrap_or(0);
62
63 Self {
64 addr: meta_addr.addr(),
65 services,
66 lastrecv,
67 inbound: meta_addr.is_inbound(),
68 banscore: meta_addr.misbehavior(),
69 subver,
70 version,
71 connection_state,
72 pingtime: meta_addr.rtt().map(|d| d.as_secs_f64()),
73 pingwait: meta_addr.ping_sent_at().map(|t| t.elapsed().as_secs_f64()),
74 }
75 }
76}
77
78impl Default for PeerInfo {
79 fn default() -> Self {
80 Self {
81 addr: PeerSocketAddr::unspecified(),
82 services: "0000000000000000".to_string(),
83 lastrecv: 0,
84 inbound: false,
85 banscore: 0,
86 subver: String::new(),
87 version: 0,
88 connection_state: String::new(),
89 pingtime: None,
90 pingwait: None,
91 }
92 }
93}