From cf9afb81802fcd7aa30236567965783ea65881f8 Mon Sep 17 00:00:00 2001 From: grumbach Date: Sat, 4 Jan 2025 03:53:58 +0900 Subject: [PATCH] feat: cli local setup --- ant-cli/src/access/network.rs | 32 +++++++++++++++++++++++++++++--- ant-cli/src/actions/connect.rs | 15 +++++++++++---- ant-cli/src/commands.rs | 2 +- ant-cli/src/commands/file.rs | 8 ++++---- ant-cli/src/commands/register.rs | 10 +++++----- ant-cli/src/commands/vault.rs | 10 +++++----- ant-cli/src/main.rs | 2 +- ant-cli/src/opt.rs | 6 ------ ant-evm/src/lib.rs | 2 +- ant-node/src/bin/antnode/main.rs | 18 +++++++++++------- evmlib/src/utils.rs | 2 +- 11 files changed, 69 insertions(+), 38 deletions(-) diff --git a/ant-cli/src/access/network.rs b/ant-cli/src/access/network.rs index 8c428e06d3..f69c7a3351 100644 --- a/ant-cli/src/access/network.rs +++ b/ant-cli/src/access/network.rs @@ -12,9 +12,35 @@ use color_eyre::eyre::Context; use color_eyre::Result; use color_eyre::Section; -pub async fn get_peers(peers: PeersArgs) -> Result> { - peers.get_addrs(None, Some(100)).await +pub enum NetworkPeers { + Local(Vec), + Public(Vec), +} + +impl NetworkPeers { + pub fn peers(&self) -> &Vec { + match self { + NetworkPeers::Local(addrs) => addrs, + NetworkPeers::Public(addrs) => addrs, + } + } + + pub fn is_local(&self) -> bool { + matches!(self, NetworkPeers::Local(_)) + } +} + +pub async fn get_peers(peers: PeersArgs) -> Result { + let addrs = peers.get_addrs(None, Some(100)).await .wrap_err("Please provide valid Network peers to connect to") .with_suggestion(|| format!("make sure you've provided network peers using the --peers option or the {ANT_PEERS_ENV} env var")) - .with_suggestion(|| "a peer address looks like this: /ip4/42.42.42.42/udp/4242/quic-v1/p2p/B64nodePeerIDvdjb3FAJF4ks3moreBase64CharsHere") + .with_suggestion(|| "a peer address looks like this: /ip4/42.42.42.42/udp/4242/quic-v1/p2p/B64nodePeerIDvdjb3FAJF4ks3moreBase64CharsHere")?; + + let net = if peers.local { + NetworkPeers::Local(addrs) + } else { + NetworkPeers::Public(addrs) + }; + + Ok(net) } diff --git a/ant-cli/src/actions/connect.rs b/ant-cli/src/actions/connect.rs index cba9ac217a..091c87e1c8 100644 --- a/ant-cli/src/actions/connect.rs +++ b/ant-cli/src/actions/connect.rs @@ -7,22 +7,29 @@ // permissions and limitations relating to use of the SAFE Network Software. use autonomi::Client; -use autonomi::Multiaddr; use color_eyre::eyre::bail; use color_eyre::eyre::Result; use indicatif::ProgressBar; use std::time::Duration; -pub async fn connect_to_network(peers: Vec) -> Result { +use crate::network::NetworkPeers; + +pub async fn connect_to_network(peers: NetworkPeers) -> Result { let progress_bar = ProgressBar::new_spinner(); progress_bar.enable_steady_tick(Duration::from_millis(120)); progress_bar.set_message("Connecting to The Autonomi Network..."); let new_style = progress_bar.style().tick_chars("⠁⠂⠄⡀⢀⠠⠐⠈🔗"); progress_bar.set_style(new_style); - progress_bar.set_message("Connecting to The Autonomi Network..."); + let res = if peers.is_local() { + progress_bar.set_message("Connecting to a local Autonomi Network..."); + Client::init_local().await + } else { + progress_bar.set_message("Connecting to The Autonomi Network..."); + Client::init_with_peers(peers.peers().to_vec()).await + }; - match Client::init_with_peers(peers).await { + match res { Ok(client) => { info!("Connected to the Network"); progress_bar.finish_with_message("Connected to the Network"); diff --git a/ant-cli/src/commands.rs b/ant-cli/src/commands.rs index ff065a06c0..c3a22e8939 100644 --- a/ant-cli/src/commands.rs +++ b/ant-cli/src/commands.rs @@ -215,7 +215,7 @@ pub async fn handle_subcommand(opt: Opt) -> Result<()> { VaultCmd::Cost => vault::cost(peers.await?).await, VaultCmd::Create => vault::create(peers.await?).await, VaultCmd::Load => vault::load(peers.await?).await, - VaultCmd::Sync { force } => vault::sync(peers.await?, force).await, + VaultCmd::Sync { force } => vault::sync(force, peers.await?).await, }, Some(SubCmd::Wallet { command }) => match command { WalletCmd::Create { diff --git a/ant-cli/src/commands/file.rs b/ant-cli/src/commands/file.rs index 146133e348..5f9c966c8e 100644 --- a/ant-cli/src/commands/file.rs +++ b/ant-cli/src/commands/file.rs @@ -6,16 +6,16 @@ // KIND, either express or implied. Please review the Licences for the specific language governing // permissions and limitations relating to use of the SAFE Network Software. +use crate::network::NetworkPeers; use crate::utils::collect_upload_summary; use crate::wallet::load_wallet; use autonomi::client::address::addr_to_str; -use autonomi::Multiaddr; use color_eyre::eyre::Context; use color_eyre::eyre::Result; use color_eyre::Section; use std::path::PathBuf; -pub async fn cost(file: &str, peers: Vec) -> Result<()> { +pub async fn cost(file: &str, peers: NetworkPeers) -> Result<()> { let client = crate::actions::connect_to_network(peers).await?; println!("Getting upload cost..."); @@ -31,7 +31,7 @@ pub async fn cost(file: &str, peers: Vec) -> Result<()> { Ok(()) } -pub async fn upload(file: &str, public: bool, peers: Vec) -> Result<()> { +pub async fn upload(file: &str, public: bool, peers: NetworkPeers) -> Result<()> { let wallet = load_wallet()?; let mut client = crate::actions::connect_to_network(peers).await?; let event_receiver = client.enable_client_events(); @@ -101,7 +101,7 @@ pub async fn upload(file: &str, public: bool, peers: Vec) -> Result<( Ok(()) } -pub async fn download(addr: &str, dest_path: &str, peers: Vec) -> Result<()> { +pub async fn download(addr: &str, dest_path: &str, peers: NetworkPeers) -> Result<()> { let mut client = crate::actions::connect_to_network(peers).await?; crate::actions::download(addr, dest_path, &mut client).await } diff --git a/ant-cli/src/commands/register.rs b/ant-cli/src/commands/register.rs index 5598fc0544..9e84d607b4 100644 --- a/ant-cli/src/commands/register.rs +++ b/ant-cli/src/commands/register.rs @@ -8,13 +8,13 @@ #![allow(deprecated)] +use crate::network::NetworkPeers; use crate::utils::collect_upload_summary; use crate::wallet::load_wallet; use autonomi::client::registers::RegisterAddress; use autonomi::client::registers::RegisterPermissions; use autonomi::client::registers::RegisterSecretKey; use autonomi::Client; -use autonomi::Multiaddr; use color_eyre::eyre::eyre; use color_eyre::eyre::Context; use color_eyre::eyre::Result; @@ -39,7 +39,7 @@ pub fn generate_key(overwrite: bool) -> Result<()> { Ok(()) } -pub async fn cost(name: &str, peers: Vec) -> Result<()> { +pub async fn cost(name: &str, peers: NetworkPeers) -> Result<()> { let register_key = crate::keys::get_register_signing_key() .wrap_err("The register key is required to perform this action")?; let client = crate::actions::connect_to_network(peers).await?; @@ -53,7 +53,7 @@ pub async fn cost(name: &str, peers: Vec) -> Result<()> { Ok(()) } -pub async fn create(name: &str, value: &str, public: bool, peers: Vec) -> Result<()> { +pub async fn create(name: &str, value: &str, public: bool, peers: NetworkPeers) -> Result<()> { let wallet = load_wallet()?; let register_key = crate::keys::get_register_signing_key() .wrap_err("The register key is required to perform this action")?; @@ -119,7 +119,7 @@ pub async fn create(name: &str, value: &str, public: bool, peers: Vec Ok(()) } -pub async fn edit(address: String, name: bool, value: &str, peers: Vec) -> Result<()> { +pub async fn edit(address: String, name: bool, value: &str, peers: NetworkPeers) -> Result<()> { let register_key = crate::keys::get_register_signing_key() .wrap_err("The register key is required to perform this action")?; let client = crate::actions::connect_to_network(peers).await?; @@ -157,7 +157,7 @@ pub async fn edit(address: String, name: bool, value: &str, peers: Vec) -> Result<()> { +pub async fn get(address: String, name: bool, peers: NetworkPeers) -> Result<()> { let register_key = crate::keys::get_register_signing_key() .wrap_err("The register key is required to perform this action")?; let client = crate::actions::connect_to_network(peers).await?; diff --git a/ant-cli/src/commands/vault.rs b/ant-cli/src/commands/vault.rs index 14e5b6350b..b1ad37257b 100644 --- a/ant-cli/src/commands/vault.rs +++ b/ant-cli/src/commands/vault.rs @@ -6,13 +6,13 @@ // KIND, either express or implied. Please review the Licences for the specific language governing // permissions and limitations relating to use of the SAFE Network Software. +use crate::network::NetworkPeers; use crate::wallet::load_wallet; -use autonomi::Multiaddr; use color_eyre::eyre::Context; use color_eyre::eyre::Result; use color_eyre::Section; -pub async fn cost(peers: Vec) -> Result<()> { +pub async fn cost(peers: NetworkPeers) -> Result<()> { let client = crate::actions::connect_to_network(peers).await?; let vault_sk = crate::keys::get_vault_secret_key()?; @@ -27,7 +27,7 @@ pub async fn cost(peers: Vec) -> Result<()> { Ok(()) } -pub async fn create(peers: Vec) -> Result<()> { +pub async fn create(peers: NetworkPeers) -> Result<()> { let client = crate::actions::connect_to_network(peers).await?; let wallet = load_wallet()?; let vault_sk = crate::keys::get_vault_secret_key()?; @@ -55,7 +55,7 @@ pub async fn create(peers: Vec) -> Result<()> { Ok(()) } -pub async fn sync(peers: Vec, force: bool) -> Result<()> { +pub async fn sync(force: bool, peers: NetworkPeers) -> Result<()> { let client = crate::actions::connect_to_network(peers).await?; let vault_sk = crate::keys::get_vault_secret_key()?; let wallet = load_wallet()?; @@ -89,7 +89,7 @@ pub async fn sync(peers: Vec, force: bool) -> Result<()> { Ok(()) } -pub async fn load(peers: Vec) -> Result<()> { +pub async fn load(peers: NetworkPeers) -> Result<()> { let client = crate::actions::connect_to_network(peers).await?; let vault_sk = crate::keys::get_vault_secret_key()?; diff --git a/ant-cli/src/main.rs b/ant-cli/src/main.rs index bc9a627500..e0fe5cf644 100644 --- a/ant-cli/src/main.rs +++ b/ant-cli/src/main.rs @@ -72,7 +72,7 @@ async fn main() -> Result<()> { } let _log_guards = init_logging_and_metrics(&opt)?; - if opt.local { + if opt.peers.local { tokio::spawn(init_metrics(std::process::id())); } diff --git a/ant-cli/src/opt.rs b/ant-cli/src/opt.rs index ef74118cd1..9d7e4edd9b 100644 --- a/ant-cli/src/opt.rs +++ b/ant-cli/src/opt.rs @@ -19,12 +19,6 @@ use std::time::Duration; #[command(disable_version_flag = true)] #[command(author, version, about, long_about = None)] pub(crate) struct Opt { - /// Specify whether the cli is operating with a local network. - /// - /// This is used to run the cli against a local test network. - #[clap(long, default_value_t = false)] - pub local: bool, - /// Available sub commands. #[clap(subcommand)] pub command: Option, diff --git a/ant-evm/src/lib.rs b/ant-evm/src/lib.rs index ece2c36083..678db86c72 100644 --- a/ant-evm/src/lib.rs +++ b/ant-evm/src/lib.rs @@ -19,7 +19,7 @@ pub use evmlib::cryptography; #[cfg(feature = "external-signer")] pub use evmlib::external_signer; pub use evmlib::utils; -pub use evmlib::utils::get_evm_network_from_env; +pub use evmlib::utils::{get_evm_network_from_env, local_evm_network_from_csv}; pub use evmlib::utils::{DATA_PAYMENTS_ADDRESS, PAYMENT_TOKEN_ADDRESS, RPC_URL}; pub use evmlib::wallet::Error as EvmWalletError; pub use evmlib::wallet::Wallet as EvmWallet; diff --git a/ant-node/src/bin/antnode/main.rs b/ant-node/src/bin/antnode/main.rs index 65ae7f6c45..fab6abcfb6 100644 --- a/ant-node/src/bin/antnode/main.rs +++ b/ant-node/src/bin/antnode/main.rs @@ -14,7 +14,7 @@ mod subcommands; use crate::subcommands::EvmNetworkCommand; use ant_bootstrap::{BootstrapCacheConfig, BootstrapCacheStore, PeersArgs}; -use ant_evm::{get_evm_network_from_env, EvmNetwork, RewardsAddress}; +use ant_evm::{get_evm_network_from_env, local_evm_network_from_csv, EvmNetwork, RewardsAddress}; use ant_logging::metrics::init_metrics; use ant_logging::{Level, LogFormat, LogOutputDest, ReloadHandle}; use ant_node::{Marker, NodeBuilder, NodeEvent, NodeEventsReceiver}; @@ -262,12 +262,16 @@ fn main() -> Result<()> { return Ok(()); } - let evm_network: EvmNetwork = opt - .evm_network - .as_ref() - .cloned() - .map(|v| Ok(v.into())) - .unwrap_or_else(get_evm_network_from_env)?; + let evm_network: EvmNetwork = if opt.peers.local { + println!("Running node in local mode"); + local_evm_network_from_csv()? + } else { + opt.evm_network + .as_ref() + .cloned() + .map(|v| Ok(v.into())) + .unwrap_or_else(get_evm_network_from_env)? + }; println!("EVM network: {evm_network:?}"); let node_socket_addr = SocketAddr::new(opt.ip, opt.port); diff --git a/evmlib/src/utils.rs b/evmlib/src/utils.rs index eb5d9ca724..422125fb5a 100644 --- a/evmlib/src/utils.rs +++ b/evmlib/src/utils.rs @@ -132,7 +132,7 @@ pub fn get_evm_network_from_env() -> Result { } /// Get the `Network::Custom` from the local EVM testnet CSV file -fn local_evm_network_from_csv() -> Result { +pub fn local_evm_network_from_csv() -> Result { // load the csv let csv_path = get_evm_testnet_csv_path()?;