From 231e6437e4a052ed06c6eb29d38948b42d2f8899 Mon Sep 17 00:00:00 2001 From: Dan Cline <6798349+Rjected@users.noreply.github.com> Date: Wed, 31 Jan 2024 17:54:33 -0500 Subject: [PATCH] feat: add with-unused-ports cli argument (#6314) --- bin/reth/src/commands/node/mod.rs | 49 ++++++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/bin/reth/src/commands/node/mod.rs b/bin/reth/src/commands/node/mod.rs index 5efa599df6..6d795e292d 100644 --- a/bin/reth/src/commands/node/mod.rs +++ b/bin/reth/src/commands/node/mod.rs @@ -77,6 +77,13 @@ pub struct NodeCommand { #[arg(long, value_name = "INSTANCE", global = true, default_value_t = 1, value_parser = value_parser!(u16).range(..=200))] pub instance: u16, + /// Sets all ports to unused, allowing the OS to choose random unused ports when sockets are + /// bound. + /// + /// Mutually exclusive with `--instance`. + #[arg(long, conflicts_with = "instance", global = true)] + pub with_unused_ports: bool, + /// Overrides the KZG trusted setup by reading from the supplied file. #[arg(long, value_name = "PATH")] pub trusted_setup_file: Option, @@ -134,6 +141,7 @@ impl NodeCommand { metrics, trusted_setup_file, instance, + with_unused_ports, network, rpc, txpool, @@ -152,6 +160,7 @@ impl NodeCommand { chain, metrics, instance, + with_unused_ports, trusted_setup_file, network, rpc, @@ -176,6 +185,7 @@ impl NodeCommand { metrics, trusted_setup_file, instance, + with_unused_ports, network, rpc, txpool, @@ -193,7 +203,7 @@ impl NodeCommand { let database = DatabaseBuilder::Real(datadir); // set up node config - let node_config = NodeConfig { + let mut node_config = NodeConfig { database, config, chain, @@ -212,6 +222,10 @@ impl NodeCommand { rollup, }; + if with_unused_ports { + node_config = node_config.with_unused_ports(); + } + let executor = ctx.task_executor; // launch the node @@ -390,4 +404,37 @@ mod tests { // check network listening port number assert_eq!(cmd.network.port, 30305); } + + #[test] + fn parse_with_unused_ports() { + let cmd = NodeCommand::<()>::parse_from(["reth", "--with-unused-ports"]); + assert!(cmd.with_unused_ports); + } + + #[test] + fn with_unused_ports_conflicts_with_instance() { + let err = + NodeCommand::<()>::try_parse_from(["reth", "--with-unused-ports", "--instance", "2"]) + .unwrap_err(); + assert_eq!(err.kind(), clap::error::ErrorKind::ArgumentConflict); + } + + #[test] + fn with_unused_ports_check_zero() { + let mut cmd = NodeCommand::<()>::parse_from(["reth"]); + cmd.rpc = cmd.rpc.with_unused_ports(); + cmd.network = cmd.network.with_unused_ports(); + + // make sure the rpc ports are zero + assert_eq!(cmd.rpc.auth_port, 0); + assert_eq!(cmd.rpc.http_port, 0); + assert_eq!(cmd.rpc.ws_port, 0); + + // make sure the network ports are zero + assert_eq!(cmd.network.port, 0); + assert_eq!(cmd.network.discovery.port, 0); + + // make sure the ipc path is not the default + assert_ne!(cmd.rpc.ipcpath, String::from("/tmp/reth.ipc")); + } }