Skip to content

Commit

Permalink
fix(kad): iterator progress to be decided by any of new peers
Browse files Browse the repository at this point in the history
  • Loading branch information
maqi committed Nov 27, 2023
1 parent d1d90c4 commit e4c266e
Showing 1 changed file with 9 additions and 8 deletions.
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

0 comments on commit e4c266e

Please sign in to comment.