From cf3ef3f09e605c2fe0f84919fac44e62b069415d Mon Sep 17 00:00:00 2001 From: nk_ysg Date: Thu, 4 Jul 2024 16:57:48 +0800 Subject: [PATCH] use op-alloy genesis types for genesis parsing --- Cargo.lock | 30 +++++++++++++++++++++++++++ Cargo.toml | 3 +++ crates/chainspec/Cargo.toml | 3 +++ crates/chainspec/src/spec.rs | 40 ++++++++++++++---------------------- 4 files changed, 51 insertions(+), 25 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index bdae392a39f60..f746eaca26a84 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5325,6 +5325,35 @@ version = "11.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0ab1bc2a289d34bd04a330323ac98a1b4bc82c9d9fcb1e66b63caa84da26b575" +[[package]] +name = "op-alloy-consensus" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "767fd3026c514f4d2ebdb4ebda5ed8857660dd1ef5bfed2aaa2ae8e42019630e" +dependencies = [ + "alloy-consensus", + "alloy-eips", + "alloy-primitives", + "alloy-rlp", + "alloy-serde", + "serde", +] + +[[package]] +name = "op-alloy-rpc-types" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c50d6e6767b0b21efc9efb40fc0802ad6d28321c66ff17f1aaa46003cd234d4d" +dependencies = [ + "alloy-network", + "alloy-primitives", + "alloy-rpc-types-eth", + "alloy-serde", + "op-alloy-consensus", + "serde", + "serde_json", +] + [[package]] name = "opaque-debug" version = "0.3.1" @@ -6556,6 +6585,7 @@ dependencies = [ "derive_more", "nybbles", "once_cell", + "op-alloy-rpc-types", "rand 0.8.5", "reth-ethereum-forks", "reth-network-peers", diff --git a/Cargo.toml b/Cargo.toml index ceb9c9532083d..f93874fbae15a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -419,6 +419,9 @@ alloy-pubsub = { version = "0.1", default-features = false } alloy-json-rpc = { version = "0.1", default-features = false } alloy-rpc-client = { version = "0.1", default-features = false } +#op +op-alloy-rpc-types = { version = "0.1" } + # misc auto_impl = "1" aquamarine = "0.5" diff --git a/crates/chainspec/Cargo.toml b/crates/chainspec/Cargo.toml index f473acc4b20a2..c0ad71b0cfa54 100644 --- a/crates/chainspec/Cargo.toml +++ b/crates/chainspec/Cargo.toml @@ -24,6 +24,9 @@ alloy-genesis.workspace = true alloy-primitives = { workspace = true, features = ["rand", "rlp"] } alloy-trie.workspace = true +# op +op-alloy-rpc-types.workspace = true + # misc once_cell.workspace = true serde = { workspace = true, optional = true } diff --git a/crates/chainspec/src/spec.rs b/crates/chainspec/src/spec.rs index 33ca2f4f02442..c7e1de9d31cd1 100644 --- a/crates/chainspec/src/spec.rs +++ b/crates/chainspec/src/spec.rs @@ -39,6 +39,9 @@ use reth_network_peers::{ op_testnet_nodes, sepolia_nodes, }; +#[cfg(feature = "optimism")] +use op_alloy_rpc_types::genesis::{OptimismBaseFeeInfo, OptimismGenesisInfo as OpGenesisInfo}; + /// The Ethereum mainnet spec pub static MAINNET: Lazy> = Lazy::new(|| { ChainSpec { @@ -748,7 +751,7 @@ impl From for ChainSpec { (EthereumHardfork::ArrowGlacier.boxed(), genesis.config.arrow_glacier_block), (EthereumHardfork::GrayGlacier.boxed(), genesis.config.gray_glacier_block), #[cfg(feature = "optimism")] - (OptimismHardfork::Bedrock.boxed(), optimism_genesis_info.bedrock_block), + (OptimismHardfork::Bedrock.boxed(), optimism_genesis_info.genesis_info.bedrock_block), ]; let mut hardforks = hardfork_opts .into_iter() @@ -777,13 +780,13 @@ impl From for ChainSpec { (EthereumHardfork::Cancun.boxed(), genesis.config.cancun_time), (EthereumHardfork::Prague.boxed(), genesis.config.prague_time), #[cfg(feature = "optimism")] - (OptimismHardfork::Regolith.boxed(), optimism_genesis_info.regolith_time), + (OptimismHardfork::Regolith.boxed(), optimism_genesis_info.genesis_info.regolith_time), #[cfg(feature = "optimism")] - (OptimismHardfork::Canyon.boxed(), optimism_genesis_info.canyon_time), + (OptimismHardfork::Canyon.boxed(), optimism_genesis_info.genesis_info.canyon_time), #[cfg(feature = "optimism")] - (OptimismHardfork::Ecotone.boxed(), optimism_genesis_info.ecotone_time), + (OptimismHardfork::Ecotone.boxed(), optimism_genesis_info.genesis_info.ecotone_time), #[cfg(feature = "optimism")] - (OptimismHardfork::Fjord.boxed(), optimism_genesis_info.fjord_time), + (OptimismHardfork::Fjord.boxed(), optimism_genesis_info.genesis_info.fjord_time), ]; let time_hardforks = time_hardfork_opts @@ -1085,32 +1088,21 @@ impl DepositContract { #[derive(Default, Debug, serde::Deserialize)] #[serde(rename_all = "camelCase")] struct OptimismGenesisInfo { - bedrock_block: Option, - regolith_time: Option, - canyon_time: Option, - ecotone_time: Option, - fjord_time: Option, + genesis_info: OpGenesisInfo, #[serde(skip)] base_fee_params: BaseFeeParamsKind, } -#[cfg(feature = "optimism")] -#[derive(Debug, Eq, PartialEq, serde::Deserialize)] -#[serde(rename_all = "camelCase")] -struct OptimismBaseFeeInfo { - eip1559_elasticity: Option, - eip1559_denominator: Option, - eip1559_denominator_canyon: Option, -} - #[cfg(feature = "optimism")] impl OptimismGenesisInfo { fn extract_from(genesis: &Genesis) -> Self { - let mut optimism_genesis_info: Self = - genesis.config.extra_fields.deserialize_as().unwrap_or_default(); + let mut optimism_genesis_info = OptimismGenesisInfo::default(); + if let Some(genesis_info) = OpGenesisInfo::extract_from(&genesis.config.extra_fields) { + optimism_genesis_info.genesis_info = genesis_info; + } - if let Some(Ok(optimism_base_fee_info)) = - genesis.config.extra_fields.get_deserialized::("optimism") + if let Some(optimism_base_fee_info) = + OptimismBaseFeeInfo::extract_from(&genesis.config.extra_fields) { if let (Some(elasticity), Some(denominator)) = ( optimism_base_fee_info.eip1559_elasticity, @@ -1135,11 +1127,9 @@ impl OptimismGenesisInfo { } else { BaseFeeParams::new(denominator as u128, elasticity as u128).into() }; - optimism_genesis_info.base_fee_params = base_fee_params; } } - optimism_genesis_info } }