reth_network_types/peers/
config.rs
1use std::{
4 collections::HashSet,
5 io::{self, ErrorKind},
6 path::Path,
7 time::Duration,
8};
9
10use reth_net_banlist::BanList;
11use reth_network_peers::{NodeRecord, TrustedPeer};
12use tracing::info;
13
14use crate::{BackoffKind, ReputationChangeWeights};
15
16pub const DEFAULT_MAX_COUNT_PEERS_OUTBOUND: u32 = 100;
18
19pub const DEFAULT_MAX_COUNT_PEERS_INBOUND: u32 = 30;
21
22pub const DEFAULT_MAX_COUNT_CONCURRENT_OUTBOUND_DIALS: usize = 15;
26
27pub const INBOUND_IP_THROTTLE_DURATION: Duration = Duration::from_secs(30);
29
30#[derive(Debug, Clone, Copy, PartialEq, Eq)]
34#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
35pub struct PeerBackoffDurations {
36 #[cfg_attr(feature = "serde", serde(with = "humantime_serde"))]
39 pub low: Duration,
40 #[cfg_attr(feature = "serde", serde(with = "humantime_serde"))]
43 pub medium: Duration,
44 #[cfg_attr(feature = "serde", serde(with = "humantime_serde"))]
46 pub high: Duration,
47 #[cfg_attr(feature = "serde", serde(with = "humantime_serde"))]
49 pub max: Duration,
50}
51
52impl PeerBackoffDurations {
53 pub const fn backoff(&self, kind: BackoffKind) -> Duration {
55 match kind {
56 BackoffKind::Low => self.low,
57 BackoffKind::Medium => self.medium,
58 BackoffKind::High => self.high,
59 }
60 }
61
62 pub fn backoff_until(&self, kind: BackoffKind, backoff_counter: u8) -> std::time::Instant {
66 let backoff_time = self.backoff(kind);
67 let backoff_time = backoff_time + backoff_time * backoff_counter as u32;
68 let now = std::time::Instant::now();
69 now + backoff_time.min(self.max)
70 }
71
72 #[cfg(any(test, feature = "test-utils"))]
74 pub const fn test() -> Self {
75 Self {
76 low: Duration::from_millis(200),
77 medium: Duration::from_millis(200),
78 high: Duration::from_millis(200),
79 max: Duration::from_millis(200),
80 }
81 }
82}
83
84impl Default for PeerBackoffDurations {
85 fn default() -> Self {
86 Self {
87 low: Duration::from_secs(30),
88 medium: Duration::from_secs(60 * 3),
90 high: Duration::from_secs(60 * 15),
92 max: Duration::from_secs(60 * 60),
94 }
95 }
96}
97
98#[derive(Debug, Clone, PartialEq, Eq)]
100#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize), serde(default))]
101pub struct ConnectionsConfig {
102 pub max_outbound: usize,
104 pub max_inbound: usize,
106 #[cfg_attr(feature = "serde", serde(default))]
108 pub max_concurrent_outbound_dials: usize,
109}
110
111impl Default for ConnectionsConfig {
112 fn default() -> Self {
113 Self {
114 max_outbound: DEFAULT_MAX_COUNT_PEERS_OUTBOUND as usize,
115 max_inbound: DEFAULT_MAX_COUNT_PEERS_INBOUND as usize,
116 max_concurrent_outbound_dials: DEFAULT_MAX_COUNT_CONCURRENT_OUTBOUND_DIALS,
117 }
118 }
119}
120
121#[derive(Debug, Clone, PartialEq, Eq)]
123#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
124#[cfg_attr(feature = "serde", serde(default))]
125pub struct PeersConfig {
126 #[cfg_attr(feature = "serde", serde(with = "humantime_serde"))]
128 pub refill_slots_interval: Duration,
129 pub trusted_nodes: Vec<TrustedPeer>,
131 #[cfg_attr(feature = "serde", serde(alias = "connect_trusted_nodes_only"))]
133 pub trusted_nodes_only: bool,
134 pub max_backoff_count: u8,
144 #[cfg_attr(feature = "serde", serde(skip))]
146 pub basic_nodes: HashSet<NodeRecord>,
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}
167
168impl Default for PeersConfig {
169 fn default() -> Self {
170 Self {
171 refill_slots_interval: Duration::from_millis(5_000),
172 connection_info: Default::default(),
173 reputation_weights: Default::default(),
174 ban_list: Default::default(),
175 ban_duration: Duration::from_secs(60 * 60 * 12),
177 backoff_durations: Default::default(),
178 trusted_nodes: Default::default(),
179 trusted_nodes_only: false,
180 basic_nodes: Default::default(),
181 max_backoff_count: 5,
182 incoming_ip_throttle_duration: INBOUND_IP_THROTTLE_DURATION,
183 }
184 }
185}
186
187impl PeersConfig {
188 pub fn with_ban_list(mut self, ban_list: BanList) -> Self {
190 self.ban_list = ban_list;
191 self
192 }
193
194 pub const fn with_ban_duration(mut self, ban_duration: Duration) -> Self {
196 self.ban_duration = ban_duration;
197 self
198 }
199
200 pub const fn with_refill_slots_interval(mut self, interval: Duration) -> Self {
202 self.refill_slots_interval = interval;
203 self
204 }
205
206 pub const fn with_max_outbound(mut self, max_outbound: usize) -> Self {
208 self.connection_info.max_outbound = max_outbound;
209 self
210 }
211
212 pub const fn with_max_inbound_opt(mut self, max_inbound: Option<usize>) -> Self {
214 if let Some(max_inbound) = max_inbound {
215 self.connection_info.max_inbound = max_inbound;
216 }
217 self
218 }
219
220 pub const fn with_max_outbound_opt(mut self, max_outbound: Option<usize>) -> Self {
222 if let Some(max_outbound) = max_outbound {
223 self.connection_info.max_outbound = max_outbound;
224 }
225 self
226 }
227
228 pub const fn with_max_inbound(mut self, max_inbound: usize) -> Self {
230 self.connection_info.max_inbound = max_inbound;
231 self
232 }
233
234 pub const fn with_max_concurrent_dials(mut self, max_concurrent_outbound_dials: usize) -> Self {
236 self.connection_info.max_concurrent_outbound_dials = max_concurrent_outbound_dials;
237 self
238 }
239
240 pub fn with_trusted_nodes(mut self, nodes: Vec<TrustedPeer>) -> Self {
242 self.trusted_nodes = nodes;
243 self
244 }
245
246 pub const fn with_trusted_nodes_only(mut self, trusted_only: bool) -> Self {
248 self.trusted_nodes_only = trusted_only;
249 self
250 }
251
252 pub fn with_basic_nodes(mut self, nodes: HashSet<NodeRecord>) -> Self {
254 self.basic_nodes = nodes;
255 self
256 }
257
258 pub const fn with_max_backoff_count(mut self, max_backoff_count: u8) -> Self {
260 self.max_backoff_count = max_backoff_count;
261 self
262 }
263
264 pub const fn with_reputation_weights(
266 mut self,
267 reputation_weights: ReputationChangeWeights,
268 ) -> Self {
269 self.reputation_weights = reputation_weights;
270 self
271 }
272
273 pub const fn with_backoff_durations(mut self, backoff_durations: PeerBackoffDurations) -> Self {
275 self.backoff_durations = backoff_durations;
276 self
277 }
278
279 pub const fn max_peers(&self) -> usize {
281 self.connection_info.max_outbound + self.connection_info.max_inbound
282 }
283
284 pub fn with_basic_nodes_from_file(
286 self,
287 optional_file: Option<impl AsRef<Path>>,
288 ) -> Result<Self, io::Error> {
289 let Some(file_path) = optional_file else { return Ok(self) };
290 let reader = match std::fs::File::open(file_path.as_ref()) {
291 Ok(file) => io::BufReader::new(file),
292 Err(e) if e.kind() == ErrorKind::NotFound => return Ok(self),
293 Err(e) => Err(e)?,
294 };
295 info!(target: "net::peers", file = %file_path.as_ref().display(), "Loading saved peers");
296 let nodes: HashSet<NodeRecord> = serde_json::from_reader(reader)?;
297 Ok(self.with_basic_nodes(nodes))
298 }
299
300 #[cfg(any(test, feature = "test-utils"))]
302 pub fn test() -> Self {
303 Self {
304 refill_slots_interval: Duration::from_millis(100),
305 backoff_durations: PeerBackoffDurations::test(),
306 ban_duration: Duration::from_millis(200),
307 ..Default::default()
308 }
309 }
310}