#![doc(
html_logo_url = "https://raw.githubusercontent.com/paradigmxyz/reth/main/assets/reth-docs.png",
html_favicon_url = "https://avatars0.githubusercontent.com/u/97369466?s=256",
issue_tracker_base_url = "https://github.com/paradigmxyz/reth/issues/"
)]
#![cfg_attr(all(not(test), feature = "optimism"), warn(unused_crate_dependencies))]
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
#![cfg(feature = "optimism")]
pub mod chainspec;
pub mod commands;
pub mod receipt_file_codec;
pub use commands::{import::ImportOpCommand, import_receipts::ImportReceiptsOpCommand};
use std::{ffi::OsString, fmt, sync::Arc};
use chainspec::OpChainSpecParser;
use clap::{command, value_parser, Parser};
use commands::Commands;
use futures_util::Future;
use reth_chainspec::ChainSpec;
use reth_cli::chainspec::ChainSpecParser;
use reth_cli_commands::node::NoArgs;
use reth_cli_runner::CliRunner;
use reth_db::DatabaseEnv;
use reth_evm_optimism::OpExecutorProvider;
use reth_node_builder::{NodeBuilder, WithLaunchContext};
use reth_node_core::{
args::LogArgs,
version::{LONG_VERSION, SHORT_VERSION},
};
use reth_node_optimism::OptimismNode;
use reth_tracing::FileWorkerGuard;
use tracing::info;
#[derive(Debug, Parser)]
#[command(author, version = SHORT_VERSION, long_version = LONG_VERSION, about = "Reth", long_about = None)]
pub struct Cli<
Spec: ChainSpecParser<ChainSpec = ChainSpec> = OpChainSpecParser,
Ext: clap::Args + fmt::Debug = NoArgs,
> {
#[command(subcommand)]
command: Commands<Spec, Ext>,
#[arg(
long,
value_name = "CHAIN_OR_PATH",
long_help = Spec::help_message(),
default_value = Spec::SUPPORTED_CHAINS[0],
value_parser = Spec::parser(),
global = true,
)]
chain: Arc<Spec::ChainSpec>,
#[arg(long, value_name = "INSTANCE", global = true, default_value_t = 1, value_parser = value_parser!(u16).range(..=200))]
instance: u16,
#[command(flatten)]
logs: LogArgs,
}
impl Cli {
pub fn parse_args() -> Self {
Self::parse()
}
pub fn try_parse_args_from<I, T>(itr: I) -> Result<Self, clap::error::Error>
where
I: IntoIterator<Item = T>,
T: Into<OsString> + Clone,
{
Self::try_parse_from(itr)
}
}
impl<Spec, Ext> Cli<Spec, Ext>
where
Spec: ChainSpecParser<ChainSpec = ChainSpec>,
Ext: clap::Args + fmt::Debug,
{
pub fn run<L, Fut>(mut self, launcher: L) -> eyre::Result<()>
where
L: FnOnce(WithLaunchContext<NodeBuilder<Arc<DatabaseEnv>>>, Ext) -> Fut,
Fut: Future<Output = eyre::Result<()>>,
{
self.logs.log_file_directory =
self.logs.log_file_directory.join(self.chain.chain.to_string());
let _guard = self.init_tracing()?;
info!(target: "reth::cli", "Initialized tracing, debug log directory: {}", self.logs.log_file_directory);
let runner = CliRunner::default();
match self.command {
Commands::Node(command) => {
runner.run_command_until_exit(|ctx| command.execute(ctx, launcher))
}
Commands::Init(command) => {
runner.run_blocking_until_ctrl_c(command.execute::<OptimismNode>())
}
Commands::InitState(command) => {
runner.run_blocking_until_ctrl_c(command.execute::<OptimismNode>())
}
Commands::ImportOp(command) => {
runner.run_blocking_until_ctrl_c(command.execute::<OptimismNode>())
}
Commands::ImportReceiptsOp(command) => {
runner.run_blocking_until_ctrl_c(command.execute::<OptimismNode>())
}
Commands::DumpGenesis(command) => runner.run_blocking_until_ctrl_c(command.execute()),
Commands::Db(command) => {
runner.run_blocking_until_ctrl_c(command.execute::<OptimismNode>())
}
Commands::Stage(command) => runner.run_command_until_exit(|ctx| {
command.execute::<OptimismNode, _, _>(ctx, OpExecutorProvider::optimism)
}),
Commands::P2P(command) => runner.run_until_ctrl_c(command.execute()),
Commands::Config(command) => runner.run_until_ctrl_c(command.execute()),
Commands::Recover(command) => {
runner.run_command_until_exit(|ctx| command.execute::<OptimismNode>(ctx))
}
Commands::Prune(command) => runner.run_until_ctrl_c(command.execute::<OptimismNode>()),
}
}
pub fn init_tracing(&self) -> eyre::Result<Option<FileWorkerGuard>> {
let guard = self.logs.init_tracing()?;
Ok(guard)
}
}