Skip to content

Commit

Permalink
Fix bug in download_pieces()
Browse files Browse the repository at this point in the history
  • Loading branch information
teor2345 committed Nov 25, 2024
1 parent d163158 commit d12935d
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions shared/subspace-data-retrieval/src/piece_fetcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use crate::object_fetcher::Error;
use crate::piece_getter::ObjectPieceGetter;
use futures::{StreamExt, TryStreamExt};
use futures::StreamExt;
use subspace_core_primitives::pieces::{Piece, PieceIndex};
use tracing::{debug, trace};

Expand All @@ -41,24 +41,31 @@ where
);

// TODO:
// - if we're close to the number of pieces in a segment, use segment downloading and piece
// - if we're close to the number of pieces in a segment, or we can't find a piece, use segment downloading and piece
// reconstruction instead
// Currently most objects are limited to 4 pieces, so this isn't needed yet.
let received_pieces = piece_getter
let mut received_pieces = piece_getter
.get_pieces(piece_indexes.iter().copied())
.await?;

// We want exact pieces, so any errors are fatal.
let received_pieces: Vec<Piece> = received_pieces
.map(|(piece_index, maybe_piece)| maybe_piece?.ok_or(Error::PieceNotFound { piece_index }))
.try_collect()
.await?;
let mut pieces = Vec::new();
pieces.resize(piece_indexes.len(), Piece::default());

while let Some((piece_index, maybe_piece)) = received_pieces.next().await {
// We want exact pieces, so any errors are fatal.
let piece = maybe_piece?.ok_or(Error::PieceNotFound { piece_index })?;
let index_position = piece_indexes
.iter()
.position(|i| *i == piece_index)
.expect("get_pieces only returns indexes it was supplied; qed");
pieces[index_position] = piece;
}

trace!(
count = piece_indexes.len(),
?piece_indexes,
"Successfully retrieved exact pieces"
);

Ok(received_pieces)
Ok(pieces)
}

0 comments on commit d12935d

Please sign in to comment.