reth_cli_commands/
prune.rs

1//! Command that runs pruning without any limits.
2use crate::common::{AccessRights, CliNodeTypes, 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 env = self.env.init::<N>(AccessRights::RW)?;
22        let provider_factory = env.provider_factory;
23        let config = env.config.prune;
24
25        // Copy data from database to static files
26        info!(target: "reth::cli", "Copying data from database to static files...");
27        let static_file_producer =
28            StaticFileProducer::new(provider_factory.clone(), config.segments.clone());
29        let lowest_static_file_height =
30            static_file_producer.lock().copy_to_static_files()?.min_block_num();
31        info!(target: "reth::cli", ?lowest_static_file_height, "Copied data from database to static files");
32
33        // Delete data which has been copied to static files.
34        if let Some(prune_tip) = lowest_static_file_height {
35            info!(target: "reth::cli", ?prune_tip, ?config, "Pruning data from database...");
36            // Run the pruner according to the configuration, and don't enforce any limits on it
37            let mut pruner = PrunerBuilder::new(config)
38                .delete_limit(usize::MAX)
39                .build_with_provider_factory(provider_factory);
40
41            pruner.run(prune_tip)?;
42            info!(target: "reth::cli", "Pruned data from database");
43        }
44
45        Ok(())
46    }
47}
48
49impl<C: ChainSpecParser> PruneCommand<C> {
50    /// Returns the underlying chain being used to run this command
51    pub fn chain_spec(&self) -> Option<&Arc<C::ChainSpec>> {
52        Some(&self.env.chain)
53    }
54}