Skip to content

Commit

Permalink
feat(autonomi): init with config, ANT_PEERS usage
Browse files Browse the repository at this point in the history
  • Loading branch information
b-zee committed Dec 10, 2024
1 parent c01a91b commit 32bd902
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 23 deletions.
33 changes: 17 additions & 16 deletions ant-bootstrap/src/initial_peers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<BootstrapCacheConfig>) -> Result<Vec<Multiaddr>> {
Expand All @@ -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(
Expand All @@ -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) {
Expand All @@ -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}",);
Expand Down
33 changes: 28 additions & 5 deletions autonomi/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,17 @@ pub struct Client {
pub(crate) client_event_sender: Arc<Option<mpsc::Sender<ClientEvent>>>,
}

/// 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<Vec<Multiaddr>>,
}

/// Error returned by [`Client::connect`].
#[derive(Debug, thiserror::Error)]
pub enum ConnectError {
Expand All @@ -78,13 +89,25 @@ pub enum ConnectError {

impl Client {
pub async fn init() -> Result<Self, ant_bootstrap::Error> {
// 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<Self, ant_bootstrap::Error> {
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.
Expand Down
8 changes: 6 additions & 2 deletions autonomi/tests/put.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,19 @@
// 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};

#[tokio::test]
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);

Expand Down

0 comments on commit 32bd902

Please sign in to comment.