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