Skip to content

Commit

Permalink
fix(bootstrap): tiny fixes and limit get_addrs count
Browse files Browse the repository at this point in the history
  • Loading branch information
RolandSherwin committed Dec 9, 2024
1 parent bd9934d commit c99cf89
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 47 deletions.
29 changes: 14 additions & 15 deletions ant-bootstrap/src/initial_peers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,22 +79,28 @@ pub struct PeersArgs {
}

impl PeersArgs {
/// Get bootstrap peers
/// 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>) -> Result<Vec<Multiaddr>> {
pub async fn get_addrs(
&self,
config: Option<BootstrapCacheConfig>,
count: Option<usize>,
) -> Result<Vec<Multiaddr>> {
Ok(self
.get_bootstrap_addr(config)
.get_bootstrap_addr(config, count)
.await?
.into_iter()
.map(|addr| addr.addr)
.collect())
}

/// Get bootstrap peers
/// 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
Expand All @@ -103,6 +109,7 @@ impl PeersArgs {
pub async fn get_bootstrap_addr(
&self,
config: Option<BootstrapCacheConfig>,
count: Option<usize>,
) -> Result<Vec<BootstrapAddr>> {
// If this is the first node, return an empty list
if self.first {
Expand Down Expand Up @@ -146,12 +153,6 @@ impl PeersArgs {
bootstrap_addresses.extend(addrs);
}

// Return here if we fetched peers from the args
if !bootstrap_addresses.is_empty() {
bootstrap_addresses.sort_by_key(|addr| addr.failure_rate() as u64);
return Ok(bootstrap_addresses);
}

// load from cache if present
if !self.ignore_cache {
let cfg = if let Some(config) = config {
Expand Down Expand Up @@ -179,11 +180,6 @@ impl PeersArgs {
}
}

if !bootstrap_addresses.is_empty() {
bootstrap_addresses.sort_by_key(|addr| addr.failure_rate() as u64);
return Ok(bootstrap_addresses);
}

if !self.disable_mainnet_contacts {
let contacts_fetcher = ContactsFetcher::with_mainnet_endpoints()?;
let addrs = contacts_fetcher.fetch_bootstrap_addresses().await?;
Expand All @@ -192,6 +188,9 @@ impl PeersArgs {

if !bootstrap_addresses.is_empty() {
bootstrap_addresses.sort_by_key(|addr| addr.failure_rate() as u64);
if let Some(count) = count {
bootstrap_addresses.truncate(count);
}
Ok(bootstrap_addresses)
} else {
error!("No initial bootstrap peers found through any means");
Expand Down
12 changes: 6 additions & 6 deletions ant-bootstrap/tests/address_format_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ async fn test_multiaddr_format_parsing() -> Result<(), Box<dyn std::error::Error
addrs: vec![addr.clone()],
network_contacts_url: vec![],
local: false,
disable_mainnet_contacts: false,
ignore_cache: false,
disable_mainnet_contacts: true,
ignore_cache: true,
bootstrap_cache_dir: None,
};

let bootstrap_addresses = args.get_bootstrap_addr(None).await?;
let bootstrap_addresses = args.get_bootstrap_addr(None, None).await?;
assert_eq!(bootstrap_addresses.len(), 1, "Should have one peer");
assert_eq!(
bootstrap_addresses[0].addr, addr,
Expand Down Expand Up @@ -85,12 +85,12 @@ async fn test_network_contacts_format() -> Result<(), Box<dyn std::error::Error>
addrs: vec![],
network_contacts_url: vec![format!("{}/peers", mock_server.uri()).parse()?],
local: false,
disable_mainnet_contacts: false,
ignore_cache: false,
disable_mainnet_contacts: true,
ignore_cache: true,
bootstrap_cache_dir: None,
};

let addrs = args.get_bootstrap_addr(None).await?;
let addrs = args.get_bootstrap_addr(None, None).await?;
assert_eq!(
addrs.len(),
2,
Expand Down
14 changes: 7 additions & 7 deletions ant-bootstrap/tests/cli_integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ async fn test_first_flag() -> Result<(), Box<dyn std::error::Error>> {
bootstrap_cache_dir: None,
};

let addrs = args.get_addrs(Some(config)).await?;
let addrs = args.get_addrs(Some(config), None).await?;

assert!(addrs.is_empty(), "First node should have no addrs");

Expand All @@ -64,7 +64,7 @@ async fn test_peer_argument() -> Result<(), Box<dyn std::error::Error>> {
bootstrap_cache_dir: None,
};

let addrs = args.get_addrs(None).await?;
let addrs = args.get_addrs(None, None).await?;

assert_eq!(addrs.len(), 1, "Should have one addr");
assert_eq!(addrs[0], peer_addr, "Should have the correct address");
Expand Down Expand Up @@ -94,12 +94,12 @@ async fn test_network_contacts_fallback() -> Result<(), Box<dyn std::error::Erro
addrs: vec![],
network_contacts_url: vec![format!("{}/peers", mock_server.uri()).parse()?],
local: false,
disable_mainnet_contacts: false,
ignore_cache: false,
disable_mainnet_contacts: true,
ignore_cache: true,
bootstrap_cache_dir: None,
};

let addrs = args.get_addrs(Some(config)).await?;
let addrs = args.get_addrs(Some(config), None).await?;
assert_eq!(
addrs.len(),
2,
Expand Down Expand Up @@ -130,7 +130,7 @@ async fn test_local_mode() -> Result<(), Box<dyn std::error::Error>> {
bootstrap_cache_dir: None,
};

let addrs = args.get_addrs(Some(config)).await?;
let addrs = args.get_addrs(Some(config), None).await?;

assert!(addrs.is_empty(), "Local mode should have no peers");

Expand Down Expand Up @@ -166,7 +166,7 @@ async fn test_test_network_peers() -> Result<(), Box<dyn std::error::Error>> {
bootstrap_cache_dir: None,
};

let addrs = args.get_addrs(Some(config)).await?;
let addrs = args.get_addrs(Some(config), None).await?;

assert_eq!(addrs.len(), 1, "Should have exactly one test network peer");
assert_eq!(
Expand Down
2 changes: 1 addition & 1 deletion ant-cli/src/access/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use color_eyre::Result;
use color_eyre::Section;

pub async fn get_peers(peers: PeersArgs) -> Result<Vec<Multiaddr>> {
peers.get_addrs(None).await
peers.get_addrs(None, Some(100)).await
.wrap_err("Please provide valid Network peers to connect to")
.with_suggestion(|| format!("make sure you've provided network peers using the --peers option or the {ANT_PEERS_ENV} env var"))
.with_suggestion(|| "a peer address looks like this: /ip4/42.42.42.42/udp/4242/quic-v1/p2p/B64nodePeerIDvdjb3FAJF4ks3moreBase64CharsHere")
Expand Down
19 changes: 2 additions & 17 deletions ant-node-manager/src/cmd/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub async fn join(
log_format: Option<LogFormat>,
owner: Option<String>,
owner_prefix: Option<String>,
peers_args: PeersArgs,
_peers_args: PeersArgs,
rpc_port: Option<PortRange>,
rewards_address: RewardsAddress,
evm_network: Option<EvmNetwork>,
Expand Down Expand Up @@ -70,21 +70,6 @@ pub async fn join(
)
.await?;

// If no peers are obtained we will attempt to join the existing local network, if one
// is running.
let peers = match peers_args.get_addrs(None).await {
Ok(peers) => Some(peers),
Err(err) => match err {
ant_bootstrap::error::Error::NoBootstrapPeersFound => {
warn!("PeersNotObtained, peers is set to None");
None
}
_ => {
error!("Failed to obtain peers: {err:?}");
return Err(err.into());
}
},
};
let options = LocalNetworkOptions {
antnode_bin_path,
enable_metrics_server,
Expand All @@ -95,7 +80,7 @@ pub async fn join(
node_port,
owner,
owner_prefix,
peers,
peers: None,
rpc_port,
skip_validation,
log_format,
Expand Down
2 changes: 1 addition & 1 deletion ant-node/src/bin/antnode/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ fn main() -> Result<()> {
// another process with these args.
#[cfg(feature = "metrics")]
rt.spawn(init_metrics(std::process::id()));
let initial_peres = rt.block_on(opt.peers.get_addrs(None))?;
let initial_peres = rt.block_on(opt.peers.get_addrs(None, Some(100)))?;
debug!("Node's owner set to: {:?}", opt.owner);
let restart_options = rt.block_on(async move {
let mut node_builder = NodeBuilder::new(
Expand Down

0 comments on commit c99cf89

Please sign in to comment.