reth_optimism_cli/
lib.rs

1//! OP-Reth CLI implementation.
2
3#![doc(
4    html_logo_url = "https://raw.githubusercontent.com/paradigmxyz/reth/main/assets/reth-docs.png",
5    html_favicon_url = "https://avatars0.githubusercontent.com/u/97369466?s=256",
6    issue_tracker_base_url = "https://github.com/paradigmxyz/reth/issues/"
7)]
8#![cfg_attr(not(test), warn(unused_crate_dependencies))]
9#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
10
11/// A configurable App on top of the cli parser.
12pub mod app;
13/// Optimism chain specification parser.
14pub mod chainspec;
15/// Optimism CLI commands.
16pub mod commands;
17/// Module with a codec for reading and encoding receipts in files.
18///
19/// Enables decoding and encoding `OpGethReceipt` type. See <https://github.com/testinprod-io/op-geth/pull/1>.
20///
21/// Currently configured to use codec [`OpGethReceipt`](receipt_file_codec::OpGethReceipt) based on
22/// export of below Bedrock data using <https://github.com/testinprod-io/op-geth/pull/1>. Codec can
23/// be replaced with regular encoding of receipts for export.
24///
25/// NOTE: receipts can be exported using regular op-geth encoding for `Receipt` type, to fit
26/// reth's needs for importing. However, this would require patching the diff in <https://github.com/testinprod-io/op-geth/pull/1> to export the `Receipt` and not `OpGethReceipt` type (originally
27/// made for op-erigon's import needs).
28pub mod receipt_file_codec;
29
30/// OVM block, same as EVM block at bedrock, except for signature of deposit transaction
31/// not having a signature back then.
32/// Enables decoding and encoding `Block` types within file contexts.
33pub mod ovm_file_codec;
34
35pub use app::CliApp;
36pub use commands::{import::ImportOpCommand, import_receipts::ImportReceiptsOpCommand};
37use reth_optimism_chainspec::OpChainSpec;
38
39use std::{ffi::OsString, fmt, sync::Arc};
40
41use chainspec::OpChainSpecParser;
42use clap::{command, Parser};
43use commands::Commands;
44use futures_util::Future;
45use reth_cli::chainspec::ChainSpecParser;
46use reth_cli_commands::launcher::FnLauncher;
47use reth_cli_runner::CliRunner;
48use reth_db::DatabaseEnv;
49use reth_node_builder::{NodeBuilder, WithLaunchContext};
50use reth_node_core::{args::LogArgs, version::version_metadata};
51use reth_optimism_node::args::RollupArgs;
52
53// This allows us to manually enable node metrics features, required for proper jemalloc metric
54// reporting
55use reth_node_metrics as _;
56
57/// The main op-reth cli interface.
58///
59/// This is the entrypoint to the executable.
60#[derive(Debug, Parser)]
61#[command(author, version = version_metadata().short_version.as_ref(), long_version = version_metadata().long_version.as_ref(), about = "Reth", long_about = None)]
62pub struct Cli<Spec: ChainSpecParser = OpChainSpecParser, Ext: clap::Args + fmt::Debug = RollupArgs>
63{
64    /// The command to run
65    #[command(subcommand)]
66    pub command: Commands<Spec, Ext>,
67
68    /// The logging configuration for the CLI.
69    #[command(flatten)]
70    pub logs: LogArgs,
71}
72
73impl Cli {
74    /// Parsers only the default CLI arguments
75    pub fn parse_args() -> Self {
76        Self::parse()
77    }
78
79    /// Parsers only the default CLI arguments from the given iterator
80    pub fn try_parse_args_from<I, T>(itr: I) -> Result<Self, clap::error::Error>
81    where
82        I: IntoIterator<Item = T>,
83        T: Into<OsString> + Clone,
84    {
85        Self::try_parse_from(itr)
86    }
87}
88
89impl<C, Ext> Cli<C, Ext>
90where
91    C: ChainSpecParser<ChainSpec = OpChainSpec>,
92    Ext: clap::Args + fmt::Debug,
93{
94    /// Configures the CLI and returns a [`CliApp`] instance.
95    ///
96    /// This method is used to prepare the CLI for execution by wrapping it in a
97    /// [`CliApp`] that can be further configured before running.
98    pub fn configure(self) -> CliApp<C, Ext> {
99        CliApp::new(self)
100    }
101
102    /// Execute the configured cli command.
103    ///
104    /// This accepts a closure that is used to launch the node via the
105    /// [`NodeCommand`](reth_cli_commands::node::NodeCommand).
106    pub fn run<L, Fut>(self, launcher: L) -> eyre::Result<()>
107    where
108        L: FnOnce(WithLaunchContext<NodeBuilder<Arc<DatabaseEnv>, C::ChainSpec>>, Ext) -> Fut,
109        Fut: Future<Output = eyre::Result<()>>,
110    {
111        self.with_runner(CliRunner::try_default_runtime()?, launcher)
112    }
113
114    /// Execute the configured cli command with the provided [`CliRunner`].
115    pub fn with_runner<L, Fut>(self, runner: CliRunner, launcher: L) -> eyre::Result<()>
116    where
117        L: FnOnce(WithLaunchContext<NodeBuilder<Arc<DatabaseEnv>, C::ChainSpec>>, Ext) -> Fut,
118        Fut: Future<Output = eyre::Result<()>>,
119    {
120        let mut this = self.configure();
121        this.set_runner(runner);
122        this.run(FnLauncher::new::<C, Ext>(async move |builder, chain_spec| {
123            launcher(builder, chain_spec).await
124        }))
125    }
126}
127
128#[cfg(test)]
129mod test {
130    use crate::{chainspec::OpChainSpecParser, commands::Commands, Cli};
131    use clap::Parser;
132    use reth_cli_commands::{node::NoArgs, NodeCommand};
133    use reth_optimism_chainspec::{BASE_MAINNET, OP_DEV};
134    use reth_optimism_node::args::RollupArgs;
135
136    #[test]
137    fn parse_dev() {
138        let cmd = NodeCommand::<OpChainSpecParser, NoArgs>::parse_from(["op-reth", "--dev"]);
139        let chain = OP_DEV.clone();
140        assert_eq!(cmd.chain.chain, chain.chain);
141        assert_eq!(cmd.chain.genesis_hash(), chain.genesis_hash());
142        assert_eq!(
143            cmd.chain.paris_block_and_final_difficulty,
144            chain.paris_block_and_final_difficulty
145        );
146        assert_eq!(cmd.chain.hardforks, chain.hardforks);
147
148        assert!(cmd.rpc.http);
149        assert!(cmd.network.discovery.disable_discovery);
150
151        assert!(cmd.dev.dev);
152    }
153
154    #[test]
155    fn parse_node() {
156        let cmd = Cli::<OpChainSpecParser, RollupArgs>::parse_from([
157            "op-reth",
158            "node",
159            "--chain",
160            "base",
161            "--datadir",
162            "/mnt/datadirs/base",
163            "--instance",
164            "2",
165            "--http",
166            "--http.addr",
167            "0.0.0.0",
168            "--ws",
169            "--ws.addr",
170            "0.0.0.0",
171            "--http.api",
172            "admin,debug,eth,net,trace,txpool,web3,rpc,reth,ots",
173            "--rollup.sequencer-http",
174            "https://mainnet-sequencer.base.org",
175            "--rpc-max-tracing-requests",
176            "1000000",
177            "--rpc.gascap",
178            "18446744073709551615",
179            "--rpc.max-connections",
180            "429496729",
181            "--rpc.max-logs-per-response",
182            "0",
183            "--rpc.max-subscriptions-per-connection",
184            "10000",
185            "--metrics",
186            "9003",
187            "--log.file.max-size",
188            "100",
189        ]);
190
191        match cmd.command {
192            Commands::Node(command) => {
193                assert_eq!(command.chain.as_ref(), BASE_MAINNET.as_ref());
194            }
195            _ => panic!("unexpected command"),
196        }
197    }
198}