diff --git a/misc/server/src/behaviour.rs b/misc/server/src/behaviour.rs index 36b18c9798d..2f8fc05bdd1 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().unwrap(); + kademlia.bootstrap(); Some(kademlia) } else { None diff --git a/protocols/kad/CHANGELOG.md b/protocols/kad/CHANGELOG.md index bd0c9857cb5..dc0a95e7104 100644 --- a/protocols/kad/CHANGELOG.md +++ b/protocols/kad/CHANGELOG.md @@ -17,6 +17,8 @@ See [PR 5148](https://github.com/libp2p/rust-libp2p/pull/5148). - Derive `Copy` for `kbucket::key::Key`. See [PR 5317](https://github.com/libp2p/rust-libp2p/pull/5317). +- Always trigger a query when bootstrapping. + 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 fb77f3c0e0f..aa5fcc186ce 100644 --- a/protocols/kad/src/behaviour.rs +++ b/protocols/kad/src/behaviour.rs @@ -914,13 +914,10 @@ where /// refreshed by initiating an additional bootstrapping query for each such /// bucket with random keys. /// - /// Returns `Ok` if bootstrapping has been initiated with a self-lookup, providing the - /// `QueryId` for the entire bootstrapping process. The progress of bootstrapping is + /// Returns 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`]. /// @@ -930,7 +927,8 @@ where /// when a new peer is inserted in the routing table. /// This parameter is used to call [`Behaviour::bootstrap`] periodically and automatically /// to ensure a healthy routing table. - pub fn bootstrap(&mut self) -> Result { + pub fn bootstrap(&mut self) -> QueryId { + self.bootstrap_status.on_started(); let local_key = *self.kbuckets.local_key(); let info = QueryInfo::Bootstrap { peer: *local_key.preimage(), @@ -938,13 +936,8 @@ where step: ProgressStep::first(), }; let peers = self.kbuckets.closest_keys(&local_key).collect::>(); - if peers.is_empty() { - Err(NoKnownPeers()) - } else { - self.bootstrap_status.on_started(); - let inner = QueryInner::new(info); - Ok(self.queries.add_iter_closest(local_key, peers, inner)) - } + let inner = QueryInner::new(info); + self.queries.add_iter_closest(local_key, peers, inner) } /// Establishes the local node as a provider of a value for the given key. @@ -2527,9 +2520,7 @@ where // Poll bootstrap periodically and automatically. if let Poll::Ready(()) = self.bootstrap_status.poll_next_bootstrap(cx) { - if let Err(e) = self.bootstrap() { - tracing::warn!("Failed to trigger bootstrap: {e}"); - } + self.bootstrap(); } loop { diff --git a/protocols/kad/src/behaviour/test.rs b/protocols/kad/src/behaviour/test.rs index 7005f39e5e6..0c27e2db71b 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().unwrap(); + let qid = swarms[0].behaviour_mut().bootstrap(); // Expected known peers let expected_known = swarm_ids.iter().skip(1).cloned().collect::>();