use crate::{BlockNumber, Compression};
use alloy_primitives::TxNumber;
use derive_more::Display;
use serde::{Deserialize, Serialize};
use std::{ops::RangeInclusive, str::FromStr};
use strum::{AsRefStr, EnumIter, EnumString};
#[derive(
Debug,
Copy,
Clone,
Eq,
PartialEq,
Hash,
Ord,
PartialOrd,
Deserialize,
Serialize,
EnumString,
EnumIter,
AsRefStr,
Display,
)]
#[cfg_attr(feature = "clap", derive(clap::ValueEnum))]
pub enum StaticFileSegment {
#[strum(serialize = "headers")]
Headers,
#[strum(serialize = "transactions")]
Transactions,
#[strum(serialize = "receipts")]
Receipts,
}
impl StaticFileSegment {
pub const fn as_str(&self) -> &'static str {
match self {
Self::Headers => "headers",
Self::Transactions => "transactions",
Self::Receipts => "receipts",
}
}
pub const fn config(&self) -> SegmentConfig {
SegmentConfig { compression: Compression::Lz4 }
}
pub const fn columns(&self) -> usize {
match self {
Self::Headers => 3,
Self::Transactions | Self::Receipts => 1,
}
}
pub fn filename(&self, block_range: &SegmentRangeInclusive) -> String {
format!("static_file_{}_{}_{}", self.as_ref(), block_range.start(), block_range.end())
}
pub fn filename_with_configuration(
&self,
compression: Compression,
block_range: &SegmentRangeInclusive,
) -> String {
let prefix = self.filename(block_range);
let filters_name = "none".to_string();
format!("{prefix}_{}_{}", filters_name, compression.as_ref())
}
pub fn parse_filename(name: &str) -> Option<(Self, SegmentRangeInclusive)> {
let mut parts = name.split('_');
if !(parts.next() == Some("static") && parts.next() == Some("file")) {
return None
}
let segment = Self::from_str(parts.next()?).ok()?;
let (block_start, block_end) = (parts.next()?.parse().ok()?, parts.next()?.parse().ok()?);
if block_start > block_end {
return None
}
Some((segment, SegmentRangeInclusive::new(block_start, block_end)))
}
pub const fn is_headers(&self) -> bool {
matches!(self, Self::Headers)
}
pub const fn is_receipts(&self) -> bool {
matches!(self, Self::Receipts)
}
pub const fn is_tx_based(&self) -> bool {
matches!(self, Self::Receipts | Self::Transactions)
}
}
#[derive(Debug, Serialize, Deserialize, Eq, PartialEq, Hash, Clone)]
pub struct SegmentHeader {
expected_block_range: SegmentRangeInclusive,
block_range: Option<SegmentRangeInclusive>,
tx_range: Option<SegmentRangeInclusive>,
segment: StaticFileSegment,
}
impl SegmentHeader {
pub const fn new(
expected_block_range: SegmentRangeInclusive,
block_range: Option<SegmentRangeInclusive>,
tx_range: Option<SegmentRangeInclusive>,
segment: StaticFileSegment,
) -> Self {
Self { expected_block_range, block_range, tx_range, segment }
}
pub const fn segment(&self) -> StaticFileSegment {
self.segment
}
pub const fn block_range(&self) -> Option<&SegmentRangeInclusive> {
self.block_range.as_ref()
}
pub const fn tx_range(&self) -> Option<&SegmentRangeInclusive> {
self.tx_range.as_ref()
}
pub const fn expected_block_start(&self) -> BlockNumber {
self.expected_block_range.start()
}
pub const fn expected_block_end(&self) -> BlockNumber {
self.expected_block_range.end()
}
pub fn block_start(&self) -> Option<BlockNumber> {
self.block_range.as_ref().map(|b| b.start())
}
pub fn block_end(&self) -> Option<BlockNumber> {
self.block_range.as_ref().map(|b| b.end())
}
pub fn tx_start(&self) -> Option<TxNumber> {
self.tx_range.as_ref().map(|t| t.start())
}
pub fn tx_end(&self) -> Option<TxNumber> {
self.tx_range.as_ref().map(|t| t.end())
}
pub fn tx_len(&self) -> Option<u64> {
self.tx_range.as_ref().map(|r| (r.end() + 1) - r.start())
}
pub fn block_len(&self) -> Option<u64> {
self.block_range.as_ref().map(|r| (r.end() + 1) - r.start())
}
pub fn increment_block(&mut self) -> BlockNumber {
if let Some(block_range) = &mut self.block_range {
block_range.end += 1;
block_range.end
} else {
self.block_range = Some(SegmentRangeInclusive::new(
self.expected_block_start(),
self.expected_block_start(),
));
self.expected_block_start()
}
}
pub fn increment_tx(&mut self) {
match self.segment {
StaticFileSegment::Headers => (),
StaticFileSegment::Transactions | StaticFileSegment::Receipts => {
if let Some(tx_range) = &mut self.tx_range {
tx_range.end += 1;
} else {
self.tx_range = Some(SegmentRangeInclusive::new(0, 0));
}
}
}
}
pub fn prune(&mut self, num: u64) {
match self.segment {
StaticFileSegment::Headers => {
if let Some(range) = &mut self.block_range {
if num > range.end - range.start {
self.block_range = None;
} else {
range.end = range.end.saturating_sub(num);
}
};
}
StaticFileSegment::Transactions | StaticFileSegment::Receipts => {
if let Some(range) = &mut self.tx_range {
if num > range.end - range.start {
self.tx_range = None;
} else {
range.end = range.end.saturating_sub(num);
}
};
}
};
}
pub fn set_block_range(&mut self, block_start: BlockNumber, block_end: BlockNumber) {
if let Some(block_range) = &mut self.block_range {
block_range.start = block_start;
block_range.end = block_end;
} else {
self.block_range = Some(SegmentRangeInclusive::new(block_start, block_end))
}
}
pub fn set_tx_range(&mut self, tx_start: TxNumber, tx_end: TxNumber) {
if let Some(tx_range) = &mut self.tx_range {
tx_range.start = tx_start;
tx_range.end = tx_end;
} else {
self.tx_range = Some(SegmentRangeInclusive::new(tx_start, tx_end))
}
}
pub fn start(&self) -> Option<u64> {
match self.segment {
StaticFileSegment::Headers => self.block_start(),
StaticFileSegment::Transactions | StaticFileSegment::Receipts => self.tx_start(),
}
}
}
#[derive(Debug, Clone, Copy)]
pub struct SegmentConfig {
pub compression: Compression,
}
#[derive(Debug, Serialize, Deserialize, Eq, PartialEq, Hash, Clone, Copy)]
pub struct SegmentRangeInclusive {
start: u64,
end: u64,
}
impl SegmentRangeInclusive {
pub const fn new(start: u64, end: u64) -> Self {
Self { start, end }
}
pub const fn start(&self) -> u64 {
self.start
}
pub const fn end(&self) -> u64 {
self.end
}
}
impl std::fmt::Display for SegmentRangeInclusive {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}..={}", self.start, self.end)
}
}
impl From<RangeInclusive<u64>> for SegmentRangeInclusive {
fn from(value: RangeInclusive<u64>) -> Self {
Self { start: *value.start(), end: *value.end() }
}
}
impl From<&SegmentRangeInclusive> for RangeInclusive<u64> {
fn from(value: &SegmentRangeInclusive) -> Self {
value.start()..=value.end()
}
}
impl From<SegmentRangeInclusive> for RangeInclusive<u64> {
fn from(value: SegmentRangeInclusive) -> Self {
(&value).into()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_filename() {
let test_vectors = [
(StaticFileSegment::Headers, 2..=30, "static_file_headers_2_30", None),
(StaticFileSegment::Receipts, 30..=300, "static_file_receipts_30_300", None),
(
StaticFileSegment::Transactions,
1_123_233..=11_223_233,
"static_file_transactions_1123233_11223233",
None,
),
(
StaticFileSegment::Headers,
2..=30,
"static_file_headers_2_30_none_lz4",
Some(Compression::Lz4),
),
(
StaticFileSegment::Headers,
2..=30,
"static_file_headers_2_30_none_zstd",
Some(Compression::Zstd),
),
(
StaticFileSegment::Headers,
2..=30,
"static_file_headers_2_30_none_zstd-dict",
Some(Compression::ZstdWithDictionary),
),
];
for (segment, block_range, filename, compression) in test_vectors {
let block_range: SegmentRangeInclusive = block_range.into();
if let Some(compression) = compression {
assert_eq!(
segment.filename_with_configuration(compression, &block_range),
filename
);
} else {
assert_eq!(segment.filename(&block_range), filename);
}
assert_eq!(StaticFileSegment::parse_filename(filename), Some((segment, block_range)));
}
assert_eq!(StaticFileSegment::parse_filename("static_file_headers_2"), None);
assert_eq!(StaticFileSegment::parse_filename("static_file_headers_"), None);
}
}