Skip to content

Commit

Permalink
use op-alloy genesis types for genesis parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
nkysg committed Jul 4, 2024
1 parent 5ccff2a commit cf3ef3f
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 25 deletions.
30 changes: 30 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 = { version = "0.1" }

# misc
auto_impl = "1"
aquamarine = "0.5"
Expand Down
3 changes: 3 additions & 0 deletions crates/chainspec/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
40 changes: 15 additions & 25 deletions crates/chainspec/src/spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Arc<ChainSpec>> = Lazy::new(|| {
ChainSpec {
Expand Down Expand Up @@ -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(), optimism_genesis_info.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(), 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
Expand Down Expand Up @@ -1085,32 +1088,21 @@ 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>,
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<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();
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::<OptimismBaseFeeInfo>("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,
Expand All @@ -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
}
}
Expand Down

0 comments on commit cf3ef3f

Please sign in to comment.