Skip to content

Commit 972cce3

Browse files
committed
feat(sim): Set appropiate spec id depending on chain.
Closes ENG-1506. Appropiately sets the `SpecId` for each chain, depending on which fork is active. This only discriminates between the "expected to activate" hard fork, and the current hard fork.
1 parent 32d1725 commit 972cce3

File tree

4 files changed

+80
-13
lines changed

4 files changed

+80
-13
lines changed

Cargo.lock

Lines changed: 17 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ alloy = { version = "1.0.35", features = [
4141
"getrandom",
4242
"provider-mev-api",
4343
] }
44+
alloy-hardforks = "0.4.0"
45+
alloy-chains = "0.2"
4446

4547
axum = "0.7.5"
4648
eyre = "0.6.12"

src/tasks/block/cfg.rs

Lines changed: 57 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,70 @@
1-
//! This file implements the [`trevm::Cfg`] and [`trevm::Block`] traits for Pecorino blocks.
1+
//! This file implements the [`trevm::Cfg`] and [`trevm::Block`] traits for Signet and host networks.
2+
3+
use alloy_chains::NamedChain;
4+
use alloy_hardforks::mainnet::MAINNET_OSAKA_TIMESTAMP;
5+
use signet_constants::pecorino;
26
use trevm::revm::{context::CfgEnv, primitives::hardfork::SpecId};
37

4-
/// PecorinoCfg holds network-level configuration values.
8+
/// [`SignetCfgEnv`] holds network-level configuration values.
59
#[derive(Debug, Clone, Copy)]
610
pub struct SignetCfgEnv {
711
/// The chain ID.
812
pub chain_id: u64,
13+
/// The block timestamp.
14+
pub timestamp: u64,
15+
}
16+
17+
impl SignetCfgEnv {
18+
/// Creates a new [`SignetCfgEnv`].
19+
pub const fn new(chain_id: u64, timestamp: u64) -> Self {
20+
Self { chain_id, timestamp }
21+
}
22+
23+
const fn spec_id(&self) -> SpecId {
24+
match self.chain_id {
25+
pecorino::HOST_CHAIN_ID | pecorino::RU_CHAIN_ID => SpecId::PRAGUE,
26+
id if id == NamedChain::Mainnet as u64 => self.mainnet_spec(),
27+
_ => SpecId::PRAGUE,
28+
}
29+
}
30+
31+
const fn mainnet_spec(&self) -> SpecId {
32+
if self.timestamp >= MAINNET_OSAKA_TIMESTAMP { SpecId::OSAKA } else { SpecId::PRAGUE }
33+
}
934
}
1035

1136
impl trevm::Cfg for SignetCfgEnv {
12-
/// Fills the configuration environment with Pecorino-specific values.
13-
///
14-
/// # Arguments
15-
///
16-
/// - `cfg_env`: The configuration environment to be filled.
1737
fn fill_cfg_env(&self, cfg_env: &mut CfgEnv) {
18-
let CfgEnv { chain_id, spec, .. } = cfg_env;
38+
cfg_env.chain_id = self.chain_id;
39+
cfg_env.spec = self.spec_id();
40+
}
41+
}
42+
43+
#[cfg(test)]
44+
mod tests {
45+
use super::*;
46+
47+
#[test]
48+
fn pecorino_cfg_env() {
49+
let cfg = SignetCfgEnv::new(pecorino::HOST_CHAIN_ID, 0);
50+
assert_eq!(cfg.spec_id(), SpecId::PRAGUE);
51+
52+
let cfg = SignetCfgEnv::new(pecorino::RU_CHAIN_ID, 0);
53+
assert_eq!(cfg.spec_id(), SpecId::PRAGUE);
54+
}
55+
56+
#[test]
57+
fn mainnet_cfg_env() {
58+
let cfg = SignetCfgEnv::new(NamedChain::Mainnet as u64, MAINNET_OSAKA_TIMESTAMP - 1);
59+
assert_eq!(cfg.spec_id(), SpecId::PRAGUE);
60+
61+
let cfg = SignetCfgEnv::new(NamedChain::Mainnet as u64, MAINNET_OSAKA_TIMESTAMP);
62+
assert_eq!(cfg.spec_id(), SpecId::OSAKA);
63+
}
1964

20-
*chain_id = self.chain_id;
21-
*spec = SpecId::default();
65+
#[test]
66+
fn unknown_chain_cfg_env() {
67+
let cfg = SignetCfgEnv::new(999999, 0);
68+
assert_eq!(cfg.spec_id(), SpecId::PRAGUE);
2269
}
2370
}

src/tasks/env.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,8 @@ impl SimEnv {
162162
constants: &SignetSystemConstants,
163163
provider: RuProvider,
164164
) -> SimRollupEnv {
165-
let rollup_cfg = SignetCfgEnv { chain_id: constants.ru_chain_id() };
165+
let timestamp = self.rollup_env().timestamp.to::<u64>();
166+
let rollup_cfg = SignetCfgEnv::new(constants.ru_chain_id(), timestamp);
166167
RollupEnv::new(self.rollup_db(provider), constants.clone(), &rollup_cfg, self.rollup_env())
167168
}
168169

@@ -177,7 +178,8 @@ impl SimEnv {
177178
constants: &SignetSystemConstants,
178179
provider: HostProvider,
179180
) -> SimHostEnv {
180-
let host_cfg = SignetCfgEnv { chain_id: constants.host_chain_id() };
181+
let timestamp = self.host_env().timestamp.to::<u64>();
182+
let host_cfg = SignetCfgEnv::new(constants.host_chain_id(), timestamp);
181183
HostEnv::new(self.host_db(provider), constants.clone(), &host_cfg, self.host_env())
182184
}
183185
}

0 commit comments

Comments
 (0)