From 6555e598e7e30633e4f70822d9cd4c33f8b8ae40 Mon Sep 17 00:00:00 2001 From: Chris O'Neil Date: Wed, 10 Jan 2024 13:32:14 +0000 Subject: [PATCH] chore: respond to feedback * Use clap to provide default node count * Mark `join` arguments as conflicting * Fix bug with single node on join * Use `find_map` rather than `find` to eliminate additional step --- src/local.rs | 16 +++++++--------- src/main.rs | 14 ++++++++------ 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/local.rs b/src/local.rs index 1950d80..1d84a08 100644 --- a/src/local.rs +++ b/src/local.rs @@ -20,8 +20,6 @@ use std::process::{Command, Stdio}; use std::str::FromStr; use sysinfo::{Pid, ProcessExt, System, SystemExt}; -const DEFAULT_NODE_COUNT: u16 = 25; - #[cfg_attr(test, automock)] pub trait Launcher { fn get_safenode_path(&self) -> PathBuf; @@ -159,7 +157,7 @@ pub fn kill_network(node_registry: &NodeRegistry, keep_directories: bool) -> Res pub struct LocalNetworkOptions { pub faucet_bin_path: PathBuf, pub join: bool, - pub node_count: Option, + pub node_count: u16, pub peers: Option>, pub safenode_bin_path: PathBuf, pub skip_validation: bool, @@ -175,16 +173,16 @@ pub async fn run_network( faucet_bin_path: network_options.faucet_bin_path.to_path_buf(), }; - let peers = if network_options.join { + let (peers, start) = if network_options.join { if let Some(peers) = network_options.peers { - peers + (peers, 1) } else { let peer = node_registry .nodes .iter() - .find(|n| n.get_multiaddr().is_some()) + .find_map(|n| n.get_multiaddr()) .ok_or_else(|| eyre!("Unable to obtain a peer to connect to"))?; - vec![peer.get_multiaddr().unwrap()] + (vec![peer], 1) } } else { let port = service_control.get_available_port()?; @@ -199,10 +197,10 @@ pub async fn run_network( &rpc_client, ) .await?; - vec![genesis_multiaddr] + (vec![genesis_multiaddr], 2) }; - for _ in 2..=network_options.node_count.unwrap_or(DEFAULT_NODE_COUNT) { + for _ in start..=network_options.node_count { let port = service_control.get_available_port()?; let rpc_port = service_control.get_available_port()?; let rpc_client = RpcClient::new(&format!("https://127.0.0.1:{rpc_port}")); diff --git a/src/main.rs b/src/main.rs index ac4b580..d8f21f7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -32,6 +32,8 @@ use sn_releases::{ReleaseType, SafeReleaseRepositoryInterface}; use std::path::PathBuf; use std::str::FromStr; +const DEFAULT_NODE_COUNT: u16 = 25; + #[derive(Parser)] #[command(author, version, about, long_about = None)] pub(crate) struct Cmd { @@ -123,12 +125,12 @@ pub enum SubCmd { #[clap(name = "join")] Join { /// The number of nodes to run. - #[clap(long)] - count: Option, + #[clap(long, default_value_t = DEFAULT_NODE_COUNT)] + count: u16, /// Path to a faucet binary /// /// The path and version arguments are mutually exclusive. - #[clap(long)] + #[clap(long, conflicts_with = "faucet_version")] faucet_path: Option, /// The version of the faucet to use. /// @@ -138,7 +140,7 @@ pub enum SubCmd { /// Path to a safenode binary /// /// The path and version arguments are mutually exclusive. - #[clap(long)] + #[clap(long, conflicts_with = "faucet_version")] node_path: Option, /// The version of safenode to use. /// @@ -178,8 +180,8 @@ pub enum SubCmd { #[clap(name = "run")] Run { /// The number of nodes to run. - #[clap(long)] - count: Option, + #[clap(long, default_value_t = DEFAULT_NODE_COUNT)] + count: u16, /// Path to a faucet binary /// /// The path and version arguments are mutually exclusive.