Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

2 changes: 2 additions & 0 deletions crates/static-file/types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ strum = { workspace = true, features = ["derive"] }

[dev-dependencies]
reth-nippy-jar.workspace = true
serde_json.workspace = true

[features]
default = ["std"]
Expand All @@ -29,5 +30,6 @@ std = [
"derive_more/std",
"serde/std",
"strum/std",
"serde_json/std",
]
clap = ["dep:clap"]
43 changes: 37 additions & 6 deletions crates/static-file/types/src/segment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use alloy_primitives::TxNumber;
use core::{ops::RangeInclusive, str::FromStr};
use derive_more::Display;
use serde::{Deserialize, Serialize};
use strum::{AsRefStr, EnumIs, EnumString};
use strum::{EnumIs, EnumString};

#[derive(
Debug,
Expand All @@ -21,28 +21,27 @@ use strum::{AsRefStr, EnumIs, EnumString};
Deserialize,
Serialize,
EnumString,
AsRefStr,
Display,
EnumIs,
)]
#[strum(serialize_all = "kebab-case")]
#[cfg_attr(feature = "clap", derive(clap::ValueEnum))]
/// Segment of the data that can be moved to static files.
pub enum StaticFileSegment {
#[strum(serialize = "headers")]
/// Static File segment responsible for the `CanonicalHeaders`, `Headers`,
/// `HeaderTerminalDifficulties` tables.
Headers,
#[strum(serialize = "transactions")]
/// Static File segment responsible for the `Transactions` table.
Transactions,
#[strum(serialize = "receipts")]
/// Static File segment responsible for the `Receipts` table.
Receipts,
}

impl StaticFileSegment {
/// Returns the segment as a string.
pub const fn as_str(&self) -> &'static str {
// `strum` doesn't generate a doc comment for `into_str` when using `IntoStaticStr` derive
// macro, so we need to manually implement it.
match self {
Self::Headers => "headers",
Self::Transactions => "transactions",
Expand Down Expand Up @@ -73,7 +72,7 @@ impl StaticFileSegment {
pub fn filename(&self, block_range: &SegmentRangeInclusive) -> String {
// ATTENTION: if changing the name format, be sure to reflect those changes in
// [`Self::parse_filename`].
format!("static_file_{}_{}_{}", self.as_ref(), block_range.start(), block_range.end())
format!("static_file_{}_{}_{}", self.as_str(), block_range.start(), block_range.end())
}

/// Returns file name for the provided segment and range, alongside filters, compression.
Expand Down Expand Up @@ -473,4 +472,36 @@ mod tests {
);
}
}

/// Used in filename writing/parsing
#[test]
fn test_static_file_segment_str_roundtrip() {
for segment in StaticFileSegment::iter() {
let static_str = segment.as_str();
assert_eq!(StaticFileSegment::from_str(static_str).unwrap(), segment);

let expected_str = match segment {
StaticFileSegment::Headers => "headers",
StaticFileSegment::Transactions => "transactions",
StaticFileSegment::Receipts => "receipts",
};
assert_eq!(static_str, expected_str);
}
}

/// Used in segment headers serialize/deserialize
#[test]
fn test_static_file_segment_serde_roundtrip() {
for segment in StaticFileSegment::iter() {
let ser = serde_json::to_string(&segment).unwrap();
assert_eq!(serde_json::from_str::<StaticFileSegment>(&ser).unwrap(), segment);

let expected_str = match segment {
StaticFileSegment::Headers => "Headers",
StaticFileSegment::Transactions => "Transactions",
StaticFileSegment::Receipts => "Receipts",
};
assert_eq!(ser, format!("\"{expected_str}\""));
}
}
}