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

@ -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));