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

chore: op chainspec #11415

Merged
merged 2 commits into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 0 additions & 2 deletions Cargo.lock

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

9 changes: 1 addition & 8 deletions crates/chainspec/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,9 @@ 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
auto_impl.workspace = true
once_cell.workspace = true
serde = { workspace = true, optional = true }
serde_json.workspace = true
derive_more.workspace = true

Expand All @@ -44,12 +40,9 @@ alloy-eips = { workspace = true, features = ["arbitrary"] }
alloy-rlp = { workspace = true, features = ["arrayvec"] }
alloy-genesis.workspace = true

# op
op-alloy-rpc-types.workspace = true

[features]
default = ["std"]
optimism = ["serde", "dep:op-alloy-rpc-types", "reth-optimism-forks"]
optimism = ["reth-optimism-forks"]
std = [
"alloy-chains/std",
"alloy-eips/std",
Expand Down
240 changes: 1 addition & 239 deletions crates/chainspec/src/spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -590,15 +590,7 @@ impl ChainSpec {

impl From<Genesis> for ChainSpec {
fn from(genesis: Genesis) -> Self {
#[cfg(not(feature = "optimism"))]
{
into_ethereum_chain_spec(genesis)
}

#[cfg(feature = "optimism")]
{
into_optimism_chain_spec(genesis)
}
into_ethereum_chain_spec(genesis)
}
}

Expand Down Expand Up @@ -638,7 +630,6 @@ impl EthereumHardforks for ChainSpec {
impl reth_optimism_forks::OptimismHardforks for ChainSpec {}

/// Convert the given [`Genesis`] into an Ethereum [`ChainSpec`].
#[cfg(not(feature = "optimism"))]
fn into_ethereum_chain_spec(genesis: Genesis) -> ChainSpec {
// Block-based hardforks
let hardfork_opts = [
Expand Down Expand Up @@ -726,105 +717,6 @@ fn into_ethereum_chain_spec(genesis: Genesis) -> ChainSpec {
}
}

#[cfg(feature = "optimism")]
/// Convert the given [`Genesis`] into an Optimism [`ChainSpec`].
fn into_optimism_chain_spec(genesis: Genesis) -> ChainSpec {
use reth_optimism_forks::OptimismHardfork;
let optimism_genesis_info = OptimismGenesisInfo::extract_from(&genesis);
let genesis_info = optimism_genesis_info.optimism_chain_info.genesis_info.unwrap_or_default();

// Block-based hardforks
let hardfork_opts = [
(EthereumHardfork::Homestead.boxed(), genesis.config.homestead_block),
(EthereumHardfork::Tangerine.boxed(), genesis.config.eip150_block),
(EthereumHardfork::SpuriousDragon.boxed(), genesis.config.eip155_block),
(EthereumHardfork::Byzantium.boxed(), genesis.config.byzantium_block),
(EthereumHardfork::Constantinople.boxed(), genesis.config.constantinople_block),
(EthereumHardfork::Petersburg.boxed(), genesis.config.petersburg_block),
(EthereumHardfork::Istanbul.boxed(), genesis.config.istanbul_block),
(EthereumHardfork::MuirGlacier.boxed(), genesis.config.muir_glacier_block),
(EthereumHardfork::Berlin.boxed(), genesis.config.berlin_block),
(EthereumHardfork::London.boxed(), genesis.config.london_block),
(EthereumHardfork::ArrowGlacier.boxed(), genesis.config.arrow_glacier_block),
(EthereumHardfork::GrayGlacier.boxed(), genesis.config.gray_glacier_block),
(OptimismHardfork::Bedrock.boxed(), genesis_info.bedrock_block),
];
let mut block_hardforks = hardfork_opts
.into_iter()
.filter_map(|(hardfork, opt)| opt.map(|block| (hardfork, ForkCondition::Block(block))))
.collect::<Vec<_>>();

// Paris
let paris_block_and_final_difficulty =
if let Some(ttd) = genesis.config.terminal_total_difficulty {
block_hardforks.push((
EthereumHardfork::Paris.boxed(),
ForkCondition::TTD {
total_difficulty: ttd,
fork_block: genesis.config.merge_netsplit_block,
},
));

genesis.config.merge_netsplit_block.map(|block| (block, ttd))
} else {
None
};

// Time-based hardforks
let time_hardfork_opts = [
(EthereumHardfork::Shanghai.boxed(), genesis.config.shanghai_time),
(EthereumHardfork::Cancun.boxed(), genesis.config.cancun_time),
(EthereumHardfork::Prague.boxed(), genesis.config.prague_time),
(OptimismHardfork::Regolith.boxed(), genesis_info.regolith_time),
(OptimismHardfork::Canyon.boxed(), genesis_info.canyon_time),
(OptimismHardfork::Ecotone.boxed(), genesis_info.ecotone_time),
(OptimismHardfork::Fjord.boxed(), genesis_info.fjord_time),
(OptimismHardfork::Granite.boxed(), genesis_info.granite_time),
];

let mut time_hardforks = time_hardfork_opts
.into_iter()
.filter_map(|(hardfork, opt)| opt.map(|time| (hardfork, ForkCondition::Timestamp(time))))
.collect::<Vec<_>>();

block_hardforks.append(&mut time_hardforks);

// Ordered Hardforks
let mainnet_hardforks = OptimismHardfork::op_mainnet();
let mainnet_order = mainnet_hardforks.forks_iter();

let mut ordered_hardforks = Vec::with_capacity(block_hardforks.len());
for (hardfork, _) in mainnet_order {
if let Some(pos) = block_hardforks.iter().position(|(e, _)| **e == *hardfork) {
ordered_hardforks.push(block_hardforks.remove(pos));
}
}

// append the remaining unknown hardforks to ensure we don't filter any out
ordered_hardforks.append(&mut block_hardforks);

// NOTE: in full node, we prune all receipts except the deposit contract's. We do not
// have the deployment block in the genesis file, so we use block zero. We use the same
// deposit topic as the mainnet contract if we have the deposit contract address in the
// genesis json.
let deposit_contract = genesis.config.deposit_contract_address.map(|address| DepositContract {
address,
block: 0,
topic: MAINNET_DEPOSIT_CONTRACT.topic,
});

ChainSpec {
chain: genesis.config.chain_id.into(),
genesis,
genesis_hash: OnceCell::new(),
hardforks: ChainHardforks::new(ordered_hardforks),
paris_block_and_final_difficulty,
deposit_contract,
base_fee_params: optimism_genesis_info.base_fee_params,
..Default::default()
}
}

/// A trait for reading the current chainspec.
#[auto_impl::auto_impl(&, Arc)]
pub trait ChainSpecProvider: Send + Sync {
Expand Down Expand Up @@ -1102,59 +994,6 @@ impl DepositContract {
}
}

/// Genesis info for Optimism.
#[cfg(feature = "optimism")]
#[derive(Default, Debug, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
struct OptimismGenesisInfo {
optimism_chain_info: op_alloy_rpc_types::genesis::OptimismChainInfo,
#[serde(skip)]
base_fee_params: BaseFeeParamsKind,
}

#[cfg(feature = "optimism")]
impl OptimismGenesisInfo {
fn extract_from(genesis: &Genesis) -> Self {
let mut info = Self {
optimism_chain_info: op_alloy_rpc_types::genesis::OptimismChainInfo::extract_from(
&genesis.config.extra_fields,
)
.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),
),
(
reth_optimism_forks::OptimismHardfork::Canyon.boxed(),
BaseFeeParams::new(canyon_denominator as u128, elasticity as u128),
),
]
.into(),
)
} else {
BaseFeeParams::new(denominator as u128, elasticity as u128).into()
};

info.base_fee_params = base_fee_params;
}
}

info
}
}

/// Verifies [`ChainSpec`] configuration against expected data in given cases.
#[cfg(any(test, feature = "test-utils"))]
pub fn test_fork_ids(spec: &ChainSpec, cases: &[(Head, ForkId)]) {
Expand Down Expand Up @@ -2477,7 +2316,6 @@ Post-merge hard forks (timestamp based):
}

#[test]
#[cfg(not(feature = "optimism"))]
fn test_fork_order_ethereum_mainnet() {
let genesis = Genesis {
config: ChainConfig {
Expand Down Expand Up @@ -2534,80 +2372,4 @@ Post-merge hard forks (timestamp based):
.all(|(expected, actual)| &**expected == *actual));
assert_eq!(expected_hardforks.len(), hardforks.len());
}

#[test]
#[cfg(feature = "optimism")]
fn test_fork_order_optimism_mainnet() {
use reth_optimism_forks::OptimismHardfork;

let genesis = Genesis {
config: ChainConfig {
chain_id: 0,
homestead_block: Some(0),
dao_fork_block: Some(0),
dao_fork_support: false,
eip150_block: Some(0),
eip155_block: Some(0),
eip158_block: Some(0),
byzantium_block: Some(0),
constantinople_block: Some(0),
petersburg_block: Some(0),
istanbul_block: Some(0),
muir_glacier_block: Some(0),
berlin_block: Some(0),
london_block: Some(0),
arrow_glacier_block: Some(0),
gray_glacier_block: Some(0),
merge_netsplit_block: Some(0),
shanghai_time: Some(0),
cancun_time: Some(0),
terminal_total_difficulty: Some(U256::ZERO),
extra_fields: [
(String::from("bedrockBlock"), 0.into()),
(String::from("regolithTime"), 0.into()),
(String::from("canyonTime"), 0.into()),
(String::from("ecotoneTime"), 0.into()),
(String::from("fjordTime"), 0.into()),
(String::from("graniteTime"), 0.into()),
]
.into_iter()
.collect(),
..Default::default()
},
..Default::default()
};

let chain_spec: ChainSpec = into_optimism_chain_spec(genesis);

let hardforks: Vec<_> = chain_spec.hardforks.forks_iter().map(|(h, _)| h).collect();
let expected_hardforks = vec![
EthereumHardfork::Homestead.boxed(),
EthereumHardfork::Tangerine.boxed(),
EthereumHardfork::SpuriousDragon.boxed(),
EthereumHardfork::Byzantium.boxed(),
EthereumHardfork::Constantinople.boxed(),
EthereumHardfork::Petersburg.boxed(),
EthereumHardfork::Istanbul.boxed(),
EthereumHardfork::MuirGlacier.boxed(),
EthereumHardfork::Berlin.boxed(),
EthereumHardfork::London.boxed(),
EthereumHardfork::ArrowGlacier.boxed(),
EthereumHardfork::GrayGlacier.boxed(),
EthereumHardfork::Paris.boxed(),
OptimismHardfork::Bedrock.boxed(),
OptimismHardfork::Regolith.boxed(),
EthereumHardfork::Shanghai.boxed(),
OptimismHardfork::Canyon.boxed(),
EthereumHardfork::Cancun.boxed(),
OptimismHardfork::Ecotone.boxed(),
OptimismHardfork::Fjord.boxed(),
OptimismHardfork::Granite.boxed(),
];

assert!(expected_hardforks
.iter()
.zip(hardforks.iter())
.all(|(expected, actual)| &**expected == *actual));
assert_eq!(expected_hardforks.len(), hardforks.len());
}
}
5 changes: 4 additions & 1 deletion crates/optimism/chainspec/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,15 @@ alloy-chains.workspace = true
alloy-genesis.workspace = true
alloy-primitives.workspace = true

# op
op-alloy-rpc-types.workspace = true

# io
serde_json.workspace = true

# misc
once_cell.workspace = true
derive_more.workspace = true
once_cell.workspace = true

[dev-dependencies]
reth-chainspec = { workspace = true, features = ["test-utils"] }
Expand Down
10 changes: 10 additions & 0 deletions crates/optimism/chainspec/src/constants.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
//! OP stack variation of chain spec constants.

use alloy_primitives::{address, b256};
use reth_chainspec::DepositContract;

//------------------------------- BASE MAINNET -------------------------------//

/// Max gas limit on Base: <https://basescan.org/block/17208876>
Expand All @@ -9,3 +12,10 @@ pub const BASE_MAINNET_MAX_GAS_LIMIT: u64 = 105_000_000;

/// Max gas limit on Base Sepolia: <https://sepolia.basescan.org/block/12506483>
pub const BASE_SEPOLIA_MAX_GAS_LIMIT: u64 = 45_000_000;

/// Deposit contract address: `0x00000000219ab540356cbb839cbe05303d7705fa`
pub(crate) const MAINNET_DEPOSIT_CONTRACT: DepositContract = DepositContract::new(
address!("00000000219ab540356cbb839cbe05303d7705fa"),
11052984,
b256!("649bbc62d0e31342afea4e5cd82d4049e7e1ee912fc0889aa790803be39038c5"),
);
Comment on lines +15 to +21
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure if you prefer to duplicate or make it pub in reth-chainspec

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually need to check if we really need this for op

Loading
Loading