chore: Remove duplicate EthereumChainSpecParser in favor of existing EthChainSpecParser (#11412)

Co-authored-by: garwah <garwah@garwah>
Co-authored-by: Matthias Seitz <matthias.seitz@outlook.de>
This commit is contained in:
garwah
2024-10-05 20:52:08 +10:00
committed by GitHub
parent a846cbdcee
commit 28750e5b0e
25 changed files with 85 additions and 189 deletions

13
Cargo.lock generated
View File

@ -6243,6 +6243,7 @@ dependencies = [
"reth-downloaders",
"reth-engine-util",
"reth-errors",
"reth-ethereum-cli",
"reth-ethereum-payload-builder",
"reth-evm",
"reth-execution-types",
@ -6526,9 +6527,12 @@ dependencies = [
name = "reth-cli"
version = "1.0.8"
dependencies = [
"alloy-genesis",
"clap",
"eyre",
"reth-cli-runner",
"serde_json",
"shellexpand",
]
[[package]]
@ -6565,6 +6569,7 @@ dependencies = [
"reth-downloaders",
"reth-ecies",
"reth-eth-wire",
"reth-ethereum-cli",
"reth-evm",
"reth-exex",
"reth-fs-util",
@ -7229,14 +7234,11 @@ dependencies = [
name = "reth-ethereum-cli"
version = "1.0.8"
dependencies = [
"alloy-genesis",
"clap",
"eyre",
"reth-chainspec",
"reth-cli",
"reth-cli-commands",
"serde_json",
"shellexpand",
]
[[package]]
@ -7843,7 +7845,6 @@ dependencies = [
name = "reth-node-core"
version = "1.0.8"
dependencies = [
"alloy-genesis",
"alloy-primitives",
"alloy-rpc-types-engine",
"clap",
@ -7856,19 +7857,16 @@ dependencies = [
"proptest",
"rand 0.8.5",
"reth-chainspec",
"reth-cli",
"reth-cli-util",
"reth-config",
"reth-consensus-common",
"reth-db",
"reth-discv4",
"reth-discv5",
"reth-fs-util",
"reth-net-nat",
"reth-network",
"reth-network-p2p",
"reth-network-peers",
"reth-optimism-chainspec",
"reth-primitives",
"reth-prune-types",
"reth-rpc-api",
@ -7883,7 +7881,6 @@ dependencies = [
"reth-transaction-pool",
"secp256k1",
"serde",
"serde_json",
"shellexpand",
"strum",
"tempfile",

View File

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

View File

@ -15,8 +15,8 @@ use reth_cli_commands::{
};
use reth_cli_runner::CliRunner;
use reth_db::DatabaseEnv;
use reth_ethereum_cli::chainspec::EthereumChainSpecParser;
use reth_node_builder::{NodeBuilder, WithLaunchContext};
use reth_node_core::args::utils::EthereumChainSpecParser;
use reth_node_ethereum::{EthExecutorProvider, EthereumNode};
use reth_tracing::FileWorkerGuard;
use std::{ffi::OsString, fmt, future::Future, sync::Arc};
@ -117,7 +117,8 @@ impl<C: ChainSpecParser<ChainSpec = ChainSpec>, Ext: clap::Args + fmt::Debug> Cl
///
/// ```no_run
/// use clap::Parser;
/// use reth::{args::utils::EthereumChainSpecParser, cli::Cli};
/// use reth::cli::Cli;
/// use reth_ethereum_cli::chainspec::EthereumChainSpecParser;
///
/// #[derive(Debug, Parser)]
/// pub struct MyArgs {
@ -238,7 +239,7 @@ mod tests {
use super::*;
use crate::args::ColorMode;
use clap::CommandFactory;
use reth_node_core::args::utils::SUPPORTED_CHAINS;
use reth_ethereum_cli::chainspec::SUPPORTED_CHAINS;
#[test]
fn parse_color_mode() {

View File

@ -90,6 +90,7 @@ pub mod dirs {
/// Re-exported from `reth_chainspec`
pub mod chainspec {
pub use reth_chainspec::*;
pub use reth_ethereum_cli::chainspec::*;
}
/// Re-exported from `reth_provider`.

View File

@ -4,7 +4,8 @@
static ALLOC: reth_cli_util::allocator::Allocator = reth_cli_util::allocator::new_allocator();
use clap::{Args, Parser};
use reth::{args::utils::EthereumChainSpecParser, cli::Cli};
use reth::cli::Cli;
use reth_ethereum_cli::chainspec::EthereumChainSpecParser;
use reth_node_builder::{
engine_tree_config::{
TreeConfig, DEFAULT_MEMORY_BLOCK_BUFFER_TARGET, DEFAULT_PERSISTENCE_THRESHOLD,

View File

@ -15,9 +15,13 @@ workspace = true
# reth
reth-cli-runner.workspace = true
alloy-genesis.workspace = true
# misc
clap.workspace = true
shellexpand.workspace = true
eyre.workspace = true
serde_json.workspace = true

View File

@ -1,4 +1,4 @@
use std::sync::Arc;
use std::{fs, path::PathBuf, sync::Arc};
use clap::builder::TypedValueParser;
@ -61,3 +61,21 @@ pub trait ChainSpecParser: Clone + Send + Sync + 'static {
format!("The chain this node is running.\nPossible values are either a built-in chain or the path to a chain specification file.\n\nBuilt-in chains:\n {}", Self::SUPPORTED_CHAINS.join(", "))
}
}
/// A helper to parse a [`Genesis`](alloy_genesis::Genesis) as argument or from disk.
pub fn parse_genesis(s: &str) -> eyre::Result<alloy_genesis::Genesis> {
// try to read json from path first
let raw = match fs::read_to_string(PathBuf::from(shellexpand::full(s)?.into_owned())) {
Ok(raw) => raw,
Err(io_err) => {
// valid json may start with "\n", but must contain "{"
if s.contains('{') {
s.to_string()
} else {
return Err(io_err.into()) // assume invalid path
}
}
};
Ok(serde_json::from_str(&raw)?)
}

View File

@ -14,6 +14,7 @@ repository.workspace = true
reth-beacon-consensus.workspace = true
reth-chainspec.workspace = true
reth-cli.workspace = true
reth-ethereum-cli.workspace = true
reth-cli-runner.workspace = true
reth-cli-util.workspace = true
reth-config.workspace = true

View File

@ -160,7 +160,7 @@ impl<C: ChainSpecParser<ChainSpec: EthChainSpec + EthereumHardforks>> Command<C>
#[cfg(test)]
mod tests {
use super::*;
use reth_node_core::args::utils::{EthereumChainSpecParser, SUPPORTED_CHAINS};
use reth_ethereum_cli::chainspec::{EthereumChainSpecParser, SUPPORTED_CHAINS};
use std::path::Path;
#[test]

View File

@ -32,7 +32,7 @@ impl<C: ChainSpecParser<ChainSpec: EthChainSpec>> DumpGenesisCommand<C> {
#[cfg(test)]
mod tests {
use super::*;
use reth_node_core::args::utils::{EthereumChainSpecParser, SUPPORTED_CHAINS};
use reth_ethereum_cli::chainspec::{EthereumChainSpecParser, SUPPORTED_CHAINS};
#[test]
fn parse_dump_genesis_command_chain_args() {

View File

@ -231,7 +231,7 @@ where
#[cfg(test)]
mod tests {
use super::*;
use reth_node_core::args::utils::{EthereumChainSpecParser, SUPPORTED_CHAINS};
use reth_ethereum_cli::chainspec::{EthereumChainSpecParser, SUPPORTED_CHAINS};
#[test]
fn parse_common_import_command_chain_args() {

View File

@ -6,11 +6,12 @@ use reth_cli::chainspec::ChainSpecParser;
use reth_cli_runner::CliContext;
use reth_cli_util::parse_socket_address;
use reth_db::{init_db, DatabaseEnv};
use reth_ethereum_cli::chainspec::EthereumChainSpecParser;
use reth_node_builder::{NodeBuilder, WithLaunchContext};
use reth_node_core::{
args::{
utils::EthereumChainSpecParser, DatabaseArgs, DatadirArgs, DebugArgs, DevArgs, NetworkArgs,
PayloadBuilderArgs, PruningArgs, RpcServerArgs, TxPoolArgs,
DatabaseArgs, DatadirArgs, DebugArgs, DevArgs, NetworkArgs, PayloadBuilderArgs,
PruningArgs, RpcServerArgs, TxPoolArgs,
},
node_config::NodeConfig,
version,
@ -210,7 +211,7 @@ pub struct NoArgs;
mod tests {
use super::*;
use reth_discv4::DEFAULT_DISCOVERY_PORT;
use reth_node_core::args::utils::SUPPORTED_CHAINS;
use reth_ethereum_cli::chainspec::SUPPORTED_CHAINS;
use std::{
net::{IpAddr, Ipv4Addr},
path::Path,

View File

@ -213,7 +213,7 @@ impl Subcommands {
#[cfg(test)]
mod tests {
use reth_node_core::args::utils::EthereumChainSpecParser;
use reth_ethereum_cli::chainspec::EthereumChainSpecParser;
use super::*;

View File

@ -15,13 +15,6 @@ workspace = true
reth-cli.workspace = true
reth-chainspec.workspace = true
# ethereum
alloy-genesis.workspace = true
# io
shellexpand.workspace = true
serde_json.workspace = true
# misc
eyre.workspace = true

View File

@ -1,48 +1,33 @@
use alloy_genesis::Genesis;
use reth_chainspec::{ChainSpec, DEV, HOLESKY, MAINNET, SEPOLIA};
use reth_cli::chainspec::ChainSpecParser;
use std::{fs, path::PathBuf, sync::Arc};
use reth_cli::chainspec::{parse_genesis, ChainSpecParser};
use std::sync::Arc;
/// Chains supported by reth. First value should be used as the default.
pub const SUPPORTED_CHAINS: &[&str] = &["mainnet", "sepolia", "holesky", "dev"];
/// Clap value parser for [`ChainSpec`]s.
///
/// The value parser matches either a known chain, the path
/// to a json file, or a json formatted string in-memory. The json needs to be a Genesis struct.
fn chain_value_parser(s: &str) -> eyre::Result<Arc<ChainSpec>, eyre::Error> {
pub fn chain_value_parser(s: &str) -> eyre::Result<Arc<ChainSpec>, eyre::Error> {
Ok(match s {
"mainnet" => MAINNET.clone(),
"sepolia" => SEPOLIA.clone(),
"holesky" => HOLESKY.clone(),
"dev" => DEV.clone(),
_ => {
// try to read json from path first
let raw = match fs::read_to_string(PathBuf::from(shellexpand::full(s)?.into_owned())) {
Ok(raw) => raw,
Err(io_err) => {
// valid json may start with "\n", but must contain "{"
if s.contains('{') {
s.to_string()
} else {
return Err(io_err.into()) // assume invalid path
}
}
};
// both serialized Genesis and ChainSpec structs supported
let genesis: Genesis = serde_json::from_str(&raw)?;
Arc::new(genesis.into())
}
_ => Arc::new(parse_genesis(s)?.into()),
})
}
/// Ethereum chain specification parser.
#[derive(Debug, Clone, Default)]
pub struct EthChainSpecParser;
#[non_exhaustive]
pub struct EthereumChainSpecParser;
impl ChainSpecParser for EthChainSpecParser {
impl ChainSpecParser for EthereumChainSpecParser {
type ChainSpec = ChainSpec;
const SUPPORTED_CHAINS: &'static [&'static str] = &["mainnet", "sepolia", "holesky", "dev"];
const SUPPORTED_CHAINS: &'static [&'static str] = SUPPORTED_CHAINS;
fn parse(s: &str) -> eyre::Result<Arc<ChainSpec>> {
chain_value_parser(s)
@ -56,8 +41,8 @@ mod tests {
#[test]
fn parse_known_chain_spec() {
for &chain in EthChainSpecParser::SUPPORTED_CHAINS {
assert!(<EthChainSpecParser as ChainSpecParser>::parse(chain).is_ok());
for &chain in EthereumChainSpecParser::SUPPORTED_CHAINS {
assert!(<EthereumChainSpecParser as ChainSpecParser>::parse(chain).is_ok());
}
}
@ -108,7 +93,7 @@ mod tests {
}
}"#;
let spec = <EthChainSpecParser as ChainSpecParser>::parse(s).unwrap();
let spec = <EthereumChainSpecParser as ChainSpecParser>::parse(s).unwrap();
assert!(spec.is_shanghai_active_at_timestamp(0));
assert!(spec.is_cancun_active_at_timestamp(0));
assert!(spec.is_prague_active_at_timestamp(0));

View File

@ -14,9 +14,7 @@ workspace = true
# reth
reth-chainspec.workspace = true
reth-primitives.workspace = true
reth-cli.workspace = true
reth-cli-util.workspace = true
reth-fs-util.workspace = true
reth-db = { workspace = true, features = ["mdbx"] }
reth-storage-errors.workspace = true
reth-storage-api.workspace = true
@ -37,10 +35,8 @@ reth-network-peers.workspace = true
reth-consensus-common.workspace = true
reth-prune-types.workspace = true
reth-stages-types.workspace = true
reth-optimism-chainspec = { workspace = true, optional = true }
# ethereum
alloy-genesis.workspace = true
alloy-primitives.workspace = true
alloy-rpc-types-engine = { workspace = true, features = ["jwt"] }
@ -59,7 +55,6 @@ thiserror.workspace = true
# io
dirs-next = "2.0.0"
shellexpand.workspace = true
serde_json.workspace = true
# tracing
tracing.workspace = true
@ -85,7 +80,6 @@ optimism = [
"reth-primitives/optimism",
"reth-rpc-types-compat/optimism",
"reth-rpc-eth-api/optimism",
"dep:reth-optimism-chainspec",
]
# Features for vergen to generate correct env vars
jemalloc = []

View File

@ -56,7 +56,5 @@ pub use datadir_args::DatadirArgs;
mod benchmark_args;
pub use benchmark_args::BenchmarkArgs;
pub mod utils;
mod error;
pub mod types;

View File

@ -1,99 +0,0 @@
//! Clap parser utilities
use std::{path::PathBuf, sync::Arc};
use alloy_genesis::Genesis;
use reth_chainspec::ChainSpec;
#[cfg(not(feature = "optimism"))]
use reth_chainspec::{DEV, HOLESKY, MAINNET, SEPOLIA};
use reth_cli::chainspec::ChainSpecParser;
use reth_fs_util as fs;
#[cfg(feature = "optimism")]
use reth_optimism_chainspec::{BASE_MAINNET, BASE_SEPOLIA, OP_DEV, OP_MAINNET, OP_SEPOLIA};
#[cfg(feature = "optimism")]
/// Chains supported by op-reth. First value should be used as the default.
pub const SUPPORTED_CHAINS: &[&str] =
&["optimism", "optimism-sepolia", "base", "base-sepolia", "dev"];
#[cfg(not(feature = "optimism"))]
/// Chains supported by reth. First value should be used as the default.
pub const SUPPORTED_CHAINS: &[&str] = &["mainnet", "sepolia", "holesky", "dev"];
/// Clap value parser for [`ChainSpec`]s.
///
/// The value parser matches either a known chain, the path
/// to a json file, or a json formatted string in-memory. The json needs to be a Genesis struct.
#[cfg(not(feature = "optimism"))]
pub fn chain_value_parser(s: &str) -> eyre::Result<Arc<ChainSpec>, eyre::Error> {
Ok(match s {
"mainnet" => MAINNET.clone(),
"sepolia" => SEPOLIA.clone(),
"holesky" => HOLESKY.clone(),
"dev" => DEV.clone(),
_ => Arc::new(parse_custom_chain_spec(s)?),
})
}
/// Clap value parser for [`OpChainSpec`](reth_optimism_chainspec::OpChainSpec)s.
///
/// The value parser matches either a known chain, the path
/// to a json file, or a json formatted string in-memory. The json needs to be a Genesis struct.
#[cfg(feature = "optimism")]
pub fn chain_value_parser(s: &str) -> eyre::Result<Arc<ChainSpec>, eyre::Error> {
Ok(Arc::new(match s {
"optimism" => OP_MAINNET.inner.clone(),
"optimism_sepolia" | "optimism-sepolia" => OP_SEPOLIA.inner.clone(),
"base" => BASE_MAINNET.inner.clone(),
"base_sepolia" | "base-sepolia" => BASE_SEPOLIA.inner.clone(),
"dev" => OP_DEV.inner.clone(),
_ => parse_custom_chain_spec(s)?,
}))
}
/// Parses a custom [`ChainSpec`].
pub fn parse_custom_chain_spec(s: &str) -> eyre::Result<ChainSpec, eyre::Error> {
// try to read json from path first
let raw = match fs::read_to_string(PathBuf::from(shellexpand::full(s)?.into_owned())) {
Ok(raw) => raw,
Err(io_err) => {
// valid json may start with "\n", but must contain "{"
if s.contains('{') {
s.to_string()
} else {
return Err(io_err.into()) // assume invalid path
}
}
};
// both serialized Genesis and ChainSpec structs supported
let genesis: Genesis = serde_json::from_str(&raw)?;
Ok(genesis.into())
}
/// A chain specification parser for ethereum chains.
#[derive(Debug, Copy, Clone, Default)]
#[non_exhaustive]
pub struct EthereumChainSpecParser;
impl ChainSpecParser for EthereumChainSpecParser {
type ChainSpec = ChainSpec;
const SUPPORTED_CHAINS: &'static [&'static str] = SUPPORTED_CHAINS;
fn parse(s: &str) -> eyre::Result<Arc<ChainSpec>> {
chain_value_parser(s)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parse_known_chain_spec() {
for chain in SUPPORTED_CHAINS {
chain_value_parser(chain).unwrap();
}
}
}

View File

@ -1,28 +1,12 @@
use std::sync::Arc;
use reth_cli::chainspec::ChainSpecParser;
use reth_node_core::args::utils::parse_custom_chain_spec;
use reth_cli::chainspec::{parse_genesis, ChainSpecParser};
use reth_optimism_chainspec::{
OpChainSpec, BASE_MAINNET, BASE_SEPOLIA, OP_DEV, OP_MAINNET, OP_SEPOLIA,
};
/// Clap value parser for [`OpChainSpec`]s.
///
/// The value parser matches either a known chain, the path
/// to a json file, or a json formatted string in-memory. The json needs to be a Genesis struct.
fn chain_value_parser(s: &str) -> eyre::Result<Arc<OpChainSpec>, eyre::Error> {
Ok(match s {
"dev" => OP_DEV.clone(),
"optimism" => OP_MAINNET.clone(),
"optimism_sepolia" | "optimism-sepolia" => OP_SEPOLIA.clone(),
"base" => BASE_MAINNET.clone(),
"base_sepolia" | "base-sepolia" => BASE_SEPOLIA.clone(),
_ => Arc::new(OpChainSpec { inner: parse_custom_chain_spec(s)? }),
})
}
use std::sync::Arc;
/// Optimism chain specification parser.
#[derive(Debug, Clone, Default)]
#[non_exhaustive]
pub struct OpChainSpecParser;
impl ChainSpecParser for OpChainSpecParser {
@ -43,6 +27,21 @@ impl ChainSpecParser for OpChainSpecParser {
}
}
/// Clap value parser for [`OpChainSpec`]s.
///
/// The value parser matches either a known chain, the path
/// to a json file, or a json formatted string in-memory. The json needs to be a Genesis struct.
pub fn chain_value_parser(s: &str) -> eyre::Result<Arc<OpChainSpec>, eyre::Error> {
Ok(match s {
"dev" => OP_DEV.clone(),
"optimism" => OP_MAINNET.clone(),
"optimism_sepolia" | "optimism-sepolia" => OP_SEPOLIA.clone(),
"base" => BASE_MAINNET.clone(),
"base_sepolia" | "base-sepolia" => BASE_SEPOLIA.clone(),
_ => Arc::new(parse_genesis(s)?.into()),
})
}
#[cfg(test)]
mod tests {
use super::*;

View File

@ -176,13 +176,14 @@ where
#[cfg(test)]
mod test {
use crate::chainspec::OpChainSpecParser;
use clap::Parser;
use reth_cli_commands::NodeCommand;
use reth_cli_commands::{node::NoArgs, NodeCommand};
use reth_optimism_chainspec::OP_DEV;
#[test]
fn parse_dev() {
let cmd: NodeCommand = NodeCommand::parse_from(["op-reth", "--dev"]);
let cmd = NodeCommand::<OpChainSpecParser, NoArgs>::parse_from(["op-reth", "--dev"]);
let chain = OP_DEV.clone();
assert_eq!(cmd.chain.chain, chain.chain);
assert_eq!(cmd.chain.genesis_hash, chain.genesis_hash);

View File

@ -23,7 +23,7 @@ use clap::Parser;
use futures_util::{stream::FuturesUnordered, StreamExt};
use mined_sidecar::MinedSidecarStream;
use reth::{
args::utils::EthereumChainSpecParser, builder::NodeHandle, cli::Cli,
builder::NodeHandle, chainspec::EthereumChainSpecParser, cli::Cli,
providers::CanonStateSubscriptions,
};
use reth_node_ethereum::EthereumNode;

View File

@ -21,7 +21,7 @@ use alloy_rpc_types_beacon::events::PayloadAttributesEvent;
use clap::Parser;
use futures_util::stream::StreamExt;
use mev_share_sse::{client::EventStream, EventClient};
use reth::{args::utils::EthereumChainSpecParser, cli::Cli};
use reth::{chainspec::EthereumChainSpecParser, cli::Cli};
use reth_node_ethereum::EthereumNode;
use std::net::{IpAddr, Ipv4Addr};
use tracing::{info, warn};

View File

@ -15,8 +15,8 @@ use alloy_rpc_types::state::EvmOverrides;
use clap::Parser;
use futures_util::StreamExt;
use reth::{
args::utils::EthereumChainSpecParser,
builder::NodeHandle,
chainspec::EthereumChainSpecParser,
cli::Cli,
primitives::BlockNumberOrTag,
revm::{

View File

@ -14,7 +14,7 @@
use clap::Parser;
use jsonrpsee::{core::RpcResult, proc_macros::rpc};
use reth::{args::utils::EthereumChainSpecParser, cli::Cli};
use reth::{chainspec::EthereumChainSpecParser, cli::Cli};
use reth_node_ethereum::EthereumNode;
use reth_transaction_pool::TransactionPool;

View File

@ -15,7 +15,7 @@ use alloy_rpc_types_trace::{parity::TraceType, tracerequest::TraceCallRequest};
use clap::Parser;
use futures_util::StreamExt;
use reth::{
args::utils::EthereumChainSpecParser, builder::NodeHandle, cli::Cli,
builder::NodeHandle, chainspec::EthereumChainSpecParser, cli::Cli,
rpc::compat::transaction::transaction_to_call_request, transaction_pool::TransactionPool,
};
use reth_node_ethereum::node::EthereumNode;