Skip to content

Commit

Permalink
feat: cli local setup
Browse files Browse the repository at this point in the history
  • Loading branch information
grumbach committed Jan 3, 2025
1 parent c207936 commit cf9afb8
Show file tree
Hide file tree
Showing 11 changed files with 69 additions and 38 deletions.
32 changes: 29 additions & 3 deletions ant-cli/src/access/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vec<Multiaddr>> {
peers.get_addrs(None, Some(100)).await
pub enum NetworkPeers {
Local(Vec<Multiaddr>),
Public(Vec<Multiaddr>),
}

impl NetworkPeers {
pub fn peers(&self) -> &Vec<Multiaddr> {
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<NetworkPeers> {
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)
}
15 changes: 11 additions & 4 deletions ant-cli/src/actions/connect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Multiaddr>) -> Result<Client> {
use crate::network::NetworkPeers;

pub async fn connect_to_network(peers: NetworkPeers) -> Result<Client> {
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");
Expand Down
2 changes: 1 addition & 1 deletion ant-cli/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
8 changes: 4 additions & 4 deletions ant-cli/src/commands/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Multiaddr>) -> Result<()> {
pub async fn cost(file: &str, peers: NetworkPeers) -> Result<()> {
let client = crate::actions::connect_to_network(peers).await?;

println!("Getting upload cost...");
Expand All @@ -31,7 +31,7 @@ pub async fn cost(file: &str, peers: Vec<Multiaddr>) -> Result<()> {
Ok(())
}

pub async fn upload(file: &str, public: bool, peers: Vec<Multiaddr>) -> 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();
Expand Down Expand Up @@ -101,7 +101,7 @@ pub async fn upload(file: &str, public: bool, peers: Vec<Multiaddr>) -> Result<(
Ok(())
}

pub async fn download(addr: &str, dest_path: &str, peers: Vec<Multiaddr>) -> 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
}
Expand Down
10 changes: 5 additions & 5 deletions ant-cli/src/commands/register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -39,7 +39,7 @@ pub fn generate_key(overwrite: bool) -> Result<()> {
Ok(())
}

pub async fn cost(name: &str, peers: Vec<Multiaddr>) -> 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?;
Expand All @@ -53,7 +53,7 @@ pub async fn cost(name: &str, peers: Vec<Multiaddr>) -> Result<()> {
Ok(())
}

pub async fn create(name: &str, value: &str, public: bool, peers: Vec<Multiaddr>) -> 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")?;
Expand Down Expand Up @@ -119,7 +119,7 @@ pub async fn create(name: &str, value: &str, public: bool, peers: Vec<Multiaddr>
Ok(())
}

pub async fn edit(address: String, name: bool, value: &str, peers: Vec<Multiaddr>) -> 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?;
Expand Down Expand Up @@ -157,7 +157,7 @@ pub async fn edit(address: String, name: bool, value: &str, peers: Vec<Multiaddr
Ok(())
}

pub async fn get(address: String, name: bool, peers: Vec<Multiaddr>) -> 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?;
Expand Down
10 changes: 5 additions & 5 deletions ant-cli/src/commands/vault.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Multiaddr>) -> 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()?;

Expand All @@ -27,7 +27,7 @@ pub async fn cost(peers: Vec<Multiaddr>) -> Result<()> {
Ok(())
}

pub async fn create(peers: Vec<Multiaddr>) -> 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()?;
Expand Down Expand Up @@ -55,7 +55,7 @@ pub async fn create(peers: Vec<Multiaddr>) -> Result<()> {
Ok(())
}

pub async fn sync(peers: Vec<Multiaddr>, 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()?;
Expand Down Expand Up @@ -89,7 +89,7 @@ pub async fn sync(peers: Vec<Multiaddr>, force: bool) -> Result<()> {
Ok(())
}

pub async fn load(peers: Vec<Multiaddr>) -> 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()?;

Expand Down
2 changes: 1 addition & 1 deletion ant-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}

Expand Down
6 changes: 0 additions & 6 deletions ant-cli/src/opt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<SubCmd>,
Expand Down
2 changes: 1 addition & 1 deletion ant-evm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
18 changes: 11 additions & 7 deletions ant-node/src/bin/antnode/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion evmlib/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ pub fn get_evm_network_from_env() -> Result<Network, Error> {
}

/// Get the `Network::Custom` from the local EVM testnet CSV file
fn local_evm_network_from_csv() -> Result<Network, Error> {
pub fn local_evm_network_from_csv() -> Result<Network, Error> {
// load the csv
let csv_path = get_evm_testnet_csv_path()?;

Expand Down

0 comments on commit cf9afb8

Please sign in to comment.