From ec71c5b01efc37addff8d2c376790b62978eb43b Mon Sep 17 00:00:00 2001 From: nk_ysg Date: Thu, 4 Jul 2024 16:57:48 +0800 Subject: [PATCH 1/4] use op-alloy genesis types for genesis parsing --- Cargo.lock | 30 ++++++++++++++++++++++++++++ Cargo.toml | 3 +++ crates/chainspec/Cargo.toml | 3 +++ crates/chainspec/src/spec.rs | 38 ++++++++++++++---------------------- 4 files changed, 51 insertions(+), 23 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index bdae392a39f6..f746eaca26a8 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 ceb9c9532083..f93874fbae15 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 f473acc4b20a..c0ad71b0cfa5 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 33ca2f4f0244..c4b64ac63e60 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, From d6b423c072b1869b2a1803b674b90575f504ec95 Mon Sep 17 00:00:00 2001 From: nk_ysg Date: Thu, 4 Jul 2024 20:10:12 +0800 Subject: [PATCH 2/4] use OptimismChainInfo --- Cargo.lock | 1 + Cargo.toml | 4 +- crates/chainspec/Cargo.toml | 9 +++- crates/chainspec/src/spec.rs | 83 +++++++++++++++++++----------------- 4 files changed, 55 insertions(+), 42 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f746eaca26a8..b2aa5f15f453 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5349,6 +5349,7 @@ dependencies = [ "alloy-primitives", "alloy-rpc-types-eth", "alloy-serde", + "arbitrary", "op-alloy-consensus", "serde", "serde_json", diff --git a/Cargo.toml b/Cargo.toml index f93874fbae15..f431365b9415 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -419,8 +419,8 @@ 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" } +# op +op-alloy-rpc-types = { version = "0.1", features = ["arbitrary"] } # misc auto_impl = "1" diff --git a/crates/chainspec/Cargo.toml b/crates/chainspec/Cargo.toml index c0ad71b0cfa5..a8250ff31013 100644 --- a/crates/chainspec/Cargo.toml +++ b/crates/chainspec/Cargo.toml @@ -25,7 +25,8 @@ alloy-primitives = { workspace = true, features = ["rand", "rlp"] } alloy-trie.workspace = true # op -op-alloy-rpc-types.workspace = true +op-alloy-rpc-types = { workspace = true, optional = true } + # misc once_cell.workspace = true @@ -43,11 +44,15 @@ alloy-genesis.workspace = true reth-rpc-types.workspace = true rand.workspace = true +# op +op-alloy-rpc-types.workspace = true + [features] default = ["std"] optimism = [ "reth-ethereum-forks/optimism", - "serde" + "serde", + "op-alloy-rpc-types/arbitrary", ] std = [] arbitrary = [ diff --git a/crates/chainspec/src/spec.rs b/crates/chainspec/src/spec.rs index c4b64ac63e60..da982728df1b 100644 --- a/crates/chainspec/src/spec.rs +++ b/crates/chainspec/src/spec.rs @@ -40,7 +40,7 @@ use reth_network_peers::{ }; #[cfg(feature = "optimism")] -use op_alloy_rpc_types::genesis::{OptimismBaseFeeInfo, OptimismGenesisInfo as OpGenesisInfo}; +use op_alloy_rpc_types::genesis::OptimismChainInfo; /// The Ethereum mainnet spec pub static MAINNET: Lazy> = Lazy::new(|| { @@ -734,6 +734,9 @@ impl From for ChainSpec { fn from(genesis: Genesis) -> Self { #[cfg(feature = "optimism")] let optimism_genesis_info = OptimismGenesisInfo::extract_from(&genesis); + #[cfg(feature = "optimism")] + let genesis_info = + optimism_genesis_info.optimism_chain_info.genesis_info.unwrap_or_default(); // Block-based hardforks let hardfork_opts = [ @@ -751,7 +754,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.genesis_info.bedrock_block), + (OptimismHardfork::Bedrock.boxed(), genesis_info.bedrock_block), ]; let mut hardforks = hardfork_opts .into_iter() @@ -780,13 +783,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.genesis_info.regolith_time), + (OptimismHardfork::Regolith.boxed(), genesis_info.regolith_time), #[cfg(feature = "optimism")] - (OptimismHardfork::Canyon.boxed(), optimism_genesis_info.genesis_info.canyon_time), + (OptimismHardfork::Canyon.boxed(), genesis_info.canyon_time), #[cfg(feature = "optimism")] - (OptimismHardfork::Ecotone.boxed(), optimism_genesis_info.genesis_info.ecotone_time), + (OptimismHardfork::Ecotone.boxed(), genesis_info.ecotone_time), #[cfg(feature = "optimism")] - (OptimismHardfork::Fjord.boxed(), optimism_genesis_info.genesis_info.fjord_time), + (OptimismHardfork::Fjord.boxed(), genesis_info.fjord_time), ]; let time_hardforks = time_hardfork_opts @@ -1088,7 +1091,7 @@ impl DepositContract { #[derive(Default, Debug, serde::Deserialize)] #[serde(rename_all = "camelCase")] struct OptimismGenesisInfo { - genesis_info: OpGenesisInfo, + optimism_chain_info: OptimismChainInfo, #[serde(skip)] base_fee_params: BaseFeeParamsKind, } @@ -1096,40 +1099,42 @@ struct OptimismGenesisInfo { #[cfg(feature = "optimism")] impl OptimismGenesisInfo { fn extract_from(genesis: &Genesis) -> Self { - 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(optimism_base_fee_info) = - OptimismBaseFeeInfo::extract_from(&genesis.config.extra_fields) + let mut optimism_genesis_info = Self::default(); + if let Some(optimism_chain_info) = + OptimismChainInfo::extract_from(&genesis.config.extra_fields) { - if let (Some(elasticity), Some(denominator)) = ( - optimism_base_fee_info.eip1559_elasticity, - optimism_base_fee_info.eip1559_denominator, - ) { - let base_fee_params = if let Some(canyon_denominator) = - optimism_base_fee_info.eip1559_denominator_canyon - { - BaseFeeParamsKind::Variable( - vec![ - ( - EthereumHardfork::London.boxed(), - BaseFeeParams::new(denominator as u128, elasticity as u128), - ), - ( - OptimismHardfork::Canyon.boxed(), - BaseFeeParams::new(canyon_denominator as u128, elasticity as u128), - ), - ] - .into(), - ) - } else { - BaseFeeParams::new(denominator as u128, elasticity as u128).into() - }; + if let Some(ref optimism_base_fee_info) = optimism_chain_info.base_fee_info { + if let (Some(elasticity), Some(denominator)) = ( + optimism_base_fee_info.eip1559_elasticity, + optimism_base_fee_info.eip1559_denominator, + ) { + let base_fee_params = if let Some(canyon_denominator) = + optimism_base_fee_info.eip1559_denominator_canyon + { + BaseFeeParamsKind::Variable( + vec![ + ( + EthereumHardfork::London.boxed(), + BaseFeeParams::new(denominator as u128, elasticity as u128), + ), + ( + OptimismHardfork::Canyon.boxed(), + BaseFeeParams::new( + canyon_denominator as u128, + elasticity as u128, + ), + ), + ] + .into(), + ) + } else { + BaseFeeParams::new(denominator as u128, elasticity as u128).into() + }; - optimism_genesis_info.base_fee_params = base_fee_params; + optimism_genesis_info.base_fee_params = base_fee_params; + } } + optimism_genesis_info.optimism_chain_info = optimism_chain_info; } optimism_genesis_info @@ -1143,6 +1148,8 @@ mod tests { use alloy_genesis::{ChainConfig, GenesisAccount}; use alloy_primitives::{b256, hex}; use core::ops::Deref; + #[cfg(feature = "optimism")] + use op_alloy_rpc_types::genesis::OptimismBaseFeeInfo; use reth_ethereum_forks::{ForkCondition, ForkHash, ForkId, Head}; use reth_trie_common::TrieAccount; use std::{collections::HashMap, str::FromStr}; From ca3e0f9a8dc948eb869f2d7bb74b8fe15cd0fd9f Mon Sep 17 00:00:00 2001 From: nk_ysg Date: Fri, 5 Jul 2024 09:53:52 +0800 Subject: [PATCH 3/4] fix deps --- Cargo.toml | 2 +- crates/chainspec/Cargo.toml | 2 +- crates/chainspec/src/spec.rs | 9 ++++----- 3 files changed, 6 insertions(+), 7 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index f431365b9415..537f7f3cf1a3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -420,7 +420,7 @@ 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", features = ["arbitrary"] } +op-alloy-rpc-types = "0.1" # misc auto_impl = "1" diff --git a/crates/chainspec/Cargo.toml b/crates/chainspec/Cargo.toml index a8250ff31013..e4574acdace9 100644 --- a/crates/chainspec/Cargo.toml +++ b/crates/chainspec/Cargo.toml @@ -52,7 +52,7 @@ default = ["std"] optimism = [ "reth-ethereum-forks/optimism", "serde", - "op-alloy-rpc-types/arbitrary", + "dep:op-alloy-rpc-types", ] std = [] arbitrary = [ diff --git a/crates/chainspec/src/spec.rs b/crates/chainspec/src/spec.rs index da982728df1b..d054d0debab6 100644 --- a/crates/chainspec/src/spec.rs +++ b/crates/chainspec/src/spec.rs @@ -39,9 +39,6 @@ use reth_network_peers::{ op_testnet_nodes, sepolia_nodes, }; -#[cfg(feature = "optimism")] -use op_alloy_rpc_types::genesis::OptimismChainInfo; - /// The Ethereum mainnet spec pub static MAINNET: Lazy> = Lazy::new(|| { ChainSpec { @@ -1091,7 +1088,7 @@ impl DepositContract { #[derive(Default, Debug, serde::Deserialize)] #[serde(rename_all = "camelCase")] struct OptimismGenesisInfo { - optimism_chain_info: OptimismChainInfo, + optimism_chain_info: op_alloy_rpc_types::genesis::OptimismChainInfo, #[serde(skip)] base_fee_params: BaseFeeParamsKind, } @@ -1101,7 +1098,9 @@ impl OptimismGenesisInfo { fn extract_from(genesis: &Genesis) -> Self { let mut optimism_genesis_info = Self::default(); if let Some(optimism_chain_info) = - OptimismChainInfo::extract_from(&genesis.config.extra_fields) + op_alloy_rpc_types::genesis::OptimismChainInfo::extract_from( + &genesis.config.extra_fields, + ) { if let Some(ref optimism_base_fee_info) = optimism_chain_info.base_fee_info { if let (Some(elasticity), Some(denominator)) = ( From 9436355e7dc58ef3953f5d723c0d8ca542ac3855 Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Fri, 5 Jul 2024 06:51:06 +0200 Subject: [PATCH 4/4] touchup --- Cargo.lock | 1 - crates/chainspec/src/spec.rs | 70 +++++++++++++++++------------------- 2 files changed, 33 insertions(+), 38 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b2aa5f15f453..f746eaca26a8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5349,7 +5349,6 @@ dependencies = [ "alloy-primitives", "alloy-rpc-types-eth", "alloy-serde", - "arbitrary", "op-alloy-consensus", "serde", "serde_json", diff --git a/crates/chainspec/src/spec.rs b/crates/chainspec/src/spec.rs index d054d0debab6..4b1083f60aab 100644 --- a/crates/chainspec/src/spec.rs +++ b/crates/chainspec/src/spec.rs @@ -1096,47 +1096,43 @@ struct OptimismGenesisInfo { #[cfg(feature = "optimism")] impl OptimismGenesisInfo { fn extract_from(genesis: &Genesis) -> Self { - let mut optimism_genesis_info = Self::default(); - if let Some(optimism_chain_info) = - op_alloy_rpc_types::genesis::OptimismChainInfo::extract_from( + let mut info = Self { + optimism_chain_info: op_alloy_rpc_types::genesis::OptimismChainInfo::extract_from( &genesis.config.extra_fields, ) - { - if let Some(ref optimism_base_fee_info) = optimism_chain_info.base_fee_info { - if let (Some(elasticity), Some(denominator)) = ( - optimism_base_fee_info.eip1559_elasticity, - optimism_base_fee_info.eip1559_denominator, - ) { - let base_fee_params = if let Some(canyon_denominator) = - optimism_base_fee_info.eip1559_denominator_canyon - { - BaseFeeParamsKind::Variable( - vec![ - ( - EthereumHardfork::London.boxed(), - BaseFeeParams::new(denominator as u128, elasticity as u128), - ), - ( - OptimismHardfork::Canyon.boxed(), - BaseFeeParams::new( - canyon_denominator as u128, - elasticity as u128, - ), - ), - ] - .into(), - ) - } else { - BaseFeeParams::new(denominator as u128, elasticity as u128).into() - }; + .unwrap_or_default(), + ..Default::default() + }; + if let Some(optimism_base_fee_info) = &info.optimism_chain_info.base_fee_info { + if let (Some(elasticity), Some(denominator)) = ( + optimism_base_fee_info.eip1559_elasticity, + optimism_base_fee_info.eip1559_denominator, + ) { + let base_fee_params = if let Some(canyon_denominator) = + optimism_base_fee_info.eip1559_denominator_canyon + { + BaseFeeParamsKind::Variable( + vec![ + ( + EthereumHardfork::London.boxed(), + BaseFeeParams::new(denominator as u128, elasticity as u128), + ), + ( + OptimismHardfork::Canyon.boxed(), + BaseFeeParams::new(canyon_denominator as u128, elasticity as u128), + ), + ] + .into(), + ) + } else { + BaseFeeParams::new(denominator as u128, elasticity as u128).into() + }; - optimism_genesis_info.base_fee_params = base_fee_params; - } + info.base_fee_params = base_fee_params; } - optimism_genesis_info.optimism_chain_info = optimism_chain_info; } - optimism_genesis_info + info } } @@ -1147,8 +1143,6 @@ mod tests { use alloy_genesis::{ChainConfig, GenesisAccount}; use alloy_primitives::{b256, hex}; use core::ops::Deref; - #[cfg(feature = "optimism")] - use op_alloy_rpc_types::genesis::OptimismBaseFeeInfo; use reth_ethereum_forks::{ForkCondition, ForkHash, ForkId, Head}; use reth_trie_common::TrieAccount; use std::{collections::HashMap, str::FromStr}; @@ -2900,6 +2894,8 @@ Post-merge hard forks (timestamp based): #[cfg(feature = "optimism")] #[test] fn parse_genesis_optimism_with_variable_base_fee_params() { + use op_alloy_rpc_types::genesis::OptimismBaseFeeInfo; + let geth_genesis = r#" { "config": {