Skip to content

Commit

Permalink
Merge pull request #3062 from autonomys/piece-download-refactor
Browse files Browse the repository at this point in the history
Refactor piece and segment downloading into subspace-data-retrieval
  • Loading branch information
teor2345 authored Oct 1, 2024
2 parents 6b049cf + dd0b98f commit fe64ec3
Show file tree
Hide file tree
Showing 9 changed files with 476 additions and 177 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 23 additions & 10 deletions crates/subspace-archiving/src/reconstructor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,13 @@ impl Reconstructor {

/// Given a set of pieces of a segment of the archived history (any half of all pieces are
/// required to be present, the rest will be recovered automatically due to use of erasure
/// coding if needed), reconstructs and returns segment header and a list of encoded blocks with
/// corresponding block numbers.
/// coding if needed), reconstructs and returns the segment itself.
///
/// It is possible to start with any segment, but when next segment is pushed, it needs to
/// follow the previous one or else error will be returned.
pub fn add_segment(
&mut self,
/// Does not modify the internal state of the reconstructor.
pub fn reconstruct_segment(
&self,
segment_pieces: &[Option<Piece>],
) -> Result<ReconstructedContents, ReconstructorError> {
) -> Result<Segment, ReconstructorError> {
let mut segment_data = RecordedHistorySegment::new_boxed();

if !segment_pieces
Expand Down Expand Up @@ -151,9 +149,24 @@ impl Reconstructor {
}
}

let Segment::V0 { items } =
Segment::decode(&mut AsRef::<[u8]>::as_ref(segment_data.as_ref()))
.map_err(ReconstructorError::SegmentDecoding)?;
let segment = Segment::decode(&mut AsRef::<[u8]>::as_ref(segment_data.as_ref()))
.map_err(ReconstructorError::SegmentDecoding)?;

Ok(segment)
}

/// Given a set of pieces of a segment of the archived history (any half of all pieces are
/// required to be present, the rest will be recovered automatically due to use of erasure
/// coding if needed), reconstructs and returns segment header and a list of encoded blocks with
/// corresponding block numbers.
///
/// It is possible to start with any segment, but when next segment is pushed, it needs to
/// follow the previous one or else error will be returned.
pub fn add_segment(
&mut self,
segment_pieces: &[Option<Piece>],
) -> Result<ReconstructedContents, ReconstructorError> {
let Segment::V0 { items } = self.reconstruct_segment(segment_pieces)?;

let mut reconstructed_contents = ReconstructedContents::default();
let mut next_block_number = 0;
Expand Down
1 change: 1 addition & 0 deletions crates/subspace-service/src/sync_from_dsn/import_blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ where
Ok(imported_blocks)
}

/// Downloads and reconstructs blocks from a DSN segment, by concurrently downloading its pieces.
pub(super) async fn download_and_reconstruct_blocks<PG>(
segment_index: SegmentIndex,
piece_getter: &PG,
Expand Down
3 changes: 3 additions & 0 deletions shared/subspace-data-retrieval/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@ include = [
]

[dependencies]
async-lock = "3.3.0"
async-trait = "0.1.83"
futures = "0.3.29"
parity-scale-codec = { version = "3.6.12", features = ["derive"] }
subspace-archiving = { version = "0.1.0", path = "../../crates/subspace-archiving" }
subspace-core-primitives = { version = "0.1.0", path = "../../crates/subspace-core-primitives" }
subspace-erasure-coding = { version = "0.1.0", path = "../../crates/subspace-erasure-coding" }
thiserror = "1.0.63"
tokio = { version = "1.39.2", features = ["sync"] }
tracing = "0.1.40"

[dev-dependencies]
Expand Down
1 change: 1 addition & 0 deletions shared/subspace-data-retrieval/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@

pub mod object_fetcher;
pub mod piece_fetcher;
pub mod piece_getter;
pub mod segment_fetcher;
Loading

0 comments on commit fe64ec3

Please sign in to comment.