reth_prune_types/
pruner.rs

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