diff --git a/Cargo.lock b/Cargo.lock index 09d780b39c..297edfbcc6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -443,6 +443,7 @@ dependencies = [ "self_encryption", "serde", "sn_client", + "sn_peers_acquisition", "sn_protocol", "sn_registers", "sn_transfers", diff --git a/autonomi/Cargo.toml b/autonomi/Cargo.toml index ff395c3661..96834b3035 100644 --- a/autonomi/Cargo.toml +++ b/autonomi/Cargo.toml @@ -23,6 +23,7 @@ rmp-serde = "1.1.1" self_encryption = "~0.29.0" serde = { version = "1.0.133", features = ["derive", "rc"] } sn_client = { path = "../sn_client" } +sn_peers_acquisition = { path = "../sn_peers_acquisition", version = "0.5.0" } sn_protocol = { version = "0.17.5", path = "../sn_protocol" } sn_registers = { path = "../sn_registers", version = "0.3.18" } sn_transfers = { path = "../sn_transfers", version = "0.19.0" } diff --git a/autonomi/tests/common/mod.rs b/autonomi/tests/common/mod.rs index 010543b566..c01ae23187 100644 --- a/autonomi/tests/common/mod.rs +++ b/autonomi/tests/common/mod.rs @@ -1,8 +1,10 @@ #![allow(dead_code)] use bytes::Bytes; +use libp2p::Multiaddr; use rand::Rng; use sn_client::acc_packet::load_account_wallet_or_create_with_mnemonic; +use sn_peers_acquisition::parse_peer_addr; use sn_transfers::{get_faucet_data_dir, HotWallet}; /// When launching a testnet locally, we can use the faucet wallet. @@ -24,3 +26,14 @@ pub fn enable_logging() { .with_env_filter(tracing_subscriber::EnvFilter::from_default_env()) .try_init(); } + +/// Parse the `SAFE_PEERS` env var into a list of Multiaddrs. +/// +/// An empty `Vec` will be returned if the env var is not set. +pub fn peers_from_env() -> Result, libp2p::multiaddr::Error> { + let Ok(peers_str) = std::env::var("SAFE_PEERS") else { + return Ok(vec![]); + }; + + peers_str.split(',').map(parse_peer_addr).collect() +} diff --git a/autonomi/tests/file.rs b/autonomi/tests/file.rs index 0e221ae6c7..63c88172f6 100644 --- a/autonomi/tests/file.rs +++ b/autonomi/tests/file.rs @@ -10,7 +10,7 @@ mod common; async fn file() -> Result<(), Box> { common::enable_logging(); - let mut client = Client::connect(&[]).await?; + let mut client = Client::connect(&common::peers_from_env()?).await?; let mut wallet = common::load_hot_wallet_from_faucet(); // let data = common::gen_random_data(1024 * 1024 * 1000); diff --git a/autonomi/tests/put.rs b/autonomi/tests/put.rs index 5c2111f36b..49eb263130 100644 --- a/autonomi/tests/put.rs +++ b/autonomi/tests/put.rs @@ -10,7 +10,9 @@ mod common; async fn put() { common::enable_logging(); - let mut client = Client::connect(&[]).await.unwrap(); + let mut client = Client::connect(&common::peers_from_env().unwrap()) + .await + .unwrap(); let mut wallet = common::load_hot_wallet_from_faucet(); let data = common::gen_random_data(1024 * 1024 * 10); diff --git a/autonomi/tests/register.rs b/autonomi/tests/register.rs index 79cd00368d..c51873b966 100644 --- a/autonomi/tests/register.rs +++ b/autonomi/tests/register.rs @@ -12,7 +12,9 @@ mod common; async fn register() { common::enable_logging(); - let mut client = Client::connect(&[]).await.unwrap(); + let mut client = Client::connect(&common::peers_from_env().unwrap()) + .await + .unwrap(); let mut wallet = common::load_hot_wallet_from_faucet(); // Owner key of the register. diff --git a/sn_peers_acquisition/src/error.rs b/sn_peers_acquisition/src/error.rs index 1e9f4b7da2..d5df7c969b 100644 --- a/sn_peers_acquisition/src/error.rs +++ b/sn_peers_acquisition/src/error.rs @@ -5,7 +5,7 @@ pub type Result = std::result::Result; #[derive(Debug, Error)] pub enum Error { #[error("Could not parse the supplied multiaddr or socket address")] - InvalidPeerAddr, + InvalidPeerAddr(#[from] libp2p::multiaddr::Error), #[error("Could not obtain network contacts from {0} after {1} retries")] FailedToObtainPeersFromUrl(String, usize), #[error("No valid multaddr was present in the contacts file at {0}")] diff --git a/sn_peers_acquisition/src/lib.rs b/sn_peers_acquisition/src/lib.rs index 10a7b1a775..db467d6249 100644 --- a/sn_peers_acquisition/src/lib.rs +++ b/sn_peers_acquisition/src/lib.rs @@ -159,7 +159,7 @@ impl PeersArgs { } /// Parse strings like `1.2.3.4:1234` and `/ip4/1.2.3.4/tcp/1234` into a multiaddr. -pub fn parse_peer_addr(addr: &str) -> Result { +pub fn parse_peer_addr(addr: &str) -> std::result::Result { // Parse valid IPv4 socket address, e.g. `1.2.3.4:1234`. if let Ok(addr) = addr.parse::() { let start_addr = Multiaddr::from(*addr.ip()); @@ -180,12 +180,7 @@ pub fn parse_peer_addr(addr: &str) -> Result { } // Parse any valid multiaddr string - if let Ok(addr) = addr.parse::() { - debug!("Parsing a full multiaddr: {:?}", addr); - return Ok(addr); - } - - Err(Error::InvalidPeerAddr) + addr.parse::() } /// Get and parse a list of peers from a URL. The URL should contain one multiaddr per line.