Skip to content
This repository has been archived by the owner on Jan 25, 2024. It is now read-only.

Commit

Permalink
chore: tidy peer management for join command
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jacderida committed Jan 15, 2024
1 parent 7368f30 commit 3f2ecbb
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 25 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ 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.1"
sn_peers_acquisition = { git = "https://github.com/jacderida/safe_network.git", branch = "fix-peers-args" }
sn_protocol = "0.10.8"
sn-releases = "0.1.6"
sysinfo = "0.29.10"
Expand Down
34 changes: 10 additions & 24 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -151,7 +150,7 @@ pub enum SubCmd {
#[clap(long)]
node_version: Option<String>,
#[command(flatten)]
peers: Option<PeersArgs>,
peers: PeersArgs,
/// Set to skip the network validation process
#[clap(long)]
skip_validation: bool,
Expand Down Expand Up @@ -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::<Vec<Multiaddr>>();
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,
Expand Down

0 comments on commit 3f2ecbb

Please sign in to comment.