Skip to content

Commit

Permalink
chore(bootstrap): return early if we have the required amount of addrs
Browse files Browse the repository at this point in the history
  • Loading branch information
RolandSherwin committed Dec 10, 2024
1 parent 171759b commit 92eab0f
Showing 1 changed file with 43 additions and 24 deletions.
67 changes: 43 additions & 24 deletions ant-bootstrap/src/initial_peers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,6 @@ pub struct PeersArgs {
impl PeersArgs {
/// Get bootstrap peers sorted by the failure rate. The peer with the lowest failure rate will be
/// the first in the list.
/// Order of precedence:
/// 1. Addresses from arguments
/// 2. Addresses from environment variable SAFE_PEERS
/// 3. Addresses from cache. `Self::bootstrap_cache_dir` will take precedence over the path provided inside `config`
/// 4. Addresses from network contacts URL
pub async fn get_addrs(
&self,
config: Option<BootstrapCacheConfig>,
Expand All @@ -101,11 +96,6 @@ impl PeersArgs {

/// Get bootstrap peers sorted by the failure rate. The peer with the lowest failure rate will be
/// the first in the list.
/// Order of precedence:
/// 1. Addresses from arguments
/// 2. Addresses from environment variable SAFE_PEERS
/// 3. Addresses from cache. `Self::bootstrap_cache_dir` will take precedence over the path provided inside `config`
/// 4. Addresses from network contacts URL
pub async fn get_bootstrap_addr(
&self,
config: Option<BootstrapCacheConfig>,
Expand Down Expand Up @@ -137,20 +127,13 @@ impl PeersArgs {
// Read from ANT_PEERS environment variable if present
bootstrap_addresses.extend(Self::read_bootstrap_addr_from_env());

// If we have a network contacts URL, fetch addrs from there.
if !self.network_contacts_url.is_empty() {
info!(
"Fetching bootstrap address from network contacts URLs: {:?}",
self.network_contacts_url
);
let addrs = self
.network_contacts_url
.iter()
.map(|url| url.parse::<Url>().map_err(|_| Error::FailedToParseUrl))
.collect::<Result<Vec<Url>>>()?;
let contacts_fetcher = ContactsFetcher::with_endpoints(addrs)?;
let addrs = contacts_fetcher.fetch_bootstrap_addresses().await?;
bootstrap_addresses.extend(addrs);
if let Some(count) = count {
if bootstrap_addresses.len() >= count {
bootstrap_addresses.sort_by_key(|addr| addr.failure_rate() as u64);
bootstrap_addresses.truncate(count);
info!("Returning early as enough bootstrap addresses are found");
return Ok(bootstrap_addresses);
}
}

// load from cache if present
Expand All @@ -176,6 +159,42 @@ impl PeersArgs {
.min_by_key(|addr| addr.failure_rate() as u64)
})
.collect();

if let Some(count) = count {
if bootstrap_addresses.len() >= count {
bootstrap_addresses.sort_by_key(|addr| addr.failure_rate() as u64);
bootstrap_addresses.truncate(count);
info!("Returning early as enough bootstrap addresses are found");
return Ok(bootstrap_addresses);
}
}
}
}
} else {
info!("Ignoring cache, not loading bootstrap addresses from cache");
}

// If we have a network contacts URL, fetch addrs from there.
if !self.network_contacts_url.is_empty() {
info!(
"Fetching bootstrap address from network contacts URLs: {:?}",
self.network_contacts_url
);
let addrs = self
.network_contacts_url
.iter()
.map(|url| url.parse::<Url>().map_err(|_| Error::FailedToParseUrl))
.collect::<Result<Vec<Url>>>()?;
let contacts_fetcher = ContactsFetcher::with_endpoints(addrs)?;
let addrs = contacts_fetcher.fetch_bootstrap_addresses().await?;
bootstrap_addresses.extend(addrs);

if let Some(count) = count {
if bootstrap_addresses.len() >= count {
bootstrap_addresses.sort_by_key(|addr| addr.failure_rate() as u64);
bootstrap_addresses.truncate(count);
info!("Returning early as enough bootstrap addresses are found");
return Ok(bootstrap_addresses);
}
}
}
Expand Down

0 comments on commit 92eab0f

Please sign in to comment.