Skip to content

Commit

Permalink
fix(kad): always trigger a query when bootstrapping
Browse files Browse the repository at this point in the history
  • Loading branch information
stormshield-frb committed Jun 10, 2024
1 parent 75483e8 commit 94c11d9
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 17 deletions.
2 changes: 2 additions & 0 deletions misc/server/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

- 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

Expand Down
2 changes: 1 addition & 1 deletion misc/server/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions protocols/kad/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
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.
See [PR 5349](https://github.com/libp2p/rust-libp2p/pull/5349).

## 0.45.3

Expand Down
21 changes: 6 additions & 15 deletions protocols/kad/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`].
///
Expand All @@ -930,21 +927,17 @@ 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<QueryId, NoKnownPeers> {
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(),
remaining: None,
step: ProgressStep::first(),
};
let peers = self.kbuckets.closest_keys(&local_key).collect::<Vec<_>>();
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.
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion protocols/kad/src/behaviour/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<HashSet<_>>();
Expand Down

0 comments on commit 94c11d9

Please sign in to comment.