cuprate_p2p/
config.rs

1use cuprate_helper::network::Network;
2use cuprate_p2p_core::NetworkZone;
3use cuprate_wire::{common::PeerSupportFlags, BasicNodeData};
4
5pub use cuprate_address_book::AddressBookConfig;
6
7/// P2P config.
8#[derive(Clone, Debug)]
9pub struct P2PConfig<N: 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<N::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, only relevant if [`P2PConfig::server_config`] is set to [`Some`]
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 inbound server configuration,
26    ///
27    /// If this is [`None`] no inbound connections will be accepted.
28    pub server_config: Option<N::ServerCfg>,
29
30    /// The port to listen on for inbound connections, only relevant if [`P2PConfig::server_config`] is set to [`Some`].
31    pub p2p_port: u16,
32    /// The public RPC port to tell peers about so wallets can use our node. `0` if we do not have a public RPC port.
33    pub rpc_port: u16,
34
35    /// The [`AddressBookConfig`].
36    pub address_book_config: AddressBookConfig,
37}
38
39impl<N: NetworkZone> P2PConfig<N> {
40    /// Returns the [`BasicNodeData`] for this [`P2PConfig`].
41    ///
42    /// [`BasicNodeData::peer_id`] is set to a random u64, so this function should only be called once
43    /// per [`NetworkZone`] per run.
44    pub(crate) fn basic_node_data(&self) -> BasicNodeData {
45        BasicNodeData {
46            my_port: u32::from(self.p2p_port),
47            network_id: self.network.network_id(),
48            peer_id: rand::random(),
49            support_flags: PeerSupportFlags::FLUFFY_BLOCKS,
50            rpc_port: self.rpc_port,
51            // We do not (and probably will never) support paying for RPC with hashes.
52            rpc_credits_per_hash: 0,
53        }
54    }
55}