diff --git a/Cargo.lock b/Cargo.lock index a70018cd9ef2..36e06083bd8c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -9393,6 +9393,7 @@ dependencies = [ "alloy-primitives", "clap", "derive_more", + "reth-nippy-jar", "serde", "strum", ] diff --git a/crates/static-file/types/Cargo.toml b/crates/static-file/types/Cargo.toml index 63ba40c8f525..f3377698b303 100644 --- a/crates/static-file/types/Cargo.toml +++ b/crates/static-file/types/Cargo.toml @@ -19,5 +19,8 @@ derive_more.workspace = true serde = { workspace = true, features = ["derive"] } strum = { workspace = true, features = ["derive"] } +[dev-dependencies] +reth-nippy-jar.workspace = true + [features] -clap = ["dep:clap"] \ No newline at end of file +clap = ["dep:clap"] diff --git a/crates/static-file/types/src/segment.rs b/crates/static-file/types/src/segment.rs index f785d83531fa..8f04ecf3ecf3 100644 --- a/crates/static-file/types/src/segment.rs +++ b/crates/static-file/types/src/segment.rs @@ -353,6 +353,8 @@ impl From for RangeInclusive { #[cfg(test)] mod tests { use super::*; + use alloy_primitives::hex; + use reth_nippy_jar::NippyJar; use strum::IntoEnumIterator; #[test] @@ -410,4 +412,49 @@ mod tests { assert_eq!(Some((segment, dummy_range)), StaticFileSegment::parse_filename(&filename)); } } + + #[test] + fn test_segment_header_backwards() { + let headers = hex!("010000000000000000000000000000001fa10700000000000100000000000000001fa10700000000000000000000030000000000000020a107000000000001010000004a02000000000000"); + let transactions = hex!("010000000000000000000000000000001fa10700000000000100000000000000001fa107000000000001000000000000000034a107000000000001000000010000000000000035a1070000000000004010000000000000"); + let receipts = hex!("010000000000000000000000000000001fa10700000000000100000000000000000000000000000000000200000001000000000000000000000000000000000000000000000000"); + + { + let headers = NippyJar::::load_from_reader(&headers[..]).unwrap(); + assert_eq!( + &SegmentHeader { + expected_block_range: SegmentRangeInclusive::new(0, 499999), + block_range: Some(SegmentRangeInclusive::new(0, 499999)), + tx_range: None, + segment: StaticFileSegment::Headers, + }, + headers.user_header() + ); + } + { + let transactions = + NippyJar::::load_from_reader(&transactions[..]).unwrap(); + assert_eq!( + &SegmentHeader { + expected_block_range: SegmentRangeInclusive::new(0, 499999), + block_range: Some(SegmentRangeInclusive::new(0, 499999)), + tx_range: Some(SegmentRangeInclusive::new(0, 1903501)), + segment: StaticFileSegment::Transactions, + }, + transactions.user_header() + ); + } + { + let receipts = NippyJar::::load_from_reader(&receipts[..]).unwrap(); + assert_eq!( + &SegmentHeader { + expected_block_range: SegmentRangeInclusive::new(0, 499999), + block_range: Some(SegmentRangeInclusive::new(0, 0)), + tx_range: None, + segment: StaticFileSegment::Receipts, + }, + receipts.user_header() + ); + } + } } diff --git a/crates/storage/nippy-jar/src/lib.rs b/crates/storage/nippy-jar/src/lib.rs index 98eddf22ee96..c6546af89bd4 100644 --- a/crates/storage/nippy-jar/src/lib.rs +++ b/crates/storage/nippy-jar/src/lib.rs @@ -17,6 +17,7 @@ use serde::{Deserialize, Serialize}; use std::{ error::Error as StdError, fs::File, + io::Read, ops::Range, path::{Path, PathBuf}, }; @@ -201,11 +202,16 @@ impl NippyJar { let config_file = File::open(&config_path) .map_err(|err| reth_fs_util::FsPathError::open(err, config_path))?; - let mut obj: Self = bincode::deserialize_from(&config_file)?; + let mut obj = Self::load_from_reader(config_file)?; obj.path = path.to_path_buf(); Ok(obj) } + /// Deserializes an instance of [`Self`] from a [`Read`] type. + pub fn load_from_reader(reader: R) -> Result { + Ok(bincode::deserialize_from(reader)?) + } + /// Returns the path for the data file pub fn data_path(&self) -> &Path { self.path.as_ref()