subspace_networking/utils/
piece_provider.rs

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
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
//! Provides methods to retrieve pieces from DSN.

use crate::protocols::request_response::handlers::cached_piece_by_index::{
    CachedPieceByIndexRequest, CachedPieceByIndexResponse, PieceResult,
};
use crate::protocols::request_response::handlers::piece_by_index::{
    PieceByIndexRequest, PieceByIndexResponse,
};
use crate::utils::multihash::ToMultihash;
use crate::{Multihash, Node};
use async_trait::async_trait;
use futures::channel::mpsc;
use futures::future::FusedFuture;
use futures::stream::FuturesUnordered;
use futures::task::noop_waker_ref;
use futures::{stream, FutureExt, Stream, StreamExt};
use libp2p::kad::store::RecordStore;
use libp2p::kad::{store, Behaviour as Kademlia, KBucketKey, ProviderRecord, Record, RecordKey};
use libp2p::swarm::NetworkBehaviour;
use libp2p::{Multiaddr, PeerId};
use parking_lot::Mutex;
use rand::prelude::*;
use std::any::type_name;
use std::borrow::Cow;
use std::collections::{HashMap, HashSet};
use std::iter::Empty;
use std::sync::Arc;
use std::task::{Context, Poll};
use std::{fmt, iter, mem};
use subspace_core_primitives::pieces::{Piece, PieceIndex};
use tokio_stream::StreamMap;
use tracing::{debug, trace, warn, Instrument};

/// Validates piece against using its commitment.
#[async_trait]
pub trait PieceValidator: Sync + Send {
    /// Validates piece against using its commitment.
    async fn validate_piece(
        &self,
        source_peer_id: PeerId,
        piece_index: PieceIndex,
        piece: Piece,
    ) -> Option<Piece>;
}

/// Stub implementation for piece validation.
#[derive(Debug, Clone, Copy)]
pub struct NoPieceValidator;

#[async_trait]
impl PieceValidator for NoPieceValidator {
    async fn validate_piece(&self, _: PeerId, _: PieceIndex, piece: Piece) -> Option<Piece> {
        Some(piece)
    }
}

/// Piece provider with cancellation and piece validator.
/// Use `NoPieceValidator` to disable validation.
pub struct PieceProvider<PV> {
    node: Node,
    piece_validator: PV,
}

impl<PV> fmt::Debug for PieceProvider<PV> {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct(&format!("PieceProvider<{}>", type_name::<PV>()))
            .finish_non_exhaustive()
    }
}

impl<PV> PieceProvider<PV>
where
    PV: PieceValidator,
{
    /// Creates new piece provider.
    pub fn new(node: Node, piece_validator: PV) -> Self {
        Self {
            node,
            piece_validator,
        }
    }

    /// Get pieces with provided indices from cache.
    ///
    /// Number of elements in returned stream is the same as number of unique `piece_indices`.
    pub async fn get_from_cache<'a, PieceIndices>(
        &'a self,
        piece_indices: PieceIndices,
    ) -> impl Stream<Item = (PieceIndex, Option<Piece>)> + Unpin + 'a
    where
        PieceIndices: IntoIterator<Item = PieceIndex> + 'a,
    {
        let (tx, mut rx) = mpsc::unbounded();
        let fut = get_from_cache_inner(
            piece_indices.into_iter(),
            &self.node,
            &self.piece_validator,
            tx,
        );
        let mut fut = Box::pin(fut.fuse());

        // Drive above future and stream back any pieces that were downloaded so far
        stream::poll_fn(move |cx| {
            if !fut.is_terminated() {
                // Result doesn't matter, we'll need to poll stream below anyway
                let _ = fut.poll_unpin(cx);
            }

            if let Poll::Ready(maybe_result) = rx.poll_next_unpin(cx) {
                return Poll::Ready(maybe_result);
            }

            // Exit will be done by the stream above
            Poll::Pending
        })
    }

    /// Returns piece by its index from farmer's piece cache (L2)
    pub async fn get_piece_from_cache(&self, piece_index: PieceIndex) -> Option<Piece> {
        let key = RecordKey::from(piece_index.to_multihash());

        let mut request_batch = self.node.get_requests_batch_handle().await;
        let get_providers_result = request_batch.get_providers(key.clone()).await;

        match get_providers_result {
            Ok(mut get_providers_stream) => {
                while let Some(provider_id) = get_providers_stream.next().await {
                    trace!(
                        %piece_index,
                        key = hex::encode(&key),
                        %provider_id,
                        "get_providers returned an item"
                    );

                    let request_result = request_batch
                        .send_generic_request(
                            provider_id,
                            Vec::new(),
                            PieceByIndexRequest {
                                piece_index,
                                cached_pieces: Arc::default(),
                            },
                        )
                        .await;

                    match request_result {
                        Ok(PieceByIndexResponse {
                            piece: Some(piece),
                            cached_pieces: _,
                        }) => {
                            trace!(
                                %piece_index,
                                key = hex::encode(&key),
                                %provider_id,
                                "Piece request succeeded"
                            );

                            return self
                                .piece_validator
                                .validate_piece(provider_id, piece_index, piece)
                                .await;
                        }
                        Ok(PieceByIndexResponse {
                            piece: None,
                            cached_pieces: _,
                        }) => {
                            debug!(
                                %piece_index,
                                key = hex::encode(&key),
                                %provider_id,
                                "Piece request returned empty piece"
                            );
                        }
                        Err(error) => {
                            debug!(
                                %piece_index,
                                key = hex::encode(&key),
                                %provider_id,
                                ?error,
                                "Piece request failed"
                            );
                        }
                    }
                }
            }
            Err(err) => {
                warn!(%piece_index,?key, ?err, "get_providers returned an error");
            }
        }

        None
    }

    /// Get piece from a particular peer.
    pub async fn get_piece_from_peer(
        &self,
        peer_id: PeerId,
        piece_index: PieceIndex,
    ) -> Option<Piece> {
        // TODO: Take advantage of `cached_pieces`
        let request_result = self
            .node
            .send_generic_request(
                peer_id,
                Vec::new(),
                PieceByIndexRequest {
                    piece_index,
                    cached_pieces: Arc::default(),
                },
            )
            .await;

        match request_result {
            Ok(PieceByIndexResponse {
                piece: Some(piece),
                cached_pieces: _,
            }) => {
                trace!(%peer_id, %piece_index, "Piece request succeeded");

                return self
                    .piece_validator
                    .validate_piece(peer_id, piece_index, piece)
                    .await;
            }
            Ok(PieceByIndexResponse {
                piece: None,
                cached_pieces: _,
            }) => {
                debug!(%peer_id, %piece_index, "Piece request returned empty piece");
            }
            Err(error) => {
                debug!(%peer_id, %piece_index, ?error, "Piece request failed");
            }
        }

        None
    }

    /// Get piece from archival storage (L1). The algorithm tries to get a piece from currently
    /// connected peers and falls back to random walking.
    pub async fn get_piece_from_archival_storage(
        &self,
        piece_index: PieceIndex,
        max_random_walking_rounds: usize,
    ) -> Option<Piece> {
        // TODO: consider using retry policy for L1 lookups as well.
        trace!(%piece_index, "Getting piece from archival storage..");

        let connected_peers = {
            let connected_peers = match self.node.connected_peers().await {
                Ok(connected_peers) => connected_peers,
                Err(err) => {
                    debug!(%piece_index, ?err, "Cannot get connected peers (DSN L1 lookup)");

                    Default::default()
                }
            };

            HashSet::<PeerId>::from_iter(connected_peers)
        };

        if connected_peers.is_empty() {
            debug!(%piece_index, "Cannot acquire piece from no connected peers (DSN L1 lookup)");
        } else {
            for peer_id in connected_peers.iter() {
                let maybe_piece = self.get_piece_from_peer(*peer_id, piece_index).await;

                if maybe_piece.is_some() {
                    trace!(%piece_index, %peer_id, "DSN L1 lookup from connected peers succeeded");

                    return maybe_piece;
                }
            }
        }

        trace!(%piece_index, "Getting piece from DSN L1 using random walk.");
        let random_walk_result = self
            .get_piece_by_random_walking(piece_index, max_random_walking_rounds)
            .await;

        if random_walk_result.is_some() {
            trace!(%piece_index, "DSN L1 lookup via random walk succeeded");

            return random_walk_result;
        } else {
            debug!(
                %piece_index,
                %max_random_walking_rounds,
                "Cannot acquire piece from DSN L1: random walk failed"
            );
        }

        None
    }

    /// Get piece from L1 by random walking
    async fn get_piece_by_random_walking(
        &self,
        piece_index: PieceIndex,
        walking_rounds: usize,
    ) -> Option<Piece> {
        for round in 0..walking_rounds {
            debug!(%piece_index, round, "Random walk round");

            let result = self
                .get_piece_by_random_walking_from_single_round(piece_index, round)
                .await;

            if result.is_some() {
                return result;
            }
        }

        debug!(%piece_index, "Random walking piece retrieval failed.");

        None
    }

    /// Get piece from L1 by random walking (single round)
    async fn get_piece_by_random_walking_from_single_round(
        &self,
        piece_index: PieceIndex,
        round: usize,
    ) -> Option<Piece> {
        // TODO: Take advantage of `cached_pieces`
        trace!(%piece_index, "get_piece_by_random_walking round");

        // Random walk key
        let key = PeerId::random();

        let mut request_batch = self.node.get_requests_batch_handle().await;
        let get_closest_peers_result = request_batch.get_closest_peers(key.into()).await;

        match get_closest_peers_result {
            Ok(mut get_closest_peers_stream) => {
                while let Some(peer_id) = get_closest_peers_stream.next().await {
                    trace!(%piece_index, %peer_id, %round, "get_closest_peers returned an item");

                    let request_result = request_batch
                        .send_generic_request(
                            peer_id,
                            Vec::new(),
                            PieceByIndexRequest {
                                piece_index,
                                cached_pieces: Arc::default(),
                            },
                        )
                        .await;

                    match request_result {
                        Ok(PieceByIndexResponse {
                            piece: Some(piece),
                            cached_pieces: _,
                        }) => {
                            trace!(%peer_id, %piece_index, ?key, %round,  "Piece request succeeded.");

                            return self
                                .piece_validator
                                .validate_piece(peer_id, piece_index, piece)
                                .await;
                        }
                        Ok(PieceByIndexResponse {
                            piece: None,
                            cached_pieces: _,
                        }) => {
                            debug!(%peer_id, %piece_index, ?key, %round, "Piece request returned empty piece.");
                        }
                        Err(error) => {
                            debug!(%peer_id, %piece_index, ?key, %round, ?error, "Piece request failed.");
                        }
                    }
                }
            }
            Err(err) => {
                warn!(%piece_index, ?key, ?err, %round, "get_closest_peers returned an error");
            }
        }

        None
    }
}

struct DummyRecordStore;

impl RecordStore for DummyRecordStore {
    type RecordsIter<'a>
        = Empty<Cow<'a, Record>>
    where
        Self: 'a;
    type ProvidedIter<'a>
        = Empty<Cow<'a, ProviderRecord>>
    where
        Self: 'a;

    fn get(&self, _key: &RecordKey) -> Option<Cow<'_, Record>> {
        // Not supported
        None
    }

    fn put(&mut self, _record: Record) -> store::Result<()> {
        // Not supported
        Ok(())
    }

    fn remove(&mut self, _key: &RecordKey) {
        // Not supported
    }

    fn records(&self) -> Self::RecordsIter<'_> {
        // Not supported
        iter::empty()
    }

    fn add_provider(&mut self, _record: ProviderRecord) -> store::Result<()> {
        // Not supported
        Ok(())
    }

    fn providers(&self, _key: &RecordKey) -> Vec<ProviderRecord> {
        // Not supported
        Vec::new()
    }

    fn provided(&self) -> Self::ProvidedIter<'_> {
        // Not supported
        iter::empty()
    }

    fn remove_provider(&mut self, _key: &RecordKey, _provider: &PeerId) {
        // Not supported
    }
}

/// Kademlia wrapper to take advantage of its internal logic of selecting closest peers
struct KademliaWrapper {
    local_peer_id: PeerId,
    kademlia: Kademlia<DummyRecordStore>,
}

impl KademliaWrapper {
    fn new(local_peer_id: PeerId) -> Self {
        Self {
            local_peer_id,
            kademlia: Kademlia::new(local_peer_id, DummyRecordStore),
        }
    }

    fn add_peer(&mut self, peer_id: &PeerId, addresses: Vec<Multiaddr>) {
        for address in addresses {
            self.kademlia.add_address(peer_id, address);
        }
        while self
            .kademlia
            .poll(&mut Context::from_waker(noop_waker_ref()))
            .is_ready()
        {
            // Simply drain useless events generated by above calls
        }
    }

    /// Returned peers are already sorted in ascending distance order
    fn closest_peers(
        &mut self,
        key: &KBucketKey<Multihash>,
    ) -> impl Iterator<Item = (PeerId, Vec<Multiaddr>)> + 'static {
        let mut closest_peers = self
            .kademlia
            .find_closest(key, &self.local_peer_id)
            .into_iter()
            .map(|peer| {
                (
                    KBucketKey::from(peer.node_id).distance(key),
                    peer.node_id,
                    peer.multiaddrs,
                )
            })
            .collect::<Vec<_>>();

        closest_peers.sort_unstable_by(|a, b| a.0.cmp(&b.0));
        closest_peers
            .into_iter()
            .map(|(_distance, peer_id, addresses)| (peer_id, addresses))
    }
}

async fn get_from_cache_inner<PV, PieceIndices>(
    piece_indices: PieceIndices,
    node: &Node,
    piece_validator: &PV,
    results: mpsc::UnboundedSender<(PieceIndex, Option<Piece>)>,
) where
    PV: PieceValidator,
    PieceIndices: Iterator<Item = PieceIndex>,
{
    let download_id = random::<u64>();

    // TODO: It'd be nice to combine downloading from connected peers with downloading from closest
    //  peers concurrently
    let fut = async move {
        // Download from connected peers first
        let pieces_to_download = download_cached_pieces_from_connected_peers(
            piece_indices,
            node,
            piece_validator,
            &results,
        )
        .await;

        if pieces_to_download.is_empty() {
            debug!("Done");
            return;
        }

        // Download from iteratively closer peers according to Kademlia rules
        download_cached_pieces_from_closest_peers(
            pieces_to_download,
            node,
            piece_validator,
            &results,
        )
        .await;

        debug!("Done #2");
    };

    fut.instrument(tracing::info_span!("", %download_id)).await;
}

/// Takes pieces to download as an input, sends results with pieces that were downloaded
/// successfully and returns those that were not downloaded from connected peer with addresses of
/// potential candidates
async fn download_cached_pieces_from_connected_peers<PV, PieceIndices>(
    piece_indices: PieceIndices,
    node: &Node,
    piece_validator: &PV,
    results: &mpsc::UnboundedSender<(PieceIndex, Option<Piece>)>,
) -> HashMap<PieceIndex, HashMap<PeerId, Vec<Multiaddr>>>
where
    PV: PieceValidator,
    PieceIndices: Iterator<Item = PieceIndex>,
{
    // Make sure every piece index has an entry since this will be the primary container for
    // tracking pieces to download going forward.
    //
    // At the end pieces that were not downloaded will remain with a collection of known closest
    // peers for them.
    let mut pieces_to_download = piece_indices
        .map(|piece_index| (piece_index, HashMap::new()))
        .collect::<HashMap<PieceIndex, HashMap<PeerId, Vec<Multiaddr>>>>();

    debug!(num_pieces = %pieces_to_download.len(), "Starting");

    let mut checked_connected_peers = HashSet::new();

    // The loop is in order to check peers that might be connected after the initial loop has
    // started.
    loop {
        let Ok(connected_peers) = node.connected_peers().await else {
            trace!("Connected peers error");
            break;
        };

        debug!(
            connected_peers = %connected_peers.len(),
            pieces_to_download = %pieces_to_download.len(),
            "Loop"
        );
        if connected_peers.is_empty() || pieces_to_download.is_empty() {
            break;
        }

        let num_pieces = pieces_to_download.len();
        let step = num_pieces / connected_peers.len().min(num_pieces);

        // Dispatch initial set of requests to peers
        let mut downloading_stream = connected_peers
            .into_iter()
            .take(num_pieces)
            .enumerate()
            .filter_map(|(peer_index, peer_id)| {
                if !checked_connected_peers.insert(peer_id) {
                    return None;
                }

                // Take unique first piece index for each connected peer and the rest just to check
                // cached pieces up to recommended limit
                let mut peer_piece_indices = pieces_to_download
                    .keys()
                    .cycle()
                    .skip(step * peer_index)
                    .take(num_pieces.min(CachedPieceByIndexRequest::RECOMMENDED_LIMIT))
                    .copied()
                    .collect::<Vec<_>>();
                // Pick first piece index as the piece we want to download
                let piece_index = peer_piece_indices.swap_remove(0);

                let fut = download_cached_piece_from_peer(
                    node,
                    piece_validator,
                    peer_id,
                    Vec::new(),
                    Arc::new(peer_piece_indices),
                    piece_index,
                    HashSet::new(),
                    HashSet::new(),
                );

                Some((piece_index, Box::pin(fut.into_stream())))
            })
            .collect::<StreamMap<_, _>>();

        // Process every response and potentially schedule follow-up request to the same peer
        while let Some((piece_index, result)) = downloading_stream.next().await {
            let DownloadedPieceFromPeer {
                peer_id,
                result,
                mut cached_pieces,
                not_cached_pieces,
            } = result;
            trace!(%piece_index, %peer_id, result = %result.is_some(), "Piece response");

            let Some(result) = result else {
                // Downloading failed, ignore peer
                continue;
            };

            match result {
                PieceResult::Piece(piece) => {
                    trace!(%piece_index, %peer_id, "Got piece");

                    // Downloaded successfully
                    pieces_to_download.remove(&piece_index);

                    results
                        .unbounded_send((piece_index, Some(piece)))
                        .expect("This future isn't polled after receiver is dropped; qed");

                    if pieces_to_download.is_empty() {
                        return HashMap::new();
                    }
                }
                PieceResult::ClosestPeers(closest_peers) => {
                    trace!(%piece_index, %peer_id, "Got closest peers");

                    // Store closer peers in case piece index was not downloaded yet
                    if let Some(peers) = pieces_to_download.get_mut(&piece_index) {
                        peers.extend(Vec::from(closest_peers));
                    }

                    // No need to ask this peer again if they didn't have the piece we expected, or
                    // they claimed to have earlier
                    continue;
                }
            }

            let mut maybe_piece_index_to_download_next = None;
            // Clear useless entries in cached pieces and find something to download next
            cached_pieces.retain(|piece_index| {
                // Clear downloaded pieces
                if !pieces_to_download.contains_key(piece_index) {
                    return false;
                }

                // Try to pick a piece to download that is not being downloaded already
                if maybe_piece_index_to_download_next.is_none()
                    && !downloading_stream.contains_key(piece_index)
                {
                    maybe_piece_index_to_download_next.replace(*piece_index);
                    // We'll not need to download it after this attempt
                    return false;
                }

                // Retain everything else
                true
            });

            let piece_index_to_download_next =
                if let Some(piece_index) = maybe_piece_index_to_download_next {
                    trace!(%piece_index, %peer_id, "Next piece to download from peer");
                    piece_index
                } else {
                    trace!(%peer_id, "Peer doesn't have anything else");
                    // Nothing left to do with this peer
                    continue;
                };

            let fut = download_cached_piece_from_peer(
                node,
                piece_validator,
                peer_id,
                Vec::new(),
                // Sample more random cached piece indices for connected peer, algorithm can be
                // improved, but has to be something simple and this should do it for now
                Arc::new(
                    pieces_to_download
                        .keys()
                        // Do a bit of work to filter-out piece indices we already know remote peer
                        // has or doesn't to decrease burden on them
                        .filter_map(|piece_index| {
                            if piece_index == &piece_index_to_download_next
                                || cached_pieces.contains(piece_index)
                                || not_cached_pieces.contains(piece_index)
                            {
                                None
                            } else {
                                Some(*piece_index)
                            }
                        })
                        .choose_multiple(
                            &mut thread_rng(),
                            CachedPieceByIndexRequest::RECOMMENDED_LIMIT,
                        ),
                ),
                piece_index_to_download_next,
                cached_pieces,
                not_cached_pieces,
            );
            downloading_stream.insert(piece_index_to_download_next, Box::pin(fut.into_stream()));
        }

        if pieces_to_download.len() == num_pieces {
            debug!(%num_pieces, "Finished downloading from connected peers");
            // Nothing was downloaded, we're done here
            break;
        }
    }

    pieces_to_download
}

/// Takes pieces to download with potential peer candidates as an input, sends results with pieces
/// that were downloaded successfully and returns those that were not downloaded
async fn download_cached_pieces_from_closest_peers<PV>(
    maybe_pieces_to_download: HashMap<PieceIndex, HashMap<PeerId, Vec<Multiaddr>>>,
    node: &Node,
    piece_validator: &PV,
    results: &mpsc::UnboundedSender<(PieceIndex, Option<Piece>)>,
) where
    PV: PieceValidator,
{
    let kademlia = &Mutex::new(KademliaWrapper::new(node.id()));
    // Collection of pieces to download and already connected peers that claim to have them
    let connected_peers_with_piece = &Mutex::new(
        maybe_pieces_to_download
            .keys()
            .map(|&piece_index| (piece_index, HashSet::<PeerId>::new()))
            .collect::<HashMap<_, _>>(),
    );

    let mut downloaded_pieces = maybe_pieces_to_download
        .into_iter()
        .map(|(piece_index, collected_peers)| async move {
            let key = piece_index.to_multihash();
            let kbucket_key = KBucketKey::from(key);
            let mut checked_closest_peers = HashSet::<PeerId>::new();

            {
                let local_closest_peers = node
                    .get_closest_local_peers(key, None)
                    .await
                    .unwrap_or_default();
                let mut kademlia = kademlia.lock();

                for (peer_id, addresses) in collected_peers {
                    kademlia.add_peer(&peer_id, addresses);
                }
                for (peer_id, addresses) in local_closest_peers {
                    kademlia.add_peer(&peer_id, addresses);
                }
            }

            loop {
                // Collect pieces that still need to be downloaded and connected peers that claim to
                // have them
                let (pieces_to_download, connected_peers) = {
                    let mut connected_peers_with_piece = connected_peers_with_piece.lock();

                    (
                        Arc::new(
                            connected_peers_with_piece
                                .keys()
                                .filter(|&candidate| candidate != &piece_index)
                                .take(CachedPieceByIndexRequest::RECOMMENDED_LIMIT)
                                .copied()
                                .collect::<Vec<_>>(),
                        ),
                        connected_peers_with_piece
                            .get_mut(&piece_index)
                            .map(mem::take)
                            .unwrap_or_default(),
                    )
                };

                // Check connected peers that claim to have the piece index first
                for peer_id in connected_peers {
                    let fut = download_cached_piece_from_peer(
                        node,
                        piece_validator,
                        peer_id,
                        Vec::new(),
                        Arc::default(),
                        piece_index,
                        HashSet::new(),
                        HashSet::new(),
                    );

                    match fut.await.result {
                        Some(PieceResult::Piece(piece)) => {
                            return (piece_index, Some(piece));
                        }
                        Some(PieceResult::ClosestPeers(closest_peers)) => {
                            let mut kademlia = kademlia.lock();

                            // Store additional closest peers reported by the peer
                            for (peer_id, addresses) in Vec::from(closest_peers) {
                                kademlia.add_peer(&peer_id, addresses);
                            }
                        }
                        None => {
                            checked_closest_peers.insert(peer_id);
                        }
                    }
                }

                // Find the closest peers that were not queried yet
                let closest_peers_to_check = kademlia.lock().closest_peers(&kbucket_key);
                let closest_peers_to_check = closest_peers_to_check
                    .filter(|(peer_id, _addresses)| checked_closest_peers.insert(*peer_id))
                    .collect::<Vec<_>>();

                if closest_peers_to_check.is_empty() {
                    // No new closest peers found, nothing left to do here
                    break;
                }

                for (peer_id, addresses) in closest_peers_to_check {
                    let fut = download_cached_piece_from_peer(
                        node,
                        piece_validator,
                        peer_id,
                        addresses,
                        Arc::clone(&pieces_to_download),
                        piece_index,
                        HashSet::new(),
                        HashSet::new(),
                    );

                    let DownloadedPieceFromPeer {
                        peer_id: _,
                        result,
                        cached_pieces,
                        not_cached_pieces: _,
                    } = fut.await;

                    if !cached_pieces.is_empty() {
                        let mut connected_peers_with_piece = connected_peers_with_piece.lock();

                        // Remember that this peer has some pieces that need to be downloaded here
                        for cached_piece_index in cached_pieces {
                            if let Some(peers) =
                                connected_peers_with_piece.get_mut(&cached_piece_index)
                            {
                                peers.insert(peer_id);
                            }
                        }
                    }

                    match result {
                        Some(PieceResult::Piece(piece)) => {
                            return (piece_index, Some(piece));
                        }
                        Some(PieceResult::ClosestPeers(closest_peers)) => {
                            let mut kademlia = kademlia.lock();

                            // Store additional closest peers
                            for (peer_id, addresses) in Vec::from(closest_peers) {
                                kademlia.add_peer(&peer_id, addresses);
                            }
                        }
                        None => {
                            checked_closest_peers.insert(peer_id);
                        }
                    }
                }
            }

            (piece_index, None)
        })
        .collect::<FuturesUnordered<_>>();

    while let Some((piece_index, maybe_piece)) = downloaded_pieces.next().await {
        connected_peers_with_piece.lock().remove(&piece_index);

        results
            .unbounded_send((piece_index, maybe_piece))
            .expect("This future isn't polled after receiver is dropped; qed");
    }
}

struct DownloadedPieceFromPeer {
    peer_id: PeerId,
    result: Option<PieceResult>,
    cached_pieces: HashSet<PieceIndex>,
    not_cached_pieces: HashSet<PieceIndex>,
}

#[allow(clippy::too_many_arguments)]
async fn download_cached_piece_from_peer<PV>(
    node: &Node,
    piece_validator: &PV,
    peer_id: PeerId,
    addresses: Vec<Multiaddr>,
    peer_piece_indices: Arc<Vec<PieceIndex>>,
    piece_index: PieceIndex,
    mut cached_pieces: HashSet<PieceIndex>,
    mut not_cached_pieces: HashSet<PieceIndex>,
) -> DownloadedPieceFromPeer
where
    PV: PieceValidator,
{
    let result = match node
        .send_generic_request(
            peer_id,
            addresses,
            CachedPieceByIndexRequest {
                piece_index,
                cached_pieces: peer_piece_indices,
            },
        )
        .await
    {
        Ok(response) => {
            let CachedPieceByIndexResponse {
                result,
                cached_pieces,
            } = response;

            match result {
                PieceResult::Piece(piece) => piece_validator
                    .validate_piece(peer_id, piece_index, piece)
                    .await
                    .map(|piece| CachedPieceByIndexResponse {
                        result: PieceResult::Piece(piece),
                        cached_pieces,
                    }),
                PieceResult::ClosestPeers(closest_peers) => Some(CachedPieceByIndexResponse {
                    result: PieceResult::ClosestPeers(closest_peers),
                    cached_pieces,
                }),
            }
        }
        Err(error) => {
            debug!(%error, %peer_id, %piece_index, "Failed to download cached piece from peer");

            None
        }
    };

    match result {
        Some(result) => DownloadedPieceFromPeer {
            peer_id,
            result: Some(result.result),
            cached_pieces: {
                cached_pieces.extend(result.cached_pieces);
                cached_pieces
            },
            not_cached_pieces,
        },
        None => {
            not_cached_pieces.insert(piece_index);

            DownloadedPieceFromPeer {
                peer_id,
                result: None,
                cached_pieces,
                not_cached_pieces,
            }
        }
    }
}