reth_cli_commands/
prune.rs

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 tracing::info;
9
10/// Prunes according to the configuration without any limits
11#[derive(Debug, Parser)]
12pub struct PruneCommand<C: ChainSpecParser> {
13    #[command(flatten)]
14    env: EnvironmentArgs<C>,
15}
16
17impl<C: ChainSpecParser<ChainSpec: EthChainSpec + EthereumHardforks>> PruneCommand<C> {
18    /// Execute the `prune` command
19    pub async fn execute<N: CliNodeTypes<ChainSpec = C::ChainSpec>>(self) -> eyre::Result<()> {
20        let Environment { config, provider_factory, .. } = self.env.init::<N>(AccessRights::RW)?;
21        let prune_config = config.prune.unwrap_or_default();
22
23        // Copy data from database to static files
24        info!(target: "reth::cli", "Copying data from database to static files...");
25        let static_file_producer =
26            StaticFileProducer::new(provider_factory.clone(), prune_config.segments.clone());
27        let lowest_static_file_height =
28            static_file_producer.lock().copy_to_static_files()?.min_block_num();
29        info!(target: "reth::cli", ?lowest_static_file_height, "Copied data from database to static files");
30
31        // Delete data which has been copied to static files.
32        if let Some(prune_tip) = lowest_static_file_height {
33            info!(target: "reth::cli", ?prune_tip, ?prune_config, "Pruning data from database...");
34            // Run the pruner according to the configuration, and don't enforce any limits on it
35            let mut pruner = PrunerBuilder::new(prune_config)
36                .delete_limit(usize::MAX)
37                .build_with_provider_factory(provider_factory);
38
39            pruner.run(prune_tip)?;
40            info!(target: "reth::cli", "Pruned data from database");
41        }
42
43        Ok(())
44    }
45}