reth_network_types/peers/
config.rs1use std::{collections::HashSet, time::Duration};
4
5use reth_net_banlist::{BanList, IpFilter};
6use reth_network_peers::{NodeRecord, TrustedPeer};
7
8use crate::{peers::PersistedPeerInfo, BackoffKind, ReputationChangeWeights};
9
10pub const DEFAULT_MAX_COUNT_PEERS_OUTBOUND: u32 = 100;
12
13pub const DEFAULT_MAX_COUNT_PEERS_INBOUND: u32 = 30;
15
16pub const DEFAULT_MAX_COUNT_CONCURRENT_OUTBOUND_DIALS: usize = 30;
20
21pub const INBOUND_IP_THROTTLE_DURATION: Duration = Duration::from_secs(30);
23
24#[derive(Debug, Clone, Copy, PartialEq, Eq)]
28#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
29pub struct PeerBackoffDurations {
30 #[cfg_attr(feature = "serde", serde(with = "humantime_serde"))]
33 pub low: Duration,
34 #[cfg_attr(feature = "serde", serde(with = "humantime_serde"))]
37 pub medium: Duration,
38 #[cfg_attr(feature = "serde", serde(with = "humantime_serde"))]
40 pub high: Duration,
41 #[cfg_attr(feature = "serde", serde(with = "humantime_serde"))]
43 pub max: Duration,
44}
45
46impl PeerBackoffDurations {
47 pub const fn backoff(&self, kind: BackoffKind) -> Duration {
49 match kind {
50 BackoffKind::Low => self.low,
51 BackoffKind::Medium => self.medium,
52 BackoffKind::High => self.high,
53 }
54 }
55
56 pub fn backoff_until(&self, kind: BackoffKind, backoff_counter: u8) -> std::time::Instant {
60 let backoff_time = self.backoff(kind);
61 let backoff_time = backoff_time + backoff_time * backoff_counter as u32;
62 let now = std::time::Instant::now();
63 now + backoff_time.min(self.max)
64 }
65
66 #[cfg(any(test, feature = "test-utils"))]
68 pub const fn test() -> Self {
69 Self {
70 low: Duration::from_millis(200),
71 medium: Duration::from_millis(200),
72 high: Duration::from_millis(200),
73 max: Duration::from_millis(200),
74 }
75 }
76}
77
78impl Default for PeerBackoffDurations {
79 fn default() -> Self {
80 Self {
81 low: Duration::from_secs(30),
82 medium: Duration::from_secs(60 * 3),
84 high: Duration::from_secs(60 * 15),
86 max: Duration::from_secs(60 * 60),
88 }
89 }
90}
91
92#[derive(Debug, Clone, PartialEq, Eq)]
94#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize), serde(default))]
95pub struct ConnectionsConfig {
96 pub max_outbound: usize,
98 pub max_inbound: usize,
100 #[cfg_attr(feature = "serde", serde(default))]
102 pub max_concurrent_outbound_dials: usize,
103}
104
105impl Default for ConnectionsConfig {
106 fn default() -> Self {
107 Self {
108 max_outbound: DEFAULT_MAX_COUNT_PEERS_OUTBOUND as usize,
109 max_inbound: DEFAULT_MAX_COUNT_PEERS_INBOUND as usize,
110 max_concurrent_outbound_dials: DEFAULT_MAX_COUNT_CONCURRENT_OUTBOUND_DIALS,
111 }
112 }
113}
114
115#[derive(Debug, Clone, PartialEq, Eq)]
117#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
118#[cfg_attr(feature = "serde", serde(default))]
119pub struct PeersConfig {
120 #[cfg_attr(feature = "serde", serde(with = "humantime_serde"))]
122 pub refill_slots_interval: Duration,
123 pub trusted_nodes: Vec<TrustedPeer>,
125 #[cfg_attr(feature = "serde", serde(alias = "connect_trusted_nodes_only"))]
127 pub trusted_nodes_only: bool,
128 #[cfg_attr(feature = "serde", serde(with = "humantime_serde"))]
130 pub trusted_nodes_resolution_interval: Duration,
131 pub max_backoff_count: u8,
141 #[cfg_attr(feature = "serde", serde(skip))]
143 pub basic_nodes: HashSet<NodeRecord>,
144 #[cfg_attr(feature = "serde", serde(skip))]
146 pub persisted_peers: Vec<PersistedPeerInfo>,
147 #[cfg_attr(feature = "serde", serde(with = "humantime_serde"))]
149 pub ban_duration: Duration,
150 #[cfg_attr(feature = "serde", serde(skip))]
152 pub ban_list: BanList,
153 pub connection_info: ConnectionsConfig,
155 pub reputation_weights: ReputationChangeWeights,
157 pub backoff_durations: PeerBackoffDurations,
161 #[cfg_attr(feature = "serde", serde(default, with = "humantime_serde"))]
165 pub incoming_ip_throttle_duration: Duration,
166 #[cfg_attr(feature = "serde", serde(skip))]
171 pub ip_filter: IpFilter,
172 pub enforce_enr_fork_id: bool,
177}
178
179impl Default for PeersConfig {
180 fn default() -> Self {
181 Self {
182 refill_slots_interval: Duration::from_millis(5_000),
183 connection_info: Default::default(),
184 reputation_weights: Default::default(),
185 ban_list: Default::default(),
186 ban_duration: Duration::from_secs(60 * 60 * 12),
188 backoff_durations: Default::default(),
189 trusted_nodes: Default::default(),
190 trusted_nodes_only: false,
191 trusted_nodes_resolution_interval: Duration::from_secs(60 * 60),
192 basic_nodes: Default::default(),
193 persisted_peers: Default::default(),
194 max_backoff_count: 5,
195 incoming_ip_throttle_duration: INBOUND_IP_THROTTLE_DURATION,
196 ip_filter: IpFilter::default(),
197 enforce_enr_fork_id: false,
198 }
199 }
200}
201
202impl PeersConfig {
203 pub fn with_ban_list(mut self, ban_list: BanList) -> Self {
205 self.ban_list = ban_list;
206 self
207 }
208
209 pub const fn with_ban_duration(mut self, ban_duration: Duration) -> Self {
211 self.ban_duration = ban_duration;
212 self
213 }
214
215 pub const fn with_refill_slots_interval(mut self, interval: Duration) -> Self {
217 self.refill_slots_interval = interval;
218 self
219 }
220
221 pub const fn with_max_outbound(mut self, max_outbound: usize) -> Self {
223 self.connection_info.max_outbound = max_outbound;
224 self
225 }
226
227 pub const fn with_max_inbound_opt(mut self, max_inbound: Option<usize>) -> Self {
229 if let Some(max_inbound) = max_inbound {
230 self.connection_info.max_inbound = max_inbound;
231 }
232 self
233 }
234
235 pub const fn with_max_outbound_opt(mut self, max_outbound: Option<usize>) -> Self {
237 if let Some(max_outbound) = max_outbound {
238 self.connection_info.max_outbound = max_outbound;
239 }
240 self
241 }
242
243 pub const fn with_max_inbound(mut self, max_inbound: usize) -> Self {
245 self.connection_info.max_inbound = max_inbound;
246 self
247 }
248
249 pub const fn with_max_concurrent_dials(mut self, max_concurrent_outbound_dials: usize) -> Self {
251 self.connection_info.max_concurrent_outbound_dials = max_concurrent_outbound_dials;
252 self
253 }
254
255 pub fn with_trusted_nodes(mut self, nodes: Vec<TrustedPeer>) -> Self {
257 self.trusted_nodes = nodes;
258 self
259 }
260
261 pub const fn with_trusted_nodes_only(mut self, trusted_only: bool) -> Self {
263 self.trusted_nodes_only = trusted_only;
264 self
265 }
266
267 pub fn with_basic_nodes(mut self, nodes: HashSet<NodeRecord>) -> Self {
269 self.basic_nodes = nodes;
270 self
271 }
272
273 pub const fn with_max_backoff_count(mut self, max_backoff_count: u8) -> Self {
275 self.max_backoff_count = max_backoff_count;
276 self
277 }
278
279 pub const fn with_reputation_weights(
281 mut self,
282 reputation_weights: ReputationChangeWeights,
283 ) -> Self {
284 self.reputation_weights = reputation_weights;
285 self
286 }
287
288 pub const fn with_backoff_durations(mut self, backoff_durations: PeerBackoffDurations) -> Self {
290 self.backoff_durations = backoff_durations;
291 self
292 }
293
294 pub const fn max_peers(&self) -> usize {
296 self.connection_info.max_outbound + self.connection_info.max_inbound
297 }
298
299 #[cfg(feature = "serde")]
306 pub fn with_basic_nodes_from_file(
307 mut self,
308 optional_file: Option<impl AsRef<std::path::Path>>,
309 ) -> Result<Self, std::io::Error> {
310 let Some(file_path) = optional_file else { return Ok(self) };
311 let raw = match std::fs::read_to_string(file_path.as_ref()) {
312 Ok(contents) => contents,
313 Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(self),
314 Err(e) => return Err(e),
315 };
316
317 tracing::info!(target: "net::peers", file = %file_path.as_ref().display(), "Loading saved peers");
318
319 let peers: Vec<PersistedPeerInfo> = serde_json::from_str(&raw)
321 .or_else(|_| {
322 let nodes: HashSet<NodeRecord> = serde_json::from_str(&raw)?;
323 Ok::<_, serde_json::Error>(
324 nodes.into_iter().map(PersistedPeerInfo::from_node_record).collect(),
325 )
326 })
327 .map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e))?;
328
329 tracing::info!(target: "net::peers", count = peers.len(), "Loaded persisted peers");
330 self.persisted_peers = peers;
331 Ok(self)
332 }
333
334 pub fn with_ip_filter(mut self, ip_filter: IpFilter) -> Self {
336 self.ip_filter = ip_filter;
337 self
338 }
339
340 pub const fn with_enforce_enr_fork_id(mut self, enforce: bool) -> Self {
343 self.enforce_enr_fork_id = enforce;
344 self
345 }
346
347 #[cfg(any(test, feature = "test-utils"))]
349 pub fn test() -> Self {
350 Self {
351 refill_slots_interval: Duration::from_millis(100),
352 backoff_durations: PeerBackoffDurations::test(),
353 ban_duration: Duration::from_millis(200),
354 ..Default::default()
355 }
356 }
357}