feat(cli): add max peer args (#4024)

This commit is contained in:
Altuğ Bakan
2023-08-01 20:44:39 +02:00
committed by GitHub
parent bfbad261ec
commit b46101afb5
2 changed files with 54 additions and 5 deletions

View File

@@ -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 {
@@ -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);
@@ -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 {
@@ -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));
}
}

View File

@@ -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;