refactor(prune): derive EnumIter instead of explicit array of segments (#19465)

This commit is contained in:
Alexey Shekhirin
2025-11-03 15:28:14 +00:00
parent 4219741510
commit 0574e68a5e
4 changed files with 20 additions and 5 deletions

1
Cargo.lock generated
View File

@@ -9820,6 +9820,7 @@ dependencies = [
"reth-codecs",
"serde",
"serde_json",
"strum 0.27.2",
"test-fuzz",
"thiserror 2.0.16",
"toml",

View File

@@ -16,6 +16,7 @@ reth-codecs = { workspace = true, optional = true }
alloy-primitives.workspace = true
derive_more.workspace = true
strum = { workspace = true, features = ["derive"] }
thiserror.workspace = true
modular-bitfield = { workspace = true, optional = true }
@@ -43,6 +44,7 @@ std = [
"serde?/std",
"serde_json/std",
"thiserror/std",
"strum/std",
]
test-utils = [
"std",

View File

@@ -1,9 +1,10 @@
use crate::MINIMUM_PRUNING_DISTANCE;
use derive_more::Display;
use strum::{EnumIter, IntoEnumIterator};
use thiserror::Error;
/// Segment of the data that can be pruned.
#[derive(Debug, Display, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[derive(Debug, Display, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash, EnumIter)]
#[cfg_attr(test, derive(arbitrary::Arbitrary))]
#[cfg_attr(any(test, feature = "reth-codec"), derive(reth_codecs::Compact))]
#[cfg_attr(any(test, feature = "reth-codec"), reth_codecs::add_arbitrary_tests(compact))]
@@ -37,6 +38,14 @@ impl Default for PruneSegment {
}
impl PruneSegment {
/// Returns an iterator over all variants of [`PruneSegment`].
///
/// Excludes deprecated variants that are no longer used, but can still be found in the
/// database.
pub fn variants() -> impl Iterator<Item = Self> {
Self::iter()
}
/// Returns minimum number of blocks to keep in the database for this segment.
pub const fn min_blocks(&self, purpose: PrunePurpose) -> u64 {
match self {

View File

@@ -3005,10 +3005,13 @@ impl<TX: DbTx + 'static, N: NodeTypes> PruneCheckpointReader for DatabaseProvide
}
fn get_prune_checkpoints(&self) -> ProviderResult<Vec<(PruneSegment, PruneCheckpoint)>> {
Ok(self
.tx
.cursor_read::<tables::PruneCheckpoints>()?
.walk(None)?
Ok(PruneSegment::variants()
.filter_map(|segment| {
self.tx
.get::<tables::PruneCheckpoints>(segment)
.transpose()
.map(|chk| chk.map(|chk| (segment, chk)))
})
.collect::<Result<_, _>>()?)
}
}