Skip to content

Commit

Permalink
Merge pull request #2115 from b-zee/test-api-peers-from-env
Browse files Browse the repository at this point in the history
test(autonomi): connect to peers from env
  • Loading branch information
joshuef authored Sep 17, 2024
2 parents 0c650cc + 035a151 commit 0bb025c
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 11 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions autonomi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
13 changes: 13 additions & 0 deletions autonomi/tests/common/mod.rs
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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<Vec<Multiaddr>, libp2p::multiaddr::Error> {
let Ok(peers_str) = std::env::var("SAFE_PEERS") else {
return Ok(vec![]);
};

peers_str.split(',').map(parse_peer_addr).collect()
}
2 changes: 1 addition & 1 deletion autonomi/tests/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ mod common;
async fn file() -> Result<(), Box<dyn std::error::Error>> {
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);
Expand Down
4 changes: 3 additions & 1 deletion autonomi/tests/put.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
4 changes: 3 additions & 1 deletion autonomi/tests/register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion sn_peers_acquisition/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pub type Result<T, E = Error> = std::result::Result<T, E>;
#[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}")]
Expand Down
9 changes: 2 additions & 7 deletions sn_peers_acquisition/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Multiaddr> {
pub fn parse_peer_addr(addr: &str) -> std::result::Result<Multiaddr, libp2p::multiaddr::Error> {
// Parse valid IPv4 socket address, e.g. `1.2.3.4:1234`.
if let Ok(addr) = addr.parse::<std::net::SocketAddrV4>() {
let start_addr = Multiaddr::from(*addr.ip());
Expand All @@ -180,12 +180,7 @@ pub fn parse_peer_addr(addr: &str) -> Result<Multiaddr> {
}

// Parse any valid multiaddr string
if let Ok(addr) = addr.parse::<Multiaddr>() {
debug!("Parsing a full multiaddr: {:?}", addr);
return Ok(addr);
}

Err(Error::InvalidPeerAddr)
addr.parse::<Multiaddr>()
}

/// Get and parse a list of peers from a URL. The URL should contain one multiaddr per line.
Expand Down

0 comments on commit 0bb025c

Please sign in to comment.