diff --git a/bin/reth/src/init.rs b/bin/reth/src/init.rs index a70949dbbe9d..cb6f885801b1 100644 --- a/bin/reth/src/init.rs +++ b/bin/reth/src/init.rs @@ -269,7 +269,7 @@ mod tests { ..Default::default() }, hardforks: BTreeMap::default(), - fork_timestamps: ForkTimestamps { shanghai: None }, + fork_timestamps: ForkTimestamps::default(), genesis_hash: None, paris_block_and_final_difficulty: None, }); diff --git a/crates/primitives/src/chain/spec.rs b/crates/primitives/src/chain/spec.rs index 356c9409b7e0..d86d375a44b3 100644 --- a/crates/primitives/src/chain/spec.rs +++ b/crates/primitives/src/chain/spec.rs @@ -306,6 +306,15 @@ impl ChainSpec { .unwrap_or_else(|| self.is_fork_active_at_timestamp(Hardfork::Shanghai, timestamp)) } + /// Convenience method to check if [Hardfork::Cancun] is active at a given timestamp. + #[inline] + pub fn is_cancun_activated_at_timestamp(&self, timestamp: u64) -> bool { + self.fork_timestamps + .cancun + .map(|cancun| timestamp >= cancun) + .unwrap_or_else(|| self.is_fork_active_at_timestamp(Hardfork::Cancun, timestamp)) + } + /// Creates a [`ForkFilter`](crate::ForkFilter) for the block described by [Head]. pub fn fork_filter(&self, head: Head) -> ForkFilter { let forks = self.forks_iter().filter_map(|(_, condition)| { @@ -433,6 +442,8 @@ impl From for ChainSpec { pub struct ForkTimestamps { /// The timestamp of the shanghai fork pub shanghai: Option, + /// The timestamp of the cancun fork + pub cancun: Option, } impl ForkTimestamps { @@ -442,6 +453,9 @@ impl ForkTimestamps { if let Some(shanghai) = forks.get(&Hardfork::Shanghai).and_then(|f| f.as_timestamp()) { timestamps = timestamps.shanghai(shanghai); } + if let Some(cancun) = forks.get(&Hardfork::Cancun).and_then(|f| f.as_timestamp()) { + timestamps = timestamps.cancun(cancun); + } timestamps } @@ -450,6 +464,12 @@ impl ForkTimestamps { self.shanghai = Some(shanghai); self } + + /// Sets the given cancun timestamp + pub fn cancun(mut self, cancun: u64) -> Self { + self.cancun = Some(cancun); + self + } } /// A helper type for compatibility with geth's config @@ -614,6 +634,13 @@ impl ChainSpecBuilder { self } + /// Enable Cancun at genesis. + pub fn cancun_activated(mut self) -> Self { + self = self.paris_activated(); + self.hardforks.insert(Hardfork::Cancun, ForkCondition::Timestamp(0)); + self + } + /// Build the resulting [`ChainSpec`]. /// /// # Panics diff --git a/crates/primitives/src/hardfork.rs b/crates/primitives/src/hardfork.rs index f26ffbcc4032..ba87a53ef77a 100644 --- a/crates/primitives/src/hardfork.rs +++ b/crates/primitives/src/hardfork.rs @@ -39,6 +39,8 @@ pub enum Hardfork { Paris, /// Shanghai. Shanghai, + /// Cancun. + Cancun, } impl Hardfork { @@ -82,6 +84,7 @@ impl FromStr for Hardfork { "grayglacier" => Hardfork::GrayGlacier, "paris" => Hardfork::Paris, "shanghai" => Hardfork::Shanghai, + "cancun" => Hardfork::Cancun, _ => return Err(format!("Unknown hardfork: {s}")), }; Ok(hardfork) @@ -97,7 +100,6 @@ impl Display for Hardfork { #[cfg(test)] mod tests { use super::*; - use crate::{Chain, Genesis}; use std::collections::BTreeMap; @@ -120,6 +122,7 @@ mod tests { "grayglacier", "PARIS", "ShAnGhAI", + "CaNcUn", ]; let expected_hardforks = [ Hardfork::Frontier, @@ -138,6 +141,7 @@ mod tests { Hardfork::GrayGlacier, Hardfork::Paris, Hardfork::Shanghai, + Hardfork::Cancun, ]; let hardforks: Vec =