|
1 | 1 | //! A set of configuration parameters to tune the discovery protocol.
|
2 | 2 | use crate::{
|
3 |
| - kbucket::MAX_NODES_PER_BUCKET, socket::ListenConfig, Enr, Executor, PermitBanList, RateLimiter, |
| 3 | + kbucket::MAX_NODES_PER_BUCKET, Enr, Executor, ListenConfig, PermitBanList, RateLimiter, |
4 | 4 | RateLimiterBuilder,
|
5 | 5 | };
|
6 |
| -use std::time::Duration; |
| 6 | + |
| 7 | +/// The minimum number of unreachable Sessions a node must allow. This enables the network to |
| 8 | +/// boostrap. |
| 9 | +const MIN_SESSIONS_UNREACHABLE_ENR: usize = 10; |
| 10 | + |
| 11 | +use std::{ops::RangeInclusive, time::Duration}; |
7 | 12 |
|
8 | 13 | /// Configuration parameters that define the performance of the discovery network.
|
9 | 14 | #[derive(Clone)]
|
@@ -96,6 +101,15 @@ pub struct Discv5Config {
|
96 | 101 | /// timing support. By default, the executor that created the discv5 struct will be used.
|
97 | 102 | pub executor: Option<Box<dyn Executor + Send + Sync>>,
|
98 | 103 |
|
| 104 | + /// The max limit for peers with unreachable ENRs. Benevolent examples of such peers are peers |
| 105 | + /// that are discovering their externally reachable socket, nodes must assist at least one |
| 106 | + /// such peer in discovering their reachable socket via ip voting, and peers behind symmetric |
| 107 | + /// NAT. Default is no limit. Minimum is 10. |
| 108 | + pub unreachable_enr_limit: Option<usize>, |
| 109 | + |
| 110 | + /// The unused port range to try and bind to when testing if this node is behind NAT based on |
| 111 | + /// observed address reported at runtime by peers. |
| 112 | + pub unused_port_range: Option<RangeInclusive<u16>>, |
99 | 113 | /// Configuration for the sockets to listen on.
|
100 | 114 | pub listen_config: ListenConfig,
|
101 | 115 | }
|
@@ -142,6 +156,8 @@ impl Discv5ConfigBuilder {
|
142 | 156 | permit_ban_list: PermitBanList::default(),
|
143 | 157 | ban_duration: Some(Duration::from_secs(3600)), // 1 hour
|
144 | 158 | executor: None,
|
| 159 | + unreachable_enr_limit: None, |
| 160 | + unused_port_range: None, |
145 | 161 | listen_config,
|
146 | 162 | };
|
147 | 163 |
|
@@ -302,13 +318,33 @@ impl Discv5ConfigBuilder {
|
302 | 318 | self
|
303 | 319 | }
|
304 | 320 |
|
| 321 | + /// Sets the maximum number of sessions with peers with unreachable ENRs to allow. Minimum is 1 |
| 322 | + /// peer. Default is no limit. |
| 323 | + pub fn unreachable_enr_limit(&mut self, peer_limit: Option<usize>) -> &mut Self { |
| 324 | + self.config.unreachable_enr_limit = peer_limit; |
| 325 | + self |
| 326 | + } |
| 327 | + |
| 328 | + /// Sets the unused port range for testing if node is behind a NAT. Default is the range |
| 329 | + /// covering user and dynamic ports. |
| 330 | + pub fn unused_port_range( |
| 331 | + &mut self, |
| 332 | + unused_port_range: Option<RangeInclusive<u16>>, |
| 333 | + ) -> &mut Self { |
| 334 | + self.config.unused_port_range = unused_port_range; |
| 335 | + self |
| 336 | + } |
| 337 | + |
305 | 338 | pub fn build(&mut self) -> Discv5Config {
|
306 | 339 | // If an executor is not provided, assume a current tokio runtime is running.
|
307 | 340 | if self.config.executor.is_none() {
|
308 | 341 | self.config.executor = Some(Box::<crate::executor::TokioExecutor>::default());
|
309 | 342 | };
|
310 | 343 |
|
311 | 344 | assert!(self.config.incoming_bucket_limit <= MAX_NODES_PER_BUCKET);
|
| 345 | + if let Some(limit) = self.config.unreachable_enr_limit { |
| 346 | + assert!(limit >= MIN_SESSIONS_UNREACHABLE_ENR); |
| 347 | + } |
312 | 348 |
|
313 | 349 | self.config.clone()
|
314 | 350 | }
|
|
0 commit comments