feat(cli): reth prune (#9055)

This commit is contained in:
Alexey Shekhirin
2024-06-26 15:54:06 +01:00
committed by GitHub
parent bdabe66426
commit 8775a93d33
20 changed files with 245 additions and 35 deletions

View File

@ -21,6 +21,7 @@ reth-nippy-jar.workspace = true
reth-tokio-util.workspace = true
reth-prune-types.workspace = true
reth-static-file-types.workspace = true
reth-stages-types.workspace = true
alloy-primitives.workspace = true

View File

@ -5,8 +5,12 @@ use alloy_primitives::BlockNumber;
use parking_lot::Mutex;
use rayon::prelude::*;
use reth_db_api::database::Database;
use reth_provider::{providers::StaticFileWriter, ProviderFactory, StaticFileProviderFactory};
use reth_provider::{
providers::StaticFileWriter, ProviderFactory, StageCheckpointReader as _,
StaticFileProviderFactory,
};
use reth_prune_types::PruneModes;
use reth_stages_types::StageId;
use reth_static_file_types::HighestStaticFiles;
use reth_storage_errors::provider::ProviderResult;
use reth_tokio_util::{EventSender, EventStream};
@ -56,7 +60,7 @@ pub struct StaticFileProducerInner<DB> {
event_sender: EventSender<StaticFileProducerEvent>,
}
/// Static File targets, per data part, measured in [`BlockNumber`].
/// Static File targets, per data segment, measured in [`BlockNumber`].
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct StaticFileTargets {
headers: Option<RangeInclusive<BlockNumber>>,
@ -167,6 +171,28 @@ impl<DB: Database> StaticFileProducerInner<DB> {
Ok(targets)
}
/// Copies data from database to static files according to
/// [stage checkpoints](reth_stages_types::StageCheckpoint).
///
/// Returns highest block numbers for all static file segments.
pub fn copy_to_static_files(&self) -> ProviderResult<HighestStaticFiles> {
let provider = self.provider_factory.provider()?;
let stages_checkpoints = [StageId::Headers, StageId::Execution, StageId::Bodies]
.into_iter()
.map(|stage| provider.get_stage_checkpoint(stage).map(|c| c.map(|c| c.block_number)))
.collect::<Result<Vec<_>, _>>()?;
let highest_static_files = HighestStaticFiles {
headers: stages_checkpoints[0],
receipts: stages_checkpoints[1],
transactions: stages_checkpoints[2],
};
let targets = self.get_static_file_targets(highest_static_files)?;
self.run(targets)?;
Ok(highest_static_files)
}
/// Returns a static file targets at the provided finalized block numbers per segment.
/// The target is determined by the check against highest `static_files` using
/// [`reth_provider::providers::StaticFileProvider::get_highest_static_files`].

View File

@ -20,7 +20,7 @@ pub use segment::{SegmentConfig, SegmentHeader, SegmentRangeInclusive, StaticFil
/// Default static file block count.
pub const BLOCKS_PER_STATIC_FILE: u64 = 500_000;
/// Highest static file block numbers, per data part.
/// Highest static file block numbers, per data segment.
#[derive(Debug, Clone, Copy, Default, Eq, PartialEq)]
pub struct HighestStaticFiles {
/// Highest static file block of headers, inclusive.
@ -53,6 +53,11 @@ impl HighestStaticFiles {
}
}
/// Returns the minimum block of all segments.
pub fn min(&self) -> Option<u64> {
[self.headers, self.transactions, self.receipts].iter().filter_map(|&option| option).min()
}
/// Returns the maximum block of all segments.
pub fn max(&self) -> Option<u64> {
[self.headers, self.transactions, self.receipts].iter().filter_map(|&option| option).max()