cuprate_p2p/
config.rs

1use cuprate_helper::network::Network;
2use cuprate_p2p_core::{NetworkZone, Transport};
3use cuprate_wire::{common::PeerSupportFlags, BasicNodeData};
4
5pub use cuprate_address_book::AddressBookConfig;
6
7/// P2P config.
8#[derive(Clone, Debug)]
9pub struct P2PConfig<Z: NetworkZone> {
10    /// The [`Network`] we should connect to.
11    pub network: Network,
12    /// Seed nodes to connect to find peers if our address book is empty.
13    pub seeds: Vec<Z::Addr>,
14
15    /// The number of outbound connections to make and try keep.
16    pub outbound_connections: usize,
17    /// The amount of extra connections we can make if we are under load from the rest of Cuprate.
18    pub extra_outbound_connections: usize,
19    /// The maximum amount of inbound connections.
20    pub max_inbound_connections: usize,
21    /// The percent of outbound peers that should be gray aka never connected to before.
22    ///
23    /// Only values 0..=1 are valid.
24    pub gray_peers_percent: f64,
25    /// The port to listen on for inbound connections.
26    pub p2p_port: u16,
27    /// The public RPC port to tell peers about so wallets can use our node. `0` if we do not have a public RPC port.
28    pub rpc_port: u16,
29
30    /// The [`AddressBookConfig`].
31    pub address_book_config: AddressBookConfig,
32}
33
34/// Configuration part responsible of transportation
35pub struct TransportConfig<Z: NetworkZone, T: Transport<Z>> {
36    /// The outbound client configuration
37    pub client_config: T::ClientConfig,
38    /// The inbound server configuration,
39    ///
40    /// If this is [`None`] no inbound connections will be accepted.
41    pub server_config: Option<T::ServerConfig>,
42}
43
44impl<Z: NetworkZone> P2PConfig<Z> {
45    /// Returns the [`BasicNodeData`] for this [`P2PConfig`].
46    ///
47    /// [`BasicNodeData::peer_id`] is set to a random u64, so this function should only be called once
48    /// per [`NetworkZone`] per run.
49    pub(crate) fn basic_node_data(&self) -> BasicNodeData {
50        BasicNodeData {
51            my_port: u32::from(self.p2p_port),
52            network_id: self.network.network_id(),
53            peer_id: rand::random(),
54            support_flags: PeerSupportFlags::FLUFFY_BLOCKS,
55            rpc_port: self.rpc_port,
56            // We do not (and probably will never) support paying for RPC with hashes.
57            rpc_credits_per_hash: 0,
58        }
59    }
60}