1//! Command that runs pruning without any limits.
2use crate::common::{AccessRights, CliNodeTypes, Environment, EnvironmentArgs};
3use clap::Parser;
4use reth_chainspec::{EthChainSpec, EthereumHardforks};
5use reth_cli::chainspec::ChainSpecParser;
6use reth_prune::PrunerBuilder;
7use reth_static_file::StaticFileProducer;
8use std::sync::Arc;
9use tracing::info;
1011/// Prunes according to the configuration without any limits
12#[derive(Debug, Parser)]
13pub struct PruneCommand<C: ChainSpecParser> {
14#[command(flatten)]
15env: EnvironmentArgs<C>,
16}
1718impl<C: ChainSpecParser<ChainSpec: EthChainSpec + EthereumHardforks>> PruneCommand<C> {
19/// Execute the `prune` command
20pub async fn execute<N: CliNodeTypes<ChainSpec = C::ChainSpec>>(self) -> eyre::Result<()> {
21let Environment { config, provider_factory, .. } = self.env.init::<N>(AccessRights::RW)?;
22let prune_config = config.prune.unwrap_or_default();
2324// Copy data from database to static files
25info!(target: "reth::cli", "Copying data from database to static files...");
26let static_file_producer =
27 StaticFileProducer::new(provider_factory.clone(), prune_config.segments.clone());
28let lowest_static_file_height =
29 static_file_producer.lock().copy_to_static_files()?.min_block_num();
30info!(target: "reth::cli", ?lowest_static_file_height, "Copied data from database to static files");
3132// Delete data which has been copied to static files.
33if let Some(prune_tip) = lowest_static_file_height {
34info!(target: "reth::cli", ?prune_tip, ?prune_config, "Pruning data from database...");
35// Run the pruner according to the configuration, and don't enforce any limits on it
36let mut pruner = PrunerBuilder::new(prune_config)
37 .delete_limit(usize::MAX)
38 .build_with_provider_factory(provider_factory);
3940 pruner.run(prune_tip)?;
41info!(target: "reth::cli", "Pruned data from database");
42 }
4344Ok(())
45 }
46/// Returns the underlying chain being used to run this command
47pub fn chain_spec(&self) -> Option<&Arc<C::ChainSpec>> {
48Some(&self.env.chain)
49 }
50}