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