feat: introduce ChainSpecParser generic in cli types (#10582)

This commit is contained in:
Arsenii Kulikov
2024-08-30 00:13:16 +04:00
committed by GitHub
parent be57b648a7
commit 51524f2534
43 changed files with 400 additions and 325 deletions

View File

@ -14,6 +14,7 @@ workspace = true
[dependencies]
# reth
reth-cli.workspace = true
reth-chainspec.workspace = true
reth-config.workspace = true
reth-primitives.workspace = true

View File

@ -1,16 +1,14 @@
//! CLI definition and entrypoint to executable
use crate::{
args::{
utils::{chain_help, chain_value_parser, SUPPORTED_CHAINS},
LogArgs,
},
args::{utils::chain_help, LogArgs},
commands::debug_cmd,
macros::block_executor,
version::{LONG_VERSION, SHORT_VERSION},
};
use clap::{value_parser, Parser, Subcommand};
use reth_chainspec::ChainSpec;
use reth_cli::chainspec::ChainSpecParser;
use reth_cli_commands::{
config_cmd, db, dump_genesis, import, init_cmd, init_state,
node::{self, NoArgs},
@ -19,6 +17,7 @@ use reth_cli_commands::{
use reth_cli_runner::CliRunner;
use reth_db::DatabaseEnv;
use reth_node_builder::{NodeBuilder, WithLaunchContext};
use reth_node_core::args::utils::DefaultChainSpecParser;
use reth_tracing::FileWorkerGuard;
use std::{ffi::OsString, fmt, future::Future, sync::Arc};
use tracing::info;
@ -35,10 +34,10 @@ pub use crate::core::cli::*;
/// This is the entrypoint to the executable.
#[derive(Debug, Parser)]
#[command(author, version = SHORT_VERSION, long_version = LONG_VERSION, about = "Reth", long_about = None)]
pub struct Cli<Ext: clap::Args + fmt::Debug = NoArgs> {
pub struct Cli<C: ChainSpecParser = DefaultChainSpecParser, Ext: clap::Args + fmt::Debug = NoArgs> {
/// The command to run
#[command(subcommand)]
command: Commands<Ext>,
command: Commands<C, Ext>,
/// The chain this node is running.
///
@ -47,11 +46,11 @@ pub struct Cli<Ext: clap::Args + fmt::Debug = NoArgs> {
long,
value_name = "CHAIN_OR_PATH",
long_help = chain_help(),
default_value = SUPPORTED_CHAINS[0],
value_parser = chain_value_parser,
default_value = C::SUPPORTED_CHAINS[0],
value_parser = C::parser(),
global = true,
)]
chain: Arc<ChainSpec>,
chain: Arc<C::ChainSpec>,
/// Add a new instance of a node.
///
@ -89,7 +88,7 @@ impl Cli {
}
}
impl<Ext: clap::Args + fmt::Debug> Cli<Ext> {
impl<C: ChainSpecParser<ChainSpec = ChainSpec>, Ext: clap::Args + fmt::Debug> Cli<C, Ext> {
/// Execute the configured cli command.
///
/// This accepts a closure that is used to launch the node via the
@ -117,14 +116,14 @@ impl<Ext: clap::Args + fmt::Debug> Cli<Ext> {
///
/// ```no_run
/// use clap::Parser;
/// use reth::cli::Cli;
/// use reth::{args::utils::DefaultChainSpecParser, cli::Cli};
///
/// #[derive(Debug, Parser)]
/// pub struct MyArgs {
/// pub enable: bool,
/// }
///
/// Cli::parse()
/// Cli::<DefaultChainSpecParser, MyArgs>::parse()
/// .run(|builder, my_args: MyArgs| async move {
/// // launch the node
///
@ -187,38 +186,38 @@ impl<Ext: clap::Args + fmt::Debug> Cli<Ext> {
/// Commands to be executed
#[derive(Debug, Subcommand)]
pub enum Commands<Ext: clap::Args + fmt::Debug = NoArgs> {
pub enum Commands<C: ChainSpecParser, Ext: clap::Args + fmt::Debug> {
/// Start the node
#[command(name = "node")]
Node(node::NodeCommand<Ext>),
Node(Box<node::NodeCommand<C, Ext>>),
/// Initialize the database from a genesis file.
#[command(name = "init")]
Init(init_cmd::InitCommand),
Init(init_cmd::InitCommand<C>),
/// Initialize the database from a state dump file.
#[command(name = "init-state")]
InitState(init_state::InitStateCommand),
InitState(init_state::InitStateCommand<C>),
/// This syncs RLP encoded blocks from a file.
#[command(name = "import")]
Import(import::ImportCommand),
Import(import::ImportCommand<C>),
/// This syncs RLP encoded OP blocks below Bedrock from a file, without executing.
#[cfg(feature = "optimism")]
#[command(name = "import-op")]
ImportOp(reth_optimism_cli::ImportOpCommand),
ImportOp(reth_optimism_cli::ImportOpCommand<C>),
/// This imports RLP encoded receipts from a file.
#[cfg(feature = "optimism")]
#[command(name = "import-receipts-op")]
ImportReceiptsOp(reth_optimism_cli::ImportReceiptsOpCommand),
ImportReceiptsOp(reth_optimism_cli::ImportReceiptsOpCommand<C>),
/// Dumps genesis block JSON configuration to stdout.
DumpGenesis(dump_genesis::DumpGenesisCommand),
DumpGenesis(dump_genesis::DumpGenesisCommand<C>),
/// Database debugging utilities
#[command(name = "db")]
Db(db::Command),
Db(db::Command<C>),
/// Manipulate individual stages.
#[command(name = "stage")]
Stage(stage::Command),
Stage(stage::Command<C>),
/// P2P Debugging utilities
#[command(name = "p2p")]
P2P(p2p::Command),
P2P(p2p::Command<C>),
/// Generate Test Vectors
#[cfg(feature = "dev")]
#[command(name = "test-vectors")]
@ -228,13 +227,13 @@ pub enum Commands<Ext: clap::Args + fmt::Debug = NoArgs> {
Config(config_cmd::Command),
/// Various debug routines
#[command(name = "debug")]
Debug(debug_cmd::Command),
Debug(debug_cmd::Command<C>),
/// Scripts for node recovery
#[command(name = "recover")]
Recover(recover::Command),
Recover(recover::Command<C>),
/// Prune according to the configuration without any limits
#[command(name = "prune")]
Prune(prune::PruneCommand),
Prune(prune::PruneCommand<C>),
}
#[cfg(test)]
@ -242,6 +241,7 @@ mod tests {
use super::*;
use crate::args::ColorMode;
use clap::CommandFactory;
use reth_node_core::args::utils::SUPPORTED_CHAINS;
#[test]
fn parse_color_mode() {
@ -254,7 +254,7 @@ mod tests {
/// runtime
#[test]
fn test_parse_help_all_subcommands() {
let reth = Cli::<NoArgs>::command();
let reth = Cli::<DefaultChainSpecParser, NoArgs>::command();
for sub_command in reth.get_subcommands() {
let err = Cli::try_parse_args_from(["reth", sub_command.get_name(), "--help"])
.err()

View File

@ -10,6 +10,8 @@ use reth_beacon_consensus::EthBeaconConsensus;
use reth_blockchain_tree::{
BlockchainTree, BlockchainTreeConfig, ShareableBlockchainTree, TreeExternals,
};
use reth_chainspec::ChainSpec;
use reth_cli::chainspec::ChainSpecParser;
use reth_cli_commands::common::{AccessRights, Environment, EnvironmentArgs};
use reth_cli_runner::CliContext;
use reth_consensus::Consensus;
@ -46,9 +48,9 @@ use tracing::*;
/// This debug routine requires that the node is positioned at the block before the target.
/// The script will then parse the block and attempt to build a similar one.
#[derive(Debug, Parser)]
pub struct Command {
pub struct Command<C: ChainSpecParser> {
#[command(flatten)]
env: EnvironmentArgs,
env: EnvironmentArgs<C>,
/// Overrides the KZG trusted setup by reading from the supplied file.
#[arg(long, value_name = "PATH")]
@ -77,7 +79,7 @@ pub struct Command {
blobs_bundle_path: Option<PathBuf>,
}
impl Command {
impl<C: ChainSpecParser<ChainSpec = ChainSpec>> Command<C> {
/// Fetches the best block block from the database.
///
/// If the database is empty, returns the genesis block.

View File

@ -5,6 +5,8 @@ use std::{path::PathBuf, sync::Arc};
use clap::Parser;
use futures::{stream::select as stream_select, StreamExt};
use reth_beacon_consensus::EthBeaconConsensus;
use reth_chainspec::ChainSpec;
use reth_cli::chainspec::ChainSpecParser;
use reth_cli_commands::common::{AccessRights, Environment, EnvironmentArgs};
use reth_cli_runner::CliContext;
use reth_cli_util::get_secret_key;
@ -38,9 +40,9 @@ use crate::{args::NetworkArgs, macros::block_executor, utils::get_single_header}
/// `reth debug execution` command
#[derive(Debug, Parser)]
pub struct Command {
pub struct Command<C: ChainSpecParser> {
#[command(flatten)]
env: EnvironmentArgs,
env: EnvironmentArgs<C>,
#[command(flatten)]
network: NetworkArgs,
@ -55,7 +57,7 @@ pub struct Command {
pub interval: u64,
}
impl Command {
impl<C: ChainSpecParser<ChainSpec = ChainSpec>> Command<C> {
fn build_pipeline<DB, Client>(
&self,
config: &Config,

View File

@ -4,6 +4,8 @@ use std::{path::PathBuf, sync::Arc};
use backon::{ConstantBuilder, Retryable};
use clap::Parser;
use reth_chainspec::ChainSpec;
use reth_cli::chainspec::ChainSpecParser;
use reth_cli_commands::common::{AccessRights, Environment, EnvironmentArgs};
use reth_cli_runner::CliContext;
use reth_cli_util::get_secret_key;
@ -38,9 +40,9 @@ use crate::{
/// The script will then download the block from p2p network and attempt to calculate and verify
/// merkle root for it.
#[derive(Debug, Parser)]
pub struct Command {
pub struct Command<C: ChainSpecParser> {
#[command(flatten)]
env: EnvironmentArgs,
env: EnvironmentArgs<C>,
#[command(flatten)]
network: NetworkArgs,
@ -54,7 +56,7 @@ pub struct Command {
skip_node_depth: Option<usize>,
}
impl Command {
impl<C: ChainSpecParser<ChainSpec = ChainSpec>> Command<C> {
async fn build_network(
&self,
config: &Config,

View File

@ -5,6 +5,8 @@ use std::{path::PathBuf, sync::Arc};
use backon::{ConstantBuilder, Retryable};
use clap::Parser;
use reth_beacon_consensus::EthBeaconConsensus;
use reth_chainspec::ChainSpec;
use reth_cli::chainspec::ChainSpecParser;
use reth_cli_commands::common::{AccessRights, Environment, EnvironmentArgs};
use reth_cli_runner::CliContext;
use reth_cli_util::get_secret_key;
@ -33,9 +35,9 @@ use crate::{args::NetworkArgs, macros::block_executor, utils::get_single_header}
/// `reth debug merkle` command
#[derive(Debug, Parser)]
pub struct Command {
pub struct Command<C: ChainSpecParser> {
#[command(flatten)]
env: EnvironmentArgs,
env: EnvironmentArgs<C>,
#[command(flatten)]
network: NetworkArgs,
@ -53,7 +55,7 @@ pub struct Command {
skip_node_depth: Option<usize>,
}
impl Command {
impl<C: ChainSpecParser<ChainSpec = ChainSpec>> Command<C> {
async fn build_network(
&self,
config: &Config,

View File

@ -1,6 +1,8 @@
//! `reth debug` command. Collection of various debugging routines.
use clap::{Parser, Subcommand};
use reth_chainspec::ChainSpec;
use reth_cli::chainspec::ChainSpecParser;
use reth_cli_runner::CliContext;
mod build_block;
@ -11,27 +13,27 @@ mod replay_engine;
/// `reth debug` command
#[derive(Debug, Parser)]
pub struct Command {
pub struct Command<C: ChainSpecParser> {
#[command(subcommand)]
command: Subcommands,
command: Subcommands<C>,
}
/// `reth debug` subcommands
#[derive(Subcommand, Debug)]
pub enum Subcommands {
pub enum Subcommands<C: ChainSpecParser> {
/// Debug the roundtrip execution of blocks as well as the generated data.
Execution(execution::Command),
Execution(execution::Command<C>),
/// Debug the clean & incremental state root calculations.
Merkle(merkle::Command),
Merkle(merkle::Command<C>),
/// Debug in-memory state root calculation.
InMemoryMerkle(in_memory_merkle::Command),
InMemoryMerkle(in_memory_merkle::Command<C>),
/// Debug block building.
BuildBlock(build_block::Command),
BuildBlock(build_block::Command<C>),
/// Debug engine API by replaying stored messages.
ReplayEngine(replay_engine::Command),
ReplayEngine(replay_engine::Command<C>),
}
impl Command {
impl<C: ChainSpecParser<ChainSpec = ChainSpec>> Command<C> {
/// Execute `debug` command
pub async fn execute(self, ctx: CliContext) -> eyre::Result<()> {
match self.command {

View File

@ -7,6 +7,8 @@ use reth_beacon_consensus::{hooks::EngineHooks, BeaconConsensusEngine, EthBeacon
use reth_blockchain_tree::{
BlockchainTree, BlockchainTreeConfig, ShareableBlockchainTree, TreeExternals,
};
use reth_chainspec::ChainSpec;
use reth_cli::chainspec::ChainSpecParser;
use reth_cli_commands::common::{AccessRights, Environment, EnvironmentArgs};
use reth_cli_runner::CliContext;
use reth_cli_util::get_secret_key;
@ -35,9 +37,9 @@ use crate::{args::NetworkArgs, macros::block_executor};
/// This script will read stored engine API messages and replay them by the timestamp.
/// It does not require
#[derive(Debug, Parser)]
pub struct Command {
pub struct Command<C: ChainSpecParser> {
#[command(flatten)]
env: EnvironmentArgs,
env: EnvironmentArgs<C>,
#[command(flatten)]
network: NetworkArgs,
@ -51,7 +53,7 @@ pub struct Command {
interval: u64,
}
impl Command {
impl<C: ChainSpecParser<ChainSpec = ChainSpec>> Command<C> {
async fn build_network(
&self,
config: &Config,

View File

@ -23,7 +23,7 @@ pub struct EngineArgs {
#[cfg(not(feature = "optimism"))]
fn main() {
use clap::Parser;
use reth::cli::Cli;
use reth::{args::utils::DefaultChainSpecParser, cli::Cli};
use reth_node_builder::EngineNodeLauncher;
use reth_node_ethereum::{node::EthereumAddOns, EthereumNode};
use reth_provider::providers::BlockchainProvider2;
@ -35,30 +35,32 @@ fn main() {
std::env::set_var("RUST_BACKTRACE", "1");
}
if let Err(err) = Cli::<EngineArgs>::parse().run(|builder, engine_args| async move {
let enable_engine2 = engine_args.experimental;
match enable_engine2 {
true => {
let handle = builder
.with_types_and_provider::<EthereumNode, BlockchainProvider2<_>>()
.with_components(EthereumNode::components())
.with_add_ons::<EthereumAddOns>()
.launch_with_fn(|builder| {
let launcher = EngineNodeLauncher::new(
builder.task_executor().clone(),
builder.config().datadir(),
);
builder.launch_with(launcher)
})
.await?;
handle.node_exit_future.await
if let Err(err) =
Cli::<DefaultChainSpecParser, EngineArgs>::parse().run(|builder, engine_args| async move {
let enable_engine2 = engine_args.experimental;
match enable_engine2 {
true => {
let handle = builder
.with_types_and_provider::<EthereumNode, BlockchainProvider2<_>>()
.with_components(EthereumNode::components())
.with_add_ons::<EthereumAddOns>()
.launch_with_fn(|builder| {
let launcher = EngineNodeLauncher::new(
builder.task_executor().clone(),
builder.config().datadir(),
);
builder.launch_with(launcher)
})
.await?;
handle.node_exit_future.await
}
false => {
let handle = builder.launch_node(EthereumNode::default()).await?;
handle.node_exit_future.await
}
}
false => {
let handle = builder.launch_node(EthereumNode::default()).await?;
handle.node_exit_future.await
}
}
}) {
})
{
eprintln!("Error: {err:?}");
std::process::exit(1);
}

View File

@ -17,6 +17,8 @@ compile_error!("Cannot build the `op-reth` binary with the `optimism` feature fl
#[cfg(feature = "optimism")]
fn main() {
use reth::args::utils::DefaultChainSpecParser;
reth_cli_util::sigsegv_handler::install();
// Enable backtraces unless a RUST_BACKTRACE value has already been explicitly provided.
@ -24,56 +26,58 @@ fn main() {
std::env::set_var("RUST_BACKTRACE", "1");
}
if let Err(err) = Cli::<RollupArgs>::parse().run(|builder, rollup_args| async move {
let enable_engine2 = rollup_args.experimental;
let sequencer_http_arg = rollup_args.sequencer_http.clone();
match enable_engine2 {
true => {
let handle = builder
.with_types_and_provider::<OptimismNode, BlockchainProvider2<_>>()
.with_components(OptimismNode::components(rollup_args))
.with_add_ons::<OptimismAddOns>()
.extend_rpc_modules(move |ctx| {
// register sequencer tx forwarder
if let Some(sequencer_http) = sequencer_http_arg {
ctx.registry
.eth_api()
.set_sequencer_client(SequencerClient::new(sequencer_http));
}
if let Err(err) =
Cli::<DefaultChainSpecParser, RollupArgs>::parse().run(|builder, rollup_args| async move {
let enable_engine2 = rollup_args.experimental;
let sequencer_http_arg = rollup_args.sequencer_http.clone();
match enable_engine2 {
true => {
let handle = builder
.with_types_and_provider::<OptimismNode, BlockchainProvider2<_>>()
.with_components(OptimismNode::components(rollup_args))
.with_add_ons::<OptimismAddOns>()
.extend_rpc_modules(move |ctx| {
// register sequencer tx forwarder
if let Some(sequencer_http) = sequencer_http_arg {
ctx.registry
.eth_api()
.set_sequencer_client(SequencerClient::new(sequencer_http));
}
Ok(())
})
.launch_with_fn(|builder| {
let launcher = EngineNodeLauncher::new(
builder.task_executor().clone(),
builder.config().datadir(),
);
builder.launch_with(launcher)
})
.await?;
Ok(())
})
.launch_with_fn(|builder| {
let launcher = EngineNodeLauncher::new(
builder.task_executor().clone(),
builder.config().datadir(),
);
builder.launch_with(launcher)
})
.await?;
handle.node_exit_future.await
handle.node_exit_future.await
}
false => {
let handle = builder
.node(OptimismNode::new(rollup_args.clone()))
.extend_rpc_modules(move |ctx| {
// register sequencer tx forwarder
if let Some(sequencer_http) = sequencer_http_arg {
ctx.registry
.eth_api()
.set_sequencer_client(SequencerClient::new(sequencer_http));
}
Ok(())
})
.launch()
.await?;
handle.node_exit_future.await
}
}
false => {
let handle = builder
.node(OptimismNode::new(rollup_args.clone()))
.extend_rpc_modules(move |ctx| {
// register sequencer tx forwarder
if let Some(sequencer_http) = sequencer_http_arg {
ctx.registry
.eth_api()
.set_sequencer_client(SequencerClient::new(sequencer_http));
}
Ok(())
})
.launch()
.await?;
handle.node_exit_future.await
}
}
}) {
})
{
eprintln!("Error: {err:?}");
std::process::exit(1);
}