From 65b38de2a806c1c243bf0bae3bd46da750d4c6f4 Mon Sep 17 00:00:00 2001 From: Herr Seppia Date: Tue, 2 Apr 2024 15:27:47 +0200 Subject: [PATCH] Change 'need_bootstrappers' to have dinamically threshold Resolves #135 --- CHANGELOG.md | 9 +++++++++ src/config.rs | 18 ++++++++++++++++++ src/lib.rs | 9 ++++++++- src/maintainer.rs | 21 ++++++++------------- 4 files changed, 43 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a37e84c..d825d18 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Added + +- Add `BucketConfig::min_peers` in configuration [#135] + +### Changed + +- Change 'need_bootstrappers' to have dinamically threshold [#135] + ## [0.6.0] - 2023-11-01 ### Added @@ -126,6 +134,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 [#115]: https://github.com/dusk-network/kadcast/issues/115 [#117]: https://github.com/dusk-network/kadcast/issues/117 [#123]: https://github.com/dusk-network/kadcast/issues/123 +[#135]: https://github.com/dusk-network/kadcast/issues/135 diff --git a/src/config.rs b/src/config.rs index 7ee0454..bf79ab1 100644 --- a/src/config.rs +++ b/src/config.rs @@ -35,6 +35,13 @@ pub const DEFAULT_SEND_RETRY_COUNT: u8 = 3; pub const DEFAULT_SEND_RETRY_SLEEP_MILLIS: u64 = 5; pub const DEFAULT_BLOCKLIST_REFRESH_SECS: u64 = 10; +/// Default minimum peers required for network integration without bootstrapping +pub const DEFAULT_MIN_PEERS_FOR_INTEGRATION: usize = 3; + +const fn default_min_peers() -> usize { + DEFAULT_MIN_PEERS_FOR_INTEGRATION +} + #[derive(Clone, Serialize, Deserialize)] pub struct Config { /// KadcastID @@ -122,6 +129,16 @@ pub struct BucketConfig { /// Default value [BUCKET_DEFAULT_TTL_SECS] #[serde(with = "humantime_serde")] pub bucket_ttl: Duration, + + /// Minimum number of nodes at which bootstrapping is not required + /// + /// This value represents the minimum number of nodes required for a peer + /// to consider bootstrapping unnecessary and seamlessly integrate into + /// the existing network. + /// + /// Default value [DEFAULT_MIN_PEERS_FOR_INTEGRATION] + #[serde(default = "default_min_peers")] + pub min_peers: usize, } impl Default for BucketConfig { @@ -132,6 +149,7 @@ impl Default for BucketConfig { ), node_ttl: Duration::from_millis(BUCKET_DEFAULT_NODE_TTL_MILLIS), bucket_ttl: Duration::from_secs(BUCKET_DEFAULT_TTL_SECS), + min_peers: default_min_peers(), } } } diff --git a/src/lib.rs b/src/lib.rs index 0da68da..9f0755a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -105,6 +105,7 @@ impl Peer { }; let nodes = config.bootstrapping_nodes.clone(); let idle_time = config.bucket.bucket_ttl; + let min_peers = config.bucket.min_peers; MessageHandler::start( table.clone(), inbound_channel_rx, @@ -118,7 +119,13 @@ impl Peer { config, blocklist, ); - TableMaintainer::start(nodes, table, outbound_channel_tx, idle_time); + TableMaintainer::start( + nodes, + table, + outbound_channel_tx, + idle_time, + min_peers, + ); task::spawn(Peer::notifier(listener_channel_rx, listener)); Ok(peer) } diff --git a/src/maintainer.rs b/src/maintainer.rs index 6b2fa6a..4021c48 100644 --- a/src/maintainer.rs +++ b/src/maintainer.rs @@ -30,6 +30,7 @@ impl TableMaintainer { ktable: RwLock>, outbound_sender: Sender, idle_time: Duration, + min_peers: usize, ) { tokio::spawn(async move { let my_ip = *ktable.read().await.root().value().address(); @@ -42,20 +43,14 @@ impl TableMaintainer { my_ip, header, }; - maintainer.monitor_buckets(idle_time).await; + maintainer.monitor_buckets(idle_time, min_peers).await; }); } /// Check if the peer need to contact the bootstrappers in order to join the /// network - async fn need_bootstrappers(&self) -> bool { - let binary_key = self.header.binary_id().as_binary(); - self.ktable - .read() - .await - .closest_peers::<10>(binary_key) - .count() - < 3 + async fn need_bootstrappers(&self, min_peers: usize) -> bool { + self.ktable.read().await.alive_nodes().count() < min_peers } /// Return a vector containing the Socket Addresses bound to the provided @@ -74,8 +69,8 @@ impl TableMaintainer { } /// Try to contact the bootstrappers node until no needed anymore - async fn contact_bootstrappers(&self) { - while self.need_bootstrappers().await { + async fn contact_bootstrappers(&self, min_peers: usize) { + while self.need_bootstrappers(min_peers).await { info!("TableMaintainer::contact_bootstrappers"); let bootstrapping_nodes_addr = self.bootstrapping_nodes_addr(); let binary_key = self.header.binary_id().as_binary(); @@ -98,10 +93,10 @@ impl TableMaintainer { /// 1. Contact bootstrappers (if needed) /// 2. Ping idle buckets /// 3. Remove idles nodes from buckets - async fn monitor_buckets(&self, idle_time: Duration) { + async fn monitor_buckets(&self, idle_time: Duration, min_peers: usize) { info!("TableMaintainer::monitor_buckets started"); loop { - self.contact_bootstrappers().await; + self.contact_bootstrappers(min_peers).await; info!("TableMaintainer::monitor_buckets back to sleep"); tokio::time::sleep(idle_time).await;