Skip to content

Commit 4d78c4b

Browse files
committed
chore: refactor spec id loading to init only once
1 parent cd05cc9 commit 4d78c4b

File tree

1 file changed

+34
-15
lines changed

1 file changed

+34
-15
lines changed

src/tasks/block/cfg.rs

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,15 @@
22
use reth_chainspec::ChainSpec;
33
use signet_block_processor::revm_spec;
44
use signet_constants::{mainnet, pecorino};
5-
use signet_genesis::PECORINO_GENESIS;
6-
use std::sync::LazyLock;
5+
use signet_genesis::{MAINNET_GENESIS, PECORINO_GENESIS};
6+
use std::sync::OnceLock;
77
use trevm::revm::{context::CfgEnv, primitives::hardfork::SpecId};
88

9-
/// The RU Pecorino [`ChainSpec`].
10-
static PECORINO_SPEC: LazyLock<ChainSpec> =
11-
LazyLock::new(|| ChainSpec::from_genesis(PECORINO_GENESIS.to_owned()));
9+
/// The RU [`ChainSpec`].
10+
static RU_SPEC: OnceLock<SpecId> = OnceLock::new();
1211

13-
/// The RU Mainnet [`ChainSpec`].
14-
static MAINNET_RU_SPEC: LazyLock<ChainSpec> =
15-
LazyLock::new(|| ChainSpec::from_genesis(signet_genesis::MAINNET_GENESIS.to_owned()));
12+
/// The Host [`ChainSpec`].
13+
static HOST_SPEC: OnceLock<SpecId> = OnceLock::new();
1614

1715
/// [`SignetCfgEnv`] holds network-level configuration values.
1816
#[derive(Debug, Clone, Copy)]
@@ -30,19 +28,40 @@ impl SignetCfgEnv {
3028
}
3129

3230
fn spec_id(&self) -> SpecId {
33-
match self.chain_id {
34-
// Pecorino
35-
pecorino::HOST_CHAIN_ID | pecorino::RU_CHAIN_ID => {
36-
revm_spec(&PECORINO_SPEC, self.timestamp)
31+
*match self.chain_id {
32+
pecorino::RU_CHAIN_ID | mainnet::RU_CHAIN_ID => {
33+
RU_SPEC.get_or_init(|| initialize_ru_spec(self.chain_id, self.timestamp))
34+
}
35+
pecorino::HOST_CHAIN_ID | mainnet::HOST_CHAIN_ID => {
36+
HOST_SPEC.get_or_init(|| initialize_host_spec(self.chain_id, self.timestamp))
3737
}
38-
// Mainnet RU
39-
mainnet::RU_CHAIN_ID => revm_spec(&MAINNET_RU_SPEC, self.timestamp),
40-
mainnet::HOST_CHAIN_ID => revm_spec(&reth_chainspec::MAINNET, self.timestamp),
4138
_ => unimplemented!("Unknown chain ID: {}", self.chain_id),
4239
}
4340
}
4441
}
4542

43+
fn initialize_ru_spec(chain_id: u64, timestamp: u64) -> SpecId {
44+
match chain_id {
45+
pecorino::RU_CHAIN_ID => {
46+
revm_spec(&ChainSpec::from_genesis(PECORINO_GENESIS.to_owned()), timestamp)
47+
}
48+
mainnet::RU_CHAIN_ID => {
49+
revm_spec(&ChainSpec::from_genesis(MAINNET_GENESIS.to_owned()), timestamp)
50+
}
51+
_ => unimplemented!("Unknown chain ID: {}", chain_id),
52+
}
53+
}
54+
55+
fn initialize_host_spec(chain_id: u64, timestamp: u64) -> SpecId {
56+
match chain_id {
57+
pecorino::HOST_CHAIN_ID => {
58+
revm_spec(&ChainSpec::from_genesis(PECORINO_GENESIS.to_owned()), timestamp)
59+
}
60+
mainnet::HOST_CHAIN_ID => revm_spec(&reth_chainspec::MAINNET, timestamp),
61+
_ => unimplemented!("Unknown chain ID: {}", chain_id),
62+
}
63+
}
64+
4665
impl trevm::Cfg for SignetCfgEnv {
4766
fn fill_cfg_env(&self, cfg_env: &mut CfgEnv) {
4867
cfg_env.chain_id = self.chain_id;

0 commit comments

Comments
 (0)