Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use op-alloy genesis types for genesis parsing #9292

Merged
merged 4 commits into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "0.1"

# misc
auto_impl = "1"
aquamarine = "0.5"
Expand Down
10 changes: 9 additions & 1 deletion crates/chainspec/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ alloy-genesis.workspace = true
alloy-primitives = { workspace = true, features = ["rand", "rlp"] }
alloy-trie.workspace = true

# op
op-alloy-rpc-types = { workspace = true, optional = true }


# misc
once_cell.workspace = true
serde = { workspace = true, optional = true }
Expand All @@ -40,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",
"dep:op-alloy-rpc-types",
]
std = []
arbitrary = [
Expand Down
94 changes: 46 additions & 48 deletions crates/chainspec/src/spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,9 @@ impl From<Genesis> 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 = [
Expand All @@ -748,7 +751,7 @@ impl From<Genesis> 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(), genesis_info.bedrock_block),
];
let mut hardforks = hardfork_opts
.into_iter()
Expand Down Expand Up @@ -777,13 +780,13 @@ impl From<Genesis> 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(), genesis_info.regolith_time),
#[cfg(feature = "optimism")]
(OptimismHardfork::Canyon.boxed(), optimism_genesis_info.canyon_time),
(OptimismHardfork::Canyon.boxed(), genesis_info.canyon_time),
#[cfg(feature = "optimism")]
(OptimismHardfork::Ecotone.boxed(), optimism_genesis_info.ecotone_time),
(OptimismHardfork::Ecotone.boxed(), genesis_info.ecotone_time),
#[cfg(feature = "optimism")]
(OptimismHardfork::Fjord.boxed(), optimism_genesis_info.fjord_time),
(OptimismHardfork::Fjord.boxed(), genesis_info.fjord_time),
];

let time_hardforks = time_hardfork_opts
Expand Down Expand Up @@ -1085,59 +1088,52 @@ impl DepositContract {
#[derive(Default, Debug, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
struct OptimismGenesisInfo {
bedrock_block: Option<u64>,
regolith_time: Option<u64>,
canyon_time: Option<u64>,
ecotone_time: Option<u64>,
fjord_time: Option<u64>,
optimism_chain_info: op_alloy_rpc_types::genesis::OptimismChainInfo,
#[serde(skip)]
base_fee_params: BaseFeeParamsKind,
}

#[cfg(feature = "optimism")]
#[derive(Debug, Eq, PartialEq, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
struct OptimismBaseFeeInfo {
eip1559_elasticity: Option<u64>,
eip1559_denominator: Option<u64>,
eip1559_denominator_canyon: Option<u64>,
}

#[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();

if let Some(Ok(optimism_base_fee_info)) =
genesis.config.extra_fields.get_deserialized::<OptimismBaseFeeInfo>("optimism")
let mut optimism_genesis_info = Self::default();
if let Some(optimism_chain_info) =
op_alloy_rpc_types::genesis::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
Expand All @@ -1151,6 +1147,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};
Expand Down