1use jsonrpsee::proc_macros::rpc;
2use multiaddr::Multiaddr;
3use serde::{Deserialize, Serialize};
4use serde_repr::{Deserialize_repr, Serialize_repr};
5use std::collections::HashMap;
6
7#[derive(Debug, Clone, Serialize, Deserialize)]
9#[serde(rename_all = "PascalCase")]
10pub struct AddrInfo {
11 #[serde(rename = "ID")]
13 pub id: PeerId,
14 pub addrs: Vec<Multiaddr>,
16}
17
18#[derive(Debug, Clone, Serialize, Deserialize)]
20#[serde(rename_all = "PascalCase")]
21pub struct Stat {
22 pub num_streams_inbound: u32,
24 pub num_streams_outbound: u32,
26 pub num_conns_inbound: u32,
28 pub num_conns_outbound: u32,
30 #[serde(rename = "NumFD")]
32 pub num_fd: u32,
33 pub memory: u32,
35}
36
37#[derive(Debug, Clone, Serialize, Deserialize)]
39#[serde(rename_all = "PascalCase")]
40pub struct ResourceManagerStats {
41 pub system: Stat,
43 pub transient: Stat,
45 pub services: HashMap<String, Stat>,
47 pub protocols: HashMap<String, Stat>,
49 pub peers: HashMap<String, Stat>,
51}
52
53#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
55pub struct PeerId(
56 #[serde(with = "tendermint_proto::serializers::from_str")] pub libp2p_identity::PeerId,
57);
58
59impl From<libp2p_identity::PeerId> for PeerId {
60 fn from(value: libp2p_identity::PeerId) -> Self {
61 PeerId(value)
62 }
63}
64
65impl From<PeerId> for libp2p_identity::PeerId {
66 fn from(value: PeerId) -> Self {
67 value.0
68 }
69}
70
71#[derive(Debug, Clone, Serialize, Deserialize)]
73#[serde(rename_all = "PascalCase")]
74pub struct BandwidthStats {
75 pub total_in: f32,
77 pub total_out: f32,
79 pub rate_in: f32,
81 pub rate_out: f32,
83}
84
85#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize_repr, Deserialize_repr)]
87#[repr(u8)]
88pub enum Connectedness {
89 NotConnected = 0,
91 Connected = 1,
93 CanConnect = 2,
95 CannotConnect = 3,
97}
98
99#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize_repr, Deserialize_repr)]
101#[repr(u8)]
102pub enum Reachability {
103 Unknown = 0,
105 Public = 1,
107 Private = 2,
109}
110
111#[rpc(client, namespace = "p2p", namespace_separator = ".")]
112pub trait P2P {
113 #[method(name = "BandwidthForPeer")]
115 async fn p2p_bandwidth_for_peer(&self, peer_id: &PeerId) -> Result<BandwidthStats, Error>;
116
117 #[method(name = "BandwidthForProtocol")]
119 async fn p2p_bandwidth_for_protocol(&self, protocol_id: &str) -> Result<BandwidthStats, Error>;
120
121 #[method(name = "BandwidthStats")]
123 async fn p2p_bandwidth_stats(&self) -> Result<BandwidthStats, Error>;
124
125 #[method(name = "BlockPeer")]
127 async fn p2p_block_peer(&self, peer_id: &PeerId) -> Result<(), Error>;
128
129 #[method(name = "ClosePeer")]
131 async fn p2p_close_peer(&self, peer_id: &PeerId) -> Result<(), Error>;
132
133 #[method(name = "Connect")]
135 async fn p2p_connect(&self, address: &AddrInfo) -> Result<(), Error>;
136
137 #[method(name = "Connectedness")]
139 async fn p2p_connectedness(&self, peer_id: &PeerId) -> Result<Connectedness, Error>;
140
141 #[method(name = "Info")]
143 async fn p2p_info(&self) -> Result<AddrInfo, Error>;
144
145 #[method(name = "IsProtected")]
147 async fn p2p_is_protected(&self, peer_id: &PeerId, tag: &str) -> Result<bool, Error>;
148
149 #[method(name = "ListBlockedPeers")]
151 async fn p2p_list_blocked_peers(&self) -> Result<Vec<PeerId>, Error>;
152
153 #[method(name = "NATStatus")]
155 async fn p2p_nat_status(&self) -> Result<Reachability, Error>;
156
157 #[method(name = "PeerInfo")]
159 async fn p2p_peer_info(&self, peer_id: &PeerId) -> Result<AddrInfo, Error>;
160
161 #[method(name = "Peers")]
163 async fn p2p_peers(&self) -> Result<Vec<PeerId>, Error>;
164
165 #[method(name = "Protect")]
167 async fn p2p_protect(&self, peer_id: &PeerId, tag: &str) -> Result<(), Error>;
168
169 #[method(name = "PubSubPeers")]
172 async fn p2p_pub_sub_peers(&self, topic: &str) -> Result<Option<Vec<PeerId>>, Error>;
173
174 #[method(name = "ResourceState")]
176 async fn p2p_resource_state(&self) -> Result<ResourceManagerStats, Error>;
177
178 #[method(name = "UnblockPeer")]
180 async fn p2p_unblock_peer(&self, peer_id: &PeerId) -> Result<(), Error>;
181
182 #[method(name = "Unprotect")]
184 async fn p2p_unprotect(&self, peer_id: &PeerId, tag: &str) -> Result<bool, Error>;
185}