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