Skip to main content

reth_ethereum_cli/
app.rs

1use crate::{
2    interface::{Commands, NoSubCmd},
3    Cli,
4};
5use clap::Subcommand;
6use eyre::{eyre, Result};
7use reth_chainspec::{ChainSpec, EthChainSpec, Hardforks};
8use reth_cli::chainspec::ChainSpecParser;
9use reth_cli_commands::{
10    common::{CliComponentsBuilder, CliNodeTypes, HeaderMut},
11    launcher::{FnLauncher, Launcher},
12};
13use reth_cli_runner::CliRunner;
14use reth_db::DatabaseEnv;
15use reth_node_api::NodePrimitives;
16use reth_node_builder::{NodeBuilder, WithLaunchContext};
17use reth_node_ethereum::{consensus::EthBeaconConsensus, EthereumNode};
18use reth_node_metrics::recorder::install_prometheus_recorder;
19use reth_rpc_server_types::RpcModuleValidator;
20use reth_tasks::RayonConfig;
21use reth_tracing::{Layers, TracingGuards};
22use std::{fmt, sync::Arc};
23
24/// A wrapper around a parsed CLI that handles command execution.
25#[derive(Debug)]
26pub struct CliApp<
27    Spec: ChainSpecParser,
28    Ext: clap::Args + fmt::Debug,
29    Rpc: RpcModuleValidator,
30    SubCmd: Subcommand + fmt::Debug = NoSubCmd,
31> {
32    cli: Cli<Spec, Ext, Rpc, SubCmd>,
33    runner: Option<CliRunner>,
34    layers: Option<Layers>,
35    guard: Option<TracingGuards>,
36}
37
38impl<C, Ext, Rpc, SubCmd> CliApp<C, Ext, Rpc, SubCmd>
39where
40    C: ChainSpecParser,
41    Ext: clap::Args + fmt::Debug,
42    Rpc: RpcModuleValidator,
43    SubCmd: ExtendedCommand + Subcommand + fmt::Debug,
44{
45    pub(crate) fn new(cli: Cli<C, Ext, Rpc, SubCmd>) -> Self {
46        Self { cli, runner: None, layers: Some(Layers::new()), guard: None }
47    }
48
49    /// Sets the runner for the CLI commander.
50    ///
51    /// This replaces any existing runner with the provided one.
52    pub fn set_runner(&mut self, runner: CliRunner) {
53        self.runner = Some(runner);
54    }
55
56    /// Access to tracing layers.
57    ///
58    /// Returns a mutable reference to the tracing layers, or error
59    /// if tracing initialized and layers have detached already.
60    pub fn access_tracing_layers(&mut self) -> Result<&mut Layers> {
61        self.layers.as_mut().ok_or_else(|| eyre!("Tracing already initialized"))
62    }
63
64    /// Execute the configured cli command.
65    ///
66    /// This accepts a closure that is used to launch the node via the
67    /// [`NodeCommand`](reth_cli_commands::node::NodeCommand).
68    pub fn run(self, launcher: impl Launcher<C, Ext>) -> Result<()>
69    where
70        C: ChainSpecParser<ChainSpec = ChainSpec>,
71    {
72        let jit_args = match &self.cli.command {
73            Commands::ReExecute(cmd) => cmd.jit.clone(),
74            _ => Default::default(),
75        };
76
77        let components = move |spec: Arc<ChainSpec>| {
78            let (evm_config, _) =
79                reth_node_ethereum::node::build_evm_config(spec.clone(), &jit_args, None)
80                    .expect("failed to start revmc JIT backend");
81            (evm_config, Arc::new(EthBeaconConsensus::new(spec)))
82        };
83
84        self.run_with_components::<EthereumNode>(components, async move |builder, ext| {
85            launcher.entrypoint(builder, ext).await
86        })
87    }
88
89    /// Execute the configured cli command with the provided [`CliComponentsBuilder`].
90    ///
91    /// This accepts a closure that is used to launch the node via the
92    /// [`NodeCommand`](reth_cli_commands::node::NodeCommand) and allows providing custom
93    /// components.
94    pub fn run_with_components<N>(
95        mut self,
96        components: impl CliComponentsBuilder<N>,
97        launcher: impl AsyncFnOnce(
98            WithLaunchContext<NodeBuilder<DatabaseEnv, C::ChainSpec>>,
99            Ext,
100        ) -> Result<()>,
101    ) -> Result<()>
102    where
103        N: CliNodeTypes<Primitives: NodePrimitives<BlockHeader: HeaderMut>, ChainSpec: Hardforks>,
104        C: ChainSpecParser<ChainSpec = N::ChainSpec>,
105    {
106        let runner = match self.runner.take() {
107            Some(runner) => runner,
108            None => {
109                let runtime_config = match &self.cli.command {
110                    Commands::Node(command) => {
111                        reth_tasks::RuntimeConfig::default().with_rayon(RayonConfig {
112                            reserved_cpu_cores: command.engine.reserved_cpu_cores,
113                            proof_storage_worker_threads: command.engine.storage_worker_count,
114                            proof_account_worker_threads: command.engine.account_worker_count,
115                            prewarming_threads: command.engine.prewarming_threads,
116                            ..Default::default()
117                        })
118                    }
119                    _ => reth_tasks::RuntimeConfig::default(),
120                };
121                CliRunner::try_with_runtime_config(runtime_config)?
122            }
123        };
124
125        // Add network name if available to the logs dir
126        if let Some(chain_spec) = self.cli.command.chain_spec() {
127            self.cli.logs.log_file_directory =
128                self.cli.logs.log_file_directory.join(chain_spec.chain().to_string());
129        }
130
131        // Apply node-specific log defaults before initializing tracing
132        if matches!(self.cli.command, Commands::Node(_)) {
133            self.cli.logs.apply_node_defaults();
134        }
135
136        self.init_tracing(&runner)?;
137
138        // Deprioritize background threads spawned by tracing/OTel libraries.
139        reth_tasks::utils::deprioritize_background_threads();
140
141        // Install the prometheus recorder to be sure to record all metrics
142        install_prometheus_recorder();
143
144        run_commands_with::<C, Ext, Rpc, N, SubCmd>(self.cli, runner, components, launcher)
145    }
146
147    /// Initializes tracing with the configured options.
148    ///
149    /// See [`Cli::init_tracing`] for more information.
150    pub fn init_tracing(&mut self, runner: &CliRunner) -> Result<()> {
151        if let Some(layers) = self.layers.take() {
152            self.guard = Some(self.cli.init_tracing(runner, layers)?);
153        }
154
155        Ok(())
156    }
157}
158
159/// Run CLI commands with the provided runner, components and launcher.
160/// This is the shared implementation used by both `CliApp` and Cli methods.
161pub(crate) fn run_commands_with<C, Ext, Rpc, N, SubCmd>(
162    cli: Cli<C, Ext, Rpc, SubCmd>,
163    runner: CliRunner,
164    components: impl CliComponentsBuilder<N>,
165    launcher: impl AsyncFnOnce(
166        WithLaunchContext<NodeBuilder<DatabaseEnv, C::ChainSpec>>,
167        Ext,
168    ) -> Result<()>,
169) -> Result<()>
170where
171    C: ChainSpecParser<ChainSpec = N::ChainSpec>,
172    Ext: clap::Args + fmt::Debug,
173    Rpc: RpcModuleValidator,
174    N: CliNodeTypes<Primitives: NodePrimitives<BlockHeader: HeaderMut>, ChainSpec: Hardforks>,
175    SubCmd: ExtendedCommand + Subcommand + fmt::Debug,
176{
177    let rt = runner.runtime();
178
179    match cli.command {
180        Commands::Node(command) => {
181            // Validate RPC modules using the configured validator
182            if let Some(http_api) = &command.rpc.http_api {
183                Rpc::validate_selection(http_api, "http.api").map_err(|e| eyre!("{e}"))?;
184            }
185            if let Some(ws_api) = &command.rpc.ws_api {
186                Rpc::validate_selection(ws_api, "ws.api").map_err(|e| eyre!("{e}"))?;
187            }
188
189            runner.run_command_until_exit(|ctx| {
190                command.execute(ctx, FnLauncher::new::<C, Ext>(launcher))
191            })
192        }
193        Commands::Init(command) => runner.run_blocking_until_ctrl_c(command.execute::<N>(rt)),
194        Commands::InitState(command) => runner.run_blocking_until_ctrl_c(command.execute::<N>(rt)),
195        Commands::Import(command) => {
196            runner.run_blocking_until_ctrl_c(command.execute::<N, _>(components, rt))
197        }
198        Commands::ImportEra(command) => runner.run_blocking_until_ctrl_c(command.execute::<N>(rt)),
199        Commands::ExportEra(command) => runner.run_blocking_until_ctrl_c(command.execute::<N>(rt)),
200        Commands::DumpGenesis(command) => runner.run_blocking_until_ctrl_c(command.execute()),
201        Commands::Db(command) => {
202            runner.run_blocking_command_until_exit(|ctx| command.execute::<N>(ctx))
203        }
204        Commands::Download(command) => runner.run_blocking_until_ctrl_c(command.execute::<N>()),
205        Commands::SnapshotManifest(command) => command.execute(),
206        Commands::Stage(command) => {
207            runner.run_command_until_exit(|ctx| command.execute::<N, _>(ctx, components))
208        }
209        Commands::P2P(command) => runner.run_until_ctrl_c(command.execute::<N>()),
210        Commands::Config(command) => runner.run_until_ctrl_c(command.execute()),
211        Commands::Prune(command) => runner.run_command_until_exit(|ctx| command.execute::<N>(ctx)),
212        #[cfg(feature = "dev")]
213        Commands::TestVectors(command) => runner.run_until_ctrl_c(command.execute()),
214        Commands::ReExecute(command) => {
215            runner.run_until_ctrl_c(command.execute::<N>(components, rt))
216        }
217        Commands::Ext(command) => command.execute(runner),
218    }
219}
220
221/// A trait for extension subcommands that can be added to the CLI.
222///
223/// Consumers implement this trait for their custom subcommands to define
224/// how they should be executed.
225pub trait ExtendedCommand {
226    /// Execute the extension command with the provided CLI runner.
227    fn execute(self, runner: CliRunner) -> Result<()>;
228}
229
230#[cfg(test)]
231mod tests {
232    use super::*;
233    use crate::chainspec::EthereumChainSpecParser;
234    use clap::Parser;
235    use reth_cli_commands::node::NoArgs;
236
237    #[test]
238    fn test_cli_app_creation() {
239        let args = vec!["reth", "config"];
240        let cli = Cli::<EthereumChainSpecParser, NoArgs>::try_parse_from(args).unwrap();
241        let app = cli.configure();
242
243        // Verify app is created correctly
244        assert!(app.runner.is_none());
245        assert!(app.layers.is_some());
246        assert!(app.guard.is_none());
247    }
248
249    #[test]
250    fn test_set_runner() {
251        let args = vec!["reth", "config"];
252        let cli = Cli::<EthereumChainSpecParser, NoArgs>::try_parse_from(args).unwrap();
253        let mut app = cli.configure();
254
255        // Create and set a runner
256        if let Ok(runner) = CliRunner::try_default_runtime() {
257            app.set_runner(runner);
258            assert!(app.runner.is_some());
259        }
260    }
261
262    #[test]
263    fn test_access_tracing_layers() {
264        let args = vec!["reth", "config"];
265        let cli = Cli::<EthereumChainSpecParser, NoArgs>::try_parse_from(args).unwrap();
266        let mut app = cli.configure();
267
268        // Should be able to access layers before initialization
269        assert!(app.access_tracing_layers().is_ok());
270
271        // After taking layers (simulating initialization), access should error
272        app.layers = None;
273        assert!(app.access_tracing_layers().is_err());
274    }
275}