1//! `reth stage` command
23use std::sync::Arc;
45use 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;
1112pub mod drop;
13pub mod dump;
14pub mod run;
15pub mod unwind;
1617/// `reth stage` command
18#[derive(Debug, Parser)]
19pub struct Command<C: ChainSpecParser> {
20#[command(subcommand)]
21command: Subcommands<C>,
22}
2324/// `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.
33Run(Box<run::Command<C>>),
34/// Drop a stage's tables from the database.
35Drop(drop::Command<C>),
36/// Dumps a stage from a range into a new database.
37Dump(dump::Command<C>),
38/// Unwinds a certain block range, deleting it from the database.
39Unwind(unwind::Command<C>),
40}
4142impl<C: ChainSpecParser<ChainSpec: EthChainSpec + Hardforks + EthereumHardforks>> Command<C> {
43/// Execute `stage` command
44pub async fn execute<N, Comp, F, P>(self, ctx: CliContext, components: F) -> eyre::Result<()>
45where
46N: CliNodeTypes<ChainSpec = C::ChainSpec>,
47 Comp: CliNodeComponents<N>,
48 F: FnOnce(Arc<C::ChainSpec>) -> Comp,
49 P: NetPrimitivesFor<N::Primitives>,
50 {
51match 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
59pub fn chain_spec(&self) -> Option<&Arc<C::ChainSpec>> {
60match self.command {
61Subcommands::Run(ref command) => command.chain_spec(),
62Subcommands::Drop(ref command) => command.chain_spec(),
63Subcommands::Dump(ref command) => command.chain_spec(),
64Subcommands::Unwind(ref command) => command.chain_spec(),
65 }
66 }
67}