reth_prune_types/
pruner.rs

1use crate::{PruneCheckpoint, PruneMode, PruneSegment};
2use alloc::vec::Vec;
3use alloy_primitives::{BlockNumber, TxNumber};
4use derive_more::Display;
5
6/// Pruner run output.
7#[derive(Debug)]
8pub struct PrunerOutput {
9    /// Pruning progress.
10    pub progress: PruneProgress,
11    /// Pruning output for each segment.
12    pub segments: Vec<(PruneSegment, SegmentOutput)>,
13}
14
15impl From<PruneProgress> for PrunerOutput {
16    fn from(progress: PruneProgress) -> Self {
17        Self { progress, segments: Vec::new() }
18    }
19}
20
21/// Represents information of a pruner run for a segment.
22#[derive(Debug, Clone, PartialEq, Eq, Display)]
23#[display("(table={segment}, pruned={pruned}, status={progress})")]
24pub struct PrunedSegmentInfo {
25    /// The pruned segment
26    pub segment: PruneSegment,
27    /// Number of pruned entries
28    pub pruned: usize,
29    /// Prune progress
30    pub progress: PruneProgress,
31}
32
33/// Segment pruning output.
34#[derive(Debug, Clone, Copy, Eq, PartialEq)]
35pub struct SegmentOutput {
36    /// Segment pruning progress.
37    pub progress: PruneProgress,
38    /// Number of entries pruned, i.e. deleted from the database.
39    pub pruned: usize,
40    /// Pruning checkpoint to save to database, if any.
41    pub checkpoint: Option<SegmentOutputCheckpoint>,
42}
43
44impl SegmentOutput {
45    /// Returns a [`SegmentOutput`] with `done = true`, `pruned = 0` and `checkpoint = None`.
46    /// Use when no pruning is needed.
47    pub const fn done() -> Self {
48        Self { progress: PruneProgress::Finished, pruned: 0, checkpoint: None }
49    }
50
51    /// Returns a [`SegmentOutput`] with `done = false`, `pruned = 0` and `checkpoint = None`.
52    /// Use when pruning is needed but cannot be done.
53    pub const fn not_done(
54        reason: PruneInterruptReason,
55        checkpoint: Option<SegmentOutputCheckpoint>,
56    ) -> Self {
57        Self { progress: PruneProgress::HasMoreData(reason), pruned: 0, checkpoint }
58    }
59}
60
61/// Segment pruning checkpoint.
62#[derive(Debug, Clone, Copy, Default, Eq, PartialEq)]
63pub struct SegmentOutputCheckpoint {
64    /// Highest pruned block number. If it's [None], the pruning for block `0` is not finished yet.
65    pub block_number: Option<BlockNumber>,
66    /// Highest pruned transaction number, if applicable.
67    pub tx_number: Option<TxNumber>,
68}
69
70impl SegmentOutputCheckpoint {
71    /// Converts [`PruneCheckpoint`] to [`SegmentOutputCheckpoint`].
72    pub const fn from_prune_checkpoint(checkpoint: PruneCheckpoint) -> Self {
73        Self { block_number: checkpoint.block_number, tx_number: checkpoint.tx_number }
74    }
75
76    /// Converts [`SegmentOutputCheckpoint`] to [`PruneCheckpoint`] with the provided [`PruneMode`]
77    pub const fn as_prune_checkpoint(&self, prune_mode: PruneMode) -> PruneCheckpoint {
78        PruneCheckpoint { block_number: self.block_number, tx_number: self.tx_number, prune_mode }
79    }
80}
81
82/// Progress of pruning.
83#[derive(Debug, PartialEq, Eq, Clone, Copy, Display)]
84pub enum PruneProgress {
85    /// There is more data to prune.
86    #[display("HasMoreData({_0})")]
87    HasMoreData(PruneInterruptReason),
88    /// Pruning has been finished.
89    #[display("Finished")]
90    Finished,
91}
92
93/// Reason for interrupting a prune run.
94#[derive(Debug, PartialEq, Eq, Clone, Copy, Display)]
95pub enum PruneInterruptReason {
96    /// Prune run timed out.
97    Timeout,
98    /// Limit on the number of deleted entries (rows in the database) per prune run was reached.
99    DeletedEntriesLimitReached,
100    /// Unknown reason for stopping prune run.
101    Unknown,
102}
103
104impl PruneInterruptReason {
105    /// Returns `true` if the reason is timeout.
106    pub const fn is_timeout(&self) -> bool {
107        matches!(self, Self::Timeout)
108    }
109
110    /// Returns `true` if the reason is reaching the limit on deleted entries.
111    pub const fn is_entries_limit_reached(&self) -> bool {
112        matches!(self, Self::DeletedEntriesLimitReached)
113    }
114}
115
116impl PruneProgress {
117    /// Returns `true` if prune run is finished.
118    pub const fn is_finished(&self) -> bool {
119        matches!(self, Self::Finished)
120    }
121}