Skip to main content

karyon_p2p/
config.rs

1use std::sync::Arc;
2
3use karyon_net::Endpoint;
4
5use crate::{
6    access_control::{AccessControl, AllowAll},
7    Version,
8};
9
10/// Configuration for the p2p network.
11///
12/// `dial_peers` are static peers dialed directly (use for fixed
13/// topologies). `bootstrap_peers` are seeds for the default Kademlia
14/// discovery; the DHT grows the connection set from there. Custom
15/// `Discovery` implementations may interpret `bootstrap_peers`
16/// differently or ignore it. Pick one, or combine.
17///
18/// # Example
19///
20/// ```
21/// use karyon_p2p::Config;
22///
23/// let config = Config {
24///     listen_endpoints: vec![
25///         "tcp://0.0.0.0:8000".parse().unwrap(),
26///     ],
27///     discovery_endpoints: vec![
28///         "tcp://0.0.0.0:7000".parse().unwrap(),
29///         "udp://0.0.0.0:7000".parse().unwrap(),
30///     ],
31///     bootstrap_peers: vec![
32///         "tcp://seed.example.com:7000".parse().unwrap(),
33///     ],
34///     ..Config::default()
35/// };
36/// ```
37pub struct Config {
38    /// Represents the network version.
39    pub version: Version,
40
41    /// Enable monitor
42    pub enable_monitor: bool,
43
44    /// Policy deciding who this node talks to. Defaults to `AllowAll`.
45    pub access_control: Arc<dyn AccessControl>,
46
47    /////////////////
48    // PeerPool
49    ////////////////
50    /// Timeout duration for the handshake with new peers, in seconds.
51    /// Applied twice on an inbound connection: once for the transport
52    /// handshake (TLS, or the first QUIC stream), then once for the
53    /// protocol handshake.
54    pub handshake_timeout: u64,
55    /// Interval at which the ping protocol sends ping messages to a peer to
56    /// maintain connections, in seconds.
57    pub ping_interval: u64,
58    /// Timeout duration for receiving the pong message corresponding to the
59    /// sent ping message, in seconds.
60    pub ping_timeout: u64,
61    /// The maximum number of retries for outbound connection establishment.
62    pub max_connect_retries: usize,
63
64    /////////////////
65    // DISCOVERY
66    ////////////////
67    //
68    // Discovery is pluggable via the `Discovery` trait. The default
69    // implementation is `KademliaDiscovery`, which is what the fields
70    // below describe. Custom implementations may interpret these fields
71    // differently or ignore them entirely.
72    //
73    /// A list of bootstrap peers for the seeding process.
74    pub bootstrap_peers: Vec<Endpoint>,
75    /// Endpoints to listen on for incoming peer connections.
76    /// e.g. [tcp://0.0.0.0:8000, quic://0.0.0.0:9000]
77    pub listen_endpoints: Vec<Endpoint>,
78    /// Endpoints used by the discovery service (Kademlia by default).
79    ///
80    /// Kademlia needs two sockets that serve different roles:
81    /// - one stream endpoint for the lookup service
82    ///   (`tcp://`, `tls://`, or `quic://` when the `quic` feature is on).
83    ///   Handles short-lived FIND_NODE / Ping queries from other peers.
84    /// - one `udp://` endpoint for the refresh service.
85    ///   Handles UDP liveness pings against routing-table entries.
86    ///
87    /// Either provide both (one of each kind) or leave the vector empty
88    /// to disable Kademlia-style DHT discovery and rely on
89    /// `dial_peers` + `bootstrap_peers` for static peering.
90    ///
91    /// e.g. [tcp://0.0.0.0:7000, udp://0.0.0.0:7000]
92    pub discovery_endpoints: Vec<Endpoint>,
93    /// A list of endpoints representing peers that the `Discovery` will
94    /// manually connect to.
95    pub dial_peers: Vec<Endpoint>,
96    /// The number of available inbound slots for incoming connections.
97    pub inbound_slots: usize,
98    /// The number of available outbound slots for outgoing connections.
99    pub outbound_slots: usize,
100    /// Time interval, in seconds, at which the Discovery restarts the
101    /// seeding process.
102    pub seeding_interval: u64,
103
104    /////////////////
105    // LOOKUP
106    ////////////////
107    /// The number of available inbound slots for incoming connections during
108    /// the lookup process.
109    pub lookup_inbound_slots: usize,
110    /// The number of available outbound slots for outgoing connections during
111    /// the lookup process.
112    pub lookup_outbound_slots: usize,
113    /// Timeout duration for a peer response during the lookup process, in
114    /// seconds.
115    pub lookup_response_timeout: u64,
116    /// Maximum allowable time for a live connection with a peer during the
117    /// lookup process, in seconds.
118    pub lookup_connection_lifespan: u64,
119    /// The maximum number of retries for outbound connection establishment
120    /// during the lookup process.
121    pub lookup_connect_retries: usize,
122
123    /////////////////
124    // REFRESH
125    ////////////////
126    /// Interval at which the table refreshes its entries, in seconds.
127    pub refresh_interval: u64,
128    /// Timeout duration for a peer response during the table refresh process,
129    /// in seconds.
130    pub refresh_response_timeout: u64,
131    /// The maximum number of retries for outbound connection establishment
132    /// during the refresh process.
133    pub refresh_connect_retries: usize,
134}
135
136impl Default for Config {
137    fn default() -> Self {
138        Config {
139            version: "0.1.0".parse().unwrap(),
140
141            enable_monitor: false,
142            access_control: Arc::new(AllowAll),
143
144            handshake_timeout: 4,
145            ping_interval: 20,
146            ping_timeout: 2,
147
148            bootstrap_peers: vec![],
149            listen_endpoints: vec![],
150            discovery_endpoints: vec![],
151            dial_peers: vec![],
152            inbound_slots: 12,
153            outbound_slots: 12,
154            max_connect_retries: 3,
155            seeding_interval: 60,
156
157            lookup_inbound_slots: 20,
158            lookup_outbound_slots: 20,
159            lookup_response_timeout: 1,
160            lookup_connection_lifespan: 3,
161            lookup_connect_retries: 3,
162
163            refresh_interval: 1800,
164            refresh_response_timeout: 1,
165            refresh_connect_retries: 3,
166        }
167    }
168}