cuprate_p2p_core/
types.rs

1//! General data structures.
2
3use std::time::{Duration, Instant};
4
5use cuprate_pruning::PruningSeed;
6use cuprate_types::{AddressType, ConnectionState};
7
8use crate::NetZoneAddress;
9
10/// Data within [`crate::services::AddressBookRequest::SetBan`].
11pub struct SetBan<A: NetZoneAddress> {
12    /// Address of the peer.
13    pub address: A,
14    /// - If [`Some`], how long this peer should be banned for
15    /// - If [`None`], the peer will be unbanned
16    pub ban: Option<Duration>,
17}
18
19/// Data within [`crate::services::AddressBookResponse::GetBans`].
20pub struct BanState<A: NetZoneAddress> {
21    /// Address of the peer.
22    pub address: A,
23    /// - If [`Some`], the peer is banned until this [`Instant`]
24    /// - If [`None`], the peer is not currently banned
25    pub unban_instant: Option<Instant>,
26}
27
28/// Data within [`crate::services::AddressBookResponse::ConnectionInfo`].
29pub struct ConnectionInfo<A: NetZoneAddress> {
30    // The following fields are mostly the same as `monerod`.
31    pub address: A,
32    pub address_type: AddressType,
33    pub avg_download: u64,
34    pub avg_upload: u64,
35    pub current_download: u64,
36    pub current_upload: u64,
37    pub height: u64,
38    /// Either a domain or an IP without the port.
39    pub host: String,
40    pub incoming: bool,
41    pub live_time: u64,
42    pub localhost: bool,
43    pub local_ip: bool,
44    pub peer_id: u64,
45    pub pruning_seed: PruningSeed,
46    pub recv_count: u64,
47    pub recv_idle_time: u64,
48    pub rpc_credits_per_hash: u32,
49    pub rpc_port: u16,
50    pub send_count: u64,
51    pub send_idle_time: u64,
52    pub state: ConnectionState,
53    pub support_flags: u32,
54
55    // The following fields are slightly different than `monerod`.
56
57    //
58    /// [`None`] if Tor/i2p or unknown.
59    pub socket_addr: Option<std::net::SocketAddr>,
60
61    /// This field does not exist for `cuprated`'s RPC, this is just a marker type:
62    /// - <https://github.com/Cuprate/cuprate/pull/320#discussion_r1811335020>
63    /// - <https://github.com/Cuprate/cuprate/pull/320#discussion_r1819826080>
64    ///
65    /// [`ConnectionId::DEFAULT_STR`] is used when mapping to the RPC type.
66    pub connection_id: ConnectionId,
67}
68
69/// Marker type for `monerod`'s connection ID.
70///
71/// `connection_id` is a 128-bit `uuid` in `monerod`.
72/// `cuprated` does not support this field so it returns
73/// the default value in the RPC interface, an all 0-bit UUID.
74///
75/// This default value in string form is [`ConnectionId::DEFAULT_STR`].
76#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
77pub struct ConnectionId;
78
79impl ConnectionId {
80    /// [`str`] representation of a default connection ID.
81    pub const DEFAULT_STR: &str = "00000000000000000000000000000000";
82}
83
84/// Used in RPC's `sync_info`.
85///
86// TODO: fix docs after <https://github.com/Cuprate/cuprate/pull/320#discussion_r1811089758>
87// Data within [`crate::services::AddressBookResponse::Spans`].
88#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
89pub struct Span<A: NetZoneAddress> {
90    pub nblocks: u64,
91    pub rate: u32,
92    pub remote_address: A,
93    pub size: u64,
94    pub speed: u32,
95    pub start_block_height: u64,
96}