1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
use crate::discv5::Enr;
use std::time::Duration;
#[derive(Clone)]
pub struct Discv5Config {
pub request_timeout: Duration,
pub query_peer_timeout: Duration,
pub query_timeout: Duration,
pub request_retries: u8,
pub session_timeout: Duration,
pub session_establish_timeout: Duration,
pub enr_update: bool,
pub enr_peer_update_min: usize,
pub query_parallelism: usize,
pub ip_limit: bool,
pub table_filter: fn(&Enr) -> bool,
pub ping_interval: Duration,
}
impl Default for Discv5Config {
fn default() -> Self {
Self {
request_timeout: Duration::from_secs(4),
query_peer_timeout: Duration::from_secs(2),
query_timeout: Duration::from_secs(60),
request_retries: 1,
session_timeout: Duration::from_secs(86400),
session_establish_timeout: Duration::from_secs(15),
enr_update: true,
enr_peer_update_min: 10,
query_parallelism: 3,
ip_limit: false,
table_filter: |_| true,
ping_interval: Duration::from_secs(300),
}
}
}
#[derive(Debug)]
pub struct Discv5ConfigBuilder {
config: Discv5Config,
}
impl Default for Discv5ConfigBuilder {
fn default() -> Self {
Self {
config: Discv5Config::default(),
}
}
}
impl Discv5ConfigBuilder {
pub fn new() -> Self {
Discv5ConfigBuilder::default()
}
pub fn request_timeout(&mut self, timeout: Duration) -> &mut Self {
self.config.request_timeout = timeout;
self
}
pub fn query_timeout(&mut self, timeout: Duration) -> &mut Self {
self.config.query_timeout = timeout;
self
}
pub fn query_peer_timeout(&mut self, timeout: Duration) -> &mut Self {
self.config.query_peer_timeout = timeout;
self
}
pub fn request_retries(&mut self, retries: u8) -> &mut Self {
self.config.request_retries = retries;
self
}
pub fn session_timeout(&mut self, timeout: Duration) -> &mut Self {
self.config.session_timeout = timeout;
self
}
pub fn session_establish_timeout(&mut self, timeout: Duration) -> &mut Self {
self.config.session_establish_timeout = timeout;
self
}
pub fn enr_update(&mut self, update: bool) -> &mut Self {
self.config.enr_update = update;
self
}
pub fn enr_peer_update_min(&mut self, min: usize) -> &mut Self {
if min < 2 {
panic!("Setting enr_peer_update_min to a value less than 2 will cause issues with discovery with peers behind NAT");
}
self.config.enr_peer_update_min = min;
self
}
pub fn query_parallelism(&mut self, parallelism: usize) -> &mut Self {
self.config.query_parallelism = parallelism;
self
}
pub fn ip_limit(&mut self, ip_limit: bool) -> &mut Self {
self.config.ip_limit = ip_limit;
self
}
pub fn table_filter(&mut self, filter: fn(&Enr) -> bool) -> &mut Self {
self.config.table_filter = filter;
self
}
pub fn ping_interval(&mut self, interval: Duration) -> &mut Self {
self.config.ping_interval = interval;
self
}
pub fn build(&self) -> Discv5Config {
self.config.clone()
}
}
impl std::fmt::Debug for Discv5Config {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let mut builder = f.debug_struct("Discv5Config");
let _ = builder.field("request_timeout", &self.request_timeout);
let _ = builder.field("query_timeout", &self.query_timeout);
let _ = builder.field("query_peer_timeout", &self.query_peer_timeout);
let _ = builder.field("request_retries", &self.request_retries);
let _ = builder.field("session_timeout", &self.session_timeout);
let _ = builder.field("session_establish_timeout", &self.session_establish_timeout);
let _ = builder.field("enr_update", &self.enr_update);
let _ = builder.field("query_parallelism", &self.query_parallelism);
let _ = builder.field("ip_limit", &self.ip_limit);
let _ = builder.field("ping_interval", &self.ping_interval);
builder.finish()
}
}