reth_cli_commands/stage/
mod.rs

1//! `reth stage` command
2
3use std::sync::Arc;
4
5use crate::common::{CliNodeComponents, CliNodeTypes};
6use clap::{Parser, Subcommand};
7use reth_chainspec::{EthChainSpec, EthereumHardforks, Hardforks};
8use reth_cli::chainspec::ChainSpecParser;
9use reth_cli_runner::CliContext;
10
11pub mod drop;
12pub mod dump;
13pub mod run;
14pub mod unwind;
15
16/// `reth stage` command
17#[derive(Debug, Parser)]
18pub struct Command<C: ChainSpecParser> {
19    #[command(subcommand)]
20    command: Subcommands<C>,
21}
22
23/// `reth stage` subcommands
24#[derive(Subcommand, Debug)]
25pub enum Subcommands<C: ChainSpecParser> {
26    /// Run a single stage.
27    ///
28    /// Note that this won't use the Pipeline and as a result runs stages
29    /// assuming that all the data can be held in memory. It is not recommended
30    /// to run a stage for really large block ranges if your computer does not have
31    /// a lot of memory to store all the data.
32    Run(Box<run::Command<C>>),
33    /// Drop a stage's tables from the database.
34    Drop(drop::Command<C>),
35    /// Dumps a stage from a range into a new database.
36    Dump(dump::Command<C>),
37    /// Unwinds a certain block range, deleting it from the database.
38    Unwind(unwind::Command<C>),
39}
40
41impl<C: ChainSpecParser<ChainSpec: EthChainSpec + Hardforks + EthereumHardforks>> Command<C> {
42    /// Execute `stage` command
43    pub async fn execute<N, Comp>(
44        self,
45        ctx: CliContext,
46        components: impl FnOnce(Arc<C::ChainSpec>) -> Comp,
47    ) -> eyre::Result<()>
48    where
49        N: CliNodeTypes<ChainSpec = C::ChainSpec>,
50        Comp: CliNodeComponents<N>,
51    {
52        match self.command {
53            Subcommands::Run(command) => command.execute::<N, _, _>(ctx, components).await,
54            Subcommands::Drop(command) => command.execute::<N>().await,
55            Subcommands::Dump(command) => command.execute::<N, _, _>(components).await,
56            Subcommands::Unwind(command) => command.execute::<N, _, _>(components).await,
57        }
58    }
59}
60
61impl<C: ChainSpecParser> Command<C> {
62    /// Returns the underlying chain being used to run this command
63    pub fn chain_spec(&self) -> Option<&Arc<C::ChainSpec>> {
64        match self.command {
65            Subcommands::Run(ref command) => command.chain_spec(),
66            Subcommands::Drop(ref command) => command.chain_spec(),
67            Subcommands::Dump(ref command) => command.chain_spec(),
68            Subcommands::Unwind(ref command) => command.chain_spec(),
69        }
70    }
71}