From 32bd902d3b75bfe0fb42e7fd2faa9a577f96b32a Mon Sep 17 00:00:00 2001 From: Benno Zeeman Date: Tue, 10 Dec 2024 10:54:14 +0100 Subject: [PATCH] feat(autonomi): init with config, ANT_PEERS usage --- ant-bootstrap/src/initial_peers.rs | 33 +++++++++++++++--------------- autonomi/src/client/mod.rs | 33 +++++++++++++++++++++++++----- autonomi/tests/put.rs | 8 ++++++-- 3 files changed, 51 insertions(+), 23 deletions(-) diff --git a/ant-bootstrap/src/initial_peers.rs b/ant-bootstrap/src/initial_peers.rs index 07d0cd3b24..61c3f98a6e 100644 --- a/ant-bootstrap/src/initial_peers.rs +++ b/ant-bootstrap/src/initial_peers.rs @@ -70,7 +70,7 @@ impl PeersArgs { /// Get bootstrap peers /// Order of precedence: /// 1. Addresses from arguments - /// 2. Addresses from environment variable SAFE_PEERS + /// 2. Addresses from environment variable `ANT_PEERS` /// 3. Addresses from cache /// 4. Addresses from network contacts URL pub async fn get_addrs(&self, config: Option) -> Result> { @@ -85,7 +85,7 @@ impl PeersArgs { /// Get bootstrap peers /// Order of precedence: /// 1. Addresses from arguments - /// 2. Addresses from environment variable SAFE_PEERS + /// 2. Addresses from environment variable `ANT_PEERS` /// 3. Addresses from cache /// 4. Addresses from network contacts URL pub async fn get_bootstrap_addr( @@ -98,14 +98,27 @@ impl PeersArgs { return Ok(vec![]); } + let mut bootstrap_addresses = vec![]; + + // Read from `ANT_PEERS` environment variable if present + if let Ok(addrs) = std::env::var(ANT_PEERS_ENV) { + for addr_str in addrs.split(',') { + if let Some(addr) = craft_valid_multiaddr_from_str(addr_str, false) { + info!("Adding addr from environment variable: {addr}"); + bootstrap_addresses.push(BootstrapAddr::new(addr)); + } else { + warn!("Invalid multiaddress format from environment variable: {addr_str}"); + } + } + return Ok(bootstrap_addresses); + } + // If local mode is enabled, return empty store (will use mDNS) if self.local || cfg!(feature = "local") { info!("Local mode enabled, using only local discovery."); return Ok(vec![]); } - let mut bootstrap_addresses = vec![]; - // Add addrs from arguments if present for addr in &self.addrs { if let Some(addr) = craft_valid_multiaddr(addr, false) { @@ -116,18 +129,6 @@ impl PeersArgs { } } - // Read from ANT_PEERS environment variable if present - if let Ok(addrs) = std::env::var(ANT_PEERS_ENV) { - for addr_str in addrs.split(',') { - if let Some(addr) = craft_valid_multiaddr_from_str(addr_str, false) { - info!("Adding addr from environment variable: {addr}"); - bootstrap_addresses.push(BootstrapAddr::new(addr)); - } else { - warn!("Invalid multiaddress format from environment variable: {addr_str}"); - } - } - } - // If we have a network contacts URL, fetch addrs from there. if let Some(url) = self.network_contacts_url.clone() { info!("Fetching bootstrap address from network contacts URL: {url}",); diff --git a/autonomi/src/client/mod.rs b/autonomi/src/client/mod.rs index 80013e9d82..a9a761c1af 100644 --- a/autonomi/src/client/mod.rs +++ b/autonomi/src/client/mod.rs @@ -65,6 +65,17 @@ pub struct Client { pub(crate) client_event_sender: Arc>>, } +/// Configuration for [`Client::init_with_config`]. +#[derive(Debug, Clone, Default)] +pub struct ClientConfig { + /// Whether we're expected to connect to a local network. + pub local: bool, + /// List of peers to connect to. + /// + /// If not provided, the client will use the default bootstrap peers. + pub peers: Option>, +} + /// Error returned by [`Client::connect`]. #[derive(Debug, thiserror::Error)] pub enum ConnectError { @@ -78,13 +89,25 @@ pub enum ConnectError { impl Client { pub async fn init() -> Result { - // Get list of peers for bootstrapping to the network. - let peers = match PeersArgs::default().get_addrs(None).await { - Ok(peers) => peers, - Err(e) => return Err(e), + Self::init_with_config(ClientConfig::default()).await + } + + pub async fn init_with_config(config: ClientConfig) -> Result { + let (network, _event_receiver) = build_client_and_run_swarm(config.local); + + let peers_args = PeersArgs { + disable_mainnet_contacts: config.local, + ..Default::default() }; - let (network, _event_receiver) = build_client_and_run_swarm(true); + let peers = if let Some(peers) = config.peers { + peers + } else { + match peers_args.get_addrs(None).await { + Ok(peers) => peers, + Err(e) => return Err(e), + } + }; let peers_len = peers.len(); // Add peers to the routing table. diff --git a/autonomi/tests/put.rs b/autonomi/tests/put.rs index 22f3c6b58d..ca4a808c7e 100644 --- a/autonomi/tests/put.rs +++ b/autonomi/tests/put.rs @@ -7,7 +7,7 @@ // permissions and limitations relating to use of the SAFE Network Software. use ant_logging::LogBuilder; -use autonomi::Client; +use autonomi::{client::ClientConfig, Client}; use eyre::Result; use test_utils::{evm::get_funded_wallet, gen_random_data}; @@ -15,7 +15,11 @@ use test_utils::{evm::get_funded_wallet, gen_random_data}; async fn put() -> Result<()> { let _log_appender_guard = LogBuilder::init_single_threaded_tokio_test("put", false); - let client = Client::init().await?; + let client = Client::init_with_config(ClientConfig { + local: true, + ..Default::default() + }) + .await?; let wallet = get_funded_wallet(); let data = gen_random_data(1024 * 1024 * 10);