reth_prune_types/
segment.rs

1use crate::MINIMUM_PRUNING_DISTANCE;
2use derive_more::Display;
3use thiserror::Error;
4
5/// Segment of the data that can be pruned.
6#[derive(Debug, Display, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)]
7#[cfg_attr(test, derive(arbitrary::Arbitrary))]
8#[cfg_attr(any(test, feature = "reth-codec"), derive(reth_codecs::Compact))]
9#[cfg_attr(any(test, feature = "reth-codec"), reth_codecs::add_arbitrary_tests(compact))]
10#[cfg_attr(any(test, feature = "serde"), derive(serde::Serialize, serde::Deserialize))]
11pub enum PruneSegment {
12    /// Prune segment responsible for the `TransactionSenders` table.
13    SenderRecovery,
14    /// Prune segment responsible for the `TransactionHashNumbers` table.
15    TransactionLookup,
16    /// Prune segment responsible for all rows in `Receipts` table.
17    Receipts,
18    /// Prune segment responsible for some rows in `Receipts` table filtered by logs.
19    ContractLogs,
20    /// Prune segment responsible for the `AccountChangeSets` and `AccountsHistory` tables.
21    AccountHistory,
22    /// Prune segment responsible for the `StorageChangeSets` and `StoragesHistory` tables.
23    StorageHistory,
24    /// Prune segment responsible for the `CanonicalHeaders`, `Headers` and
25    /// `HeaderTerminalDifficulties` tables.
26    Headers,
27    /// Prune segment responsible for the `Transactions` table.
28    Transactions,
29}
30
31impl PruneSegment {
32    /// Returns minimum number of blocks to keep in the database for this segment.
33    pub const fn min_blocks(&self, purpose: PrunePurpose) -> u64 {
34        match self {
35            Self::SenderRecovery | Self::TransactionLookup | Self::Headers | Self::Transactions => {
36                0
37            }
38            Self::Receipts if purpose.is_static_file() => 0,
39            Self::ContractLogs | Self::AccountHistory | Self::StorageHistory => {
40                MINIMUM_PRUNING_DISTANCE
41            }
42            Self::Receipts => MINIMUM_PRUNING_DISTANCE,
43        }
44    }
45}
46
47/// Prune purpose.
48#[derive(Debug, Clone, Copy)]
49pub enum PrunePurpose {
50    /// Prune data according to user configuration.
51    User,
52    /// Prune data according to highest `static_files` to delete the data from database.
53    StaticFile,
54}
55
56impl PrunePurpose {
57    /// Returns true if the purpose is [`PrunePurpose::User`].
58    pub const fn is_user(self) -> bool {
59        matches!(self, Self::User)
60    }
61
62    /// Returns true if the purpose is [`PrunePurpose::StaticFile`].
63    pub const fn is_static_file(self) -> bool {
64        matches!(self, Self::StaticFile)
65    }
66}
67
68/// `PruneSegment` error type.
69#[derive(Debug, Error, PartialEq, Eq, Clone)]
70pub enum PruneSegmentError {
71    /// Invalid configuration of a prune segment.
72    #[error("the configuration provided for {0} is invalid")]
73    Configuration(PruneSegment),
74}
75
76#[cfg(test)]
77impl Default for PruneSegment {
78    fn default() -> Self {
79        Self::SenderRecovery
80    }
81}