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

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();
}
}
}