Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(kad): iterator progress to be decided by any of new peers #4932

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ libp2p-floodsub = { version = "0.44.0", path = "protocols/floodsub" }
libp2p-gossipsub = { version = "0.46.1", path = "protocols/gossipsub" }
libp2p-identify = { version = "0.44.1", path = "protocols/identify" }
libp2p-identity = { version = "0.2.8" }
libp2p-kad = { version = "0.45.2", path = "protocols/kad" }
libp2p-kad = { version = "0.45.3", path = "protocols/kad" }
libp2p-mdns = { version = "0.45.1", path = "protocols/mdns" }
libp2p-memory-connection-limits = { version = "0.2.0", path = "misc/memory-connection-limits" }
libp2p-metrics = { version = "0.14.1", path = "misc/metrics" }
Expand Down
5 changes: 5 additions & 0 deletions protocols/kad/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 0.45.3

- The progress of the close query iterator shall be decided by ANY of the new peers.
See [PR 4932](https://github.com/libp2p/rust-libp2p/pull/4932).

## 0.45.2

- Ensure `Multiaddr` handled and returned by `Behaviour` are `/p2p` terminated.
Expand Down
2 changes: 1 addition & 1 deletion protocols/kad/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "libp2p-kad"
edition = "2021"
rust-version = { workspace = true }
description = "Kademlia protocol for libp2p"
version = "0.45.2"
version = "0.45.3"
authors = ["Parity Technologies <admin@parity.io>"]
license = "MIT"
repository = "https://github.com/libp2p/rust-libp2p"
Expand Down
17 changes: 9 additions & 8 deletions protocols/kad/src/query/peers/closest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,10 +175,14 @@ impl ClosestPeersIter {
},
}

let num_closest = self.closest_peers.len();
let mut progress = false;

// Incorporate the reported closer peers into the iterator.
//
// The iterator makes progress if:
// 1, the iterator did not yet accumulate enough closest peers.
// OR
// 2, any of the new peers is closer to the target than any peer seen so far
// (i.e. is the first entry after being incorporated)
let mut progress = self.closest_peers.len() < self.config.num_results.get();
for peer in closer_peers {
let key = peer.into();
let distance = self.target.distance(&key);
Expand All @@ -187,11 +191,8 @@ impl ClosestPeersIter {
state: PeerState::NotContacted,
};
self.closest_peers.entry(distance).or_insert(peer);
// The iterator makes progress if the new peer is either closer to the target
// than any peer seen so far (i.e. is the first entry), or the iterator did
// not yet accumulate enough closest peers.
progress = self.closest_peers.keys().next() == Some(&distance)
|| num_closest < self.config.num_results.get();

progress = self.closest_peers.keys().next() == Some(&distance) || progress;
}

// Update the iterator state.
Expand Down