Skip to content

Commit

Permalink
feat: add Hardfork::Cancun (#3933)
Browse files Browse the repository at this point in the history
Co-authored-by: Dan Cline <[email protected]>
  • Loading branch information
mattsse and Rjected authored Jul 26, 2023
1 parent 96b108f commit aa5d39d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
2 changes: 1 addition & 1 deletion bin/reth/src/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
Expand Down
27 changes: 27 additions & 0 deletions crates/primitives/src/chain/spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)| {
Expand Down Expand Up @@ -433,6 +442,8 @@ impl From<Genesis> for ChainSpec {
pub struct ForkTimestamps {
/// The timestamp of the shanghai fork
pub shanghai: Option<u64>,
/// The timestamp of the cancun fork
pub cancun: Option<u64>,
}

impl ForkTimestamps {
Expand All @@ -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
}

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion crates/primitives/src/hardfork.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ pub enum Hardfork {
Paris,
/// Shanghai.
Shanghai,
/// Cancun.
Cancun,
}

impl Hardfork {
Expand Down Expand Up @@ -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)
Expand All @@ -97,7 +100,6 @@ impl Display for Hardfork {
#[cfg(test)]
mod tests {
use super::*;

use crate::{Chain, Genesis};
use std::collections::BTreeMap;

Expand All @@ -120,6 +122,7 @@ mod tests {
"grayglacier",
"PARIS",
"ShAnGhAI",
"CaNcUn",
];
let expected_hardforks = [
Hardfork::Frontier,
Expand All @@ -138,6 +141,7 @@ mod tests {
Hardfork::GrayGlacier,
Hardfork::Paris,
Hardfork::Shanghai,
Hardfork::Cancun,
];

let hardforks: Vec<Hardfork> =
Expand Down

0 comments on commit aa5d39d

Please sign in to comment.