Skip to main content

reth_static_file_types/
segment.rs

1use crate::{find_fixed_range, BlockNumber, Compression};
2use alloc::{format, string::String};
3use alloy_primitives::TxNumber;
4use core::{
5    ops::{Range, RangeInclusive},
6    str::FromStr,
7};
8use reth_stages_types::StageId;
9use serde::{de::Visitor, ser::SerializeStruct, Deserialize, Deserializer, Serialize, Serializer};
10use strum::{EnumIs, EnumString};
11
12#[derive(
13    Debug,
14    Copy,
15    Clone,
16    Eq,
17    PartialEq,
18    Hash,
19    Ord,
20    PartialOrd,
21    EnumString,
22    derive_more::Display,
23    EnumIs,
24    Serialize,
25    Deserialize,
26    fixed_map::Key,
27)]
28#[strum(serialize_all = "kebab-case")]
29#[cfg_attr(feature = "clap", derive(clap::ValueEnum))]
30/// Segment of the data that can be moved to static files.
31pub enum StaticFileSegment {
32    /// Static File segment responsible for the `CanonicalHeaders`, `Headers`,
33    /// `HeaderTerminalDifficulties` tables.
34    Headers,
35    /// Static File segment responsible for the `Transactions` table.
36    Transactions,
37    /// Static File segment responsible for the `Receipts` table.
38    Receipts,
39    /// Static File segment responsible for the `TransactionSenders` table.
40    TransactionSenders,
41    /// Static File segment responsible for the `AccountChangeSets` table.
42    ///
43    /// Account changeset static files append block-by-block changesets sorted by address.
44    /// For example, a changeset static file for three blocks, with two changes each, would be
45    /// organized with six rows, as follows:
46    ///
47    /// Block 1:
48    /// * address 0xaa, account info
49    /// * address 0xbb, account info
50    ///
51    /// Block 2:
52    /// * address 0xaa, account info
53    /// * address 0xcc, account info
54    ///
55    /// Block 3:
56    /// * address 0xbb, account info
57    /// * address 0xcc, account info
58    AccountChangeSets,
59    /// Static File segment responsible for the `StorageChangeSets` table.
60    ///
61    /// Storage changeset static files append block-by-block changesets sorted by address and
62    /// storage slot.
63    StorageChangeSets,
64}
65
66impl StaticFileSegment {
67    /// Returns a string representation of the segment.
68    pub const fn as_str(&self) -> &'static str {
69        // `strum` doesn't generate a doc comment for `into_str` when using `IntoStaticStr` derive
70        // macro, so we need to manually implement it.
71        //
72        // NOTE: this name cannot have underscores in it, as underscores are used as delimiters in
73        // static file paths, for fetching static files for a specific block range
74        match self {
75            Self::Headers => "headers",
76            Self::Transactions => "transactions",
77            Self::Receipts => "receipts",
78            Self::TransactionSenders => "transaction-senders",
79            Self::AccountChangeSets => "account-change-sets",
80            Self::StorageChangeSets => "storage-change-sets",
81        }
82    }
83
84    /// Returns a short string representation of the segment.
85    ///
86    /// Linux threads have a length limit of 15 characters: <https://man7.org/linux/man-pages/man3/pthread_setname_np.3.html#DESCRIPTION>.
87    pub const fn as_short_str(&self) -> &'static str {
88        match self {
89            Self::Headers => "headers",
90            Self::Transactions => "transactions",
91            Self::Receipts => "receipts",
92            Self::TransactionSenders => "tx-senders",
93            Self::AccountChangeSets => "account-changes",
94            Self::StorageChangeSets => "storage-changes",
95        }
96    }
97
98    /// Returns an iterator over all segments.
99    pub fn iter() -> impl Iterator<Item = Self> {
100        // The order of segments is significant and must be maintained to ensure correctness.
101        [
102            Self::Headers,
103            Self::Transactions,
104            Self::Receipts,
105            Self::TransactionSenders,
106            Self::AccountChangeSets,
107            Self::StorageChangeSets,
108        ]
109        .into_iter()
110    }
111
112    /// Returns the default configuration of the segment.
113    pub const fn config(&self) -> SegmentConfig {
114        SegmentConfig { compression: Compression::Lz4 }
115    }
116
117    /// Returns the number of columns for the segment
118    pub const fn columns(&self) -> usize {
119        match self {
120            Self::Headers => 3,
121            Self::Transactions |
122            Self::Receipts |
123            Self::TransactionSenders |
124            Self::AccountChangeSets |
125            Self::StorageChangeSets => 1,
126        }
127    }
128
129    /// Returns the default file name for the provided segment and range.
130    pub fn filename(&self, block_range: &SegmentRangeInclusive) -> String {
131        // ATTENTION: if changing the name format, be sure to reflect those changes in
132        // [`Self::parse_filename`].
133        format!("static_file_{}_{}_{}", self.as_str(), block_range.start(), block_range.end())
134    }
135
136    /// Returns file name for the provided segment and range, alongside filters, compression.
137    pub fn filename_with_configuration(
138        &self,
139        compression: Compression,
140        block_range: &SegmentRangeInclusive,
141    ) -> String {
142        let prefix = self.filename(block_range);
143
144        let filters_name = "none";
145
146        // ATTENTION: if changing the name format, be sure to reflect those changes in
147        // [`Self::parse_filename`.]
148        format!("{prefix}_{}_{}", filters_name, compression.as_ref())
149    }
150
151    /// Parses a filename into a `StaticFileSegment` and its expected block range.
152    ///
153    /// The filename is expected to follow the format:
154    /// "`static_file`_{segment}_{`block_start`}_{`block_end`}". This function checks
155    /// for the correct prefix ("`static_file`"), and then parses the segment and the inclusive
156    /// ranges for blocks. It ensures that the start of each range is less than or equal to the
157    /// end.
158    ///
159    /// # Returns
160    /// - `Some((segment, block_range))` if parsing is successful and all conditions are met.
161    /// - `None` if any condition fails, such as an incorrect prefix, parsing error, or invalid
162    ///   range.
163    ///
164    /// # Note
165    /// This function is tightly coupled with the naming convention defined in [`Self::filename`].
166    /// Any changes in the filename format in `filename` should be reflected here.
167    pub fn parse_filename(name: &str) -> Option<(Self, SegmentRangeInclusive)> {
168        let mut parts = name.split('_');
169        if !(parts.next() == Some("static") && parts.next() == Some("file")) {
170            return None
171        }
172
173        let segment = Self::from_str(parts.next()?).ok()?;
174        let (block_start, block_end) = (parts.next()?.parse().ok()?, parts.next()?.parse().ok()?);
175
176        if block_start > block_end {
177            return None
178        }
179
180        Some((segment, SegmentRangeInclusive::new(block_start, block_end)))
181    }
182
183    /// Returns `true` if a segment row is linked to a transaction.
184    pub const fn is_tx_based(&self) -> bool {
185        match self {
186            Self::Receipts | Self::Transactions | Self::TransactionSenders => true,
187            Self::Headers | Self::AccountChangeSets | Self::StorageChangeSets => false,
188        }
189    }
190
191    /// Returns `true` if the segment is change-based.
192    pub const fn is_change_based(&self) -> bool {
193        match self {
194            Self::AccountChangeSets | Self::StorageChangeSets => true,
195            Self::Receipts | Self::Transactions | Self::Headers | Self::TransactionSenders => false,
196        }
197    }
198
199    /// Returns `true` if a segment row is linked to a block.
200    pub const fn is_block_based(&self) -> bool {
201        match self {
202            Self::Headers => true,
203            Self::Receipts |
204            Self::Transactions |
205            Self::TransactionSenders |
206            Self::AccountChangeSets |
207            Self::StorageChangeSets => false,
208        }
209    }
210
211    /// Returns `true` if the segment is either block or change based. This should be used to ensure
212    /// that the user header contains a block range or a max block
213    pub const fn is_block_or_change_based(&self) -> bool {
214        self.is_block_based() || self.is_change_based()
215    }
216
217    /// Maps this segment to the [`StageId`] responsible for it.
218    pub const fn to_stage_id(&self) -> StageId {
219        match self {
220            Self::Headers => StageId::Headers,
221            Self::Transactions => StageId::Bodies,
222            Self::Receipts | Self::AccountChangeSets | Self::StorageChangeSets => {
223                StageId::Execution
224            }
225            Self::TransactionSenders => StageId::SenderRecovery,
226        }
227    }
228}
229
230/// A changeset offset, also with the number of elements in the offset for convenience
231#[derive(Debug, Serialize, Deserialize, Eq, PartialEq, Hash, Clone)]
232pub struct ChangesetOffset {
233    /// Offset for the row for this block
234    offset: u64,
235
236    /// Number of changes in this changeset
237    num_changes: u64,
238}
239
240impl ChangesetOffset {
241    /// Creates a new changeset offset.
242    pub const fn new(offset: u64, num_changes: u64) -> Self {
243        Self { offset, num_changes }
244    }
245
246    /// Returns the start offset for the row for this block
247    pub const fn offset(&self) -> u64 {
248        self.offset
249    }
250
251    /// Returns the number of changes in this changeset
252    pub const fn num_changes(&self) -> u64 {
253        self.num_changes
254    }
255
256    /// Returns a range corresponding to the changes.
257    pub const fn changeset_range(&self) -> Range<u64> {
258        self.offset..(self.offset + self.num_changes)
259    }
260
261    /// Increments the number of changes by 1.
262    pub const fn increment_num_changes(&mut self) {
263        self.num_changes += 1;
264    }
265}
266
267/// A segment header that contains information common to all segments. Used for storage.
268#[derive(Debug, Eq, PartialEq, Hash, Clone)]
269pub struct SegmentHeader {
270    /// Defines the expected block range for a static file segment. This attribute is crucial for
271    /// scenarios where the file contains no data, allowing for a representation beyond a
272    /// simple `start..=start` range. It ensures clarity in differentiating between an empty file
273    /// and a file with a single block numbered 0.
274    expected_block_range: SegmentRangeInclusive,
275    /// Block range of data on the static file segment
276    block_range: Option<SegmentRangeInclusive>,
277    /// Transaction range of data of the static file segment
278    tx_range: Option<SegmentRangeInclusive>,
279    /// Segment type
280    segment: StaticFileSegment,
281    /// List of offsets, for where each block's changeset starts.
282    changeset_offsets_len: u64,
283}
284
285struct SegmentHeaderVisitor;
286
287impl<'de> Visitor<'de> for SegmentHeaderVisitor {
288    type Value = SegmentHeader;
289
290    fn expecting(&self, formatter: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
291        formatter.write_str("a header struct with 4 or 5 fields")
292    }
293
294    fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
295    where
296        A: serde::de::SeqAccess<'de>,
297    {
298        // First 4 fields are always present in both old and new format
299        let expected_block_range =
300            seq.next_element()?.ok_or_else(|| serde::de::Error::invalid_length(0, &self))?;
301
302        let block_range =
303            seq.next_element()?.ok_or_else(|| serde::de::Error::invalid_length(1, &self))?;
304
305        let tx_range =
306            seq.next_element()?.ok_or_else(|| serde::de::Error::invalid_length(2, &self))?;
307
308        let segment: StaticFileSegment =
309            seq.next_element()?.ok_or_else(|| serde::de::Error::invalid_length(3, &self))?;
310
311        let changeset_offsets_len = if segment.is_change_based() {
312            // Try to read the 5th field (changeset_offsets_len)
313            match seq.next_element::<u64>()? {
314                Some(len) => len,
315                None => {
316                    return Err(serde::de::Error::custom(
317                        "changeset_offsets_len should exist for changeset static files",
318                    ))
319                }
320            }
321        } else {
322            0
323        };
324
325        Ok(SegmentHeader {
326            expected_block_range,
327            block_range,
328            tx_range,
329            segment,
330            changeset_offsets_len,
331        })
332    }
333}
334
335impl<'de> Deserialize<'de> for SegmentHeader {
336    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
337    where
338        D: Deserializer<'de>,
339    {
340        // Tell the deserializer we're expecting a struct
341        // The field names are for formats that use them
342        // Bincode ignores these and just uses the sequence order
343        const FIELDS: &[&str] =
344            &["expected_block_range", "block_range", "tx_range", "segment", "changeset_offsets"];
345
346        deserializer.deserialize_struct("SegmentHeader", FIELDS, SegmentHeaderVisitor)
347    }
348}
349
350impl Serialize for SegmentHeader {
351    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
352    where
353        S: Serializer,
354    {
355        // We serialize an extra field, the changeset offsets, for change-based segments
356        let len = if self.segment.is_change_based() { 5 } else { 4 };
357
358        let mut state = serializer.serialize_struct("SegmentHeader", len)?;
359        state.serialize_field("expected_block_range", &self.expected_block_range)?;
360        state.serialize_field("block_range", &self.block_range)?;
361        state.serialize_field("tx_range", &self.tx_range)?;
362        state.serialize_field("segment", &self.segment)?;
363
364        if self.segment.is_change_based() {
365            state.serialize_field("changeset_offsets", &self.changeset_offsets_len)?;
366        }
367
368        state.end()
369    }
370}
371
372impl SegmentHeader {
373    /// Returns [`SegmentHeader`].
374    pub const fn new(
375        expected_block_range: SegmentRangeInclusive,
376        block_range: Option<SegmentRangeInclusive>,
377        tx_range: Option<SegmentRangeInclusive>,
378        segment: StaticFileSegment,
379    ) -> Self {
380        Self { expected_block_range, block_range, tx_range, segment, changeset_offsets_len: 0 }
381    }
382
383    /// Returns the static file segment kind.
384    pub const fn segment(&self) -> StaticFileSegment {
385        self.segment
386    }
387
388    /// Returns the expected block range.
389    pub const fn expected_block_range(&self) -> SegmentRangeInclusive {
390        self.expected_block_range
391    }
392
393    /// Returns the block range.
394    pub const fn block_range(&self) -> Option<SegmentRangeInclusive> {
395        self.block_range
396    }
397
398    /// Returns the transaction range.
399    pub const fn tx_range(&self) -> Option<SegmentRangeInclusive> {
400        self.tx_range
401    }
402
403    /// Returns the number of changeset offset entries.
404    /// The actual offsets are stored in the `.csoff` sidecar file.
405    pub const fn changeset_offsets_len(&self) -> u64 {
406        self.changeset_offsets_len
407    }
408
409    /// Sets the changeset offsets length.
410    pub const fn set_changeset_offsets_len(&mut self, len: u64) {
411        self.changeset_offsets_len = len;
412    }
413
414    /// Increments the changeset offsets length by 1.
415    pub const fn increment_changeset_offsets_len(&mut self) {
416        self.changeset_offsets_len += 1;
417    }
418
419    /// The expected block start of the segment.
420    pub const fn expected_block_start(&self) -> BlockNumber {
421        self.expected_block_range.start()
422    }
423
424    /// Sets the expected block start of the segment, using the file boundary end
425    /// from `find_fixed_range`.
426    ///
427    /// This is useful for non-zero genesis blocks where the actual starting block
428    /// differs from the file range start determined by `find_fixed_range`.
429    /// For example, if `blocks_per_file` is 500 and genesis is at 502, the range
430    /// becomes 502..=999 (start at genesis, end at file boundary). The file keeps the name of
431    /// its fixed range (500..=999), and its provider indexes it by that name.
432    pub const fn set_expected_block_start(&mut self, block: BlockNumber) {
433        let blocks_per_file =
434            self.expected_block_range.end() - self.expected_block_range.start() + 1;
435        let file_range = find_fixed_range(block, blocks_per_file);
436        self.expected_block_range = SegmentRangeInclusive::new(block, file_range.end());
437    }
438
439    /// The expected block end of the segment.
440    pub const fn expected_block_end(&self) -> BlockNumber {
441        self.expected_block_range.end()
442    }
443
444    /// Returns the first block number of the segment.
445    pub fn block_start(&self) -> Option<BlockNumber> {
446        self.block_range.as_ref().map(|b| b.start())
447    }
448
449    /// Returns the last block number of the segment.
450    pub fn block_end(&self) -> Option<BlockNumber> {
451        self.block_range.as_ref().map(|b| b.end())
452    }
453
454    /// Returns the first transaction number of the segment.
455    pub fn tx_start(&self) -> Option<TxNumber> {
456        self.tx_range.as_ref().map(|t| t.start())
457    }
458
459    /// Returns the last transaction number of the segment.
460    pub fn tx_end(&self) -> Option<TxNumber> {
461        self.tx_range.as_ref().map(|t| t.end())
462    }
463
464    /// Number of transactions.
465    pub fn tx_len(&self) -> Option<u64> {
466        self.tx_range.as_ref().map(|r| r.len())
467    }
468
469    /// Number of blocks.
470    pub fn block_len(&self) -> Option<u64> {
471        self.block_range.as_ref().map(|r| r.len())
472    }
473
474    /// Increments block end range depending on segment
475    pub const fn increment_block(&mut self) -> BlockNumber {
476        let block_num = if let Some(block_range) = &mut self.block_range {
477            block_range.end += 1;
478            block_range.end
479        } else {
480            self.block_range = Some(SegmentRangeInclusive::new(
481                self.expected_block_start(),
482                self.expected_block_start(),
483            ));
484            self.expected_block_start()
485        };
486
487        // For changeset segments, increment the offsets length.
488        // The actual offset entry is written to the sidecar file by the caller.
489        if self.segment.is_change_based() {
490            self.changeset_offsets_len += 1;
491        }
492
493        block_num
494    }
495
496    /// Increments tx end range depending on segment
497    pub const fn increment_tx(&mut self) {
498        if self.segment.is_tx_based() {
499            if let Some(tx_range) = &mut self.tx_range {
500                tx_range.end += 1;
501            } else {
502                self.tx_range = Some(SegmentRangeInclusive::new(0, 0));
503            }
504        }
505    }
506
507    /// Removes `num` elements from end of tx or block range.
508    ///
509    /// For changeset segments, also decrements the changeset offsets length.
510    /// The caller must truncate the sidecar file accordingly.
511    pub const fn prune(&mut self, num: u64) {
512        // Changesets also contain a block range, but are not strictly block-based
513        if self.segment.is_block_or_change_based() {
514            if let Some(range) = &mut self.block_range {
515                if num > range.end - range.start {
516                    self.block_range = None;
517                    // Clear all changeset offsets if we're clearing all blocks
518                    if self.segment.is_change_based() {
519                        self.changeset_offsets_len = 0;
520                    }
521                } else {
522                    let old_end = range.end;
523                    range.end = range.end.saturating_sub(num);
524
525                    // Update changeset offsets length for changeset segments
526                    if self.segment.is_change_based() {
527                        // Calculate how many blocks we're removing
528                        let blocks_to_remove = old_end - range.end;
529                        self.changeset_offsets_len =
530                            self.changeset_offsets_len.saturating_sub(blocks_to_remove);
531                    }
532                }
533            };
534        } else if let Some(range) = &mut self.tx_range {
535            if num > range.end - range.start {
536                self.tx_range = None;
537            } else {
538                range.end = range.end.saturating_sub(num);
539            }
540        }
541    }
542
543    /// Sets a new `block_range`.
544    pub const fn set_block_range(&mut self, block_start: BlockNumber, block_end: BlockNumber) {
545        if let Some(block_range) = &mut self.block_range {
546            block_range.start = block_start;
547            block_range.end = block_end;
548        } else {
549            self.block_range = Some(SegmentRangeInclusive::new(block_start, block_end))
550        }
551    }
552
553    /// Synchronizes changeset offsets length with the current block range for changeset segments.
554    ///
555    /// This should be called after modifying the block range when dealing with changeset segments
556    /// to ensure the offsets length matches the block range size.
557    /// The caller must also truncate the sidecar file accordingly.
558    pub const fn sync_changeset_offsets(&mut self) {
559        if !self.segment.is_change_based() {
560            return;
561        }
562
563        if let Some(block_range) = &self.block_range {
564            let expected_len = block_range.end - block_range.start + 1;
565            if self.changeset_offsets_len > expected_len {
566                self.changeset_offsets_len = expected_len;
567            }
568        } else {
569            // No block range means no offsets
570            self.changeset_offsets_len = 0;
571        }
572    }
573
574    /// Sets a new `tx_range`.
575    pub const fn set_tx_range(&mut self, tx_start: TxNumber, tx_end: TxNumber) {
576        if let Some(tx_range) = &mut self.tx_range {
577            tx_range.start = tx_start;
578            tx_range.end = tx_end;
579        } else {
580            self.tx_range = Some(SegmentRangeInclusive::new(tx_start, tx_end))
581        }
582    }
583
584    /// Returns the row offset which depends on whether the segment is block or transaction based.
585    pub fn start(&self) -> Option<u64> {
586        if self.segment.is_change_based() {
587            return Some(0)
588        }
589
590        if self.segment.is_block_based() {
591            return self.block_start()
592        }
593        self.tx_start()
594    }
595
596    /// Returns the index into the sidecar file for a given block's changeset offset.
597    ///
598    /// Returns `None` if the block is not in the block range.
599    /// To get the changeset offset, the caller must read the offset from the sidecar file at this
600    /// index.
601    pub fn changeset_offset_index(&self, block: BlockNumber) -> Option<u64> {
602        let block_range = self.block_range()?;
603        if !block_range.contains(block) {
604            return None;
605        }
606
607        let index = block - block_range.start();
608        if index >= self.changeset_offsets_len {
609            return None;
610        }
611
612        Some(index)
613    }
614}
615
616/// Configuration used on the segment.
617#[derive(Debug, Clone, Copy)]
618pub struct SegmentConfig {
619    /// Compression used on the segment
620    pub compression: Compression,
621}
622
623/// Helper type to handle segment transaction and block INCLUSIVE ranges.
624///
625/// They can be modified on a hot loop, which makes the `std::ops::RangeInclusive` a poor fit.
626#[derive(Debug, Serialize, Deserialize, Eq, PartialEq, Hash, Clone, Copy)]
627pub struct SegmentRangeInclusive {
628    start: u64,
629    end: u64,
630}
631
632impl SegmentRangeInclusive {
633    /// Creates a new [`SegmentRangeInclusive`]
634    pub const fn new(start: u64, end: u64) -> Self {
635        Self { start, end }
636    }
637
638    /// Start of the inclusive range
639    pub const fn start(&self) -> u64 {
640        self.start
641    }
642
643    /// End of the inclusive range
644    pub const fn end(&self) -> u64 {
645        self.end
646    }
647
648    /// Returns the length of the inclusive range.
649    pub const fn len(&self) -> u64 {
650        self.end.saturating_sub(self.start).saturating_add(1)
651    }
652
653    /// Returns true if the range is empty.
654    pub const fn is_empty(&self) -> bool {
655        self.start > self.end
656    }
657
658    /// Returns whether or not the segment range includes the number
659    pub fn contains(&self, number: u64) -> bool {
660        (self.start..=self.end).contains(&number)
661    }
662}
663
664impl core::fmt::Display for SegmentRangeInclusive {
665    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
666        write!(f, "{}..={}", self.start, self.end)
667    }
668}
669
670impl From<RangeInclusive<u64>> for SegmentRangeInclusive {
671    fn from(value: RangeInclusive<u64>) -> Self {
672        Self { start: *value.start(), end: *value.end() }
673    }
674}
675
676impl From<&SegmentRangeInclusive> for RangeInclusive<u64> {
677    fn from(value: &SegmentRangeInclusive) -> Self {
678        value.start()..=value.end()
679    }
680}
681
682impl From<SegmentRangeInclusive> for RangeInclusive<u64> {
683    fn from(value: SegmentRangeInclusive) -> Self {
684        (&value).into()
685    }
686}
687
688#[cfg(test)]
689mod tests {
690    use super::*;
691    use alloy_primitives::Bytes;
692    use reth_nippy_jar::NippyJar;
693    use std::env::temp_dir;
694
695    #[test]
696    fn test_filename() {
697        let test_vectors = [
698            (StaticFileSegment::Headers, 2..=30, "static_file_headers_2_30", None),
699            (StaticFileSegment::Receipts, 30..=300, "static_file_receipts_30_300", None),
700            (
701                StaticFileSegment::Transactions,
702                1_123_233..=11_223_233,
703                "static_file_transactions_1123233_11223233",
704                None,
705            ),
706            (
707                StaticFileSegment::AccountChangeSets,
708                1_123_233..=11_223_233,
709                "static_file_account-change-sets_1123233_11223233",
710                None,
711            ),
712            (
713                StaticFileSegment::StorageChangeSets,
714                1_123_233..=11_223_233,
715                "static_file_storage-change-sets_1123233_11223233",
716                None,
717            ),
718            (
719                StaticFileSegment::Headers,
720                2..=30,
721                "static_file_headers_2_30_none_lz4",
722                Some(Compression::Lz4),
723            ),
724            (
725                StaticFileSegment::Headers,
726                2..=30,
727                "static_file_headers_2_30_none_zstd",
728                Some(Compression::Zstd),
729            ),
730            (
731                StaticFileSegment::Headers,
732                2..=30,
733                "static_file_headers_2_30_none_zstd-dict",
734                Some(Compression::ZstdWithDictionary),
735            ),
736        ];
737
738        for (segment, block_range, filename, compression) in test_vectors {
739            let block_range: SegmentRangeInclusive = block_range.into();
740            if let Some(compression) = compression {
741                assert_eq!(
742                    segment.filename_with_configuration(compression, &block_range),
743                    filename
744                );
745            } else {
746                assert_eq!(segment.filename(&block_range), filename);
747            }
748
749            assert_eq!(StaticFileSegment::parse_filename(filename), Some((segment, block_range)));
750        }
751
752        assert_eq!(StaticFileSegment::parse_filename("static_file_headers_2"), None);
753        assert_eq!(StaticFileSegment::parse_filename("static_file_headers_"), None);
754
755        // roundtrip test
756        let dummy_range = SegmentRangeInclusive::new(123, 1230);
757        for segment in StaticFileSegment::iter() {
758            let filename = segment.filename(&dummy_range);
759            assert_eq!(Some((segment, dummy_range)), StaticFileSegment::parse_filename(&filename));
760        }
761    }
762
763    #[test]
764    fn test_segment_config_serialization() {
765        let segments = vec![
766            SegmentHeader {
767                expected_block_range: SegmentRangeInclusive::new(0, 200),
768                block_range: Some(SegmentRangeInclusive::new(0, 100)),
769                tx_range: None,
770                segment: StaticFileSegment::Headers,
771                changeset_offsets_len: 0,
772            },
773            SegmentHeader {
774                expected_block_range: SegmentRangeInclusive::new(0, 200),
775                block_range: None,
776                tx_range: Some(SegmentRangeInclusive::new(0, 300)),
777                segment: StaticFileSegment::Transactions,
778                changeset_offsets_len: 0,
779            },
780            SegmentHeader {
781                expected_block_range: SegmentRangeInclusive::new(0, 200),
782                block_range: Some(SegmentRangeInclusive::new(0, 100)),
783                tx_range: Some(SegmentRangeInclusive::new(0, 300)),
784                segment: StaticFileSegment::Receipts,
785                changeset_offsets_len: 0,
786            },
787            SegmentHeader {
788                expected_block_range: SegmentRangeInclusive::new(0, 200),
789                block_range: Some(SegmentRangeInclusive::new(0, 100)),
790                tx_range: Some(SegmentRangeInclusive::new(0, 300)),
791                segment: StaticFileSegment::TransactionSenders,
792                changeset_offsets_len: 0,
793            },
794            SegmentHeader {
795                expected_block_range: SegmentRangeInclusive::new(0, 200),
796                block_range: Some(SegmentRangeInclusive::new(0, 100)),
797                tx_range: Some(SegmentRangeInclusive::new(0, 300)),
798                segment: StaticFileSegment::AccountChangeSets,
799                changeset_offsets_len: 100,
800            },
801            SegmentHeader {
802                expected_block_range: SegmentRangeInclusive::new(0, 200),
803                block_range: Some(SegmentRangeInclusive::new(0, 100)),
804                tx_range: None,
805                segment: StaticFileSegment::StorageChangeSets,
806                changeset_offsets_len: 100,
807            },
808        ];
809        // Check that we test all segments
810        assert_eq!(
811            segments.iter().map(|segment| segment.segment()).collect::<Vec<_>>(),
812            StaticFileSegment::iter().collect::<Vec<_>>()
813        );
814
815        for header in segments {
816            let segment_jar = NippyJar::new(1, &temp_dir(), header.clone());
817            let mut serialized = Vec::new();
818            segment_jar.save_to_writer(&mut serialized).unwrap();
819
820            let deserialized =
821                NippyJar::<SegmentHeader>::load_from_reader(&serialized[..]).unwrap();
822            assert_eq!(deserialized.user_header(), segment_jar.user_header());
823
824            insta::assert_snapshot!(header.segment().to_string(), Bytes::from(serialized));
825        }
826    }
827
828    /// Used in filename writing/parsing
829    #[test]
830    fn test_static_file_segment_str_roundtrip() {
831        for segment in StaticFileSegment::iter() {
832            let static_str = segment.as_str();
833            assert_eq!(StaticFileSegment::from_str(static_str).unwrap(), segment);
834
835            let expected_str = match segment {
836                StaticFileSegment::Headers => "headers",
837                StaticFileSegment::Transactions => "transactions",
838                StaticFileSegment::Receipts => "receipts",
839                StaticFileSegment::TransactionSenders => "transaction-senders",
840                StaticFileSegment::AccountChangeSets => "account-change-sets",
841                StaticFileSegment::StorageChangeSets => "storage-change-sets",
842            };
843            assert_eq!(static_str, expected_str);
844        }
845    }
846
847    /// Used in segment headers serialize/deserialize
848    #[test]
849    fn test_static_file_segment_serde_roundtrip() {
850        for segment in StaticFileSegment::iter() {
851            let ser = serde_json::to_string(&segment).unwrap();
852            assert_eq!(serde_json::from_str::<StaticFileSegment>(&ser).unwrap(), segment);
853
854            let expected_str = match segment {
855                StaticFileSegment::Headers => "Headers",
856                StaticFileSegment::Transactions => "Transactions",
857                StaticFileSegment::Receipts => "Receipts",
858                StaticFileSegment::TransactionSenders => "TransactionSenders",
859                StaticFileSegment::AccountChangeSets => "AccountChangeSets",
860                StaticFileSegment::StorageChangeSets => "StorageChangeSets",
861            };
862            assert_eq!(ser, format!("\"{expected_str}\""));
863        }
864    }
865
866    #[test]
867    fn test_static_file_segment_as_short_str() {
868        for segment in StaticFileSegment::iter() {
869            assert!(
870                segment.as_short_str().len() <= 15,
871                "{segment} segment name is too long: {}",
872                segment.as_short_str()
873            );
874        }
875    }
876}