From 7bbccf5cc238271dce7a67fa1f1e9afd1e9584b3 Mon Sep 17 00:00:00 2001 From: Roland Sherwin Date: Fri, 6 Dec 2024 19:36:56 +0530 Subject: [PATCH] chore: update based on comments --- ant-bootstrap/src/cache_store.rs | 2 +- ant-networking/src/driver.rs | 7 ++----- ant-networking/src/event/swarm.rs | 18 +++++++++--------- ant-node/src/bin/antnode/main.rs | 2 ++ ant-node/src/error.rs | 2 -- ant-node/src/node.rs | 20 ++++---------------- 6 files changed, 18 insertions(+), 33 deletions(-) diff --git a/ant-bootstrap/src/cache_store.rs b/ant-bootstrap/src/cache_store.rs index a6f63b45d8..c435fbec23 100644 --- a/ant-bootstrap/src/cache_store.rs +++ b/ant-bootstrap/src/cache_store.rs @@ -178,7 +178,7 @@ impl BootstrapCacheStore { Ok(store) } - /// Create a CacheStore from the given peers argument. + /// Create a empty CacheStore from the given peers argument. /// This also modifies the cfg if provided based on the PeersArgs. /// And also performs some actions based on the PeersArgs. pub fn new_from_peers_args( diff --git a/ant-networking/src/driver.rs b/ant-networking/src/driver.rs index 9276c39237..3c14874823 100644 --- a/ant-networking/src/driver.rs +++ b/ant-networking/src/driver.rs @@ -1062,11 +1062,8 @@ impl SwarmDriver { Self::duration_with_variance(bootstrap_cache.config().max_cache_save_duration, 1); // scale up the interval until we reach the max - let new_duration = Duration::from_secs( - std::cmp::min( - current_interval.period().as_secs() * bootstrap_cache.config().cache_save_scaling_factor, - max_cache_save_duration.as_secs(), - )); + let scaled = current_interval.period().as_secs().saturating_mul(bootstrap_cache.config().cache_save_scaling_factor); + let new_duration = Duration::from_secs(std::cmp::min(scaled, max_cache_save_duration.as_secs())); info!("Scaling up the bootstrap cache save interval to {new_duration:?}"); // `Interval` ticks immediately for Tokio, but not for `wasmtimer`, which is used for wasm32. diff --git a/ant-networking/src/event/swarm.rs b/ant-networking/src/event/swarm.rs index 6d0c283a0c..84127c43d3 100644 --- a/ant-networking/src/event/swarm.rs +++ b/ant-networking/src/event/swarm.rs @@ -515,18 +515,18 @@ impl SwarmDriver { } }; - // Just track failures during outgoing connection with `failed_peer_id` inside the bootstrap cache. - // OutgoingConnectionError without peer_id can happen when dialing multiple addresses of a peer. - // And similarly IncomingConnectionError can happen when a peer has multiple transports/listen addrs. - if let (Some((_, failed_addr, _)), Some(bootstrap_cache)) = - (connection_details, self.bootstrap_cache.as_mut()) - { - bootstrap_cache.update_addr_status(&failed_addr, false); - } - if should_clean_peer { warn!("Tracking issue of {failed_peer_id:?}. Clearing it out for now"); + // Just track failures during outgoing connection with `failed_peer_id` inside the bootstrap cache. + // OutgoingConnectionError without peer_id can happen when dialing multiple addresses of a peer. + // And similarly IncomingConnectionError can happen when a peer has multiple transports/listen addrs. + if let (Some((_, failed_addr, _)), Some(bootstrap_cache)) = + (connection_details, self.bootstrap_cache.as_mut()) + { + bootstrap_cache.update_addr_status(&failed_addr, false); + } + if let Some(dead_peer) = self .swarm .behaviour_mut() diff --git a/ant-node/src/bin/antnode/main.rs b/ant-node/src/bin/antnode/main.rs index a6d25b9cf5..6246206211 100644 --- a/ant-node/src/bin/antnode/main.rs +++ b/ant-node/src/bin/antnode/main.rs @@ -295,6 +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))?; debug!("Node's owner set to: {:?}", opt.owner); let restart_options = rt.block_on(async move { let mut node_builder = NodeBuilder::new( @@ -307,6 +308,7 @@ fn main() -> Result<()> { #[cfg(feature = "upnp")] opt.upnp, ); + node_builder.initial_peers(initial_peres); node_builder.bootstrap_cache(bootstrap_cache); node_builder.is_behind_home_network(opt.home_network); #[cfg(feature = "open-metrics")] diff --git a/ant-node/src/error.rs b/ant-node/src/error.rs index 4a80796eb2..86aba2df5c 100644 --- a/ant-node/src/error.rs +++ b/ant-node/src/error.rs @@ -81,8 +81,6 @@ pub enum Error { // ---------- Initialize Errors #[error("Failed to generate a reward key")] FailedToGenerateRewardKey, - #[error("Cannot set both initial_peers and bootstrap_cache")] - InitialPeersAndBootstrapCacheSet, // ---------- Miscellaneous Errors #[error("Failed to obtain node's current port")] diff --git a/ant-node/src/node.rs b/ant-node/src/node.rs index 9f5ac21bba..018ef4596a 100644 --- a/ant-node/src/node.rs +++ b/ant-node/src/node.rs @@ -11,7 +11,7 @@ use super::{ }; #[cfg(feature = "open-metrics")] use crate::metrics::NodeMetricsRecorder; -use crate::{error::Error, RunningNode}; +use crate::RunningNode; use ant_bootstrap::BootstrapCacheStore; use ant_evm::{AttoTokens, RewardsAddress}; #[cfg(feature = "open-metrics")] @@ -134,12 +134,12 @@ impl NodeBuilder { self.metrics_server_port = port; } - /// Set the initialized bootstrap cache. This is mutually exclusive with `initial_peers` + /// Set the initialized bootstrap cache. pub fn bootstrap_cache(&mut self, cache: BootstrapCacheStore) { self.bootstrap_cache = Some(cache); } - /// Set the initial peers to dial at startup. This is mutually exclusive with `bootstrap_cache` + /// Set the initial peers to dial at startup. pub fn initial_peers(&mut self, peers: Vec) { self.initial_peers = peers; } @@ -177,18 +177,6 @@ impl NodeBuilder { None }; - if !self.initial_peers.is_empty() && self.bootstrap_cache.is_some() { - return Err(Error::InitialPeersAndBootstrapCacheSet); - } - - let initial_peers = if !self.initial_peers.is_empty() { - self.initial_peers.clone() - } else if let Some(cache) = &self.bootstrap_cache { - cache.get_sorted_addrs().cloned().collect() - } else { - vec![] - }; - network_builder.listen_addr(self.addr); #[cfg(feature = "open-metrics")] network_builder.metrics_server_port(self.metrics_server_port); @@ -207,7 +195,7 @@ impl NodeBuilder { let node = NodeInner { network: network.clone(), events_channel: node_events_channel.clone(), - initial_peers, + initial_peers: self.initial_peers, reward_address: self.evm_address, #[cfg(feature = "open-metrics")] metrics_recorder,