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
31#[cfg(test)]
32#[allow(clippy::derivable_impls)]
33impl Default for PruneSegment {
34    fn default() -> Self {
35        Self::SenderRecovery
36    }
37}
38
39impl PruneSegment {
40    /// Returns minimum number of blocks to keep in the database for this segment.
41    pub const fn min_blocks(&self, purpose: PrunePurpose) -> u64 {
42        match self {
43            Self::SenderRecovery | Self::TransactionLookup | Self::Headers | Self::Transactions => {
44                0
45            }
46            Self::Receipts if purpose.is_static_file() => 0,
47            Self::ContractLogs | Self::AccountHistory | Self::StorageHistory => {
48                MINIMUM_PRUNING_DISTANCE
49            }
50            Self::Receipts => MINIMUM_PRUNING_DISTANCE,
51        }
52    }
53
54    /// Returns true if this is [`Self::AccountHistory`].
55    pub const fn is_account_history(&self) -> bool {
56        matches!(self, Self::AccountHistory)
57    }
58
59    /// Returns true if this is [`Self::StorageHistory`].
60    pub const fn is_storage_history(&self) -> bool {
61        matches!(self, Self::StorageHistory)
62    }
63}
64
65/// Prune purpose.
66#[derive(Debug, Clone, Copy)]
67pub enum PrunePurpose {
68    /// Prune data according to user configuration.
69    User,
70    /// Prune data according to highest `static_files` to delete the data from database.
71    StaticFile,
72}
73
74impl PrunePurpose {
75    /// Returns true if the purpose is [`PrunePurpose::User`].
76    pub const fn is_user(self) -> bool {
77        matches!(self, Self::User)
78    }
79
80    /// Returns true if the purpose is [`PrunePurpose::StaticFile`].
81    pub const fn is_static_file(self) -> bool {
82        matches!(self, Self::StaticFile)
83    }
84}
85
86/// `PruneSegment` error type.
87#[derive(Debug, Error, PartialEq, Eq, Clone)]
88pub enum PruneSegmentError {
89    /// Invalid configuration of a prune segment.
90    #[error("the configuration provided for {0} is invalid")]
91    Configuration(PruneSegment),
92}