Skip to content

Commit

Permalink
just addressing the problem
Browse files Browse the repository at this point in the history
  • Loading branch information
stormshield-frb committed Jun 13, 2024
1 parent 08148ad commit 345424b
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 9 deletions.
2 changes: 0 additions & 2 deletions misc/server/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

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();
kademlia.bootstrap().unwrap();
Some(kademlia)
} else {
None
Expand Down
2 changes: 1 addition & 1 deletion protocols/kad/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 handles the `NoKnownPeers` on automatic bootstrap.
See [PR 5349](https://github.com/libp2p/rust-libp2p/pull/5349).

## 0.45.3
Expand Down
19 changes: 15 additions & 4 deletions protocols/kad/src/behaviour.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`].
///
Expand All @@ -928,15 +931,21 @@ where
/// > 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> {
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<_>>();
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.
Expand Down Expand Up @@ -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 {
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();
let qid = swarms[0].behaviour_mut().bootstrap().unwrap();

// Expected known peers
let expected_known = swarm_ids.iter().skip(1).cloned().collect::<HashSet<_>>();
Expand Down

0 comments on commit 345424b

Please sign in to comment.