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

kad: Refactor FindNode query, keep K best results and add tests #114

Merged
merged 14 commits into from
May 24, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions src/protocol/libp2p/kademlia/query/find_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,18 @@ impl<T: Clone + Into<Vec<u8>>> FindNodeContext<T> {
self.responses.insert(distance, peer);
} else {
// Update the furthest peer if this response is closer.
if let Some(mut entry) = self.responses.last_entry() {
if entry.key() > &distance {
entry.insert(peer);
// Find the furthest distance.
let furthest_distance =
self.responses.last_entry().map(|entry| *entry.key()).unwrap_or(distance);

// The response received from the peer is closer than the furthest response.
if distance < furthest_distance {
// Update the entries only if the distance is not already present.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the drawback of overwriting the entry with this distance? I.e., there should be exactly one peer with such distance as it uniquely identifies the hash, so it should be safe to overwrite without checking.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I followed the libp2p here. However, I believe we can overwrite the entries without checking here, and we'll have a "fresher" report in the end. Will do that, thanks!

if !self.responses.contains_key(&distance) {
self.responses.insert(distance, peer);

// Remove the furthest entry.
self.responses.pop_last();
}
}
}
Expand Down