Skip to content

Commit

Permalink
chore: simplify OptimismGenesisInfo extraction
Browse files Browse the repository at this point in the history
  • Loading branch information
Vid201 committed Jun 22, 2024
1 parent f137ca8 commit bbb11c8
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 64 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

4 changes: 3 additions & 1 deletion crates/chainspec/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ alloy-trie.workspace = true

# misc
once_cell.workspace = true
serde = { workspace = true, optional = true }
serde_json.workspace = true
derive_more.workspace = true

Expand All @@ -42,7 +43,8 @@ rand.workspace = true
[features]
default = ["std"]
optimism = [
"reth-ethereum-forks/optimism"
"reth-ethereum-forks/optimism",
"serde"
]
std = []
arbitrary = [
Expand Down
120 changes: 57 additions & 63 deletions crates/chainspec/src/spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ use reth_primitives_traits::{
Header, SealedHeader,
};
use reth_trie_common::root::state_root_ref_unhashed;
#[cfg(feature = "optimism")]
use serde::Deserialize;
#[cfg(feature = "std")]
use std::{collections::BTreeMap, sync::Arc};

Expand Down Expand Up @@ -298,6 +300,12 @@ pub enum BaseFeeParamsKind {
Variable(ForkBaseFeeParams),
}

impl Default for BaseFeeParamsKind {
fn default() -> Self {
BaseFeeParams::ethereum().into()
}
}

impl From<BaseFeeParams> for BaseFeeParamsKind {
fn from(params: BaseFeeParams) -> Self {
Self::Constant(params)
Expand Down Expand Up @@ -1160,86 +1168,72 @@ impl DepositContract {
}
}

/// Genesis info for Optimism.
#[cfg(feature = "optimism")]
#[derive(Default, Debug, 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>,
#[serde(skip)]
base_fee_params: BaseFeeParamsKind,
}

#[cfg(feature = "optimism")]
#[derive(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 optimism_config =
genesis.config.extra_fields.get("optimism").and_then(|value| value.as_object());

let eip1559_elasticity = optimism_config
.and_then(|config| config.get("eip1559Elasticity"))
.and_then(|value| value.as_u64());

let eip1559_denominator = optimism_config
.and_then(|config| config.get("eip1559Denominator"))
.and_then(|value| value.as_u64());
let mut optimism_genesis_info: Self =
genesis.config.extra_fields.deserialize_as().unwrap_or_default();

let eip1559_denominator_canyon = optimism_config
.and_then(|config| config.get("eip1559DenominatorCanyon"))
.and_then(|value| value.as_u64());
if let Some(optimism_object) = genesis.config.extra_fields.get("optimism") {
if let Ok(optimism_base_fee_info) =
serde_json::from_value::<OptimismBaseFeeInfo>(optimism_object.clone())
{
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![
(
Hardfork::London,
BaseFeeParams::new(denominator as u128, elasticity as u128),
),
(
Hardfork::Canyon,
BaseFeeParams::new(
canyon_denominator as u128,
elasticity as u128,
),
),
]
.into(),
)
} else {
BaseFeeParams::new(denominator as u128, elasticity as u128).into()
};

let base_fee_params = if let (Some(elasticity), Some(denominator)) =
(eip1559_elasticity, eip1559_denominator)
{
if let Some(canyon_denominator) = eip1559_denominator_canyon {
BaseFeeParamsKind::Variable(
vec![
(
Hardfork::London,
BaseFeeParams::new(denominator as u128, elasticity as u128),
),
(
Hardfork::Canyon,
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;
}
}
} else {
BaseFeeParams::ethereum().into()
};

Self {
bedrock_block: genesis
.config
.extra_fields
.get("bedrockBlock")
.and_then(|value| value.as_u64()),
regolith_time: genesis
.config
.extra_fields
.get("regolithTime")
.and_then(|value| value.as_u64()),
canyon_time: genesis
.config
.extra_fields
.get("canyonTime")
.and_then(|value| value.as_u64()),
ecotone_time: genesis
.config
.extra_fields
.get("ecotoneTime")
.and_then(|value| value.as_u64()),
fjord_time: genesis
.config
.extra_fields
.get("fjordTime")
.and_then(|value| value.as_u64()),
base_fee_params,
}

optimism_genesis_info
}
}

Expand Down

0 comments on commit bbb11c8

Please sign in to comment.