reth_db/static_file/
mod.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
//! reth's static file database table import and access

use std::{
    collections::{hash_map::Entry, HashMap},
    path::Path,
};

mod cursor;
pub use cursor::StaticFileCursor;

mod mask;
pub use mask::*;
use reth_nippy_jar::{NippyJar, NippyJarError};
use reth_primitives::{
    static_file::{SegmentHeader, SegmentRangeInclusive},
    StaticFileSegment,
};

mod masks;
pub use masks::*;

/// Alias type for a map of [`StaticFileSegment`] and sorted lists of existing static file ranges.
type SortedStaticFiles =
    HashMap<StaticFileSegment, Vec<(SegmentRangeInclusive, Option<SegmentRangeInclusive>)>>;

/// Given the `static_files` directory path, it returns a list over the existing `static_files`
/// organized by [`StaticFileSegment`]. Each segment has a sorted list of block ranges and
/// transaction ranges as presented in the file configuration.
pub fn iter_static_files(path: impl AsRef<Path>) -> Result<SortedStaticFiles, NippyJarError> {
    let path = path.as_ref();
    if !path.exists() {
        reth_fs_util::create_dir_all(path).map_err(|err| NippyJarError::Custom(err.to_string()))?;
    }

    let mut static_files = SortedStaticFiles::default();
    let entries = reth_fs_util::read_dir(path)
        .map_err(|err| NippyJarError::Custom(err.to_string()))?
        .filter_map(Result::ok)
        .collect::<Vec<_>>();

    for entry in entries {
        if entry.metadata().is_ok_and(|metadata| metadata.is_file()) {
            if let Some((segment, _)) =
                StaticFileSegment::parse_filename(&entry.file_name().to_string_lossy())
            {
                let jar = NippyJar::<SegmentHeader>::load(&entry.path())?;

                let (block_range, tx_range) = (
                    jar.user_header().block_range().copied(),
                    jar.user_header().tx_range().copied(),
                );

                if let Some(block_range) = block_range {
                    match static_files.entry(segment) {
                        Entry::Occupied(mut entry) => {
                            entry.get_mut().push((block_range, tx_range));
                        }
                        Entry::Vacant(entry) => {
                            entry.insert(vec![(block_range, tx_range)]);
                        }
                    }
                }
            }
        }
    }

    for range_list in static_files.values_mut() {
        // Sort by block end range.
        range_list.sort_by(|a, b| a.0.end().cmp(&b.0.end()));
    }

    Ok(static_files)
}