Skip to content

Commit

Permalink
test_segment_config_backwards
Browse files Browse the repository at this point in the history
  • Loading branch information
joshieDo committed Dec 13, 2024
1 parent 138004b commit 74fbd94
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 2 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

5 changes: 4 additions & 1 deletion crates/static-file/types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
clap = ["dep:clap"]
47 changes: 47 additions & 0 deletions crates/static-file/types/src/segment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,8 @@ impl From<SegmentRangeInclusive> for RangeInclusive<u64> {
#[cfg(test)]
mod tests {
use super::*;
use alloy_primitives::hex;
use reth_nippy_jar::NippyJar;
use strum::IntoEnumIterator;

#[test]
Expand Down Expand Up @@ -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::<SegmentHeader>::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::<SegmentHeader>::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::<SegmentHeader>::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()
);
}
}
}
8 changes: 7 additions & 1 deletion crates/storage/nippy-jar/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use serde::{Deserialize, Serialize};
use std::{
error::Error as StdError,
fs::File,
io::Read,
ops::Range,
path::{Path, PathBuf},
};
Expand Down Expand Up @@ -201,11 +202,16 @@ impl<H: NippyJarHeader> NippyJar<H> {
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<R: Read>(reader: R) -> Result<Self, NippyJarError> {
Ok(bincode::deserialize_from(reader)?)
}

/// Returns the path for the data file
pub fn data_path(&self) -> &Path {
self.path.as_ref()
Expand Down

0 comments on commit 74fbd94

Please sign in to comment.