From 6b42af50903957003130f61441d0cd133a19b2b6 Mon Sep 17 00:00:00 2001 From: Chris O'Neil Date: Mon, 15 Jan 2024 21:18:03 +0000 Subject: [PATCH] chore: tidy peer management for `join` command The `peers` argument is no longer declared with an `Option` and the manual parsing of `SAFE_PEERS` is removed. If the user doesn't specify any peers using `--peer` or `SAFE_PEERS`, the `get_peers_from_args` function will return a particular type of error. In this case, we can just assume the user wants the nodes to join an existing local network. --- Cargo.toml | 2 +- src/main.rs | 34 ++++++++++------------------------ 2 files changed, 11 insertions(+), 25 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 1842b9d..301d1fc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,7 +24,7 @@ serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" service-manager = "0.5.1" sn_node_rpc_client = "0.2.4" -sn_peers_acquisition = "0.2.1" +sn_peers_acquisition = "0.2.2" sn_protocol = "0.10.8" sn-releases = "0.1.6" sysinfo = "0.29.10" diff --git a/src/main.rs b/src/main.rs index c97b48e..dc6a189 100644 --- a/src/main.rs +++ b/src/main.rs @@ -22,11 +22,10 @@ use crate::service::{NodeServiceManager, ServiceControl}; use clap::{Parser, Subcommand}; use color_eyre::{eyre::eyre, Help, Result}; use colored::Colorize; -use libp2p::Multiaddr; use libp2p_identity::PeerId; use semver::Version; use sn_node_rpc_client::RpcClient; -use sn_peers_acquisition::{get_peers_from_args, parse_peer_addr, PeersArgs}; +use sn_peers_acquisition::{get_peers_from_args, PeersArgs}; use sn_protocol::node_registry::{get_local_node_registry_path, NodeRegistry}; use sn_releases::{ReleaseType, SafeReleaseRepositoryInterface}; use std::path::PathBuf; @@ -151,7 +150,7 @@ pub enum SubCmd { #[clap(long)] node_version: Option, #[command(flatten)] - peers: Option, + peers: PeersArgs, /// Set to skip the network validation process #[clap(long)] skip_validation: bool, @@ -359,28 +358,15 @@ async fn main() -> Result<()> { ) .await?; - // Unfortunately because the peers argument is an option, clap does not handle parsing - // `SAFE_PEERS`. - // - // It needs to be an option, otherwise we wouldn't be able to use the `join` command - // without peers to automatically add new nodes to an existing local network. - let peers = match std::env::var("SAFE_PEERS") { - Ok(var) => { - let peers = var - .split(',') - .map(|addr| parse_peer_addr(addr.trim()).unwrap()) - .collect::>(); - Some(peers) - } - Err(_) => { - if let Some(peers) = peers { - Some(get_peers_from_args(peers).await?) - } else { - None - } - } + // If no peers are obtained we will attempt to join the existing local network, if one + // is running. + let peers = match get_peers_from_args(peers).await { + Ok(peers) => Some(peers), + Err(e) => match e { + sn_peers_acquisition::error::Error::PeersNotObtained => None, + _ => return Err(e.into()), + }, }; - let options = LocalNetworkOptions { faucet_bin_path: faucet_path, join: true,