Skip to content

Commit

Permalink
introduce taiko/hardforks and complete chainspec
Browse files Browse the repository at this point in the history
  • Loading branch information
mask-pp committed Nov 20, 2024
1 parent 13aff58 commit 3455496
Show file tree
Hide file tree
Showing 9 changed files with 496 additions and 11 deletions.
16 changes: 16 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ members = [
"testing/testing-utils",
"crates/taiko/primitives",
"crates/taiko/engine-primitives",
"crates/taiko/hardforks",
]
default-members = ["bin/reth"]
exclude = ["book/sources"]
Expand Down Expand Up @@ -413,6 +414,7 @@ reth-trie-parallel = { path = "crates/trie/parallel" }
# taiko
taiko-reth-primitives = { path = "crates/taiko/primitives" }
taiko-reth-engine-primitives = { path = "crates/taiko/engine-primitives" }
taiko-reth-forks = { path = "crates/taiko/hardforks" }

# revm
revm = { version = "14.0.3", features = [
Expand Down
6 changes: 6 additions & 0 deletions crates/chainspec/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ auto_impl.workspace = true
once_cell.workspace = true
serde_json.workspace = true
derive_more.workspace = true
serde = { version = "1.0.214", features = ["derive"] }
strum = "0.26.3"
num_enum = "0.7.3"
revm-primitives.workspace = true
taiko-reth-forks.workspace = true

[dev-dependencies]
# eth
Expand All @@ -52,3 +57,4 @@ std = [
]
arbitrary = ["alloy-chains/arbitrary"]
test-utils = []
taiko = []
6 changes: 5 additions & 1 deletion crates/chainspec/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
html_favicon_url = "https://avatars0.githubusercontent.com/u/97369466?s=256",
issue_tracker_base_url = "https://github.com/paradigmxyz/reth/issues/"
)]
#![cfg_attr(not(test), warn(unused_crate_dependencies))]
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
#![cfg_attr(not(feature = "std"), no_std)]

Expand All @@ -20,6 +19,11 @@ mod api;
mod info;
/// The chain spec module.
mod spec;
#[cfg(feature = "taiko")]
pub mod taiko;

#[cfg(feature = "taiko")]
pub use spec::{TAIKO_HEKLA, TAIKO_INTERNAL_L2_A, TAIKO_MAINNET, TAIKO_TESTNET};

pub use alloy_chains::{Chain, ChainKind, NamedChain};
/// Re-export for convenience
Expand Down
99 changes: 89 additions & 10 deletions crates/chainspec/src/spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@ use reth_primitives_traits::{
Header, SealedHeader,
};
use reth_trie_common::root::state_root_ref_unhashed;

use taiko_reth_forks::hardfork::TaikoHardfork;
use crate::{constants::MAINNET_DEPOSIT_CONTRACT, once_cell_set, EthChainSpec};

#[cfg(feature = "taiko")]
use crate::taiko::{get_taiko_genesis, TaikoNamedChain, HEKLA_ONTAKE_BLOCK, INTERNAL_DEVNET_ONTAKE_BLOCK, MAINNET_ONTAKE_BLOCK};

/// The Ethereum mainnet spec
pub static MAINNET: Lazy<Arc<ChainSpec>> = Lazy::new(|| {
let mut spec = ChainSpec {
Expand Down Expand Up @@ -118,7 +121,59 @@ pub static DEV: Lazy<Arc<ChainSpec>> = Lazy::new(|| {
deposit_contract: None, // TODO: do we even have?
..Default::default()
}
.into()
.into()
});

/// The Taiko internal L2 A spec
#[cfg(feature = "taiko")]
pub static TAIKO_INTERNAL_L2_A: Lazy<Arc<ChainSpec>> = Lazy::new(|| {
ChainSpec {
chain: TaikoNamedChain::TaikoInternalL2a.into(),
genesis: get_taiko_genesis(TaikoNamedChain::TaikoInternalL2a),
hardforks: TaikoHardfork::internal_l2_a(),
base_fee_params: BaseFeeParamsKind::Constant(BaseFeeParams {
max_change_denominator: 8,
elasticity_multiplier: 2,
}),
..Default::default()
}.into()
});

/// The Taiko testnet spec
#[cfg(feature = "taiko")]
pub static TAIKO_TESTNET: Lazy<Arc<ChainSpec>> = Lazy::new(|| {
ChainSpec {
chain: TaikoNamedChain::Katla.into(),
genesis: get_taiko_genesis(TaikoNamedChain::Katla),
hardforks: TaikoHardfork::testnet(),
base_fee_params: BaseFeeParamsKind::Constant(BaseFeeParams {
max_change_denominator: 8,
elasticity_multiplier: 2,
}),
..Default::default()
}.into()
});

/// The Taiko A7 spec
#[cfg(feature = "taiko")]
pub static TAIKO_HEKLA: Lazy<Arc<ChainSpec>> = Lazy::new(|| {
ChainSpec {
chain: TaikoNamedChain::Hekla.into(),
genesis: get_taiko_genesis(TaikoNamedChain::Hekla),
hardforks: TaikoHardfork::hekla(),
..Default::default()
}.into()
});

/// The Taiko Mainnet spec
#[cfg(feature = "taiko")]
pub static TAIKO_MAINNET: Lazy<Arc<ChainSpec>> = Lazy::new(|| {
ChainSpec {
chain: TaikoNamedChain::Mainnet.into(),
genesis: get_taiko_genesis(TaikoNamedChain::Mainnet),
hardforks: TaikoHardfork::mainnet(),
..Default::default()
}.into()
});

/// A wrapper around [`BaseFeeParams`] that allows for specifying constant or dynamic EIP-1559
Expand Down Expand Up @@ -254,6 +309,20 @@ impl ChainSpec {
self.chain.is_optimism()
}

/// Returns `true` if this is a Taiko chain.
#[inline]
#[cfg(not(feature = "taiko"))]
pub const fn is_taiko(&self) -> bool {
let id = self.chain.id();
id >= 167000 && id <= 168000
}

/// Returns `true` if ontake fork is active at the given block number.
#[inline]
pub fn is_ontake_fork(&self, block_number: u64) -> bool {
self.is_fork_active_at_block(TaikoHardfork::Ontake, block_number)
}

/// Returns `true` if this chain is Optimism mainnet.
#[inline]
pub fn is_optimism_mainnet(&self) -> bool {
Expand Down Expand Up @@ -347,7 +416,7 @@ impl ChainSpec {
// given timestamp.
for (fork, params) in bf_params.iter().rev() {
if self.hardforks.is_fork_active_at_timestamp(fork.clone(), timestamp) {
return *params
return *params;
}
}

Expand All @@ -366,7 +435,7 @@ impl ChainSpec {
// given timestamp.
for (fork, params) in bf_params.iter().rev() {
if self.hardforks.is_fork_active_at_block(fork.clone(), block_number) {
return *params
return *params;
}
}

Expand Down Expand Up @@ -481,7 +550,7 @@ impl ChainSpec {
} else {
// we can return here because this block fork is not active, so we set the
// `next` value
return ForkId { hash: forkhash, next: block }
return ForkId { hash: forkhash, next: block };
}
}
}
Expand All @@ -502,7 +571,7 @@ impl ChainSpec {
// can safely return here because we have already handled all block forks and
// have handled all active timestamp forks, and set the next value to the
// timestamp that is known but not active yet
return ForkId { hash: forkhash, next: timestamp }
return ForkId { hash: forkhash, next: timestamp };
}
}

Expand Down Expand Up @@ -546,17 +615,17 @@ impl ChainSpec {
ForkCondition::TTD { fork_block, .. } => {
// handle Sepolia merge netsplit case
if fork_block.is_some() {
return *fork_block
return *fork_block;
}
// ensure curr_cond is indeed ForkCondition::Block and return block_num
if let ForkCondition::Block(block_num) = curr_cond {
return Some(block_num)
return Some(block_num);
}
}
ForkCondition::Timestamp(_) => {
// ensure curr_cond is indeed ForkCondition::Block and return block_num
if let ForkCondition::Block(block_num) = curr_cond {
return Some(block_num)
return Some(block_num);
}
}
ForkCondition::Block(_) | ForkCondition::Never => continue,
Expand Down Expand Up @@ -590,6 +659,14 @@ impl ChainSpec {

impl From<Genesis> for ChainSpec {
fn from(genesis: Genesis) -> Self {
#[cfg(feature = "taiko")]
let ontake_block = match TaikoNamedChain::try_from(genesis.config.chain_id) {
Ok(TaikoNamedChain::TaikoInternalL2a) => Some(INTERNAL_DEVNET_ONTAKE_BLOCK),
Ok(TaikoNamedChain::Hekla) => Some(HEKLA_ONTAKE_BLOCK),
Ok(TaikoNamedChain::Mainnet) => Some(MAINNET_ONTAKE_BLOCK),
_ => None,
};

// Block-based hardforks
let hardfork_opts = [
(EthereumHardfork::Homestead.boxed(), genesis.config.homestead_block),
Expand All @@ -605,6 +682,8 @@ impl From<Genesis> for ChainSpec {
(EthereumHardfork::London.boxed(), genesis.config.london_block),
(EthereumHardfork::ArrowGlacier.boxed(), genesis.config.arrow_glacier_block),
(EthereumHardfork::GrayGlacier.boxed(), genesis.config.gray_glacier_block),
#[cfg(feature = "taiko")]
(TaikoHardfork::Hekla.boxed(), ontake_block),
];
let mut hardforks = hardfork_opts
.into_iter()
Expand Down Expand Up @@ -682,7 +761,7 @@ impl Hardforks for ChainSpec {
self.hardforks.fork(fork)
}

fn forks_iter(&self) -> impl Iterator<Item = (&dyn Hardfork, ForkCondition)> {
fn forks_iter(&self) -> impl Iterator<Item=(&dyn Hardfork, ForkCondition)> {
self.hardforks.forks_iter()
}

Expand Down
Loading

0 comments on commit 3455496

Please sign in to comment.