diff --git a/ant-bootstrap/src/initial_peers.rs b/ant-bootstrap/src/initial_peers.rs index 64cd6972a7..afa983b0de 100644 --- a/ant-bootstrap/src/initial_peers.rs +++ b/ant-bootstrap/src/initial_peers.rs @@ -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) -> Result> { + pub async fn get_addrs( + &self, + config: Option, + count: Option, + ) -> Result> { 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 @@ -103,6 +109,7 @@ impl PeersArgs { pub async fn get_bootstrap_addr( &self, config: Option, + count: Option, ) -> Result> { // If this is the first node, return an empty list if self.first { @@ -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 { @@ -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?; @@ -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"); diff --git a/ant-bootstrap/tests/address_format_tests.rs b/ant-bootstrap/tests/address_format_tests.rs index a953608039..88369f4cd8 100644 --- a/ant-bootstrap/tests/address_format_tests.rs +++ b/ant-bootstrap/tests/address_format_tests.rs @@ -47,12 +47,12 @@ async fn test_multiaddr_format_parsing() -> Result<(), Box Result<(), Box 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, diff --git a/ant-bootstrap/tests/cli_integration_tests.rs b/ant-bootstrap/tests/cli_integration_tests.rs index 8ac0ab571b..98341ae452 100644 --- a/ant-bootstrap/tests/cli_integration_tests.rs +++ b/ant-bootstrap/tests/cli_integration_tests.rs @@ -38,7 +38,7 @@ async fn test_first_flag() -> Result<(), Box> { 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"); @@ -64,7 +64,7 @@ async fn test_peer_argument() -> Result<(), Box> { 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"); @@ -94,12 +94,12 @@ async fn test_network_contacts_fallback() -> Result<(), Box Result<(), Box> { 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"); @@ -166,7 +166,7 @@ async fn test_test_network_peers() -> Result<(), Box> { 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!( diff --git a/ant-cli/src/access/network.rs b/ant-cli/src/access/network.rs index acf7acfae6..8c428e06d3 100644 --- a/ant-cli/src/access/network.rs +++ b/ant-cli/src/access/network.rs @@ -13,7 +13,7 @@ use color_eyre::Result; use color_eyre::Section; pub async fn get_peers(peers: PeersArgs) -> Result> { - 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") diff --git a/ant-node-manager/src/cmd/local.rs b/ant-node-manager/src/cmd/local.rs index cdf0bd375c..2f0b3b465b 100644 --- a/ant-node-manager/src/cmd/local.rs +++ b/ant-node-manager/src/cmd/local.rs @@ -36,7 +36,7 @@ pub async fn join( log_format: Option, owner: Option, owner_prefix: Option, - peers_args: PeersArgs, + _peers_args: PeersArgs, rpc_port: Option, rewards_address: RewardsAddress, evm_network: Option, @@ -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, @@ -95,7 +80,7 @@ pub async fn join( node_port, owner, owner_prefix, - peers, + peers: None, rpc_port, skip_validation, log_format, diff --git a/ant-node/src/bin/antnode/main.rs b/ant-node/src/bin/antnode/main.rs index 6246206211..ec8d759f7b 100644 --- a/ant-node/src/bin/antnode/main.rs +++ b/ant-node/src/bin/antnode/main.rs @@ -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(