|
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; |
2 | 6 | use trevm::revm::{context::CfgEnv, primitives::hardfork::SpecId}; |
3 | 7 |
|
4 | | -/// PecorinoCfg holds network-level configuration values. |
| 8 | +/// [`SignetCfgEnv`] holds network-level configuration values. |
5 | 9 | #[derive(Debug, Clone, Copy)] |
6 | 10 | pub struct SignetCfgEnv { |
7 | 11 | /// The chain ID. |
8 | 12 | 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 | + } |
9 | 34 | } |
10 | 35 |
|
11 | 36 | 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. |
17 | 37 | 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 | + } |
19 | 64 |
|
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); |
22 | 69 | } |
23 | 70 | } |
0 commit comments