1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
// Copyright 2023 litep2p developers
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

use bytes::Bytes;

use crate::{
    protocol::libp2p::kademlia::{
        message::KademliaMessage,
        query::{QueryAction, QueryId},
        types::{Distance, KademliaPeer, Key},
    },
    PeerId,
};

use std::collections::{BTreeMap, HashMap, HashSet, VecDeque};

/// Logging target for the file.
const LOG_TARGET: &str = "litep2p::ipfs::kademlia::query::find_node";

/// Default timeout for a peer to respond to a query.
const DEFAULT_PEER_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(10);

/// The configuration needed to instantiate a new [`FindNodeContext`].
#[derive(Debug, Clone)]
pub struct FindNodeConfig<T: Clone + Into<Vec<u8>>> {
    /// Local peer ID.
    pub local_peer_id: PeerId,

    /// Replication factor.
    pub replication_factor: usize,

    /// Parallelism factor.
    pub parallelism_factor: usize,

    /// Query ID.
    pub query: QueryId,

    /// Target key.
    pub target: Key<T>,
}

/// Context for `FIND_NODE` queries.
#[derive(Debug)]
pub struct FindNodeContext<T: Clone + Into<Vec<u8>>> {
    /// Query immutable config.
    pub config: FindNodeConfig<T>,

    /// Cached Kademlia message to send.
    kad_message: Bytes,

    /// Peers from whom the `QueryEngine` is waiting to hear a response.
    pub pending: HashMap<PeerId, (KademliaPeer, std::time::Instant)>,

    /// Queried candidates.
    ///
    /// These are the peers for whom the query has already been sent
    /// and who have either returned their closest peers or failed to answer.
    pub queried: HashSet<PeerId>,

    /// Candidates.
    pub candidates: BTreeMap<Distance, KademliaPeer>,

    /// Responses.
    pub responses: BTreeMap<Distance, KademliaPeer>,

    /// The timeout after which the pending request is no longer
    /// counting towards the parallelism factor.
    ///
    /// This is used to prevent the query from getting stuck when a peer
    /// is slow or fails to respond in due time.
    peer_timeout: std::time::Duration,
    /// The number of pending responses that count towards the parallelism factor.
    ///
    /// These represent the number of peers added to the `Self::pending` minus the number of peers
    /// that have failed to respond within the `Self::peer_timeout`
    pending_responses: usize,
}

impl<T: Clone + Into<Vec<u8>>> FindNodeContext<T> {
    /// Create new [`FindNodeContext`].
    pub fn new(config: FindNodeConfig<T>, in_peers: VecDeque<KademliaPeer>) -> Self {
        let mut candidates = BTreeMap::new();

        for candidate in &in_peers {
            let distance = config.target.distance(&candidate.key);
            candidates.insert(distance, candidate.clone());
        }

        let kad_message = KademliaMessage::find_node(config.target.clone().into_preimage());

        Self {
            config,
            kad_message,

            candidates,
            pending: HashMap::new(),
            queried: HashSet::new(),
            responses: BTreeMap::new(),

            peer_timeout: DEFAULT_PEER_TIMEOUT,
            pending_responses: 0,
        }
    }

    /// Register response failure for `peer`.
    pub fn register_response_failure(&mut self, peer: PeerId) {
        let Some((peer, instant)) = self.pending.remove(&peer) else {
            tracing::debug!(target: LOG_TARGET, query = ?self.config.query, ?peer, "pending peer doesn't exist during response failure");
            return;
        };
        self.pending_responses = self.pending_responses.saturating_sub(1);

        tracing::trace!(target: LOG_TARGET, query = ?self.config.query, ?peer, elapsed = ?instant.elapsed(), "peer failed to respond");

        self.queried.insert(peer.peer);
    }

    /// Register `FIND_NODE` response from `peer`.
    pub fn register_response(&mut self, peer: PeerId, peers: Vec<KademliaPeer>) {
        let Some((peer, instant)) = self.pending.remove(&peer) else {
            tracing::debug!(target: LOG_TARGET, query = ?self.config.query, ?peer, "received response from peer but didn't expect it");
            return;
        };
        self.pending_responses = self.pending_responses.saturating_sub(1);

        tracing::trace!(target: LOG_TARGET, query = ?self.config.query, ?peer, elapsed = ?instant.elapsed(), "received response from peer");

        // calculate distance for `peer` from target and insert it if
        //  a) the map doesn't have 20 responses
        //  b) it can replace some other peer that has a higher distance
        let distance = self.config.target.distance(&peer.key);

        // always mark the peer as queried to prevent it getting queried again
        self.queried.insert(peer.peer);

        if self.responses.len() < self.config.replication_factor {
            self.responses.insert(distance, peer);
        } else {
            // Update the furthest peer if this response is closer.
            // 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 {
                self.responses.insert(distance, peer);

                // Remove the furthest entry.
                if self.responses.len() > self.config.replication_factor {
                    self.responses.pop_last();
                }
            }
        }

        let to_query_candidate = peers.into_iter().filter_map(|peer| {
            // Peer already produced a response.
            if self.queried.contains(&peer.peer) {
                return None;
            }

            // Peer was queried, awaiting response.
            if self.pending.contains_key(&peer.peer) {
                return None;
            }

            // Local node.
            if self.config.local_peer_id == peer.peer {
                return None;
            }

            Some(peer)
        });

        for candidate in to_query_candidate {
            let distance = self.config.target.distance(&candidate.key);
            self.candidates.insert(distance, candidate);
        }
    }

    /// Get next action for `peer`.
    pub fn next_peer_action(&mut self, peer: &PeerId) -> Option<QueryAction> {
        self.pending.contains_key(peer).then_some(QueryAction::SendMessage {
            query: self.config.query,
            peer: *peer,
            message: self.kad_message.clone(),
        })
    }

    /// Schedule next peer for outbound `FIND_NODE` query.
    fn schedule_next_peer(&mut self) -> Option<QueryAction> {
        tracing::trace!(target: LOG_TARGET, query = ?self.config.query, "get next peer");

        let (_, candidate) = self.candidates.pop_first()?;
        let peer = candidate.peer;

        tracing::trace!(target: LOG_TARGET, query = ?self.config.query, ?peer, "current candidate");
        self.pending.insert(candidate.peer, (candidate, std::time::Instant::now()));
        self.pending_responses = self.pending_responses.saturating_add(1);

        Some(QueryAction::SendMessage {
            query: self.config.query,
            peer,
            message: self.kad_message.clone(),
        })
    }

    /// Check if the query cannot make any progress.
    ///
    /// Returns true when there are no pending responses and no candidates to query.
    fn is_done(&self) -> bool {
        self.pending.is_empty() && self.candidates.is_empty()
    }

    /// Get next action for a `FIND_NODE` query.
    pub fn next_action(&mut self) -> Option<QueryAction> {
        // If we cannot make progress, return the final result.
        // A query failed when we are not able to identify one single peer.
        if self.is_done() {
            tracing::trace!(
                target: LOG_TARGET,
                query = ?self.config.query,
                pending = self.pending.len(),
                candidates = self.candidates.len(),
                "query finished"
            );

            return if self.responses.is_empty() {
                Some(QueryAction::QueryFailed {
                    query: self.config.query,
                })
            } else {
                Some(QueryAction::QuerySucceeded {
                    query: self.config.query,
                })
            };
        }

        for (peer, instant) in self.pending.values() {
            if instant.elapsed() > self.peer_timeout {
                tracing::trace!(
                    target: LOG_TARGET,
                    query = ?self.config.query,
                    ?peer,
                    elapsed = ?instant.elapsed(),
                    "peer no longer counting towards parallelism factor"
                );
                self.pending_responses = self.pending_responses.saturating_sub(1);
            }
        }

        // At this point, we either have pending responses or candidates to query; and we need more
        // results. Ensure we do not exceed the parallelism factor.
        if self.pending_responses == self.config.parallelism_factor {
            return None;
        }

        // Schedule the next peer to fill up the responses.
        if self.responses.len() < self.config.replication_factor {
            return self.schedule_next_peer();
        }

        // We can finish the query here, but check if there is a better candidate for the query.
        match (
            self.candidates.first_key_value(),
            self.responses.last_key_value(),
        ) {
            (Some((_, candidate_peer)), Some((worst_response_distance, _))) => {
                let first_candidate_distance = self.config.target.distance(&candidate_peer.key);
                if first_candidate_distance < *worst_response_distance {
                    return self.schedule_next_peer();
                }
            }

            _ => (),
        }

        // We have found enough responses and there are no better candidates to query.
        Some(QueryAction::QuerySucceeded {
            query: self.config.query,
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::protocol::libp2p::kademlia::types::ConnectionType;

    fn default_config() -> FindNodeConfig<Vec<u8>> {
        FindNodeConfig {
            local_peer_id: PeerId::random(),
            replication_factor: 20,
            parallelism_factor: 10,
            query: QueryId(0),
            target: Key::new(vec![1, 2, 3].into()),
        }
    }

    fn peer_to_kad(peer: PeerId) -> KademliaPeer {
        KademliaPeer {
            peer,
            key: Key::from(peer),
            addresses: vec![],
            connection: ConnectionType::Connected,
        }
    }

    fn setup_closest_responses() -> (PeerId, PeerId, FindNodeConfig<PeerId>) {
        let peer_a = PeerId::random();
        let peer_b = PeerId::random();
        let target = PeerId::random();

        let distance_a = Key::from(peer_a).distance(&Key::from(target));
        let distance_b = Key::from(peer_b).distance(&Key::from(target));

        let (closest, furthest) = if distance_a < distance_b {
            (peer_a, peer_b)
        } else {
            (peer_b, peer_a)
        };

        let config = FindNodeConfig {
            parallelism_factor: 1,
            replication_factor: 1,
            target: Key::from(target),
            local_peer_id: PeerId::random(),
            query: QueryId(0),
        };

        (closest, furthest, config)
    }

    #[test]
    fn completes_when_no_candidates() {
        let config = default_config();
        let mut context = FindNodeContext::new(config, VecDeque::new());
        assert!(context.is_done());
        let event = context.next_action().unwrap();
        assert_eq!(event, QueryAction::QueryFailed { query: QueryId(0) });
    }

    #[test]
    fn fulfill_parallelism() {
        let config = FindNodeConfig {
            parallelism_factor: 3,
            ..default_config()
        };

        let in_peers_set = (0..3).map(|_| PeerId::random()).collect::<HashSet<_>>();
        let in_peers = in_peers_set.iter().map(|peer| peer_to_kad(*peer)).collect();
        let mut context = FindNodeContext::new(config, in_peers);

        for num in 0..3 {
            let event = context.next_action().unwrap();
            match event {
                QueryAction::SendMessage { query, peer, .. } => {
                    assert_eq!(query, QueryId(0));
                    // Added as pending.
                    assert_eq!(context.pending.len(), num + 1);
                    assert!(context.pending.contains_key(&peer));

                    // Check the peer is the one provided.
                    assert!(in_peers_set.contains(&peer));
                }
                _ => panic!("Unexpected event"),
            }
        }

        // Fulfilled parallelism.
        assert!(context.next_action().is_none());
    }

    #[test]
    fn fulfill_parallelism_with_timeout_optimization() {
        let config = FindNodeConfig {
            parallelism_factor: 3,
            ..default_config()
        };

        let in_peers_set = (0..4).map(|_| PeerId::random()).collect::<HashSet<_>>();
        let in_peers = in_peers_set.iter().map(|peer| peer_to_kad(*peer)).collect();
        let mut context = FindNodeContext::new(config, in_peers);
        // Test overwrite.
        context.peer_timeout = std::time::Duration::from_secs(1);

        for num in 0..3 {
            let event = context.next_action().unwrap();
            match event {
                QueryAction::SendMessage { query, peer, .. } => {
                    assert_eq!(query, QueryId(0));
                    // Added as pending.
                    assert_eq!(context.pending.len(), num + 1);
                    assert!(context.pending.contains_key(&peer));

                    // Check the peer is the one provided.
                    assert!(in_peers_set.contains(&peer));
                }
                _ => panic!("Unexpected event"),
            }
        }

        // Fulfilled parallelism.
        assert!(context.next_action().is_none());

        // Sleep more than 1 second.
        std::thread::sleep(std::time::Duration::from_secs(2));

        // The pending responses are reset only on the next query action.
        assert_eq!(context.pending_responses, 3);
        assert_eq!(context.pending.len(), 3);

        // This allows other peers to be queried.
        let event = context.next_action().unwrap();
        match event {
            QueryAction::SendMessage { query, peer, .. } => {
                assert_eq!(query, QueryId(0));
                // Added as pending.
                assert_eq!(context.pending.len(), 4);
                assert!(context.pending.contains_key(&peer));

                // Check the peer is the one provided.
                assert!(in_peers_set.contains(&peer));
            }
            _ => panic!("Unexpected event"),
        }

        assert_eq!(context.pending_responses, 1);
        assert_eq!(context.pending.len(), 4);
    }

    #[test]
    fn completes_when_responses() {
        let config = FindNodeConfig {
            parallelism_factor: 3,
            replication_factor: 3,
            ..default_config()
        };

        let peer_a = PeerId::random();
        let peer_b = PeerId::random();
        let peer_c = PeerId::random();

        let in_peers_set: HashSet<_> = [peer_a, peer_b, peer_c].into_iter().collect();
        assert_eq!(in_peers_set.len(), 3);

        let in_peers = [peer_a, peer_b, peer_c].iter().map(|peer| peer_to_kad(*peer)).collect();
        let mut context = FindNodeContext::new(config, in_peers);

        // Schedule peer queries.
        for num in 0..3 {
            let event = context.next_action().unwrap();
            match event {
                QueryAction::SendMessage { query, peer, .. } => {
                    assert_eq!(query, QueryId(0));
                    // Added as pending.
                    assert_eq!(context.pending.len(), num + 1);
                    assert!(context.pending.contains_key(&peer));

                    // Check the peer is the one provided.
                    assert!(in_peers_set.contains(&peer));
                }
                _ => panic!("Unexpected event"),
            }
        }

        // Checks a failed query that was not initiated.
        let peer_d = PeerId::random();
        context.register_response_failure(peer_d);
        assert_eq!(context.pending.len(), 3);
        assert!(context.queried.is_empty());

        // Provide responses back.
        context.register_response(peer_a, vec![]);
        assert_eq!(context.pending.len(), 2);
        assert_eq!(context.queried.len(), 1);
        assert_eq!(context.responses.len(), 1);

        // Provide different response from peer b with peer d as candidate.
        context.register_response(peer_b, vec![peer_to_kad(peer_d.clone())]);
        assert_eq!(context.pending.len(), 1);
        assert_eq!(context.queried.len(), 2);
        assert_eq!(context.responses.len(), 2);
        assert_eq!(context.candidates.len(), 1);

        // Peer C fails.
        context.register_response_failure(peer_c);
        assert!(context.pending.is_empty());
        assert_eq!(context.queried.len(), 3);
        assert_eq!(context.responses.len(), 2);

        // Drain the last candidate.
        let event = context.next_action().unwrap();
        match event {
            QueryAction::SendMessage { query, peer, .. } => {
                assert_eq!(query, QueryId(0));
                // Added as pending.
                assert_eq!(context.pending.len(), 1);
                assert_eq!(peer, peer_d);
            }
            _ => panic!("Unexpected event"),
        }

        // Peer D responds.
        context.register_response(peer_d, vec![]);

        // Produces the result.
        let event = context.next_action().unwrap();
        assert_eq!(event, QueryAction::QuerySucceeded { query: QueryId(0) });
    }

    #[test]
    fn offers_closest_responses() {
        let (closest, furthest, config) = setup_closest_responses();

        // Scenario where we should return with the number of responses.
        let in_peers = vec![peer_to_kad(furthest), peer_to_kad(closest)];
        let mut context = FindNodeContext::new(config.clone(), in_peers.into_iter().collect());

        let event = context.next_action().unwrap();
        match event {
            QueryAction::SendMessage { query, peer, .. } => {
                assert_eq!(query, QueryId(0));
                // Added as pending.
                assert_eq!(context.pending.len(), 1);
                assert!(context.pending.contains_key(&peer));

                // The closest should be queried first regardless of the input order.
                assert_eq!(closest, peer);
            }
            _ => panic!("Unexpected event"),
        }

        context.register_response(closest, vec![]);

        let event = context.next_action().unwrap();
        assert_eq!(event, QueryAction::QuerySucceeded { query: QueryId(0) });
    }

    #[test]
    fn offers_closest_responses_with_better_candidates() {
        let (closest, furthest, config) = setup_closest_responses();

        // Scenario where the query is fulfilled however it continues because
        // there is a closer peer to query.
        let in_peers = vec![peer_to_kad(furthest)];
        let mut context = FindNodeContext::new(config, in_peers.into_iter().collect());

        let event = context.next_action().unwrap();
        match event {
            QueryAction::SendMessage { query, peer, .. } => {
                assert_eq!(query, QueryId(0));
                // Added as pending.
                assert_eq!(context.pending.len(), 1);
                assert!(context.pending.contains_key(&peer));

                // Furthest is the only peer available.
                assert_eq!(furthest, peer);
            }
            _ => panic!("Unexpected event"),
        }

        // Furthest node produces a response with the closest node.
        // Even if we reach a total of 1 (parallelism factor) replies, we should continue.
        context.register_response(furthest, vec![peer_to_kad(closest)]);

        let event = context.next_action().unwrap();
        match event {
            QueryAction::SendMessage { query, peer, .. } => {
                assert_eq!(query, QueryId(0));
                // Added as pending.
                assert_eq!(context.pending.len(), 1);
                assert!(context.pending.contains_key(&peer));

                // Furthest provided another peer that is closer.
                assert_eq!(closest, peer);
            }
            _ => panic!("Unexpected event"),
        }

        // Even if we have the total number of responses, we have at least one
        // inflight query which might be closer to the target.
        assert!(context.next_action().is_none());

        // Query finishes when receiving the response back.
        context.register_response(closest, vec![]);

        let event = context.next_action().unwrap();
        assert_eq!(event, QueryAction::QuerySucceeded { query: QueryId(0) });
    }

    #[test]
    fn keep_k_best_results() {
        let mut peers = (0..6).map(|_| PeerId::random()).collect::<Vec<_>>();
        let target = Key::from(PeerId::random());
        // Sort the peers by their distance to the target in descending order.
        peers.sort_by_key(|peer| std::cmp::Reverse(target.distance(&Key::from(*peer))));

        let config = FindNodeConfig {
            parallelism_factor: 3,
            replication_factor: 3,
            target,
            local_peer_id: PeerId::random(),
            query: QueryId(0),
        };

        let in_peers = vec![peers[0], peers[1], peers[2]]
            .iter()
            .map(|peer| peer_to_kad(*peer))
            .collect();
        let mut context = FindNodeContext::new(config, in_peers);

        // Schedule peer queries.
        for num in 0..3 {
            let event = context.next_action().unwrap();
            match event {
                QueryAction::SendMessage { query, peer, .. } => {
                    assert_eq!(query, QueryId(0));
                    // Added as pending.
                    assert_eq!(context.pending.len(), num + 1);
                    assert!(context.pending.contains_key(&peer));
                }
                _ => panic!("Unexpected event"),
            }
        }

        // Each peer responds with a better (closer) peer.
        context.register_response(peers[0], vec![peer_to_kad(peers[3])]);
        context.register_response(peers[1], vec![peer_to_kad(peers[4])]);
        context.register_response(peers[2], vec![peer_to_kad(peers[5])]);

        // Must schedule better peers.
        for num in 0..3 {
            let event = context.next_action().unwrap();
            match event {
                QueryAction::SendMessage { query, peer, .. } => {
                    assert_eq!(query, QueryId(0));
                    // Added as pending.
                    assert_eq!(context.pending.len(), num + 1);
                    assert!(context.pending.contains_key(&peer));
                }
                _ => panic!("Unexpected event"),
            }
        }

        context.register_response(peers[3], vec![]);
        context.register_response(peers[4], vec![]);
        context.register_response(peers[5], vec![]);

        // Produces the result.
        let event = context.next_action().unwrap();
        assert_eq!(event, QueryAction::QuerySucceeded { query: QueryId(0) });

        // Because the FindNode query keeps a window of the best K (3 in this case) peers,
        // we expect to produce the best K peers. As opposed to having only the last entry
        // updated, which would have produced [peer[0], peer[1], peer[5]].

        // Check the responses.
        let responses = context.responses.values().map(|peer| peer.peer).collect::<Vec<_>>();
        // Note: peers are returned in order closest to the target, our `peers` input is sorted in
        // decreasing order.
        assert_eq!(responses, [peers[5], peers[4], peers[3]]);
    }
}