Skip to content

Commit

Permalink
fix(kad): iterator progress shall not be decided only by the last new…
Browse files Browse the repository at this point in the history
… peer
  • Loading branch information
maqi committed Nov 27, 2023
1 parent d1d90c4 commit bf7fd61
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
2 changes: 2 additions & 0 deletions protocols/kad/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

- Ensure `Multiaddr` handled and returned by `Behaviour` are `/p2p` terminated.
See [PR 4596](https://github.com/libp2p/rust-libp2p/pull/4596).
- 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.1

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 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

0 comments on commit bf7fd61

Please sign in to comment.