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, EthEvmConfig, EthereumNode};
18use reth_node_metrics::recorder::install_prometheus_recorder;
19use reth_rpc_server_types::RpcModuleValidator;
20use reth_tasks::RayonConfig;
21use reth_tracing::{FileWorkerGuard, Layers};
22use std::{fmt, sync::Arc};
23
24#[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<FileWorkerGuard>,
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 pub fn set_runner(&mut self, runner: CliRunner) {
53 self.runner = Some(runner);
54 }
55
56 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 pub fn run(self, launcher: impl Launcher<C, Ext>) -> Result<()>
69 where
70 C: ChainSpecParser<ChainSpec = ChainSpec>,
71 {
72 let components = |spec: Arc<ChainSpec>| {
73 (EthEvmConfig::ethereum(spec.clone()), Arc::new(EthBeaconConsensus::new(spec)))
74 };
75
76 self.run_with_components::<EthereumNode>(components, async move |builder, ext| {
77 launcher.entrypoint(builder, ext).await
78 })
79 }
80
81 pub fn run_with_components<N>(
87 mut self,
88 components: impl CliComponentsBuilder<N>,
89 launcher: impl AsyncFnOnce(
90 WithLaunchContext<NodeBuilder<DatabaseEnv, C::ChainSpec>>,
91 Ext,
92 ) -> Result<()>,
93 ) -> Result<()>
94 where
95 N: CliNodeTypes<Primitives: NodePrimitives<BlockHeader: HeaderMut>, ChainSpec: Hardforks>,
96 C: ChainSpecParser<ChainSpec = N::ChainSpec>,
97 {
98 let runner = match self.runner.take() {
99 Some(runner) => runner,
100 None => {
101 let runtime_config = match &self.cli.command {
102 Commands::Node(command) => {
103 reth_tasks::RuntimeConfig::default().with_rayon(RayonConfig {
104 reserved_cpu_cores: command.engine.reserved_cpu_cores,
105 proof_storage_worker_threads: command.engine.storage_worker_count,
106 proof_account_worker_threads: command.engine.account_worker_count,
107 prewarming_threads: command.engine.prewarming_threads,
108 ..Default::default()
109 })
110 }
111 _ => reth_tasks::RuntimeConfig::default(),
112 };
113 CliRunner::try_with_runtime_config(runtime_config)?
114 }
115 };
116
117 if let Some(chain_spec) = self.cli.command.chain_spec() {
119 self.cli.logs.log_file_directory =
120 self.cli.logs.log_file_directory.join(chain_spec.chain().to_string());
121 }
122
123 if matches!(self.cli.command, Commands::Node(_)) {
125 self.cli.logs.apply_node_defaults();
126 }
127
128 self.init_tracing(&runner)?;
129
130 reth_tasks::utils::deprioritize_background_threads();
132
133 install_prometheus_recorder();
135
136 run_commands_with::<C, Ext, Rpc, N, SubCmd>(self.cli, runner, components, launcher)
137 }
138
139 pub fn init_tracing(&mut self, runner: &CliRunner) -> Result<()> {
143 if let Some(layers) = self.layers.take() {
144 self.guard = self.cli.init_tracing(runner, layers)?;
145 }
146
147 Ok(())
148 }
149}
150
151pub(crate) fn run_commands_with<C, Ext, Rpc, N, SubCmd>(
154 cli: Cli<C, Ext, Rpc, SubCmd>,
155 runner: CliRunner,
156 components: impl CliComponentsBuilder<N>,
157 launcher: impl AsyncFnOnce(
158 WithLaunchContext<NodeBuilder<DatabaseEnv, C::ChainSpec>>,
159 Ext,
160 ) -> Result<()>,
161) -> Result<()>
162where
163 C: ChainSpecParser<ChainSpec = N::ChainSpec>,
164 Ext: clap::Args + fmt::Debug,
165 Rpc: RpcModuleValidator,
166 N: CliNodeTypes<Primitives: NodePrimitives<BlockHeader: HeaderMut>, ChainSpec: Hardforks>,
167 SubCmd: ExtendedCommand + Subcommand + fmt::Debug,
168{
169 let rt = runner.runtime();
170
171 match cli.command {
172 Commands::Node(command) => {
173 if let Some(http_api) = &command.rpc.http_api {
175 Rpc::validate_selection(http_api, "http.api").map_err(|e| eyre!("{e}"))?;
176 }
177 if let Some(ws_api) = &command.rpc.ws_api {
178 Rpc::validate_selection(ws_api, "ws.api").map_err(|e| eyre!("{e}"))?;
179 }
180
181 runner.run_command_until_exit(|ctx| {
182 command.execute(ctx, FnLauncher::new::<C, Ext>(launcher))
183 })
184 }
185 Commands::Init(command) => runner.run_blocking_until_ctrl_c(command.execute::<N>(rt)),
186 Commands::InitState(command) => runner.run_blocking_until_ctrl_c(command.execute::<N>(rt)),
187 Commands::Import(command) => {
188 runner.run_blocking_until_ctrl_c(command.execute::<N, _>(components, rt))
189 }
190 Commands::ImportEra(command) => runner.run_blocking_until_ctrl_c(command.execute::<N>(rt)),
191 Commands::ExportEra(command) => runner.run_blocking_until_ctrl_c(command.execute::<N>(rt)),
192 Commands::DumpGenesis(command) => runner.run_blocking_until_ctrl_c(command.execute()),
193 Commands::Db(command) => {
194 runner.run_blocking_command_until_exit(|ctx| command.execute::<N>(ctx))
195 }
196 Commands::Download(command) => runner.run_blocking_until_ctrl_c(command.execute::<N>()),
197 Commands::SnapshotManifest(command) => command.execute(),
198 Commands::Stage(command) => {
199 runner.run_command_until_exit(|ctx| command.execute::<N, _>(ctx, components))
200 }
201 Commands::P2P(command) => runner.run_until_ctrl_c(command.execute::<N>()),
202 Commands::Config(command) => runner.run_until_ctrl_c(command.execute()),
203 Commands::Prune(command) => runner.run_command_until_exit(|ctx| command.execute::<N>(ctx)),
204 #[cfg(feature = "dev")]
205 Commands::TestVectors(command) => runner.run_until_ctrl_c(command.execute()),
206 Commands::ReExecute(command) => {
207 runner.run_until_ctrl_c(command.execute::<N>(components, rt))
208 }
209 Commands::Ext(command) => command.execute(runner),
210 }
211}
212
213pub trait ExtendedCommand {
218 fn execute(self, runner: CliRunner) -> Result<()>;
220}
221
222#[cfg(test)]
223mod tests {
224 use super::*;
225 use crate::chainspec::EthereumChainSpecParser;
226 use clap::Parser;
227 use reth_cli_commands::node::NoArgs;
228
229 #[test]
230 fn test_cli_app_creation() {
231 let args = vec!["reth", "config"];
232 let cli = Cli::<EthereumChainSpecParser, NoArgs>::try_parse_from(args).unwrap();
233 let app = cli.configure();
234
235 assert!(app.runner.is_none());
237 assert!(app.layers.is_some());
238 assert!(app.guard.is_none());
239 }
240
241 #[test]
242 fn test_set_runner() {
243 let args = vec!["reth", "config"];
244 let cli = Cli::<EthereumChainSpecParser, NoArgs>::try_parse_from(args).unwrap();
245 let mut app = cli.configure();
246
247 if let Ok(runner) = CliRunner::try_default_runtime() {
249 app.set_runner(runner);
250 assert!(app.runner.is_some());
251 }
252 }
253
254 #[test]
255 fn test_access_tracing_layers() {
256 let args = vec!["reth", "config"];
257 let cli = Cli::<EthereumChainSpecParser, NoArgs>::try_parse_from(args).unwrap();
258 let mut app = cli.configure();
259
260 assert!(app.access_tracing_layers().is_ok());
262
263 app.layers = None;
265 assert!(app.access_tracing_layers().is_err());
266 }
267}