Skip to content

Commit

Permalink
feat(cli): add max peer args (#4024)
Browse files Browse the repository at this point in the history
  • Loading branch information
altugbakan authored Aug 1, 2023
1 parent bfbad26 commit b46101a
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 5 deletions.
41 changes: 36 additions & 5 deletions bin/reth/src/args/network_args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ pub struct NetworkArgs {
/// Network listening port. default: 30303
#[arg(long = "port", value_name = "PORT")]
pub port: Option<u16>,

/// Maximum number of outbound requests. default: 100
#[arg(long)]
pub max_outbound_peers: Option<usize>,

/// Maximum number of inbound requests. default: 30
#[arg(long)]
pub max_inbound_peers: Option<usize>,
}

impl NetworkArgs {
Expand All @@ -78,9 +86,17 @@ impl NetworkArgs {
let chain_bootnodes = chain_spec.chain.bootnodes().unwrap_or_else(mainnet_nodes);
let peers_file = self.peers_file.clone().unwrap_or(default_peers_file);

// Configure basic network stack.
// Configure peer connections
let peer_config = config
.peers
.clone()
.with_max_inbound_opt(self.max_inbound_peers)
.with_max_outbound_opt(self.max_outbound_peers);

// Configure basic network stack
let mut network_config_builder = config
.network_config(self.nat, self.persistent_peers_file(peers_file), secret_key)
.peer_config(peer_config)
.boot_nodes(self.bootnodes.clone().unwrap_or(chain_bootnodes))
.chain_spec(chain_spec);

Expand All @@ -91,11 +107,7 @@ impl NetworkArgs {

self.discovery.apply_to_builder(network_config_builder)
}
}

// === impl NetworkArgs ===

impl NetworkArgs {
/// If `no_persist_peers` is true then this returns the path to the persistent peers file path.
pub fn persistent_peers_file(&self, peers_file: PathBuf) -> Option<PathBuf> {
if self.no_persist_peers {
Expand Down Expand Up @@ -163,4 +175,23 @@ mod tests {
CommandParser::<NetworkArgs>::parse_from(["reth", "--nat", "extip:0.0.0.0"]).args;
assert_eq!(args.nat, NatResolver::ExternalIp("0.0.0.0".parse().unwrap()));
}

#[test]
fn parse_peer_args() {
let args =
CommandParser::<NetworkArgs>::parse_from(["reth", "--max-outbound-peers", "50"]).args;
assert_eq!(args.max_outbound_peers, Some(50));
assert_eq!(args.max_inbound_peers, None);

let args = CommandParser::<NetworkArgs>::parse_from([
"reth",
"--max-outbound-peers",
"75",
"--max-inbound-peers",
"15",
])
.args;
assert_eq!(args.max_outbound_peers, Some(75));
assert_eq!(args.max_inbound_peers, Some(15));
}
}
18 changes: 18 additions & 0 deletions crates/net/network/src/peers/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1134,11 +1134,29 @@ impl PeersConfig {
self.connection_info.num_inbound = num_inbound;
self
}

/// Maximum allowed outbound connections.
pub fn with_max_outbound(mut self, max_outbound: usize) -> Self {
self.connection_info.max_outbound = max_outbound;
self
}

/// Maximum allowed inbound connections with optional update.
pub fn with_max_inbound_opt(mut self, max_inbound: Option<usize>) -> Self {
if let Some(max_inbound) = max_inbound {
self.connection_info.max_inbound = max_inbound;
}
self
}

/// Maximum allowed outbound connections with optional update.
pub fn with_max_outbound_opt(mut self, max_outbound: Option<usize>) -> Self {
if let Some(max_outbound) = max_outbound {
self.connection_info.max_outbound = max_outbound;
}
self
}

/// Maximum allowed inbound connections.
pub fn with_max_inbound(mut self, max_inbound: usize) -> Self {
self.connection_info.max_inbound = max_inbound;
Expand Down

0 comments on commit b46101a

Please sign in to comment.