Skip to content

Commit

Permalink
chore: Remove duplicate EthereumChainSpecParser in favor of existing …
Browse files Browse the repository at this point in the history
…EthChainSpecParser (paradigmxyz#11412)

Co-authored-by: garwah <garwah@garwah>
Co-authored-by: Matthias Seitz <[email protected]>
  • Loading branch information
3 people authored Oct 5, 2024
1 parent a846cbd commit 28750e5
Show file tree
Hide file tree
Showing 25 changed files with 85 additions and 189 deletions.
13 changes: 5 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions bin/reth/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions bin/reth/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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() {
Expand Down
1 change: 1 addition & 0 deletions bin/reth/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
3 changes: 2 additions & 1 deletion bin/reth/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 4 additions & 0 deletions crates/cli/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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



20 changes: 19 additions & 1 deletion crates/cli/cli/src/chainspec.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::sync::Arc;
use std::{fs, path::PathBuf, sync::Arc};

use clap::builder::TypedValueParser;

Expand Down Expand Up @@ -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)?)
}
1 change: 1 addition & 0 deletions crates/cli/commands/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion crates/cli/commands/src/db/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion crates/cli/commands/src/dump_genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion crates/cli/commands/src/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
7 changes: 4 additions & 3 deletions crates/cli/commands/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion crates/cli/commands/src/stage/unwind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;

Expand Down
7 changes: 0 additions & 7 deletions crates/ethereum/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
43 changes: 14 additions & 29 deletions crates/ethereum/cli/src/chainspec.rs
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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());
}
}

Expand Down Expand Up @@ -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));
Expand Down
6 changes: 0 additions & 6 deletions crates/node/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"] }

Expand All @@ -59,7 +55,6 @@ thiserror.workspace = true
# io
dirs-next = "2.0.0"
shellexpand.workspace = true
serde_json.workspace = true

# tracing
tracing.workspace = true
Expand All @@ -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 = []
Expand Down
2 changes: 0 additions & 2 deletions crates/node/core/src/args/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Loading

0 comments on commit 28750e5

Please sign in to comment.