feat: add Hardfork::Cancun (#3933)

Co-authored-by: Dan Cline <6798349+Rjected@users.noreply.github.com>
This commit is contained in:
Matthias Seitz
2023-07-26 18:42:05 +02:00
committed by GitHub
parent 96b108f25a
commit aa5d39dd6d
3 changed files with 33 additions and 2 deletions

View File

@@ -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,
});

View File

@@ -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<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 {
@@ -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

View File

@@ -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<Hardfork> =