Skip to main content

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