Skip to main content

reth_node_core/args/
network.rs

1//! clap [Args](clap::Args) for network related arguments.
2
3use alloy_eips::BlockNumHash;
4use alloy_primitives::B256;
5use std::{
6    net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6},
7    num::NonZeroUsize,
8    ops::Not,
9    path::PathBuf,
10    sync::OnceLock,
11};
12
13use crate::version::version_metadata;
14use clap::{
15    builder::{OsStr, Resettable},
16    Args,
17};
18use reth_chainspec::EthChainSpec;
19use reth_cli_util::{get_secret_key, load_secret_key::SecretKeyError};
20use reth_config::Config;
21use reth_discv4::{NodeRecord, DEFAULT_DISCOVERY_ADDR, DEFAULT_DISCOVERY_PORT};
22use reth_discv5::{
23    discv5::ListenConfig, DEFAULT_COUNT_BOOTSTRAP_LOOKUPS, DEFAULT_DISCOVERY_V5_PORT,
24    DEFAULT_SECONDS_BOOTSTRAP_LOOKUP_INTERVAL, DEFAULT_SECONDS_LOOKUP_INTERVAL,
25};
26use reth_net_banlist::IpFilter;
27use reth_net_nat::{NatResolver, DEFAULT_NET_IF_NAME};
28use reth_network::{
29    transactions::{
30        config::{TransactionIngressPolicy, TransactionPropagationKind},
31        constants::{
32            tx_fetcher::{
33                DEFAULT_MAX_CAPACITY_CACHE_PENDING_FETCH, DEFAULT_MAX_COUNT_CONCURRENT_REQUESTS,
34                DEFAULT_MAX_COUNT_CONCURRENT_REQUESTS_PER_PEER,
35            },
36            tx_manager::{
37                DEFAULT_MAX_COUNT_PENDING_POOL_IMPORTS,
38                DEFAULT_MAX_COUNT_TRANSACTIONS_SEEN_BY_PEER,
39                DEFAULT_TX_MANAGER_CHANNEL_MEMORY_LIMIT_BYTES,
40            },
41        },
42        TransactionFetcherConfig, TransactionPropagationMode, TransactionsManagerConfig,
43        DEFAULT_SOFT_LIMIT_BYTE_SIZE_POOLED_TRANSACTIONS_RESP_ON_PACK_GET_POOLED_TRANSACTIONS_REQ,
44        SOFT_LIMIT_BYTE_SIZE_POOLED_TRANSACTIONS_RESPONSE,
45    },
46    HelloMessageWithProtocols, NetworkConfigBuilder, NetworkPrimitives,
47};
48use reth_network_peers::{mainnet_nodes, TrustedPeer};
49use reth_tasks::Runtime;
50use secp256k1::SecretKey;
51use std::str::FromStr;
52use tracing::error;
53
54/// Global static network defaults
55static NETWORK_DEFAULTS: OnceLock<DefaultNetworkArgs> = OnceLock::new();
56
57/// Default values for network CLI arguments that can be customized.
58///
59/// Global defaults can be set via [`DefaultNetworkArgs::try_init`].
60#[derive(Debug, Clone)]
61pub struct DefaultNetworkArgs {
62    /// Default DNS retries.
63    pub dns_retries: usize,
64    /// Default NAT resolver.
65    pub nat: NatResolver,
66    /// Default network listening address.
67    pub addr: IpAddr,
68    /// Default network listening port.
69    pub port: u16,
70    /// Default max concurrent `GetPooledTransactions` requests.
71    pub max_concurrent_tx_requests: u32,
72    /// Default max concurrent `GetPooledTransactions` requests per peer.
73    pub max_concurrent_tx_requests_per_peer: u8,
74    /// Default max number of seen transactions to remember per peer.
75    pub max_seen_tx_history: u32,
76    /// Default max number of transactions to import concurrently.
77    pub max_pending_pool_imports: usize,
78    /// Default max accumulated byte size of transactions to pack in one response.
79    pub soft_limit_byte_size_pooled_transactions_response: usize,
80    /// Default max accumulated byte size of transactions to request in one request.
81    pub soft_limit_byte_size_pooled_transactions_response_on_pack_request: usize,
82    /// Default max capacity of cache of hashes for transactions pending fetch.
83    pub max_capacity_cache_txns_pending_fetch: u32,
84    /// Default memory limit (in bytes) for the network manager → transactions manager channel.
85    pub tx_channel_memory_limit_bytes: usize,
86    /// Default transaction propagation policy.
87    pub tx_propagation_policy: TransactionPropagationKind,
88    /// Default transaction ingress policy.
89    pub tx_ingress_policy: TransactionIngressPolicy,
90    /// Default transaction propagation mode.
91    pub propagation_mode: TransactionPropagationMode,
92    /// Default enforce ENR fork ID setting.
93    pub enforce_enr_fork_id: bool,
94}
95
96impl DefaultNetworkArgs {
97    /// Initialize the global network defaults with this configuration.
98    pub fn try_init(self) -> Result<(), Self> {
99        NETWORK_DEFAULTS.set(self)
100    }
101
102    /// Get a reference to the global network defaults.
103    pub fn get_global() -> &'static Self {
104        NETWORK_DEFAULTS.get_or_init(Self::default)
105    }
106
107    /// Set the default DNS retries.
108    pub const fn with_dns_retries(mut self, v: usize) -> Self {
109        self.dns_retries = v;
110        self
111    }
112
113    /// Set the default NAT resolver.
114    pub fn with_nat(mut self, v: NatResolver) -> Self {
115        self.nat = v;
116        self
117    }
118
119    /// Set the default network listening address.
120    pub const fn with_addr(mut self, v: IpAddr) -> Self {
121        self.addr = v;
122        self
123    }
124
125    /// Set the default network listening port.
126    pub const fn with_port(mut self, v: u16) -> Self {
127        self.port = v;
128        self
129    }
130
131    /// Set the default max concurrent `GetPooledTransactions` requests.
132    pub const fn with_max_concurrent_tx_requests(mut self, v: u32) -> Self {
133        self.max_concurrent_tx_requests = v;
134        self
135    }
136
137    /// Set the default max concurrent `GetPooledTransactions` requests per peer.
138    pub const fn with_max_concurrent_tx_requests_per_peer(mut self, v: u8) -> Self {
139        self.max_concurrent_tx_requests_per_peer = v;
140        self
141    }
142
143    /// Set the default max number of seen transactions to remember per peer.
144    pub const fn with_max_seen_tx_history(mut self, v: u32) -> Self {
145        self.max_seen_tx_history = v;
146        self
147    }
148
149    /// Set the default max number of transactions to import concurrently.
150    pub const fn with_max_pending_pool_imports(mut self, v: usize) -> Self {
151        self.max_pending_pool_imports = v;
152        self
153    }
154
155    /// Set the default max accumulated byte size of transactions to pack in one response.
156    pub const fn with_soft_limit_byte_size_pooled_transactions_response(
157        mut self,
158        v: usize,
159    ) -> Self {
160        self.soft_limit_byte_size_pooled_transactions_response = v;
161        self
162    }
163
164    /// Set the default max accumulated byte size of transactions to request in one request.
165    pub const fn with_soft_limit_byte_size_pooled_transactions_response_on_pack_request(
166        mut self,
167        v: usize,
168    ) -> Self {
169        self.soft_limit_byte_size_pooled_transactions_response_on_pack_request = v;
170        self
171    }
172
173    /// Set the default max capacity of cache of hashes for transactions pending fetch.
174    pub const fn with_max_capacity_cache_txns_pending_fetch(mut self, v: u32) -> Self {
175        self.max_capacity_cache_txns_pending_fetch = v;
176        self
177    }
178
179    /// Set the default memory limit (in bytes) for the network manager → transactions
180    /// manager channel.
181    pub const fn with_tx_channel_memory_limit_bytes(mut self, v: usize) -> Self {
182        self.tx_channel_memory_limit_bytes = v;
183        self
184    }
185
186    /// Set the default transaction propagation policy.
187    pub const fn with_tx_propagation_policy(mut self, v: TransactionPropagationKind) -> Self {
188        self.tx_propagation_policy = v;
189        self
190    }
191
192    /// Set the default transaction ingress policy.
193    pub const fn with_tx_ingress_policy(mut self, v: TransactionIngressPolicy) -> Self {
194        self.tx_ingress_policy = v;
195        self
196    }
197
198    /// Set the default transaction propagation mode.
199    pub const fn with_propagation_mode(mut self, v: TransactionPropagationMode) -> Self {
200        self.propagation_mode = v;
201        self
202    }
203
204    /// Set the default enforce ENR fork ID setting.
205    pub const fn with_enforce_enr_fork_id(mut self, v: bool) -> Self {
206        self.enforce_enr_fork_id = v;
207        self
208    }
209}
210
211impl Default for DefaultNetworkArgs {
212    fn default() -> Self {
213        Self {
214            dns_retries: 0,
215            nat: NatResolver::Any,
216            addr: DEFAULT_DISCOVERY_ADDR,
217            port: DEFAULT_DISCOVERY_PORT,
218            max_concurrent_tx_requests: DEFAULT_MAX_COUNT_CONCURRENT_REQUESTS,
219            max_concurrent_tx_requests_per_peer: DEFAULT_MAX_COUNT_CONCURRENT_REQUESTS_PER_PEER,
220            max_seen_tx_history: DEFAULT_MAX_COUNT_TRANSACTIONS_SEEN_BY_PEER,
221            max_pending_pool_imports: DEFAULT_MAX_COUNT_PENDING_POOL_IMPORTS,
222            soft_limit_byte_size_pooled_transactions_response:
223                SOFT_LIMIT_BYTE_SIZE_POOLED_TRANSACTIONS_RESPONSE,
224            soft_limit_byte_size_pooled_transactions_response_on_pack_request:
225                DEFAULT_SOFT_LIMIT_BYTE_SIZE_POOLED_TRANSACTIONS_RESP_ON_PACK_GET_POOLED_TRANSACTIONS_REQ,
226            max_capacity_cache_txns_pending_fetch: DEFAULT_MAX_CAPACITY_CACHE_PENDING_FETCH,
227            tx_channel_memory_limit_bytes: DEFAULT_TX_MANAGER_CHANNEL_MEMORY_LIMIT_BYTES,
228            tx_propagation_policy: TransactionPropagationKind::default(),
229            tx_ingress_policy: TransactionIngressPolicy::default(),
230            propagation_mode: TransactionPropagationMode::Sqrt,
231            enforce_enr_fork_id: false,
232        }
233    }
234}
235
236/// Parameters for configuring the network more granularity via CLI
237#[derive(Debug, Clone, Args, PartialEq, Eq)]
238#[command(next_help_heading = "Networking")]
239pub struct NetworkArgs {
240    /// Arguments to setup discovery service.
241    #[command(flatten)]
242    pub discovery: DiscoveryArgs,
243
244    #[expect(clippy::doc_markdown)]
245    /// Comma separated enode URLs of trusted peers for P2P connections.
246    ///
247    /// --trusted-peers enode://abcd@192.168.0.1:30303
248    #[arg(long, value_delimiter = ',')]
249    pub trusted_peers: Vec<TrustedPeer>,
250
251    /// Connect to or accept from trusted peers only
252    #[arg(long)]
253    pub trusted_only: bool,
254
255    /// Comma separated enode URLs for P2P discovery bootstrap.
256    ///
257    /// Will fall back to a network-specific default if not specified.
258    #[arg(long, value_delimiter = ',')]
259    pub bootnodes: Option<Vec<TrustedPeer>>,
260
261    /// Amount of DNS resolution requests retries to perform when peering.
262    #[arg(long, default_value_t = DefaultNetworkArgs::get_global().dns_retries)]
263    pub dns_retries: usize,
264
265    /// The path to the known peers file. Connected peers are dumped to this file on nodes
266    /// shutdown, and read on startup. Cannot be used with `--no-persist-peers`.
267    #[arg(long, value_name = "FILE", verbatim_doc_comment, conflicts_with = "no_persist_peers")]
268    pub peers_file: Option<PathBuf>,
269
270    /// Custom node identity
271    #[arg(long, value_name = "IDENTITY", default_value = version_metadata().p2p_client_version.as_ref())]
272    pub identity: String,
273
274    /// Secret key to use for this node.
275    ///
276    /// This will also deterministically set the peer ID. If not specified, it will be set in the
277    /// data dir for the chain being used.
278    #[arg(long, value_name = "PATH", conflicts_with = "p2p_secret_key_hex")]
279    pub p2p_secret_key: Option<PathBuf>,
280
281    /// Hex encoded secret key to use for this node.
282    ///
283    /// This will also deterministically set the peer ID. Cannot be used together with
284    /// `--p2p-secret-key`.
285    #[arg(long, value_name = "HEX", conflicts_with = "p2p_secret_key")]
286    pub p2p_secret_key_hex: Option<B256>,
287
288    /// Do not persist peers.
289    #[arg(long, verbatim_doc_comment)]
290    pub no_persist_peers: bool,
291
292    /// NAT resolution method (any|none|upnp|publicip|extip:\<IP\>)
293    #[arg(long, default_value_t = DefaultNetworkArgs::get_global().nat.clone())]
294    pub nat: NatResolver,
295
296    /// Network listening address
297    #[arg(long = "addr", value_name = "ADDR", default_value_t = DefaultNetworkArgs::get_global().addr)]
298    pub addr: IpAddr,
299
300    /// Network listening port
301    #[arg(long = "port", value_name = "PORT", default_value_t = DefaultNetworkArgs::get_global().port)]
302    pub port: u16,
303
304    /// Maximum number of outbound peers. default: 100
305    #[arg(long)]
306    pub max_outbound_peers: Option<usize>,
307
308    /// Maximum number of inbound peers. default: 30
309    #[arg(long)]
310    pub max_inbound_peers: Option<usize>,
311
312    /// Maximum number of total peers (inbound + outbound).
313    ///
314    /// Splits peers using approximately 2:1 inbound:outbound ratio. Cannot be used together with
315    /// `--max-outbound-peers` or `--max-inbound-peers`.
316    #[arg(
317        long,
318        value_name = "COUNT",
319        conflicts_with = "max_outbound_peers",
320        conflicts_with = "max_inbound_peers"
321    )]
322    pub max_peers: Option<usize>,
323
324    /// Max concurrent `GetPooledTransactions` requests.
325    #[arg(long = "max-tx-reqs", value_name = "COUNT", default_value_t = DefaultNetworkArgs::get_global().max_concurrent_tx_requests, verbatim_doc_comment)]
326    pub max_concurrent_tx_requests: u32,
327
328    /// Max concurrent `GetPooledTransactions` requests per peer.
329    #[arg(long = "max-tx-reqs-peer", value_name = "COUNT", default_value_t = DefaultNetworkArgs::get_global().max_concurrent_tx_requests_per_peer, verbatim_doc_comment)]
330    pub max_concurrent_tx_requests_per_peer: u8,
331
332    /// Max number of seen transactions to remember per peer.
333    ///
334    /// Default is 320 transaction hashes.
335    #[arg(long = "max-seen-tx-history", value_name = "COUNT", default_value_t = DefaultNetworkArgs::get_global().max_seen_tx_history, verbatim_doc_comment)]
336    pub max_seen_tx_history: u32,
337
338    #[arg(long = "max-pending-imports", value_name = "COUNT", default_value_t = DefaultNetworkArgs::get_global().max_pending_pool_imports, verbatim_doc_comment)]
339    /// Max number of transactions to import concurrently.
340    pub max_pending_pool_imports: usize,
341
342    /// Experimental, for usage in research. Sets the max accumulated byte size of transactions
343    /// to pack in one response.
344    /// Spec'd at 2MiB.
345    #[arg(long = "pooled-tx-response-soft-limit", value_name = "BYTES", default_value_t = DefaultNetworkArgs::get_global().soft_limit_byte_size_pooled_transactions_response, verbatim_doc_comment)]
346    pub soft_limit_byte_size_pooled_transactions_response: usize,
347
348    /// Experimental, for usage in research. Sets the max accumulated byte size of transactions to
349    /// request in one request.
350    ///
351    /// Since `RLPx` protocol version 68, the byte size of a transaction is shared as metadata in a
352    /// transaction announcement (see `RLPx` specs). This allows a node to request a specific size
353    /// response.
354    ///
355    /// By default, nodes request only 128 KiB worth of transactions, but should a peer request
356    /// more, up to 2 MiB, a node will answer with more than 128 KiB.
357    ///
358    /// Default is 128 KiB.
359    #[arg(long = "pooled-tx-pack-soft-limit", value_name = "BYTES", default_value_t = DefaultNetworkArgs::get_global().soft_limit_byte_size_pooled_transactions_response_on_pack_request, verbatim_doc_comment)]
360    pub soft_limit_byte_size_pooled_transactions_response_on_pack_request: usize,
361
362    /// Max capacity of cache of hashes for transactions pending fetch.
363    #[arg(long = "max-tx-pending-fetch", value_name = "COUNT", default_value_t = DefaultNetworkArgs::get_global().max_capacity_cache_txns_pending_fetch, verbatim_doc_comment)]
364    pub max_capacity_cache_txns_pending_fetch: u32,
365
366    /// Memory limit (in bytes) for the channel that buffers transaction events flowing
367    /// from the network manager to the transactions manager.
368    ///
369    /// When the budget is exhausted, new events are dropped (see metric
370    /// `total_dropped_tx_events_at_full_capacity`). Acts as a backstop against unbounded
371    /// memory growth under sustained P2P transaction flooding.
372    #[arg(long = "tx-channel-memory-limit", value_name = "BYTES", default_value_t = DefaultNetworkArgs::get_global().tx_channel_memory_limit_bytes, verbatim_doc_comment)]
373    pub tx_channel_memory_limit_bytes: usize,
374
375    /// Name of network interface used to communicate with peers.
376    ///
377    /// If flag is set, but no value is passed, the default interface for docker `eth0` is tried.
378    /// If `--discovery.addr` is left at its default, discv4 will also bind to the resolved
379    /// interface address.
380    #[arg(long = "net-if.experimental", conflicts_with = "addr", value_name = "IF_NAME")]
381    pub net_if: Option<String>,
382
383    /// Transaction Propagation Policy
384    ///
385    /// The policy determines which peers transactions are gossiped to.
386    #[arg(long = "tx-propagation-policy", default_value_t = DefaultNetworkArgs::get_global().tx_propagation_policy)]
387    pub tx_propagation_policy: TransactionPropagationKind,
388
389    /// Transaction ingress policy
390    ///
391    /// Determines which peers' transactions are accepted over P2P.
392    #[arg(long = "tx-ingress-policy", default_value_t = DefaultNetworkArgs::get_global().tx_ingress_policy)]
393    pub tx_ingress_policy: TransactionIngressPolicy,
394
395    /// Disable transaction pool gossip
396    ///
397    /// Disables gossiping of transactions in the mempool to peers. This can be omitted for
398    /// personal nodes, though providers should always opt to enable this flag.
399    #[arg(long = "disable-tx-gossip")]
400    pub disable_tx_gossip: bool,
401
402    /// Sets the transaction propagation mode by determining how new pending transactions are
403    /// propagated to other peers in full.
404    ///
405    /// Examples: sqrt, all, max:10
406    #[arg(
407        long = "tx-propagation-mode",
408        default_value_t = DefaultNetworkArgs::get_global().propagation_mode,
409        help = "Transaction propagation mode (sqrt, all, max:<number>)"
410    )]
411    pub propagation_mode: TransactionPropagationMode,
412
413    /// Comma separated list of required block hashes or block number=hash pairs.
414    /// Peers that don't have these blocks will be filtered out.
415    /// Format: hash or `block_number=hash` (e.g., 23115201=0x1234...)
416    #[arg(long = "required-block-hashes", value_delimiter = ',', value_parser = parse_block_num_hash)]
417    pub required_block_hashes: Vec<BlockNumHash>,
418
419    /// Optional network ID to override the chain specification's network ID for P2P connections
420    #[arg(long)]
421    pub network_id: Option<u64>,
422
423    /// Maximum allowed ETH message size in bytes. Default is 10 MiB.
424    #[arg(long = "eth-max-message-size", value_name = "BYTES")]
425    pub eth_max_message_size: Option<NonZeroUsize>,
426
427    /// Restrict network communication to the given IP networks (CIDR masks).
428    ///
429    /// Comma separated list of CIDR network specifications.
430    /// Only peers with IP addresses within these ranges will be allowed to connect.
431    ///
432    /// Example: --netrestrict "192.168.0.0/16,10.0.0.0/8"
433    #[arg(long, value_name = "NETRESTRICT")]
434    pub netrestrict: Option<String>,
435
436    /// Enforce EIP-868 ENR fork ID validation for discovered peers.
437    ///
438    /// When enabled, peers discovered without a confirmed fork ID are not added to the peer set
439    /// until their fork ID is verified via EIP-868 ENR request. This filters out peers from other
440    /// networks that pollute the discovery table.
441    #[arg(long, default_value_t = DefaultNetworkArgs::get_global().enforce_enr_fork_id)]
442    pub enforce_enr_fork_id: bool,
443}
444
445impl NetworkArgs {
446    /// Returns the IP address used for the `RLPx` TCP listener.
447    ///
448    /// If `--net-if.experimental` is set, this resolves the interface name to a concrete local IP
449    /// address. That concrete address is also passed to discv5 as the default address to advertise
450    /// in the local ENR.
451    ///
452    /// If no interface is configured, this returns the configured `--addr` value.
453    pub fn resolved_addr(&self) -> IpAddr {
454        if let Some(ref if_name) = self.net_if {
455            let if_name = if if_name.is_empty() { DEFAULT_NET_IF_NAME } else { if_name };
456            return match reth_net_nat::net_if::resolve_net_if_ip(if_name) {
457                Ok(addr) => addr,
458                Err(err) => {
459                    error!(target: "reth::cli",
460                        if_name,
461                        %err,
462                        "Failed to read network interface IP"
463                    );
464
465                    DEFAULT_DISCOVERY_ADDR
466                }
467            };
468        }
469
470        self.addr
471    }
472
473    /// Returns the IP address used for the discovery v4 UDP bind socket.
474    ///
475    /// By default this is `--discovery.addr`. When `--net-if.experimental` is set and
476    /// `--discovery.addr` is still the wildcard default, discovery v4 follows the resolved `RLPx`
477    /// listener address. That keeps discv4 and discv5 on the same concrete UDP address when they
478    /// share a port; otherwise discv4 would bind wildcard while discv5 binds the resolved
479    /// interface address, which prevents socket sharing.
480    fn resolved_discovery_addr(&self, listener_addr: IpAddr) -> IpAddr {
481        if self.net_if.is_some() && self.discovery.addr == DEFAULT_DISCOVERY_ADDR {
482            return listener_addr;
483        }
484
485        self.discovery.addr
486    }
487
488    /// Returns the resolved bootnodes if any are provided.
489    pub fn resolved_bootnodes(&self) -> Option<Vec<NodeRecord>> {
490        self.bootnodes.clone().map(|bootnodes| {
491            bootnodes.into_iter().filter_map(|node| node.resolve_blocking().ok()).collect()
492        })
493    }
494
495    /// Returns the max inbound peers (2:1 ratio).
496    pub fn resolved_max_inbound_peers(&self) -> Option<usize> {
497        if let Some(max_peers) = self.max_peers {
498            if max_peers == 0 {
499                Some(0)
500            } else {
501                let outbound = (max_peers / 3).max(1);
502                Some(max_peers.saturating_sub(outbound))
503            }
504        } else {
505            self.max_inbound_peers
506        }
507    }
508
509    /// Returns the max outbound peers (1:2 ratio).
510    pub fn resolved_max_outbound_peers(&self) -> Option<usize> {
511        if let Some(max_peers) = self.max_peers {
512            if max_peers == 0 {
513                Some(0)
514            } else {
515                Some((max_peers / 3).max(1))
516            }
517        } else {
518            self.max_outbound_peers
519        }
520    }
521
522    /// Configures and returns a `TransactionsManagerConfig` based on the current settings.
523    pub const fn transactions_manager_config(&self) -> TransactionsManagerConfig {
524        TransactionsManagerConfig {
525            transaction_fetcher_config: TransactionFetcherConfig::new(
526                self.max_concurrent_tx_requests,
527                self.max_concurrent_tx_requests_per_peer,
528                self.soft_limit_byte_size_pooled_transactions_response,
529                self.soft_limit_byte_size_pooled_transactions_response_on_pack_request,
530                self.max_capacity_cache_txns_pending_fetch,
531            ),
532            max_transactions_seen_by_peer_history: self.max_seen_tx_history,
533            propagation_mode: self.propagation_mode,
534            ingress_policy: self.tx_ingress_policy,
535            tx_channel_memory_limit_bytes: self.tx_channel_memory_limit_bytes,
536        }
537    }
538
539    /// Build a [`NetworkConfigBuilder`] from a [`Config`] and a [`EthChainSpec`], in addition to
540    /// the values in this option struct.
541    ///
542    /// The `default_peers_file` will be used as the default location to store the persistent peers
543    /// file if `no_persist_peers` is false, and there is no provided `peers_file`.
544    ///
545    /// Configured Bootnodes are prioritized, if unset, the chain spec bootnodes are used
546    /// Priority order for bootnodes configuration:
547    /// 1. --bootnodes flag
548    /// 2. Network preset flags (e.g. --holesky)
549    /// 3. default to mainnet nodes
550    pub fn network_config<N: NetworkPrimitives>(
551        &self,
552        config: &Config,
553        chain_spec: impl EthChainSpec,
554        secret_key: SecretKey,
555        default_peers_file: PathBuf,
556        executor: Runtime,
557    ) -> NetworkConfigBuilder<N> {
558        // `listener_addr` is the concrete RLPx TCP bind address. With `--net-if.experimental`,
559        // this is the resolved interface IP and is also used as discv5's default address.
560        let listener_addr = self.resolved_addr();
561        // Discovery v4 has its own CLI bind address. If left at the wildcard default while
562        // `--net-if.experimental` is active, make it follow the listener address so discv4 and
563        // discv5 can share the same UDP socket.
564        let discovery_addr = self.resolved_discovery_addr(listener_addr);
565        let chain_bootnodes = self
566            .resolved_bootnodes()
567            .unwrap_or_else(|| chain_spec.bootnodes().unwrap_or_else(mainnet_nodes));
568        let peers_file = self.peers_file.clone().unwrap_or(default_peers_file);
569
570        // Configure peer connections
571        let ip_filter = self.ip_filter().unwrap_or_default();
572        let peers_config = config
573            .peers_config_with_basic_nodes_from_file(
574                self.persistent_peers_file(peers_file).as_deref(),
575            )
576            .with_max_inbound_opt(self.resolved_max_inbound_peers())
577            .with_max_outbound_opt(self.resolved_max_outbound_peers())
578            .with_ip_filter(ip_filter)
579            .with_enforce_enr_fork_id(self.enforce_enr_fork_id);
580
581        // Configure basic network stack
582        NetworkConfigBuilder::<N>::new(secret_key, executor)
583            .external_ip_resolver(self.nat.clone())
584            .sessions_config(
585                config.sessions.clone().with_upscaled_event_buffer(peers_config.max_peers()),
586            )
587            .peer_config(peers_config)
588            .boot_nodes(chain_bootnodes.clone())
589            .transactions_manager_config(self.transactions_manager_config())
590            // Configure node identity
591            .apply(|builder| {
592                let peer_id = builder.get_peer_id();
593                builder.hello_message(
594                    HelloMessageWithProtocols::builder(peer_id)
595                        .client_version(&self.identity)
596                        .build(),
597                )
598            })
599            // apply discovery settings
600            .apply(|builder| {
601                let rlpx_socket = (listener_addr, self.port).into();
602                self.discovery.apply_to_builder(builder, rlpx_socket, chain_bootnodes)
603            })
604            .listener_addr(SocketAddr::new(listener_addr, self.port))
605            .discovery_addr(SocketAddr::new(discovery_addr, self.discovery.port))
606            .disable_tx_gossip(self.disable_tx_gossip)
607            .required_block_hashes(self.required_block_hashes.clone())
608            .eth_max_message_size_opt(self.eth_max_message_size.map(NonZeroUsize::get))
609            .network_id(self.network_id)
610    }
611
612    /// If `no_persist_peers` is false then this returns the path to the persistent peers file path.
613    pub fn persistent_peers_file(&self, peers_file: PathBuf) -> Option<PathBuf> {
614        self.no_persist_peers.not().then_some(peers_file)
615    }
616
617    /// Configures the [`DiscoveryArgs`].
618    pub const fn with_discovery(mut self, discovery: DiscoveryArgs) -> Self {
619        self.discovery = discovery;
620        self
621    }
622
623    /// Sets the p2p port to zero, to allow the OS to assign a random unused port when
624    /// the network components bind to a socket.
625    pub const fn with_unused_p2p_port(mut self) -> Self {
626        self.port = 0;
627        self
628    }
629
630    /// Sets the p2p and discovery ports to zero, allowing the OD to assign a random unused port
631    /// when network components bind to sockets.
632    pub const fn with_unused_ports(mut self) -> Self {
633        self = self.with_unused_p2p_port();
634        self.discovery = self.discovery.with_unused_discovery_port();
635        self
636    }
637
638    /// Configures the [`NatResolver`]
639    pub fn with_nat_resolver(mut self, nat: NatResolver) -> Self {
640        self.nat = nat;
641        self
642    }
643
644    /// Change networking port numbers based on the instance number, if provided.
645    /// Ports are updated to `previous_value + instance - 1`
646    ///
647    /// # Panics
648    /// Warning: if `instance` is zero in debug mode, this will panic.
649    pub fn adjust_instance_ports(&mut self, instance: Option<u16>) {
650        if let Some(instance) = instance {
651            debug_assert_ne!(instance, 0, "instance must be non-zero");
652            self.port += instance - 1;
653            self.discovery.adjust_instance_ports(instance);
654        }
655    }
656
657    /// Resolve all trusted peers at once
658    pub async fn resolve_trusted_peers(&self) -> Result<Vec<NodeRecord>, std::io::Error> {
659        futures::future::try_join_all(
660            self.trusted_peers.iter().map(|peer| async move { peer.resolve().await }),
661        )
662        .await
663    }
664
665    /// Load the p2p secret key from the provided options.
666    ///
667    /// If `p2p_secret_key_hex` is provided, it will be used directly.
668    /// If `p2p_secret_key` is provided, it will be loaded from the file.
669    /// If neither is provided, the `default_secret_key_path` will be used.
670    pub fn secret_key(
671        &self,
672        default_secret_key_path: PathBuf,
673    ) -> Result<SecretKey, SecretKeyError> {
674        if let Some(b256) = &self.p2p_secret_key_hex {
675            // Use the B256 value directly (already validated as 32 bytes)
676            SecretKey::from_slice(b256.as_slice()).map_err(SecretKeyError::SecretKeyDecodeError)
677        } else {
678            // Load from file (either provided path or default)
679            let secret_key_path = self.p2p_secret_key.clone().unwrap_or(default_secret_key_path);
680            get_secret_key(&secret_key_path)
681        }
682    }
683
684    /// Creates an IP filter from the netrestrict argument.
685    ///
686    /// Returns an error if the CIDR format is invalid.
687    pub fn ip_filter(&self) -> Result<IpFilter, ipnet::AddrParseError> {
688        if let Some(netrestrict) = &self.netrestrict {
689            IpFilter::from_cidr_string(netrestrict)
690        } else {
691            Ok(IpFilter::allow_all())
692        }
693    }
694}
695
696impl Default for NetworkArgs {
697    fn default() -> Self {
698        let DefaultNetworkArgs {
699            dns_retries,
700            nat,
701            addr,
702            port,
703            max_concurrent_tx_requests,
704            max_concurrent_tx_requests_per_peer,
705            max_seen_tx_history,
706            max_pending_pool_imports,
707            soft_limit_byte_size_pooled_transactions_response,
708            soft_limit_byte_size_pooled_transactions_response_on_pack_request,
709            max_capacity_cache_txns_pending_fetch,
710            tx_channel_memory_limit_bytes,
711            tx_propagation_policy,
712            tx_ingress_policy,
713            propagation_mode,
714            enforce_enr_fork_id,
715        } = DefaultNetworkArgs::get_global().clone();
716        Self {
717            discovery: DiscoveryArgs::default(),
718            trusted_peers: vec![],
719            trusted_only: false,
720            bootnodes: None,
721            dns_retries,
722            peers_file: None,
723            identity: version_metadata().p2p_client_version.to_string(),
724            p2p_secret_key: None,
725            p2p_secret_key_hex: None,
726            no_persist_peers: false,
727            nat,
728            addr,
729            port,
730            max_outbound_peers: None,
731            max_inbound_peers: None,
732            max_peers: None,
733            max_concurrent_tx_requests,
734            max_concurrent_tx_requests_per_peer,
735            soft_limit_byte_size_pooled_transactions_response,
736            soft_limit_byte_size_pooled_transactions_response_on_pack_request,
737            max_pending_pool_imports,
738            max_seen_tx_history,
739            max_capacity_cache_txns_pending_fetch,
740            tx_channel_memory_limit_bytes,
741            net_if: None,
742            tx_propagation_policy,
743            tx_ingress_policy,
744            disable_tx_gossip: false,
745            propagation_mode,
746            required_block_hashes: vec![],
747            network_id: None,
748            eth_max_message_size: None,
749            netrestrict: None,
750            enforce_enr_fork_id,
751        }
752    }
753}
754
755/// Global static discovery defaults
756static DISCOVERY_DEFAULTS: OnceLock<DefaultDiscoveryArgs> = OnceLock::new();
757
758/// Default values for discovery CLI arguments that can be customized.
759#[derive(Debug, Clone, Copy)]
760pub struct DefaultDiscoveryArgs {
761    /// Default for `--disable-discovery`.
762    pub disable_discovery: bool,
763    /// Default for `--disable-dns-discovery`.
764    pub disable_dns_discovery: bool,
765    /// Default for `--disable-discv4-discovery`.
766    pub disable_discv4_discovery: bool,
767    /// Default for `--disable-discv5-discovery`.
768    pub disable_discv5_discovery: bool,
769    /// Default for `--disable-nat`.
770    pub disable_nat: bool,
771    /// Default UDP address for devp2p discovery v4.
772    pub addr: IpAddr,
773    /// Default UDP port for devp2p discovery v4.
774    pub port: u16,
775    /// Default UDP IPv4 address for devp2p discovery v5.
776    pub discv5_addr: Option<Ipv4Addr>,
777    /// Default UDP IPv6 address for devp2p discovery v5.
778    pub discv5_addr_ipv6: Option<Ipv6Addr>,
779    /// Default UDP IPv4 port for devp2p discovery v5.
780    pub discv5_port: Option<u16>,
781    /// Default UDP IPv6 port for devp2p discovery v5.
782    pub discv5_port_ipv6: Option<u16>,
783    /// Default discv5 periodic lookup interval (seconds).
784    pub discv5_lookup_interval: u64,
785    /// Default discv5 bootstrap lookup interval (seconds).
786    pub discv5_bootstrap_lookup_interval: u64,
787    /// Default discv5 bootstrap lookup countdown.
788    pub discv5_bootstrap_lookup_countdown: u64,
789}
790
791impl DefaultDiscoveryArgs {
792    /// Initialize the global discovery defaults with this configuration.
793    pub fn try_init(self) -> Result<(), Self> {
794        DISCOVERY_DEFAULTS.set(self)
795    }
796
797    /// Get a reference to the global discovery defaults.
798    pub fn get_global() -> &'static Self {
799        DISCOVERY_DEFAULTS.get_or_init(Self::default)
800    }
801
802    /// Set the default for `--disable-discovery`.
803    pub const fn with_disable_discovery(mut self, disable: bool) -> Self {
804        self.disable_discovery = disable;
805        self
806    }
807
808    /// Set the default for `--disable-dns-discovery`.
809    pub const fn with_disable_dns_discovery(mut self, disable: bool) -> Self {
810        self.disable_dns_discovery = disable;
811        self
812    }
813
814    /// Set the default for `--disable-discv4-discovery`.
815    pub const fn with_disable_discv4_discovery(mut self, disable: bool) -> Self {
816        self.disable_discv4_discovery = disable;
817        self
818    }
819
820    /// Set the default for `--disable-discv5-discovery`.
821    pub const fn with_disable_discv5_discovery(mut self, disable: bool) -> Self {
822        self.disable_discv5_discovery = disable;
823        self
824    }
825
826    /// Set the default for `--disable-nat`.
827    pub const fn with_disable_nat(mut self, disable: bool) -> Self {
828        self.disable_nat = disable;
829        self
830    }
831
832    /// Set the default discovery v4 address.
833    pub const fn with_addr(mut self, addr: IpAddr) -> Self {
834        self.addr = addr;
835        self
836    }
837
838    /// Set the default discovery v4 port.
839    pub const fn with_port(mut self, port: u16) -> Self {
840        self.port = port;
841        self
842    }
843
844    /// Set the default discovery v5 IPv4 address.
845    pub fn with_discv5_addr(mut self, addr: impl Into<Option<Ipv4Addr>>) -> Self {
846        self.discv5_addr = addr.into();
847        self
848    }
849
850    /// Set the default discovery v5 IPv6 address.
851    pub fn with_discv5_addr_ipv6(mut self, addr: impl Into<Option<Ipv6Addr>>) -> Self {
852        self.discv5_addr_ipv6 = addr.into();
853        self
854    }
855
856    /// Set the default discovery V5 port.
857    pub fn with_discv5_port(mut self, port: impl Into<Option<u16>>) -> Self {
858        self.discv5_port = port.into();
859        self
860    }
861
862    /// Set the default discovery v5 IPv6 port.
863    pub fn with_discv5_port_ipv6(mut self, port: impl Into<Option<u16>>) -> Self {
864        self.discv5_port_ipv6 = port.into();
865        self
866    }
867
868    /// Set the default discv5 periodic lookup interval (seconds).
869    pub const fn with_discv5_lookup_interval(mut self, interval: u64) -> Self {
870        self.discv5_lookup_interval = interval;
871        self
872    }
873
874    /// Set the default discv5 bootstrap lookup interval (seconds).
875    pub const fn with_discv5_bootstrap_lookup_interval(mut self, interval: u64) -> Self {
876        self.discv5_bootstrap_lookup_interval = interval;
877        self
878    }
879
880    /// Set the default discv5 bootstrap lookup countdown.
881    pub const fn with_discv5_bootstrap_lookup_countdown(mut self, countdown: u64) -> Self {
882        self.discv5_bootstrap_lookup_countdown = countdown;
883        self
884    }
885}
886
887impl Default for DefaultDiscoveryArgs {
888    fn default() -> Self {
889        Self {
890            disable_discovery: false,
891            disable_dns_discovery: false,
892            disable_discv4_discovery: false,
893            disable_discv5_discovery: false,
894            disable_nat: false,
895            addr: DEFAULT_DISCOVERY_ADDR,
896            port: DEFAULT_DISCOVERY_PORT,
897            discv5_addr: None,
898            discv5_addr_ipv6: None,
899            discv5_port: Some(DEFAULT_DISCOVERY_V5_PORT),
900            discv5_port_ipv6: Some(DEFAULT_DISCOVERY_V5_PORT),
901            discv5_lookup_interval: DEFAULT_SECONDS_LOOKUP_INTERVAL,
902            discv5_bootstrap_lookup_interval: DEFAULT_SECONDS_BOOTSTRAP_LOOKUP_INTERVAL,
903            discv5_bootstrap_lookup_countdown: DEFAULT_COUNT_BOOTSTRAP_LOOKUPS,
904        }
905    }
906}
907
908/// Arguments to setup discovery
909#[derive(Debug, Clone, Args, PartialEq, Eq)]
910pub struct DiscoveryArgs {
911    /// Disable the discovery service.
912    #[arg(short, long, default_value_if("dev", "true", "true"), default_value_t = DefaultDiscoveryArgs::get_global().disable_discovery)]
913    pub disable_discovery: bool,
914
915    /// Disable the DNS discovery.
916    #[arg(long, conflicts_with = "disable_discovery", default_value_t = DefaultDiscoveryArgs::get_global().disable_dns_discovery)]
917    pub disable_dns_discovery: bool,
918
919    /// Disable Discv4 discovery.
920    #[arg(long, conflicts_with = "disable_discovery", default_value_t = DefaultDiscoveryArgs::get_global().disable_discv4_discovery)]
921    pub disable_discv4_discovery: bool,
922
923    /// Enable Discv5 discovery.
924    ///
925    /// Discv5 is now enabled by default, so this flag is a no-op and will be removed in a future
926    /// release.
927    #[arg(long, conflicts_with = "disable_discovery", hide = true)]
928    pub enable_discv5_discovery: bool,
929
930    /// Disable Discv5 discovery.
931    #[arg(long, conflicts_with = "disable_discovery", default_value_t = DefaultDiscoveryArgs::get_global().disable_discv5_discovery)]
932    pub disable_discv5_discovery: bool,
933
934    /// Disable Nat discovery.
935    #[arg(long, conflicts_with = "disable_discovery", default_value_t = DefaultDiscoveryArgs::get_global().disable_nat)]
936    pub disable_nat: bool,
937
938    /// The UDP address to use for devp2p peer discovery version 4.
939    ///
940    /// If unset and `--net-if.experimental` is used, discv4 binds to the resolved interface
941    /// address.
942    #[arg(id = "discovery.addr", long = "discovery.addr", value_name = "DISCOVERY_ADDR", default_value_t = DefaultDiscoveryArgs::get_global().addr)]
943    pub addr: IpAddr,
944
945    /// The UDP port to use for devp2p peer discovery version 4.
946    #[arg(id = "discovery.port", long = "discovery.port", value_name = "DISCOVERY_PORT", default_value_t = DefaultDiscoveryArgs::get_global().port)]
947    pub port: u16,
948
949    /// The UDP IPv4 address to use for devp2p peer discovery version 5. Overwritten by `RLPx`
950    /// address, if it's also IPv4.
951    #[arg(id = "discovery.v5.addr", long = "discovery.v5.addr", value_name = "DISCOVERY_V5_ADDR", default_value = Resettable::from(DefaultDiscoveryArgs::get_global().discv5_addr.map(|a| OsStr::from(a.to_string()))))]
952    pub discv5_addr: Option<Ipv4Addr>,
953
954    /// The UDP IPv6 address to use for devp2p peer discovery version 5. Overwritten by `RLPx`
955    /// address, if it's also IPv6.
956    #[arg(id = "discovery.v5.addr.ipv6", long = "discovery.v5.addr.ipv6", value_name = "DISCOVERY_V5_ADDR_IPV6", default_value = Resettable::from(DefaultDiscoveryArgs::get_global().discv5_addr_ipv6.map(|a| OsStr::from(a.to_string()))))]
957    pub discv5_addr_ipv6: Option<Ipv6Addr>,
958
959    /// The UDP IPv4 port to use for devp2p peer discovery version 5. Not used unless `--addr` is
960    /// IPv4, or `--discovery.v5.addr` is set.
961    #[arg(id = "discovery.v5.port", long = "discovery.v5.port", value_name = "DISCOVERY_V5_PORT", default_value = Resettable::from(DefaultDiscoveryArgs::get_global().discv5_port.map(|p| OsStr::from(p.to_string()))))]
962    pub discv5_port: Option<u16>,
963
964    /// The UDP IPv6 port to use for devp2p peer discovery version 5. Not used unless `--addr` is
965    /// IPv6, or `--discovery.addr.ipv6` is set.
966    ///
967    /// If not provided, discovery V5 defaults to same port as discovery V4 (--discovery.port).
968    #[arg(id = "discovery.v5.port.ipv6", long = "discovery.v5.port.ipv6", value_name = "DISCOVERY_V5_PORT_IPV6", default_value = Resettable::from(DefaultDiscoveryArgs::get_global().discv5_port_ipv6.map(|p| OsStr::from(p.to_string()))))]
969    pub discv5_port_ipv6: Option<u16>,
970
971    /// The interval in seconds at which to carry out periodic lookup queries, for the whole
972    /// run of the program.
973    #[arg(id = "discovery.v5.lookup-interval", long = "discovery.v5.lookup-interval", value_name = "DISCOVERY_V5_LOOKUP_INTERVAL", default_value_t = DefaultDiscoveryArgs::get_global().discv5_lookup_interval)]
974    pub discv5_lookup_interval: u64,
975
976    /// The interval in seconds at which to carry out boost lookup queries, for a fixed number of
977    /// times, at bootstrap.
978    #[arg(id = "discovery.v5.bootstrap.lookup-interval", long = "discovery.v5.bootstrap.lookup-interval", value_name = "DISCOVERY_V5_BOOTSTRAP_LOOKUP_INTERVAL",
979        default_value_t = DefaultDiscoveryArgs::get_global().discv5_bootstrap_lookup_interval)]
980    pub discv5_bootstrap_lookup_interval: u64,
981
982    /// The number of times to carry out boost lookup queries at bootstrap.
983    #[arg(id = "discovery.v5.bootstrap.lookup-countdown", long = "discovery.v5.bootstrap.lookup-countdown", value_name = "DISCOVERY_V5_BOOTSTRAP_LOOKUP_COUNTDOWN",
984        default_value_t = DefaultDiscoveryArgs::get_global().discv5_bootstrap_lookup_countdown)]
985    pub discv5_bootstrap_lookup_countdown: u64,
986}
987
988impl DiscoveryArgs {
989    /// Apply the discovery settings to the given [`NetworkConfigBuilder`]
990    pub fn apply_to_builder<N>(
991        &self,
992        mut network_config_builder: NetworkConfigBuilder<N>,
993        rlpx_tcp_socket: SocketAddr,
994        boot_nodes: impl IntoIterator<Item = NodeRecord>,
995    ) -> NetworkConfigBuilder<N>
996    where
997        N: NetworkPrimitives,
998    {
999        if self.disable_discovery || self.disable_dns_discovery {
1000            network_config_builder = network_config_builder.disable_dns_discovery();
1001        }
1002
1003        if self.disable_discovery || self.disable_discv4_discovery {
1004            network_config_builder = network_config_builder.disable_discv4_discovery();
1005        }
1006
1007        if self.disable_nat {
1008            // we only check for `disable-nat` here and not for disable discovery because nat:extip can be used without discovery: <https://github.com/paradigmxyz/reth/issues/14878>
1009            network_config_builder = network_config_builder.disable_nat();
1010        }
1011
1012        if self.should_enable_discv5() {
1013            network_config_builder = network_config_builder
1014                .discovery_v5(self.discovery_v5_builder(rlpx_tcp_socket, boot_nodes));
1015        }
1016
1017        network_config_builder
1018    }
1019
1020    /// Creates a [`reth_discv5::ConfigBuilder`] filling it with the values from this struct.
1021    pub fn discovery_v5_builder(
1022        &self,
1023        rlpx_tcp_socket: SocketAddr,
1024        boot_nodes: impl IntoIterator<Item = NodeRecord>,
1025    ) -> reth_discv5::ConfigBuilder {
1026        let Self {
1027            discv5_addr,
1028            discv5_addr_ipv6,
1029            discv5_port,
1030            discv5_port_ipv6,
1031            discv5_lookup_interval,
1032            discv5_bootstrap_lookup_interval,
1033            discv5_bootstrap_lookup_countdown,
1034            port,
1035            ..
1036        } = self;
1037
1038        let has_discv5_addr_args = discv5_addr.is_some() || discv5_addr_ipv6.is_some();
1039
1040        // Use rlpx address if none given
1041        let discv5_addr_ipv4 = discv5_addr.or(match rlpx_tcp_socket {
1042            SocketAddr::V4(addr) => Some(*addr.ip()),
1043            SocketAddr::V6(_) => None,
1044        });
1045        let discv5_addr_ipv6 = discv5_addr_ipv6.or(match rlpx_tcp_socket {
1046            SocketAddr::V4(_) => None,
1047            SocketAddr::V6(addr) => Some(*addr.ip()),
1048        });
1049
1050        let mut discv5_config_builder =
1051            reth_discv5::discv5::ConfigBuilder::new(ListenConfig::from_two_sockets(
1052                discv5_addr_ipv4.map(|addr| SocketAddrV4::new(addr, discv5_port.unwrap_or(*port))),
1053                discv5_addr_ipv6
1054                    .map(|addr| SocketAddrV6::new(addr, discv5_port_ipv6.unwrap_or(*port), 0, 0)),
1055            ));
1056
1057        if has_discv5_addr_args || self.disable_nat {
1058            // disable native enr update if addresses manually set or nat disabled
1059            discv5_config_builder.disable_enr_update();
1060        }
1061        reth_discv5::Config::builder(rlpx_tcp_socket)
1062            .discv5_config(discv5_config_builder.build())
1063            .add_unsigned_boot_nodes(boot_nodes)
1064            .lookup_interval(*discv5_lookup_interval)
1065            .bootstrap_lookup_interval(*discv5_bootstrap_lookup_interval)
1066            .bootstrap_lookup_countdown(*discv5_bootstrap_lookup_countdown)
1067    }
1068
1069    /// Returns true if discv5 discovery should be configured.
1070    ///
1071    /// Discv5 is enabled by default and can be disabled with `--disable-discv5-discovery`.
1072    const fn should_enable_discv5(&self) -> bool {
1073        if self.disable_discovery || self.disable_discv5_discovery {
1074            return false;
1075        }
1076
1077        true
1078    }
1079
1080    /// Set the discovery ports to zero, to allow the OS to assign random unused ports when
1081    /// discovery binds to the sockets.
1082    pub const fn with_unused_discovery_port(mut self) -> Self {
1083        self.port = 0;
1084        self.discv5_port = Some(0);
1085        self.discv5_port_ipv6 = Some(0);
1086        self
1087    }
1088
1089    /// Set the discovery V5 port
1090    pub fn with_discv5_port(mut self, port: impl Into<Option<u16>>) -> Self {
1091        self.discv5_port = port.into();
1092        self
1093    }
1094
1095    /// Change networking port numbers based on the instance number.
1096    /// Ports are updated to `previous_value + instance - 1`
1097    ///
1098    /// # Panics
1099    /// Warning: if `instance` is zero in debug mode, this will panic.
1100    pub fn adjust_instance_ports(&mut self, instance: u16) {
1101        debug_assert_ne!(instance, 0, "instance must be non-zero");
1102        self.port += instance - 1;
1103        self.discv5_port = self.discv5_port.map(|port| port + instance - 1);
1104        self.discv5_port_ipv6 = self.discv5_port_ipv6.map(|port| port + instance - 1);
1105    }
1106}
1107
1108impl Default for DiscoveryArgs {
1109    fn default() -> Self {
1110        let DefaultDiscoveryArgs {
1111            disable_discovery,
1112            disable_dns_discovery,
1113            disable_discv4_discovery,
1114            disable_discv5_discovery,
1115            disable_nat,
1116            addr,
1117            port,
1118            discv5_addr,
1119            discv5_addr_ipv6,
1120            discv5_port,
1121            discv5_port_ipv6,
1122            discv5_lookup_interval,
1123            discv5_bootstrap_lookup_interval,
1124            discv5_bootstrap_lookup_countdown,
1125        } = *DefaultDiscoveryArgs::get_global();
1126        Self {
1127            disable_discovery,
1128            disable_dns_discovery,
1129            disable_discv4_discovery,
1130            enable_discv5_discovery: false,
1131            disable_discv5_discovery,
1132            disable_nat,
1133            addr,
1134            port,
1135            discv5_addr,
1136            discv5_addr_ipv6,
1137            discv5_port,
1138            discv5_port_ipv6,
1139            discv5_lookup_interval,
1140            discv5_bootstrap_lookup_interval,
1141            discv5_bootstrap_lookup_countdown,
1142        }
1143    }
1144}
1145
1146/// Parse a block number=hash pair or just a hash into `BlockNumHash`
1147fn parse_block_num_hash(s: &str) -> Result<BlockNumHash, String> {
1148    if let Some((num_str, hash_str)) = s.split_once('=') {
1149        let number = num_str.parse().map_err(|_| format!("Invalid block number: {}", num_str))?;
1150        let hash = B256::from_str(hash_str).map_err(|_| format!("Invalid hash: {}", hash_str))?;
1151        Ok(BlockNumHash::new(number, hash))
1152    } else {
1153        // For backward compatibility, treat as hash-only with number 0
1154        let hash = B256::from_str(s).map_err(|_| format!("Invalid hash: {}", s))?;
1155        Ok(BlockNumHash::new(0, hash))
1156    }
1157}
1158
1159#[cfg(test)]
1160mod tests {
1161    use super::*;
1162    use clap::Parser;
1163    use reth_chainspec::MAINNET;
1164    use reth_config::Config;
1165    use reth_network_peers::NodeRecord;
1166    use secp256k1::SecretKey;
1167    use std::{
1168        fs,
1169        time::{SystemTime, UNIX_EPOCH},
1170    };
1171
1172    /// A helper type to parse Args more easily
1173    #[derive(Parser)]
1174    struct CommandParser<T: Args> {
1175        #[command(flatten)]
1176        args: T,
1177    }
1178
1179    #[test]
1180    fn parse_nat_args() {
1181        let args = CommandParser::<NetworkArgs>::parse_from(["reth", "--nat", "none"]).args;
1182        assert_eq!(args.nat, NatResolver::None);
1183
1184        let args =
1185            CommandParser::<NetworkArgs>::parse_from(["reth", "--nat", "extip:0.0.0.0"]).args;
1186        assert_eq!(args.nat, NatResolver::ExternalIp("0.0.0.0".parse().unwrap()));
1187    }
1188
1189    #[test]
1190    fn parse_peer_args() {
1191        let args =
1192            CommandParser::<NetworkArgs>::parse_from(["reth", "--max-outbound-peers", "50"]).args;
1193        assert_eq!(args.max_outbound_peers, Some(50));
1194        assert_eq!(args.max_inbound_peers, None);
1195
1196        let args = CommandParser::<NetworkArgs>::parse_from([
1197            "reth",
1198            "--max-outbound-peers",
1199            "75",
1200            "--max-inbound-peers",
1201            "15",
1202        ])
1203        .args;
1204        assert_eq!(args.max_outbound_peers, Some(75));
1205        assert_eq!(args.max_inbound_peers, Some(15));
1206    }
1207
1208    #[test]
1209    fn parse_trusted_peer_args() {
1210        let args =
1211            CommandParser::<NetworkArgs>::parse_from([
1212            "reth",
1213            "--trusted-peers",
1214            "enode://d860a01f9722d78051619d1e2351aba3f43f943f6f00718d1b9baa4101932a1f5011f16bb2b1bb35db20d6fe28fa0bf09636d26a87d31de9ec6203eeedb1f666@18.138.108.67:30303,enode://22a8232c3abc76a16ae9d6c3b164f98775fe226f0917b0ca871128a74a8e9630b458460865bab457221f1d448dd9791d24c4e5d88786180ac185df813a68d4de@3.209.45.79:30303"
1215        ])
1216        .args;
1217
1218        assert_eq!(
1219            args.trusted_peers,
1220            vec![
1221            "enode://d860a01f9722d78051619d1e2351aba3f43f943f6f00718d1b9baa4101932a1f5011f16bb2b1bb35db20d6fe28fa0bf09636d26a87d31de9ec6203eeedb1f666@18.138.108.67:30303".parse().unwrap(),
1222            "enode://22a8232c3abc76a16ae9d6c3b164f98775fe226f0917b0ca871128a74a8e9630b458460865bab457221f1d448dd9791d24c4e5d88786180ac185df813a68d4de@3.209.45.79:30303".parse().unwrap()
1223            ]
1224        );
1225    }
1226
1227    #[test]
1228    fn parse_retry_strategy_args() {
1229        let tests = vec![0, 10];
1230
1231        for retries in tests {
1232            let retries_str = retries.to_string();
1233            let args = CommandParser::<NetworkArgs>::parse_from([
1234                "reth",
1235                "--dns-retries",
1236                retries_str.as_str(),
1237            ])
1238            .args;
1239
1240            assert_eq!(args.dns_retries, retries);
1241        }
1242    }
1243
1244    #[test]
1245    fn parse_disable_tx_gossip_args() {
1246        let args = CommandParser::<NetworkArgs>::parse_from(["reth", "--disable-tx-gossip"]).args;
1247        assert!(args.disable_tx_gossip);
1248    }
1249
1250    #[test]
1251    fn parse_max_peers_flag() {
1252        let args = CommandParser::<NetworkArgs>::parse_from(["reth", "--max-peers", "90"]).args;
1253
1254        assert_eq!(args.max_peers, Some(90));
1255        assert_eq!(args.max_outbound_peers, None);
1256        assert_eq!(args.max_inbound_peers, None);
1257        assert_eq!(args.resolved_max_outbound_peers(), Some(30));
1258        assert_eq!(args.resolved_max_inbound_peers(), Some(60));
1259    }
1260
1261    #[test]
1262    fn max_peers_conflicts_with_outbound() {
1263        let result = CommandParser::<NetworkArgs>::try_parse_from([
1264            "reth",
1265            "--max-peers",
1266            "90",
1267            "--max-outbound-peers",
1268            "50",
1269        ]);
1270        assert!(
1271            result.is_err(),
1272            "Should fail when both --max-peers and --max-outbound-peers are used"
1273        );
1274    }
1275
1276    #[test]
1277    fn max_peers_conflicts_with_inbound() {
1278        let result = CommandParser::<NetworkArgs>::try_parse_from([
1279            "reth",
1280            "--max-peers",
1281            "90",
1282            "--max-inbound-peers",
1283            "30",
1284        ]);
1285        assert!(
1286            result.is_err(),
1287            "Should fail when both --max-peers and --max-inbound-peers are used"
1288        );
1289    }
1290
1291    #[test]
1292    fn max_peers_split_calculation() {
1293        let args = CommandParser::<NetworkArgs>::parse_from(["reth", "--max-peers", "90"]).args;
1294
1295        assert_eq!(args.max_peers, Some(90));
1296        assert_eq!(args.resolved_max_outbound_peers(), Some(30));
1297        assert_eq!(args.resolved_max_inbound_peers(), Some(60));
1298    }
1299
1300    #[test]
1301    fn max_peers_small_values() {
1302        let args1 = CommandParser::<NetworkArgs>::parse_from(["reth", "--max-peers", "1"]).args;
1303        assert_eq!(args1.resolved_max_outbound_peers(), Some(1));
1304        assert_eq!(args1.resolved_max_inbound_peers(), Some(0));
1305
1306        let args2 = CommandParser::<NetworkArgs>::parse_from(["reth", "--max-peers", "2"]).args;
1307        assert_eq!(args2.resolved_max_outbound_peers(), Some(1));
1308        assert_eq!(args2.resolved_max_inbound_peers(), Some(1));
1309
1310        let args3 = CommandParser::<NetworkArgs>::parse_from(["reth", "--max-peers", "3"]).args;
1311        assert_eq!(args3.resolved_max_outbound_peers(), Some(1));
1312        assert_eq!(args3.resolved_max_inbound_peers(), Some(2));
1313    }
1314
1315    #[test]
1316    fn resolved_peers_without_max_peers() {
1317        let args = CommandParser::<NetworkArgs>::parse_from([
1318            "reth",
1319            "--max-outbound-peers",
1320            "75",
1321            "--max-inbound-peers",
1322            "15",
1323        ])
1324        .args;
1325
1326        assert_eq!(args.max_peers, None);
1327        assert_eq!(args.resolved_max_outbound_peers(), Some(75));
1328        assert_eq!(args.resolved_max_inbound_peers(), Some(15));
1329    }
1330
1331    #[test]
1332    fn resolved_peers_with_defaults() {
1333        let args = CommandParser::<NetworkArgs>::parse_from(["reth"]).args;
1334
1335        assert_eq!(args.max_peers, None);
1336        assert_eq!(args.resolved_max_outbound_peers(), None);
1337        assert_eq!(args.resolved_max_inbound_peers(), None);
1338    }
1339
1340    #[test]
1341    fn network_args_default_sanity_test() {
1342        let default_args = NetworkArgs::default();
1343        let args = CommandParser::<NetworkArgs>::parse_from(["reth"]).args;
1344
1345        assert_eq!(args, default_args);
1346    }
1347
1348    #[test]
1349    fn net_if_uses_resolved_addr_for_default_discovery_addr() {
1350        let args =
1351            CommandParser::<NetworkArgs>::parse_from(["reth", "--net-if.experimental", "en0"]).args;
1352        let listener_addr = IpAddr::V4(Ipv4Addr::new(192, 0, 2, 1));
1353
1354        assert_eq!(args.resolved_discovery_addr(listener_addr), listener_addr);
1355    }
1356
1357    #[test]
1358    fn net_if_preserves_custom_discovery_addr() {
1359        let custom_discovery_addr = IpAddr::V4(Ipv4Addr::new(192, 0, 2, 2));
1360        let args = CommandParser::<NetworkArgs>::parse_from([
1361            "reth",
1362            "--net-if.experimental",
1363            "en0",
1364            "--discovery.addr",
1365            "192.0.2.2",
1366        ])
1367        .args;
1368        let listener_addr = IpAddr::V4(Ipv4Addr::new(192, 0, 2, 1));
1369
1370        assert_eq!(args.resolved_discovery_addr(listener_addr), custom_discovery_addr);
1371    }
1372
1373    #[test]
1374    fn default_discovery_addr_is_preserved_without_net_if() {
1375        let args = CommandParser::<NetworkArgs>::parse_from(["reth"]).args;
1376        let listener_addr = IpAddr::V4(Ipv4Addr::new(192, 0, 2, 1));
1377
1378        assert_eq!(args.resolved_discovery_addr(listener_addr), DEFAULT_DISCOVERY_ADDR);
1379    }
1380
1381    #[test]
1382    fn parse_eth_max_message_size() {
1383        let args = CommandParser::<NetworkArgs>::parse_from([
1384            "reth",
1385            "--eth-max-message-size",
1386            "15728640",
1387        ])
1388        .args;
1389
1390        assert_eq!(args.eth_max_message_size, Some(NonZeroUsize::new(15 * 1024 * 1024).unwrap()));
1391    }
1392
1393    #[test]
1394    fn parse_eth_max_message_size_zero_rejected() {
1395        let result =
1396            CommandParser::<NetworkArgs>::try_parse_from(["reth", "--eth-max-message-size", "0"]);
1397        assert!(result.is_err());
1398    }
1399
1400    #[test]
1401    fn parse_eth_max_message_size_above_rlpx_cap() {
1402        let result = CommandParser::<NetworkArgs>::try_parse_from([
1403            "reth",
1404            "--eth-max-message-size",
1405            "16777216",
1406        ]);
1407        assert!(result.is_ok());
1408        let args = result.unwrap().args;
1409        assert_eq!(args.eth_max_message_size, Some(NonZeroUsize::new(16 * 1024 * 1024).unwrap()));
1410    }
1411
1412    #[test]
1413    fn parse_required_block_hashes() {
1414        let args = CommandParser::<NetworkArgs>::parse_from([
1415            "reth",
1416            "--required-block-hashes",
1417            "0x1111111111111111111111111111111111111111111111111111111111111111,23115201=0x2222222222222222222222222222222222222222222222222222222222222222",
1418        ])
1419        .args;
1420
1421        assert_eq!(args.required_block_hashes.len(), 2);
1422        // First hash without block number (should default to 0)
1423        assert_eq!(args.required_block_hashes[0].number, 0);
1424        assert_eq!(
1425            args.required_block_hashes[0].hash.to_string(),
1426            "0x1111111111111111111111111111111111111111111111111111111111111111"
1427        );
1428        // Second with block number=hash format
1429        assert_eq!(args.required_block_hashes[1].number, 23115201);
1430        assert_eq!(
1431            args.required_block_hashes[1].hash.to_string(),
1432            "0x2222222222222222222222222222222222222222222222222222222222222222"
1433        );
1434    }
1435
1436    #[test]
1437    fn parse_empty_required_block_hashes() {
1438        let args = CommandParser::<NetworkArgs>::parse_from(["reth"]).args;
1439        assert!(args.required_block_hashes.is_empty());
1440    }
1441
1442    #[test]
1443    fn test_parse_block_num_hash() {
1444        // Test hash only format
1445        let result = parse_block_num_hash(
1446            "0x1111111111111111111111111111111111111111111111111111111111111111",
1447        );
1448        assert!(result.is_ok());
1449        assert_eq!(result.unwrap().number, 0);
1450
1451        // Test block_number=hash format
1452        let result = parse_block_num_hash(
1453            "23115201=0x2222222222222222222222222222222222222222222222222222222222222222",
1454        );
1455        assert!(result.is_ok());
1456        assert_eq!(result.unwrap().number, 23115201);
1457
1458        // Test invalid formats
1459        assert!(parse_block_num_hash("invalid").is_err());
1460        assert!(parse_block_num_hash(
1461            "abc=0x1111111111111111111111111111111111111111111111111111111111111111"
1462        )
1463        .is_err());
1464    }
1465
1466    #[test]
1467    fn parse_p2p_secret_key_hex() {
1468        let hex = "4c0883a69102937d6231471b5dbb6204fe512961708279f8c5c58b3b9c4e8b8f";
1469        let args =
1470            CommandParser::<NetworkArgs>::parse_from(["reth", "--p2p-secret-key-hex", hex]).args;
1471
1472        let expected: B256 = hex.parse().unwrap();
1473        assert_eq!(args.p2p_secret_key_hex, Some(expected));
1474        assert_eq!(args.p2p_secret_key, None);
1475    }
1476
1477    #[test]
1478    fn parse_p2p_secret_key_hex_with_0x_prefix() {
1479        let hex = "0x4c0883a69102937d6231471b5dbb6204fe512961708279f8c5c58b3b9c4e8b8f";
1480        let args =
1481            CommandParser::<NetworkArgs>::parse_from(["reth", "--p2p-secret-key-hex", hex]).args;
1482
1483        let expected: B256 = hex.parse().unwrap();
1484        assert_eq!(args.p2p_secret_key_hex, Some(expected));
1485        assert_eq!(args.p2p_secret_key, None);
1486    }
1487
1488    #[test]
1489    fn test_p2p_secret_key_and_hex_are_mutually_exclusive() {
1490        let result = CommandParser::<NetworkArgs>::try_parse_from([
1491            "reth",
1492            "--p2p-secret-key",
1493            "/path/to/key",
1494            "--p2p-secret-key-hex",
1495            "4c0883a69102937d6231471b5dbb6204fe512961708279f8c5c58b3b9c4e8b8f",
1496        ]);
1497
1498        assert!(result.is_err());
1499    }
1500
1501    #[test]
1502    fn test_secret_key_method_with_hex() {
1503        let hex = "4c0883a69102937d6231471b5dbb6204fe512961708279f8c5c58b3b9c4e8b8f";
1504        let args =
1505            CommandParser::<NetworkArgs>::parse_from(["reth", "--p2p-secret-key-hex", hex]).args;
1506
1507        let temp_dir = std::env::temp_dir();
1508        let default_path = temp_dir.join("default_key");
1509        let secret_key = args.secret_key(default_path).unwrap();
1510
1511        // Verify the secret key matches the hex input
1512        assert_eq!(alloy_primitives::hex::encode(secret_key.secret_bytes()), hex);
1513    }
1514
1515    #[test]
1516    fn parse_netrestrict_single_network() {
1517        let args =
1518            CommandParser::<NetworkArgs>::parse_from(["reth", "--netrestrict", "192.168.0.0/16"])
1519                .args;
1520
1521        assert_eq!(args.netrestrict, Some("192.168.0.0/16".to_string()));
1522
1523        let ip_filter = args.ip_filter().unwrap();
1524        assert!(ip_filter.has_restrictions());
1525        assert!(ip_filter.is_allowed(&"192.168.1.1".parse().unwrap()));
1526        assert!(!ip_filter.is_allowed(&"10.0.0.1".parse().unwrap()));
1527    }
1528
1529    #[test]
1530    fn parse_netrestrict_multiple_networks() {
1531        let args = CommandParser::<NetworkArgs>::parse_from([
1532            "reth",
1533            "--netrestrict",
1534            "192.168.0.0/16,10.0.0.0/8",
1535        ])
1536        .args;
1537
1538        assert_eq!(args.netrestrict, Some("192.168.0.0/16,10.0.0.0/8".to_string()));
1539
1540        let ip_filter = args.ip_filter().unwrap();
1541        assert!(ip_filter.has_restrictions());
1542        assert!(ip_filter.is_allowed(&"192.168.1.1".parse().unwrap()));
1543        assert!(ip_filter.is_allowed(&"10.5.10.20".parse().unwrap()));
1544        assert!(!ip_filter.is_allowed(&"172.16.0.1".parse().unwrap()));
1545    }
1546
1547    #[test]
1548    fn parse_netrestrict_ipv6() {
1549        let args =
1550            CommandParser::<NetworkArgs>::parse_from(["reth", "--netrestrict", "2001:db8::/32"])
1551                .args;
1552
1553        let ip_filter = args.ip_filter().unwrap();
1554        assert!(ip_filter.has_restrictions());
1555        assert!(ip_filter.is_allowed(&"2001:db8::1".parse().unwrap()));
1556        assert!(!ip_filter.is_allowed(&"2001:db9::1".parse().unwrap()));
1557    }
1558
1559    #[test]
1560    fn netrestrict_not_set() {
1561        let args = CommandParser::<NetworkArgs>::parse_from(["reth"]).args;
1562        assert_eq!(args.netrestrict, None);
1563
1564        let ip_filter = args.ip_filter().unwrap();
1565        assert!(!ip_filter.has_restrictions());
1566        assert!(ip_filter.is_allowed(&"192.168.1.1".parse().unwrap()));
1567        assert!(ip_filter.is_allowed(&"10.0.0.1".parse().unwrap()));
1568    }
1569
1570    #[test]
1571    fn netrestrict_invalid_cidr() {
1572        let args =
1573            CommandParser::<NetworkArgs>::parse_from(["reth", "--netrestrict", "invalid-cidr"])
1574                .args;
1575
1576        assert!(args.ip_filter().is_err());
1577    }
1578
1579    #[test]
1580    fn network_config_preserves_basic_nodes_from_peers_file() {
1581        let enode = "enode://6f8a80d14311c39f35f516fa664deaaaa13e85b2f7493f37f6144d86991ec012937307647bd3b9a82abe2974e1407241d54947bbb39763a4cac9f77166ad92a0@10.3.58.6:30303?discport=30301";
1582        let unique = SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_nanos();
1583
1584        let peers_file = std::env::temp_dir().join(format!("reth_peers_test_{}.json", unique));
1585        fs::write(&peers_file, format!("[\"{}\"]", enode)).expect("write peers file");
1586
1587        // Build NetworkArgs with peers_file set and no_persist_peers=false
1588        let args = NetworkArgs {
1589            peers_file: Some(peers_file.clone()),
1590            no_persist_peers: false,
1591            ..Default::default()
1592        };
1593
1594        // Build the network config using a deterministic secret key
1595        let secret_key = SecretKey::from_byte_array(&[1u8; 32]).unwrap();
1596        let builder = args.network_config::<reth_network::EthNetworkPrimitives>(
1597            &Config::default(),
1598            MAINNET.clone(),
1599            secret_key,
1600            peers_file.clone(),
1601            Runtime::test(),
1602        );
1603
1604        let net_cfg = builder.build_with_noop_provider(MAINNET.clone());
1605
1606        // Assert persisted_peers contains our node (legacy format is auto-converted)
1607        let node: NodeRecord = enode.parse().unwrap();
1608        assert!(net_cfg.peers_config.persisted_peers.iter().any(|p| p.record == node));
1609
1610        // Cleanup
1611        let _ = fs::remove_file(&peers_file);
1612    }
1613}