Skip to main content

reth_network_types/peers/
config.rs

1//! Configuration for peering.
2
3use 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
10/// Maximum number of available slots for outbound sessions.
11pub const DEFAULT_MAX_COUNT_PEERS_OUTBOUND: u32 = 100;
12
13/// Maximum number of available slots for inbound sessions.
14pub const DEFAULT_MAX_COUNT_PEERS_INBOUND: u32 = 30;
15
16/// Maximum number of available slots for concurrent outgoing dials.
17///
18/// This restricts how many outbound dials can be performed concurrently.
19pub const DEFAULT_MAX_COUNT_CONCURRENT_OUTBOUND_DIALS: usize = 30;
20
21/// A temporary timeout for ips on incoming connection attempts.
22pub const INBOUND_IP_THROTTLE_DURATION: Duration = Duration::from_secs(30);
23
24/// The durations to use when a backoff should be applied to a peer.
25///
26/// See also [`BackoffKind`].
27#[derive(Debug, Clone, Copy, PartialEq, Eq)]
28#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
29pub struct PeerBackoffDurations {
30    /// Applies to connection problems where there is a chance that they will be resolved after the
31    /// short duration.
32    #[cfg_attr(feature = "serde", serde(with = "humantime_serde"))]
33    pub low: Duration,
34    /// Applies to more severe connection problems where there is a lower chance that they will be
35    /// resolved.
36    #[cfg_attr(feature = "serde", serde(with = "humantime_serde"))]
37    pub medium: Duration,
38    /// Intended for spammers, or bad peers in general.
39    #[cfg_attr(feature = "serde", serde(with = "humantime_serde"))]
40    pub high: Duration,
41    /// Maximum total backoff duration.
42    #[cfg_attr(feature = "serde", serde(with = "humantime_serde"))]
43    pub max: Duration,
44}
45
46impl PeerBackoffDurations {
47    /// Returns the corresponding [`Duration`]
48    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    /// Returns the timestamp until which we should backoff.
57    ///
58    /// The Backoff duration is capped by the configured maximum backoff duration.
59    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    /// Returns durations for testing.
67    #[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            // 3min
83            medium: Duration::from_secs(60 * 3),
84            // 15min
85            high: Duration::from_secs(60 * 15),
86            // 1h
87            max: Duration::from_secs(60 * 60),
88        }
89    }
90}
91
92/// Tracks stats about connected nodes
93#[derive(Debug, Clone, PartialEq, Eq)]
94#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize), serde(default))]
95pub struct ConnectionsConfig {
96    /// Maximum allowed outbound connections.
97    pub max_outbound: usize,
98    /// Maximum allowed inbound connections.
99    pub max_inbound: usize,
100    /// Maximum allowed concurrent outbound dials.
101    #[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/// Config type for initiating a `PeersManager` instance.
116#[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    /// How often to recheck free slots for outbound connections.
121    #[cfg_attr(feature = "serde", serde(with = "humantime_serde"))]
122    pub refill_slots_interval: Duration,
123    /// Trusted nodes to connect to or accept from
124    pub trusted_nodes: Vec<TrustedPeer>,
125    /// Connect to or accept from trusted nodes only?
126    #[cfg_attr(feature = "serde", serde(alias = "connect_trusted_nodes_only"))]
127    pub trusted_nodes_only: bool,
128    /// Interval to update trusted nodes DNS resolution
129    #[cfg_attr(feature = "serde", serde(with = "humantime_serde"))]
130    pub trusted_nodes_resolution_interval: Duration,
131    /// Maximum number of backoff attempts before we give up on a peer and dropping.
132    ///
133    /// The max time spent of a peer before it's removed from the set is determined by the
134    /// configured backoff duration and the max backoff count.
135    ///
136    /// With a backoff counter of 5 and a backoff duration of 1h, the minimum time spent of the
137    /// peer in the table is the sum of all backoffs (1h + 2h + 3h + 4h + 5h = 15h).
138    ///
139    /// Note: this does not apply to trusted peers.
140    pub max_backoff_count: u8,
141    /// Basic nodes to connect to.
142    #[cfg_attr(feature = "serde", serde(skip))]
143    pub basic_nodes: HashSet<NodeRecord>,
144    /// Peers restored from a previous run, containing richer metadata than basic nodes.
145    #[cfg_attr(feature = "serde", serde(skip))]
146    pub persisted_peers: Vec<PersistedPeerInfo>,
147    /// How long to ban bad peers.
148    #[cfg_attr(feature = "serde", serde(with = "humantime_serde"))]
149    pub ban_duration: Duration,
150    /// Restrictions on `PeerIds` and Ips.
151    #[cfg_attr(feature = "serde", serde(skip))]
152    pub ban_list: BanList,
153    /// Restrictions on connections.
154    pub connection_info: ConnectionsConfig,
155    /// How to weigh reputation changes.
156    pub reputation_weights: ReputationChangeWeights,
157    /// How long to backoff peers that we are failed to connect to for non-fatal reasons.
158    ///
159    /// The backoff duration increases with number of backoff attempts.
160    pub backoff_durations: PeerBackoffDurations,
161    /// How long to temporarily ban ips on incoming connection attempts.
162    ///
163    /// This acts as an IP based rate limit.
164    #[cfg_attr(feature = "serde", serde(default, with = "humantime_serde"))]
165    pub incoming_ip_throttle_duration: Duration,
166    /// IP address filter for restricting network connections to specific IP ranges.
167    ///
168    /// Similar to geth's --netrestrict flag. If configured, only connections to/from
169    /// IPs within the specified CIDR ranges will be allowed.
170    #[cfg_attr(feature = "serde", serde(skip))]
171    pub ip_filter: IpFilter,
172    /// If true, discovered peers without a confirmed ENR [`ForkId`](alloy_eip2124::ForkId)
173    /// (EIP-868) will not be added to the peer set until their fork ID is verified.
174    ///
175    /// This filters out peers from other networks that pollute the discovery table.
176    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 peers for 12h
187            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    /// A set of `peer_ids` and ip addr that we want to never connect to
204    pub fn with_ban_list(mut self, ban_list: BanList) -> Self {
205        self.ban_list = ban_list;
206        self
207    }
208
209    /// Configure how long to ban bad peers
210    pub const fn with_ban_duration(mut self, ban_duration: Duration) -> Self {
211        self.ban_duration = ban_duration;
212        self
213    }
214
215    /// Configure how long to refill outbound slots
216    pub const fn with_refill_slots_interval(mut self, interval: Duration) -> Self {
217        self.refill_slots_interval = interval;
218        self
219    }
220
221    /// Maximum allowed outbound connections.
222    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    /// Maximum allowed inbound connections with optional update.
228    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    /// Maximum allowed outbound connections with optional update.
236    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    /// Maximum allowed inbound connections.
244    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    /// Maximum allowed concurrent outbound dials.
250    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    /// Nodes to always connect to.
256    pub fn with_trusted_nodes(mut self, nodes: Vec<TrustedPeer>) -> Self {
257        self.trusted_nodes = nodes;
258        self
259    }
260
261    /// Connect only to trusted nodes.
262    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    /// Nodes available at launch.
268    pub fn with_basic_nodes(mut self, nodes: HashSet<NodeRecord>) -> Self {
269        self.basic_nodes = nodes;
270        self
271    }
272
273    /// Configures the max allowed backoff count.
274    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    /// Configures how to weigh reputation changes.
280    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    /// Configures how long to backoff peers that are we failed to connect to for non-fatal reasons
289    pub const fn with_backoff_durations(mut self, backoff_durations: PeerBackoffDurations) -> Self {
290        self.backoff_durations = backoff_durations;
291        self
292    }
293
294    /// Returns the maximum number of peers, inbound and outbound.
295    pub const fn max_peers(&self) -> usize {
296        self.connection_info.max_outbound + self.connection_info.max_inbound
297    }
298
299    /// Read persisted peers from file at launch.
300    ///
301    /// Supports both the current [`PersistedPeerInfo`] format and the legacy `Vec<NodeRecord>`
302    /// format. Legacy entries are converted to [`PersistedPeerInfo`] with default metadata.
303    ///
304    /// Ignored if `optional_file` is `None` or the file does not exist.
305    #[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        // Try the new format first, fall back to legacy Vec<NodeRecord>
320        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    /// Configure the IP filter for restricting network connections to specific IP ranges.
335    pub fn with_ip_filter(mut self, ip_filter: IpFilter) -> Self {
336        self.ip_filter = ip_filter;
337        self
338    }
339
340    /// If set, discovered peers without a confirmed ENR [`ForkId`](alloy_eip2124::ForkId) will not
341    /// be added to the peer set until their fork ID is verified via EIP-868.
342    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    /// Returns settings for testing
348    #[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}