diff --git a/crates/net/discv4/src/lib.rs b/crates/net/discv4/src/lib.rs index af93537c1..8aef5337d 100644 --- a/crates/net/discv4/src/lib.rs +++ b/crates/net/discv4/src/lib.rs @@ -1378,9 +1378,12 @@ impl Discv4Service { fn respond_closest(&mut self, target: PeerId, to: SocketAddr) { let key = kad_key(target); let expire = self.send_neighbours_expiration(); - let all_nodes = self.kbuckets.closest_values(&key).collect::>(); - for nodes in all_nodes.chunks(SAFE_MAX_DATAGRAM_NEIGHBOUR_RECORDS) { + // get the MAX_NODES_PER_BUCKET closest nodes to the target + let closest_nodes = + self.kbuckets.closest_values(&key).take(MAX_NODES_PER_BUCKET).collect::>(); + + for nodes in closest_nodes.chunks(SAFE_MAX_DATAGRAM_NEIGHBOUR_RECORDS) { let nodes = nodes.iter().map(|node| node.value.record).collect::>(); trace!(target: "discv4", len = nodes.len(), to=?to,"Sent neighbours packet"); let msg = Message::Neighbours(Neighbours { nodes, expire });