From f1dfb2bc47d68242570776a3e5848a1057546257 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20RIBEAU?= Date: Thu, 13 Jun 2024 12:08:35 +0200 Subject: [PATCH] just addressing the problem --- misc/server/CHANGELOG.md | 2 -- misc/server/src/behaviour.rs | 2 +- protocols/kad/CHANGELOG.md | 2 +- protocols/kad/src/behaviour.rs | 19 +++++++++++++++---- protocols/kad/src/behaviour/test.rs | 2 +- 5 files changed, 18 insertions(+), 9 deletions(-) diff --git a/misc/server/CHANGELOG.md b/misc/server/CHANGELOG.md index e7a60e5f3de..254ab1d92be 100644 --- a/misc/server/CHANGELOG.md +++ b/misc/server/CHANGELOG.md @@ -4,8 +4,6 @@ - Use periodic and automatic bootstrap of Kademlia. See [PR 4838](https://github.com/libp2p/rust-libp2p/pull/4838). -- Integrate Kademlia bootstrap function breaking change. - See [PR 5349](https://github.com/libp2p/rust-libp2p/pull/5349). ## 0.12.6 diff --git a/misc/server/src/behaviour.rs b/misc/server/src/behaviour.rs index 2f8fc05bdd1..36b18c9798d 100644 --- a/misc/server/src/behaviour.rs +++ b/misc/server/src/behaviour.rs @@ -49,7 +49,7 @@ impl Behaviour { for peer in &BOOTNODES { kademlia.add_address(&PeerId::from_str(peer).unwrap(), bootaddr.clone()); } - kademlia.bootstrap(); + kademlia.bootstrap().unwrap(); Some(kademlia) } else { None diff --git a/protocols/kad/CHANGELOG.md b/protocols/kad/CHANGELOG.md index 2e0e5d21a3c..5cd537cae9e 100644 --- a/protocols/kad/CHANGELOG.md +++ b/protocols/kad/CHANGELOG.md @@ -19,7 +19,7 @@ See [PR 5317](https://github.com/libp2p/rust-libp2p/pull/5317). - Use `web-time` instead of `instant`. See [PR 5347](https://github.com/libp2p/rust-libp2p/pull/5347). -- Always trigger a query when bootstrapping. +- Correctly handle the `NoKnownPeers` error on automatic bootstrap. See [PR 5349](https://github.com/libp2p/rust-libp2p/pull/5349). ## 0.45.3 diff --git a/protocols/kad/src/behaviour.rs b/protocols/kad/src/behaviour.rs index 0ddff994a70..abf745f3857 100644 --- a/protocols/kad/src/behaviour.rs +++ b/protocols/kad/src/behaviour.rs @@ -914,10 +914,13 @@ where /// refreshed by initiating an additional bootstrapping query for each such /// bucket with random keys. /// - /// Returns the `QueryId` for the entire bootstrapping process. The progress of bootstrapping is + /// Returns `Ok` if bootstrapping has been initiated with a self-lookup, providing the + /// `QueryId` for the entire bootstrapping process. The progress of bootstrapping is /// reported via [`Event::OutboundQueryProgressed{QueryResult::Bootstrap}`] events, /// with one such event per bootstrapping query. /// + /// Returns `Err` if bootstrapping is impossible due an empty routing table. + /// /// > **Note**: Bootstrapping requires at least one node of the DHT to be known. /// > See [`Behaviour::add_address`]. /// @@ -928,6 +931,7 @@ where /// > This parameter is used to call [`Behaviour::bootstrap`] periodically and automatically /// > to ensure a healthy routing table. pub fn bootstrap(&mut self) -> Result { + self.bootstrap_status.on_started(); let local_key = *self.kbuckets.local_key(); let info = QueryInfo::Bootstrap { peer: *local_key.preimage(), @@ -935,8 +939,13 @@ where step: ProgressStep::first(), }; let peers = self.kbuckets.closest_keys(&local_key).collect::>(); - let inner = QueryInner::new(info); - self.queries.add_iter_closest(local_key, peers, inner) + if peers.is_empty() { + self.bootstrap_status.on_finish(); + Err(NoKnownPeers()) + } else { + let inner = QueryInner::new(info); + Ok(self.queries.add_iter_closest(local_key, peers, inner)) + } } /// Establishes the local node as a provider of a value for the given key. @@ -2519,7 +2528,9 @@ where // Poll bootstrap periodically and automatically. if let Poll::Ready(()) = self.bootstrap_status.poll_next_bootstrap(cx) { - self.bootstrap(); + if let Err(e) = self.bootstrap() { + tracing::warn!("Failed to trigger bootstrap: {e}"); + } } loop { diff --git a/protocols/kad/src/behaviour/test.rs b/protocols/kad/src/behaviour/test.rs index 0c27e2db71b..7005f39e5e6 100644 --- a/protocols/kad/src/behaviour/test.rs +++ b/protocols/kad/src/behaviour/test.rs @@ -178,7 +178,7 @@ fn bootstrap() { let swarm_ids: Vec<_> = swarms.iter().map(Swarm::local_peer_id).cloned().collect(); - let qid = swarms[0].behaviour_mut().bootstrap(); + let qid = swarms[0].behaviour_mut().bootstrap().unwrap(); // Expected known peers let expected_known = swarm_ids.iter().skip(1).cloned().collect::>();