Skip to content

Commit

Permalink
Add Canyon Hardfork (optimism)
Browse files Browse the repository at this point in the history
tests
  • Loading branch information
clabby committed Nov 18, 2023
1 parent 49d69c6 commit 1d86043
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 19 deletions.
69 changes: 58 additions & 11 deletions crates/primitives/src/chain/spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ pub static OP_GOERLI: Lazy<Arc<ChainSpec>> = Lazy::new(|| {
),
(Hardfork::Bedrock, ForkCondition::Block(4061224)),
(Hardfork::Regolith, ForkCondition::Timestamp(1679079600)),
(Hardfork::Canyon, ForkCondition::Timestamp(1699981200)),
]),
base_fee_params: BaseFeeParams::optimism(),
prune_delete_limit: 1700,
Expand Down Expand Up @@ -315,6 +316,7 @@ pub static BASE_GOERLI: Lazy<Arc<ChainSpec>> = Lazy::new(|| {
),
(Hardfork::Bedrock, ForkCondition::Block(0)),
(Hardfork::Regolith, ForkCondition::Timestamp(1683219600)),
(Hardfork::Canyon, ForkCondition::Timestamp(1699981200)),
]),
base_fee_params: BaseFeeParams::optimism_goerli(),
prune_delete_limit: 1700,
Expand Down Expand Up @@ -668,7 +670,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 @@ -689,7 +691,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 All @@ -704,7 +706,7 @@ impl ChainSpec {
// to satisfy every timestamp ForkCondition, we find the last ForkCondition::Block
// if one exists, and include its block_num in the returned Head
if let Some(last_block_num) = self.last_block_fork_before_merge_or_timestamp() {
return Head { timestamp, number: last_block_num, ..Default::default() }
return Head { timestamp, number: last_block_num, ..Default::default() };
}
Head { timestamp, ..Default::default() }
}
Expand Down Expand Up @@ -732,17 +734,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 @@ -1032,16 +1034,23 @@ impl ChainSpecBuilder {
self
}

/// Enable Regolith at the timestamp of activation.
/// For post-bedrock op-stack chains, this will be at genesis.
/// For pre-bedrock op-stack chains, this will be at the timestamp of regolith activation.
/// Enable Regolith at genesis
#[cfg(feature = "optimism")]
pub fn regolith_activated(mut self) -> Self {
self = self.bedrock_activated();
self.hardforks.insert(Hardfork::Regolith, ForkCondition::Timestamp(0));
self
}

/// Enable Canyon at genesis
#[cfg(feature = "optimism")]
pub fn canyon_activated(mut self) -> Self {
self = self.regolith_activated();
self.hardforks.insert(Hardfork::Shanghai, ForkCondition::Timestamp(0));
self.hardforks.insert(Hardfork::Canyon, ForkCondition::Timestamp(0));
self
}

/// Build the resulting [`ChainSpec`].
///
/// # Panics
Expand Down Expand Up @@ -1989,8 +1998,46 @@ Post-merge hard forks (timestamp based):
ForkId { hash: ForkHash([0x03, 0x47, 0x85, 0x69]), next: 1679079600 },
),
(
Head { number: 4061224, timestamp: 1679079600, ..Default::default() },
ForkId { hash: ForkHash([0x6d, 0x43, 0x1d, 0x6c]), next: 0 },
Head { number: 4061225, timestamp: 1679079600, ..Default::default() },
ForkId { hash: ForkHash([0x6d, 0x43, 0x1d, 0x6c]), next: 1699981200 },
),
(
Head { number: 4061226, timestamp: 1699981199, ..Default::default() },
ForkId { hash: ForkHash([0x6d, 0x43, 0x1d, 0x6c]), next: 1699981200 },
),
(
Head { number: 4061227, timestamp: 1699981200, ..Default::default() },
ForkId { hash: ForkHash([0x7f, 0x4a, 0x72, 0x1f]), next: 0 },
),
],
);
}

#[cfg(feature = "optimism")]
#[test]
fn base_goerli_forkids() {
test_fork_ids(
&BASE_GOERLI,
&[
(
Head { number: 0, ..Default::default() },
ForkId { hash: ForkHash([0xd4, 0x0c, 0x23, 0x50]), next: 1683219600 },
),
(
Head { number: 1, timestamp: 1683219599, ..Default::default() },
ForkId { hash: ForkHash([0xd4, 0x0c, 0x23, 0x50]), next: 1683219600 },
),
(
Head { number: 2, timestamp: 1683219600, ..Default::default() },
ForkId { hash: ForkHash([0xd5, 0x45, 0x43, 0x5d]), next: 1699981200 },
),
(
Head { number: 3, timestamp: 1699981199, ..Default::default() },
ForkId { hash: ForkHash([0xd5, 0x45, 0x43, 0x5d]), next: 1699981200 },
),
(
Head { number: 4, timestamp: 1699981200, ..Default::default() },
ForkId { hash: ForkHash([0xb3, 0x29, 0x13, 0xde]), next: 0 },
),
],
);
Expand Down
9 changes: 7 additions & 2 deletions crates/primitives/src/hardfork.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ pub enum Hardfork {
/// Regolith
#[cfg(feature = "optimism")]
Regolith,
/// Canyon
#[cfg(feature = "optimism")]
Canyon,
}

impl Hardfork {
Expand Down Expand Up @@ -95,6 +98,8 @@ impl FromStr for Hardfork {
"bedrock" => Hardfork::Bedrock,
#[cfg(feature = "optimism")]
"regolith" => Hardfork::Regolith,
#[cfg(feature = "optimism")]
"canyon" => Hardfork::Canyon,
_ => return Err(format!("Unknown hardfork: {s}")),
};
Ok(hardfork)
Expand Down Expand Up @@ -163,8 +168,8 @@ mod tests {
#[test]
#[cfg(feature = "optimism")]
fn check_op_hardfork_from_str() {
let hardfork_str = ["beDrOck", "rEgOlITH"];
let expected_hardforks = [Hardfork::Bedrock, Hardfork::Regolith];
let hardfork_str = ["beDrOck", "rEgOlITH", "cAnYoN"];
let expected_hardforks = [Hardfork::Bedrock, Hardfork::Regolith, Hardfork::Canyon];

let hardforks: Vec<Hardfork> =
hardfork_str.iter().map(|h| Hardfork::from_str(h).unwrap()).collect();
Expand Down
21 changes: 15 additions & 6 deletions crates/primitives/src/revm/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ pub fn revm_spec_by_timestamp_after_merge(
) -> revm_primitives::SpecId {
#[cfg(feature = "optimism")]
if chain_spec.is_optimism() {
if chain_spec.fork(Hardfork::Regolith).active_at_timestamp(timestamp) {
return revm_primitives::REGOLITH
if chain_spec.fork(Hardfork::Canyon).active_at_timestamp(timestamp) {
unimplemented!()
} else if chain_spec.fork(Hardfork::Regolith).active_at_timestamp(timestamp) {
return revm_primitives::REGOLITH;
} else {
return revm_primitives::BEDROCK
return revm_primitives::BEDROCK;
}
}

Expand All @@ -30,10 +32,12 @@ pub fn revm_spec_by_timestamp_after_merge(
pub fn revm_spec(chain_spec: &ChainSpec, block: Head) -> revm_primitives::SpecId {
#[cfg(feature = "optimism")]
if chain_spec.is_optimism() {
if chain_spec.fork(Hardfork::Regolith).active_at_head(&block) {
return revm_primitives::REGOLITH
if chain_spec.fork(Hardfork::Canyon).active_at_head(&block) {
unimplemented!()
} else if chain_spec.fork(Hardfork::Regolith).active_at_head(&block) {
return revm_primitives::REGOLITH;
} else if chain_spec.fork(Hardfork::Bedrock).active_at_head(&block) {
return revm_primitives::BEDROCK
return revm_primitives::BEDROCK;
}
}

Expand Down Expand Up @@ -138,6 +142,11 @@ mod tests {
f(cs).build()
}

// TODO: Enable once `reth` has been updated to a revm version that knows about Canyon.
// assert_eq!(
// revm_spec(&op_cs(|cs| cs.canyon_activated()), Head::default()),
// revm_primitives::CANYON
// );
assert_eq!(
revm_spec(&op_cs(|cs| cs.bedrock_activated()), Head::default()),
revm_primitives::BEDROCK
Expand Down

0 comments on commit 1d86043

Please sign in to comment.