Skip to main content

celestia_rpc/
p2p.rs

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/// An id and addresses of a peer in p2p network.
8#[derive(Debug, Clone, Serialize, Deserialize)]
9#[serde(rename_all = "PascalCase")]
10pub struct AddrInfo {
11    /// An id of a peer.
12    #[serde(rename = "ID")]
13    pub id: PeerId,
14    /// A list of addresses where peer listens on.
15    pub addrs: Vec<Multiaddr>,
16}
17
18/// Information about the resources used by the node.
19#[derive(Debug, Clone, Serialize, Deserialize)]
20#[serde(rename_all = "PascalCase")]
21pub struct Stat {
22    /// Amount of inbound streams.
23    pub num_streams_inbound: u32,
24    /// Amount of outbound streams.
25    pub num_streams_outbound: u32,
26    /// Amount of inbound connections.
27    pub num_conns_inbound: u32,
28    /// Amount of outbound connections.
29    pub num_conns_outbound: u32,
30    /// Amound of open file descriptors.
31    #[serde(rename = "NumFD")]
32    pub num_fd: u32,
33    /// A memory usage.
34    pub memory: u32,
35}
36
37/// Statistics of the `ResourceManager` component in Go p2p nodes.
38#[derive(Debug, Clone, Serialize, Deserialize)]
39#[serde(rename_all = "PascalCase")]
40pub struct ResourceManagerStats {
41    /// System statistics.
42    pub system: Stat,
43    /// Transient statistics.
44    pub transient: Stat,
45    /// Statistics per service.
46    pub services: HashMap<String, Stat>,
47    /// Statistics per protocol.
48    pub protocols: HashMap<String, Stat>,
49    /// Statistics per peer.
50    pub peers: HashMap<String, Stat>,
51}
52
53/// A wrapper for the libp2p `PeerId`.
54#[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/// Bandwidth statistics reported by the node.
72#[derive(Debug, Clone, Serialize, Deserialize)]
73#[serde(rename_all = "PascalCase")]
74pub struct BandwidthStats {
75    /// Total bytes received.
76    pub total_in: f32,
77    /// Total bytes sent.
78    pub total_out: f32,
79    /// Rate of receiving.
80    pub rate_in: f32,
81    /// Rate of sending.
82    pub rate_out: f32,
83}
84
85/// A representation of the connection status and capabilities.
86#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize_repr, Deserialize_repr)]
87#[repr(u8)]
88pub enum Connectedness {
89    /// Not connected.
90    NotConnected = 0,
91    /// Connected.
92    Connected = 1,
93    /// Not connected but connection is possible.
94    CanConnect = 2,
95    /// Not connected and connection is impossible.
96    CannotConnect = 3,
97}
98
99/// A representation of the peer's reachability in network.
100#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize_repr, Deserialize_repr)]
101#[repr(u8)]
102pub enum Reachability {
103    /// Unknown address.
104    Unknown = 0,
105    /// Public address.
106    Public = 1,
107    /// Private address.
108    Private = 2,
109}
110
111#[rpc(client, namespace = "p2p", namespace_separator = ".")]
112pub trait P2P {
113    /// BandwidthForPeer returns a Stats struct with bandwidth metrics associated with the given peer.ID. The metrics returned include all traffic sent / received for the peer, regardless of protocol.
114    #[method(name = "BandwidthForPeer")]
115    async fn p2p_bandwidth_for_peer(&self, peer_id: &PeerId) -> Result<BandwidthStats, Error>;
116
117    /// BandwidthForProtocol returns a Stats struct with bandwidth metrics associated with the given protocol.ID.
118    #[method(name = "BandwidthForProtocol")]
119    async fn p2p_bandwidth_for_protocol(&self, protocol_id: &str) -> Result<BandwidthStats, Error>;
120
121    /// BandwidthStats returns a Stats struct with bandwidth metrics for all data sent/received by the local peer, regardless of protocol or remote peer IDs.
122    #[method(name = "BandwidthStats")]
123    async fn p2p_bandwidth_stats(&self) -> Result<BandwidthStats, Error>;
124
125    /// BlockPeer adds a peer to the set of blocked peers.
126    #[method(name = "BlockPeer")]
127    async fn p2p_block_peer(&self, peer_id: &PeerId) -> Result<(), Error>;
128
129    /// ClosePeer closes the connection to a given peer.
130    #[method(name = "ClosePeer")]
131    async fn p2p_close_peer(&self, peer_id: &PeerId) -> Result<(), Error>;
132
133    /// Connect ensures there is a connection between this host and the peer with given peer.
134    #[method(name = "Connect")]
135    async fn p2p_connect(&self, address: &AddrInfo) -> Result<(), Error>;
136
137    /// Connectedness returns a state signaling connection capabilities.
138    #[method(name = "Connectedness")]
139    async fn p2p_connectedness(&self, peer_id: &PeerId) -> Result<Connectedness, Error>;
140
141    /// Info returns address information about the host.
142    #[method(name = "Info")]
143    async fn p2p_info(&self) -> Result<AddrInfo, Error>;
144
145    /// IsProtected returns whether the given peer is protected.
146    #[method(name = "IsProtected")]
147    async fn p2p_is_protected(&self, peer_id: &PeerId, tag: &str) -> Result<bool, Error>;
148
149    /// ListBlockedPeers returns a list of blocked peers.
150    #[method(name = "ListBlockedPeers")]
151    async fn p2p_list_blocked_peers(&self) -> Result<Vec<PeerId>, Error>;
152
153    /// NATStatus returns the current NAT status.
154    #[method(name = "NATStatus")]
155    async fn p2p_nat_status(&self) -> Result<Reachability, Error>;
156
157    /// PeerInfo returns a small slice of information Peerstore has on the given peer.
158    #[method(name = "PeerInfo")]
159    async fn p2p_peer_info(&self, peer_id: &PeerId) -> Result<AddrInfo, Error>;
160
161    /// Peers returns connected peers.
162    #[method(name = "Peers")]
163    async fn p2p_peers(&self) -> Result<Vec<PeerId>, Error>;
164
165    /// Protect adds a peer to the list of peers who have a bidirectional peering agreement that they are protected from being trimmed, dropped or negatively scored.
166    #[method(name = "Protect")]
167    async fn p2p_protect(&self, peer_id: &PeerId, tag: &str) -> Result<(), Error>;
168
169    // We might get null in response here, so Option is needed
170    /// PubSubPeers returns the peer IDs of the peers joined on the given topic.
171    #[method(name = "PubSubPeers")]
172    async fn p2p_pub_sub_peers(&self, topic: &str) -> Result<Option<Vec<PeerId>>, Error>;
173
174    /// ResourceState returns the state of the resource manager.
175    #[method(name = "ResourceState")]
176    async fn p2p_resource_state(&self) -> Result<ResourceManagerStats, Error>;
177
178    /// UnblockPeer removes a peer from the set of blocked peers.
179    #[method(name = "UnblockPeer")]
180    async fn p2p_unblock_peer(&self, peer_id: &PeerId) -> Result<(), Error>;
181
182    /// Unprotect removes a peer from the list of peers who have a bidirectional peering agreement that they are protected from being trimmed, dropped or negatively scored, returning a bool representing whether the given peer is protected or not.
183    #[method(name = "Unprotect")]
184    async fn p2p_unprotect(&self, peer_id: &PeerId, tag: &str) -> Result<bool, Error>;
185}