1pub use alloy_eips::eip1559::BaseFeeParams;
2use alloy_evm::eth::spec::EthExecutorSpec;
3
4use crate::{
5 constants::{MAINNET_DEPOSIT_CONTRACT, MAINNET_PRUNE_DELETE_LIMIT},
6 ethereum::SEPOLIA_PARIS_TTD,
7 holesky, hoodi, mainnet,
8 mainnet::{MAINNET_PARIS_BLOCK, MAINNET_PARIS_TTD},
9 sepolia,
10 sepolia::SEPOLIA_PARIS_BLOCK,
11 EthChainSpec,
12};
13use alloc::{
14 boxed::Box,
15 collections::BTreeMap,
16 format,
17 string::{String, ToString},
18 sync::Arc,
19 vec::Vec,
20};
21use alloy_chains::{Chain, NamedChain};
22use alloy_consensus::{
23 constants::{
24 EMPTY_WITHDRAWALS, HOLESKY_GENESIS_HASH, HOODI_GENESIS_HASH, MAINNET_GENESIS_HASH,
25 SEPOLIA_GENESIS_HASH,
26 },
27 Header,
28};
29use alloy_eips::{
30 eip1559::INITIAL_BASE_FEE, eip7685::EMPTY_REQUESTS_HASH, eip7840::BlobParams,
31 eip7892::BlobScheduleBlobParams,
32};
33use alloy_genesis::{ChainConfig, Genesis};
34use alloy_primitives::{address, b256, Address, BlockNumber, B256, U256};
35use alloy_trie::root::state_root_ref_unhashed;
36use core::fmt::Debug;
37use derive_more::From;
38use reth_ethereum_forks::{
39 ChainHardforks, DisplayHardforks, EthereumHardfork, EthereumHardforks, ForkCondition,
40 ForkFilter, ForkFilterKey, ForkHash, ForkId, Hardfork, Hardforks, Head, DEV_HARDFORKS,
41};
42use reth_network_peers::{holesky_nodes, hoodi_nodes, mainnet_nodes, sepolia_nodes, NodeRecord};
43use reth_primitives_traits::{sync::LazyLock, BlockHeader, SealedHeader};
44
45pub fn make_genesis_header(genesis: &Genesis, hardforks: &ChainHardforks) -> Header {
47 let base_fee_per_gas = hardforks
49 .fork(EthereumHardfork::London)
50 .active_at_block(0)
51 .then(|| genesis.base_fee_per_gas.map(|fee| fee as u64).unwrap_or(INITIAL_BASE_FEE));
52
53 let withdrawals_root = hardforks
56 .fork(EthereumHardfork::Shanghai)
57 .active_at_timestamp(genesis.timestamp)
58 .then_some(EMPTY_WITHDRAWALS);
59
60 let (parent_beacon_block_root, blob_gas_used, excess_blob_gas) =
65 if hardforks.fork(EthereumHardfork::Cancun).active_at_timestamp(genesis.timestamp) {
66 let blob_gas_used = genesis.blob_gas_used.unwrap_or(0);
67 let excess_blob_gas = genesis.excess_blob_gas.unwrap_or(0);
68 (Some(B256::ZERO), Some(blob_gas_used), Some(excess_blob_gas))
69 } else {
70 (None, None, None)
71 };
72
73 let requests_hash = hardforks
75 .fork(EthereumHardfork::Prague)
76 .active_at_timestamp(genesis.timestamp)
77 .then_some(EMPTY_REQUESTS_HASH);
78
79 Header {
80 number: genesis.number.unwrap_or_default(),
81 parent_hash: genesis.parent_hash.unwrap_or_default(),
82 gas_limit: genesis.gas_limit,
83 difficulty: genesis.difficulty,
84 nonce: genesis.nonce.into(),
85 extra_data: genesis.extra_data.clone(),
86 state_root: state_root_ref_unhashed(&genesis.alloc),
87 timestamp: genesis.timestamp,
88 mix_hash: genesis.mix_hash,
89 beneficiary: genesis.coinbase,
90 base_fee_per_gas,
91 withdrawals_root,
92 parent_beacon_block_root,
93 blob_gas_used,
94 excess_blob_gas,
95 requests_hash,
96 ..Default::default()
97 }
98}
99
100pub static MAINNET: LazyLock<Arc<ChainSpec>> = LazyLock::new(|| {
102 let genesis = serde_json::from_str(include_str!("../res/genesis/mainnet.json"))
103 .expect("Can't deserialize Mainnet genesis json");
104 let hardforks = EthereumHardfork::mainnet().into();
105 let mut spec = ChainSpec {
106 chain: Chain::mainnet(),
107 genesis_header: SealedHeader::new(
108 make_genesis_header(&genesis, &hardforks),
109 MAINNET_GENESIS_HASH,
110 ),
111 genesis,
112 paris_block_and_final_difficulty: Some((
114 MAINNET_PARIS_BLOCK,
115 U256::from(58_750_003_716_598_352_816_469u128),
116 )),
117 hardforks,
118 deposit_contract: Some(MAINNET_DEPOSIT_CONTRACT),
120 base_fee_params: BaseFeeParamsKind::Constant(BaseFeeParams::ethereum()),
121 prune_delete_limit: MAINNET_PRUNE_DELETE_LIMIT,
122 blob_params: BlobScheduleBlobParams::default().with_scheduled([
123 (mainnet::MAINNET_BPO1_TIMESTAMP, BlobParams::bpo1()),
124 (mainnet::MAINNET_BPO2_TIMESTAMP, BlobParams::bpo2()),
125 ]),
126 };
127 spec.genesis.config.dao_fork_support = true;
128 spec.into()
129});
130
131pub static SEPOLIA: LazyLock<Arc<ChainSpec>> = LazyLock::new(|| {
133 let genesis = serde_json::from_str(include_str!("../res/genesis/sepolia.json"))
134 .expect("Can't deserialize Sepolia genesis json");
135 let hardforks = EthereumHardfork::sepolia().into();
136 let mut spec = ChainSpec {
137 chain: Chain::sepolia(),
138 genesis_header: SealedHeader::new(
139 make_genesis_header(&genesis, &hardforks),
140 SEPOLIA_GENESIS_HASH,
141 ),
142 genesis,
143 paris_block_and_final_difficulty: Some((
145 SEPOLIA_PARIS_BLOCK,
146 U256::from(17_000_018_015_853_232u128),
147 )),
148 hardforks,
149 deposit_contract: Some(DepositContract::new(
151 address!("0x7f02c3e3c98b133055b8b348b2ac625669ed295d"),
152 1273020,
153 b256!("0x649bbc62d0e31342afea4e5cd82d4049e7e1ee912fc0889aa790803be39038c5"),
154 )),
155 base_fee_params: BaseFeeParamsKind::Constant(BaseFeeParams::ethereum()),
156 prune_delete_limit: 10000,
157 blob_params: BlobScheduleBlobParams::default().with_scheduled([
158 (sepolia::SEPOLIA_BPO1_TIMESTAMP, BlobParams::bpo1()),
159 (sepolia::SEPOLIA_BPO2_TIMESTAMP, BlobParams::bpo2()),
160 ]),
161 };
162 spec.genesis.config.dao_fork_support = true;
163 spec.into()
164});
165
166pub static HOLESKY: LazyLock<Arc<ChainSpec>> = LazyLock::new(|| {
168 let genesis = serde_json::from_str(include_str!("../res/genesis/holesky.json"))
169 .expect("Can't deserialize Holesky genesis json");
170 let hardforks = EthereumHardfork::holesky().into();
171 let mut spec = ChainSpec {
172 chain: Chain::holesky(),
173 genesis_header: SealedHeader::new(
174 make_genesis_header(&genesis, &hardforks),
175 HOLESKY_GENESIS_HASH,
176 ),
177 genesis,
178 paris_block_and_final_difficulty: Some((0, U256::from(1))),
179 hardforks,
180 deposit_contract: Some(DepositContract::new(
181 address!("0x4242424242424242424242424242424242424242"),
182 0,
183 b256!("0x649bbc62d0e31342afea4e5cd82d4049e7e1ee912fc0889aa790803be39038c5"),
184 )),
185 base_fee_params: BaseFeeParamsKind::Constant(BaseFeeParams::ethereum()),
186 prune_delete_limit: 10000,
187 blob_params: BlobScheduleBlobParams::default().with_scheduled([
188 (holesky::HOLESKY_BPO1_TIMESTAMP, BlobParams::bpo1()),
189 (holesky::HOLESKY_BPO2_TIMESTAMP, BlobParams::bpo2()),
190 ]),
191 };
192 spec.genesis.config.dao_fork_support = true;
193 spec.into()
194});
195
196pub static HOODI: LazyLock<Arc<ChainSpec>> = LazyLock::new(|| {
200 let genesis = serde_json::from_str(include_str!("../res/genesis/hoodi.json"))
201 .expect("Can't deserialize Hoodi genesis json");
202 let hardforks = EthereumHardfork::hoodi().into();
203 let mut spec = ChainSpec {
204 chain: Chain::hoodi(),
205 genesis_header: SealedHeader::new(
206 make_genesis_header(&genesis, &hardforks),
207 HOODI_GENESIS_HASH,
208 ),
209 genesis,
210 paris_block_and_final_difficulty: Some((0, U256::from(0))),
211 hardforks,
212 deposit_contract: Some(DepositContract::new(
213 address!("0x00000000219ab540356cBB839Cbe05303d7705Fa"),
214 0,
215 b256!("0x649bbc62d0e31342afea4e5cd82d4049e7e1ee912fc0889aa790803be39038c5"),
216 )),
217 base_fee_params: BaseFeeParamsKind::Constant(BaseFeeParams::ethereum()),
218 prune_delete_limit: 10000,
219 blob_params: BlobScheduleBlobParams::default().with_scheduled([
220 (hoodi::HOODI_BPO1_TIMESTAMP, BlobParams::bpo1()),
221 (hoodi::HOODI_BPO2_TIMESTAMP, BlobParams::bpo2()),
222 ]),
223 };
224 spec.genesis.config.dao_fork_support = true;
225 spec.into()
226});
227
228pub static DEV: LazyLock<Arc<ChainSpec>> = LazyLock::new(|| {
233 let genesis = serde_json::from_str(include_str!("../res/genesis/dev.json"))
234 .expect("Can't deserialize Dev testnet genesis json");
235 let hardforks = DEV_HARDFORKS.clone();
236 ChainSpec {
237 chain: Chain::dev(),
238 genesis_header: SealedHeader::seal_slow(make_genesis_header(&genesis, &hardforks)),
239 genesis,
240 paris_block_and_final_difficulty: Some((0, U256::from(0))),
241 hardforks,
242 base_fee_params: BaseFeeParamsKind::Constant(BaseFeeParams::ethereum()),
243 deposit_contract: None, ..Default::default()
245 }
246 .into()
247});
248
249pub fn create_chain_config(
252 chain: Option<Chain>,
253 hardforks: &ChainHardforks,
254 deposit_contract_address: Option<Address>,
255 blob_schedule: BTreeMap<String, BlobParams>,
256) -> ChainConfig {
257 let block_num = |fork: EthereumHardfork| hardforks.fork(fork).block_number();
259
260 let timestamp = |fork: EthereumHardfork| -> Option<u64> {
262 match hardforks.fork(fork) {
263 ForkCondition::Timestamp(t) => Some(t),
264 _ => None,
265 }
266 };
267
268 let (terminal_total_difficulty, terminal_total_difficulty_passed) =
270 match hardforks.fork(EthereumHardfork::Paris) {
271 ForkCondition::TTD { total_difficulty, .. } => (Some(total_difficulty), true),
272 _ => (None, false),
273 };
274
275 let dao_fork_support = hardforks.fork(EthereumHardfork::Dao) != ForkCondition::Never;
277
278 #[allow(clippy::needless_update)]
279 ChainConfig {
280 chain_id: chain.map(|c| c.id()).unwrap_or(0),
281 homestead_block: block_num(EthereumHardfork::Homestead),
282 dao_fork_block: block_num(EthereumHardfork::Dao),
283 dao_fork_support,
284 eip150_block: block_num(EthereumHardfork::Tangerine),
285 eip155_block: block_num(EthereumHardfork::SpuriousDragon),
286 eip158_block: block_num(EthereumHardfork::SpuriousDragon),
287 byzantium_block: block_num(EthereumHardfork::Byzantium),
288 constantinople_block: block_num(EthereumHardfork::Constantinople),
289 petersburg_block: block_num(EthereumHardfork::Petersburg),
290 istanbul_block: block_num(EthereumHardfork::Istanbul),
291 muir_glacier_block: block_num(EthereumHardfork::MuirGlacier),
292 berlin_block: block_num(EthereumHardfork::Berlin),
293 london_block: block_num(EthereumHardfork::London),
294 arrow_glacier_block: block_num(EthereumHardfork::ArrowGlacier),
295 gray_glacier_block: block_num(EthereumHardfork::GrayGlacier),
296 merge_netsplit_block: None,
297 shanghai_time: timestamp(EthereumHardfork::Shanghai),
298 cancun_time: timestamp(EthereumHardfork::Cancun),
299 prague_time: timestamp(EthereumHardfork::Prague),
300 osaka_time: timestamp(EthereumHardfork::Osaka),
301 bpo1_time: timestamp(EthereumHardfork::Bpo1),
302 bpo2_time: timestamp(EthereumHardfork::Bpo2),
303 bpo3_time: timestamp(EthereumHardfork::Bpo3),
304 bpo4_time: timestamp(EthereumHardfork::Bpo4),
305 bpo5_time: timestamp(EthereumHardfork::Bpo5),
306 terminal_total_difficulty,
307 terminal_total_difficulty_passed,
308 ethash: None,
309 clique: None,
310 parlia: None,
311 extra_fields: Default::default(),
312 deposit_contract_address,
313 blob_schedule,
314 ..Default::default()
315 }
316}
317
318pub fn mainnet_chain_config() -> ChainConfig {
320 let hardforks: ChainHardforks = EthereumHardfork::mainnet().into();
321 let blob_schedule = blob_params_to_schedule(&MAINNET.blob_params, &hardforks);
322 create_chain_config(
323 Some(Chain::mainnet()),
324 &hardforks,
325 Some(MAINNET_DEPOSIT_CONTRACT.address),
326 blob_schedule,
327 )
328}
329
330pub fn blob_params_to_schedule(
332 params: &BlobScheduleBlobParams,
333 hardforks: &ChainHardforks,
334) -> BTreeMap<String, BlobParams> {
335 let mut schedule = BTreeMap::new();
336 schedule.insert("cancun".to_string(), params.cancun);
337 schedule.insert("prague".to_string(), params.prague);
338 schedule.insert("osaka".to_string(), params.osaka);
339
340 let bpo_forks = EthereumHardfork::bpo_variants();
342 for (timestamp, blob_params) in ¶ms.scheduled {
343 for bpo_fork in bpo_forks {
344 if let ForkCondition::Timestamp(fork_ts) = hardforks.fork(bpo_fork) &&
345 fork_ts == *timestamp
346 {
347 schedule.insert(bpo_fork.name().to_lowercase(), *blob_params);
348 break;
349 }
350 }
351 }
352
353 schedule
354}
355
356#[derive(Clone, Debug, PartialEq, Eq)]
359pub enum BaseFeeParamsKind {
360 Constant(BaseFeeParams),
362 Variable(ForkBaseFeeParams),
365}
366
367impl Default for BaseFeeParamsKind {
368 fn default() -> Self {
369 BaseFeeParams::ethereum().into()
370 }
371}
372
373impl From<BaseFeeParams> for BaseFeeParamsKind {
374 fn from(params: BaseFeeParams) -> Self {
375 Self::Constant(params)
376 }
377}
378
379impl From<ForkBaseFeeParams> for BaseFeeParamsKind {
380 fn from(params: ForkBaseFeeParams) -> Self {
381 Self::Variable(params)
382 }
383}
384
385#[derive(Clone, Debug, PartialEq, Eq, From)]
388pub struct ForkBaseFeeParams(Vec<(Box<dyn Hardfork>, BaseFeeParams)>);
389
390impl<H: BlockHeader> core::ops::Deref for ChainSpec<H> {
391 type Target = ChainHardforks;
392
393 fn deref(&self) -> &Self::Target {
394 &self.hardforks
395 }
396}
397
398#[derive(Debug, Clone, PartialEq, Eq)]
406pub struct ChainSpec<H: BlockHeader = Header> {
407 pub chain: Chain,
409
410 pub genesis: Genesis,
412
413 pub genesis_header: SealedHeader<H>,
415
416 pub paris_block_and_final_difficulty: Option<(u64, U256)>,
419
420 pub hardforks: ChainHardforks,
422
423 pub deposit_contract: Option<DepositContract>,
425
426 pub base_fee_params: BaseFeeParamsKind,
428
429 pub prune_delete_limit: usize,
431
432 pub blob_params: BlobScheduleBlobParams,
434}
435
436impl<H: BlockHeader> Default for ChainSpec<H> {
437 fn default() -> Self {
438 Self {
439 chain: Default::default(),
440 genesis: Default::default(),
441 genesis_header: Default::default(),
442 paris_block_and_final_difficulty: Default::default(),
443 hardforks: Default::default(),
444 deposit_contract: Default::default(),
445 base_fee_params: BaseFeeParamsKind::Constant(BaseFeeParams::ethereum()),
446 prune_delete_limit: MAINNET_PRUNE_DELETE_LIMIT,
447 blob_params: Default::default(),
448 }
449 }
450}
451
452impl ChainSpec {
453 pub fn from_genesis(genesis: Genesis) -> Self {
455 genesis.into()
456 }
457
458 pub fn builder() -> ChainSpecBuilder {
460 ChainSpecBuilder::default()
461 }
462
463 pub fn from_chain_id(chain_id: u64) -> Option<Arc<Self>> {
465 match NamedChain::try_from(chain_id).ok()? {
466 NamedChain::Mainnet => Some(MAINNET.clone()),
467 NamedChain::Sepolia => Some(SEPOLIA.clone()),
468 NamedChain::Holesky => Some(HOLESKY.clone()),
469 NamedChain::Hoodi => Some(HOODI.clone()),
470 NamedChain::Dev => Some(DEV.clone()),
471 _ => None,
472 }
473 }
474}
475
476impl<H: BlockHeader> ChainSpec<H> {
477 pub const fn chain(&self) -> Chain {
479 self.chain
480 }
481
482 #[inline]
484 pub const fn is_ethereum(&self) -> bool {
485 self.chain.is_ethereum()
486 }
487
488 #[inline]
490 pub fn is_optimism_mainnet(&self) -> bool {
491 self.chain == Chain::optimism_mainnet()
492 }
493
494 #[inline]
496 pub fn paris_block(&self) -> Option<u64> {
497 self.paris_block_and_final_difficulty.map(|(block, _)| block)
498 }
499
500 pub const fn genesis(&self) -> &Genesis {
504 &self.genesis
505 }
506
507 pub fn genesis_header(&self) -> &H {
509 &self.genesis_header
510 }
511
512 pub fn sealed_genesis_header(&self) -> SealedHeader<H> {
514 SealedHeader::new(self.genesis_header().clone(), self.genesis_hash())
515 }
516
517 pub fn initial_base_fee(&self) -> Option<u64> {
519 let genesis_base_fee =
521 self.genesis.base_fee_per_gas.map(|fee| fee as u64).unwrap_or(INITIAL_BASE_FEE);
522
523 self.hardforks.fork(EthereumHardfork::London).active_at_block(0).then_some(genesis_base_fee)
525 }
526
527 pub fn base_fee_params_at_timestamp(&self, timestamp: u64) -> BaseFeeParams {
529 match self.base_fee_params {
530 BaseFeeParamsKind::Constant(bf_params) => bf_params,
531 BaseFeeParamsKind::Variable(ForkBaseFeeParams(ref bf_params)) => {
532 for (fork, params) in bf_params.iter().rev() {
536 if self.hardforks.is_fork_active_at_timestamp(fork.clone(), timestamp) {
537 return *params
538 }
539 }
540
541 bf_params.first().map(|(_, params)| *params).unwrap_or_else(BaseFeeParams::ethereum)
542 }
543 }
544 }
545
546 pub fn genesis_hash(&self) -> B256 {
548 self.genesis_header.hash()
549 }
550
551 pub const fn genesis_timestamp(&self) -> u64 {
553 self.genesis.timestamp
554 }
555
556 pub fn get_final_paris_total_difficulty(&self) -> Option<U256> {
558 self.paris_block_and_final_difficulty.map(|(_, final_difficulty)| final_difficulty)
559 }
560
561 pub fn hardfork_fork_filter<HF: Hardfork + Clone>(&self, fork: HF) -> Option<ForkFilter> {
563 match self.hardforks.fork(fork.clone()) {
564 ForkCondition::Never => None,
565 _ => Some(self.fork_filter(self.satisfy(self.hardforks.fork(fork)))),
566 }
567 }
568
569 pub fn display_hardforks(&self) -> DisplayHardforks {
571 let hardforks_with_meta = self.hardforks.forks_iter().map(|(fork, condition)| {
573 let metadata = match condition {
575 ForkCondition::Timestamp(timestamp) => {
576 EthChainSpec::blob_params_at_timestamp(self, timestamp).map(|params| {
579 format!(
580 "blob: (target: {}, max: {}, fraction: {})",
581 params.target_blob_count, params.max_blob_count, params.update_fraction
582 )
583 })
584 }
585 _ => None,
586 };
587 (fork, condition, metadata)
588 });
589
590 DisplayHardforks::with_meta(hardforks_with_meta)
591 }
592
593 #[inline]
595 pub fn hardfork_fork_id<HF: Hardfork + Clone>(&self, fork: HF) -> Option<ForkId> {
596 let condition = self.hardforks.fork(fork);
597 match condition {
598 ForkCondition::Never => None,
599 _ => Some(self.fork_id(&self.satisfy(condition))),
600 }
601 }
602
603 #[inline]
606 pub fn shanghai_fork_id(&self) -> Option<ForkId> {
607 self.hardfork_fork_id(EthereumHardfork::Shanghai)
608 }
609
610 #[inline]
613 pub fn cancun_fork_id(&self) -> Option<ForkId> {
614 self.hardfork_fork_id(EthereumHardfork::Cancun)
615 }
616
617 #[inline]
620 pub fn latest_fork_id(&self) -> ForkId {
621 self.hardfork_fork_id(self.hardforks.last().unwrap().0).unwrap()
622 }
623
624 pub fn fork_filter(&self, head: Head) -> ForkFilter {
626 let forks = self.hardforks.forks_iter().filter_map(|(_, condition)| {
627 Some(match condition {
630 ForkCondition::Block(block) |
631 ForkCondition::TTD { fork_block: Some(block), .. } => ForkFilterKey::Block(block),
632 ForkCondition::Timestamp(time) => ForkFilterKey::Time(time),
633 _ => return None,
634 })
635 });
636
637 ForkFilter::new(head, self.genesis_hash(), self.genesis_timestamp(), forks)
638 }
639
640 pub fn fork_id(&self, head: &Head) -> ForkId {
652 let mut forkhash = ForkHash::from(self.genesis_hash());
653
654 let mut current_applied = 0;
660
661 for (_, cond) in self.hardforks.forks_iter() {
663 if let ForkCondition::Block(block) |
666 ForkCondition::TTD { fork_block: Some(block), .. } = cond
667 {
668 if head.number >= block {
669 if block != current_applied {
671 forkhash += block;
672 current_applied = block;
673 }
674 } else {
675 return ForkId { hash: forkhash, next: block }
678 }
679 }
680 }
681
682 for timestamp in self.hardforks.forks_iter().filter_map(|(_, cond)| {
686 cond.as_timestamp().filter(|time| time > &self.genesis.timestamp)
688 }) {
689 if head.timestamp >= timestamp {
690 if timestamp != current_applied {
692 forkhash += timestamp;
693 current_applied = timestamp;
694 }
695 } else {
696 return ForkId { hash: forkhash, next: timestamp }
700 }
701 }
702
703 ForkId { hash: forkhash, next: 0 }
704 }
705
706 pub(crate) fn satisfy(&self, cond: ForkCondition) -> Head {
712 match cond {
713 ForkCondition::Block(number) => Head { number, ..Default::default() },
714 ForkCondition::Timestamp(timestamp) => {
715 Head {
718 timestamp,
719 number: self.last_block_fork_before_merge_or_timestamp().unwrap_or_default(),
720 ..Default::default()
721 }
722 }
723 ForkCondition::TTD { total_difficulty, fork_block, .. } => Head {
724 total_difficulty,
725 number: fork_block.unwrap_or_default(),
726 ..Default::default()
727 },
728 ForkCondition::Never => unreachable!(),
729 }
730 }
731
732 pub(crate) fn last_block_fork_before_merge_or_timestamp(&self) -> Option<u64> {
744 let mut hardforks_iter = self.hardforks.forks_iter().peekable();
745 while let Some((_, curr_cond)) = hardforks_iter.next() {
746 if let Some((_, next_cond)) = hardforks_iter.peek() {
747 match next_cond {
751 ForkCondition::TTD { fork_block: Some(block), .. } => return Some(*block),
754
755 ForkCondition::TTD { .. } | ForkCondition::Timestamp(_) => {
758 if let ForkCondition::Block(block_num) = curr_cond {
761 return Some(block_num);
762 }
763 }
764 ForkCondition::Block(_) | ForkCondition::Never => {}
765 }
766 }
767 }
768 None
769 }
770
771 pub fn bootnodes(&self) -> Option<Vec<NodeRecord>> {
773 use NamedChain as C;
774
775 match self.chain.try_into().ok()? {
776 C::Mainnet => Some(mainnet_nodes()),
777 C::Sepolia => Some(sepolia_nodes()),
778 C::Holesky => Some(holesky_nodes()),
779 C::Hoodi => Some(hoodi_nodes()),
780 _ => None,
781 }
782 }
783
784 pub fn map_header<NewH: BlockHeader>(self, f: impl FnOnce(H) -> NewH) -> ChainSpec<NewH> {
786 let Self {
787 chain,
788 genesis,
789 genesis_header,
790 paris_block_and_final_difficulty,
791 hardforks,
792 deposit_contract,
793 base_fee_params,
794 prune_delete_limit,
795 blob_params,
796 } = self;
797 ChainSpec {
798 chain,
799 genesis,
800 genesis_header: SealedHeader::new_unhashed(f(genesis_header.into_header())),
801 paris_block_and_final_difficulty,
802 hardforks,
803 deposit_contract,
804 base_fee_params,
805 prune_delete_limit,
806 blob_params,
807 }
808 }
809}
810
811impl From<Genesis> for ChainSpec {
812 fn from(genesis: Genesis) -> Self {
813 let hardfork_opts = [
815 (EthereumHardfork::Frontier.boxed(), Some(0)),
816 (EthereumHardfork::Homestead.boxed(), genesis.config.homestead_block),
817 (EthereumHardfork::Dao.boxed(), genesis.config.dao_fork_block),
818 (EthereumHardfork::Tangerine.boxed(), genesis.config.eip150_block),
819 (EthereumHardfork::SpuriousDragon.boxed(), genesis.config.eip155_block),
820 (EthereumHardfork::Byzantium.boxed(), genesis.config.byzantium_block),
821 (EthereumHardfork::Constantinople.boxed(), genesis.config.constantinople_block),
822 (EthereumHardfork::Petersburg.boxed(), genesis.config.petersburg_block),
823 (EthereumHardfork::Istanbul.boxed(), genesis.config.istanbul_block),
824 (EthereumHardfork::MuirGlacier.boxed(), genesis.config.muir_glacier_block),
825 (EthereumHardfork::Berlin.boxed(), genesis.config.berlin_block),
826 (EthereumHardfork::London.boxed(), genesis.config.london_block),
827 (EthereumHardfork::ArrowGlacier.boxed(), genesis.config.arrow_glacier_block),
828 (EthereumHardfork::GrayGlacier.boxed(), genesis.config.gray_glacier_block),
829 ];
830 let mut hardforks = hardfork_opts
831 .into_iter()
832 .filter_map(|(hardfork, opt)| opt.map(|block| (hardfork, ForkCondition::Block(block))))
833 .collect::<Vec<_>>();
834
835 let paris_block_and_final_difficulty = if let Some(ttd) =
839 genesis.config.terminal_total_difficulty
840 {
841 hardforks.push((
842 EthereumHardfork::Paris.boxed(),
843 ForkCondition::TTD {
844 activation_block_number: genesis
847 .config
848 .merge_netsplit_block
849 .or_else(|| {
850 match genesis.config.chain_id {
858 1 if ttd == MAINNET_PARIS_TTD => return Some(MAINNET_PARIS_BLOCK),
859 11155111 if ttd == SEPOLIA_PARIS_TTD => {
860 return Some(SEPOLIA_PARIS_BLOCK)
861 }
862 _ => {}
863 };
864 None
865 })
866 .unwrap_or_default(),
867 total_difficulty: ttd,
868 fork_block: genesis.config.merge_netsplit_block,
869 },
870 ));
871
872 genesis.config.merge_netsplit_block.map(|block| (block, ttd))
873 } else {
874 None
875 };
876
877 let time_hardfork_opts = [
879 (EthereumHardfork::Shanghai.boxed(), genesis.config.shanghai_time),
880 (EthereumHardfork::Cancun.boxed(), genesis.config.cancun_time),
881 (EthereumHardfork::Prague.boxed(), genesis.config.prague_time),
882 (EthereumHardfork::Osaka.boxed(), genesis.config.osaka_time),
883 (EthereumHardfork::Bpo1.boxed(), genesis.config.bpo1_time),
884 (EthereumHardfork::Bpo2.boxed(), genesis.config.bpo2_time),
885 (EthereumHardfork::Bpo3.boxed(), genesis.config.bpo3_time),
886 (EthereumHardfork::Bpo4.boxed(), genesis.config.bpo4_time),
887 (EthereumHardfork::Bpo5.boxed(), genesis.config.bpo5_time),
888 ];
889
890 let mut time_hardforks = time_hardfork_opts
891 .into_iter()
892 .filter_map(|(hardfork, opt)| {
893 opt.map(|time| (hardfork, ForkCondition::Timestamp(time)))
894 })
895 .collect::<Vec<_>>();
896
897 hardforks.append(&mut time_hardforks);
898
899 let mainnet_hardforks: ChainHardforks = EthereumHardfork::mainnet().into();
901 let mainnet_order = mainnet_hardforks.forks_iter();
902
903 let mut ordered_hardforks = Vec::with_capacity(hardforks.len());
904 for (hardfork, _) in mainnet_order {
905 if let Some(pos) = hardforks.iter().position(|(e, _)| **e == *hardfork) {
906 ordered_hardforks.push(hardforks.remove(pos));
907 }
908 }
909
910 ordered_hardforks.append(&mut hardforks);
912
913 let blob_params = genesis.config.blob_schedule_blob_params();
915
916 let deposit_contract = genesis.config.deposit_contract_address.map(|address| {
921 DepositContract { address, block: 0, topic: MAINNET_DEPOSIT_CONTRACT.topic }
922 });
923
924 let hardforks = ChainHardforks::new(ordered_hardforks);
925
926 Self {
927 chain: genesis.config.chain_id.into(),
928 genesis_header: SealedHeader::new_unhashed(make_genesis_header(&genesis, &hardforks)),
929 genesis,
930 hardforks,
931 paris_block_and_final_difficulty,
932 deposit_contract,
933 blob_params,
934 ..Default::default()
935 }
936 }
937}
938
939impl<H: BlockHeader> Hardforks for ChainSpec<H> {
940 fn fork<HF: Hardfork>(&self, fork: HF) -> ForkCondition {
941 self.hardforks.fork(fork)
942 }
943
944 fn forks_iter(&self) -> impl Iterator<Item = (&dyn Hardfork, ForkCondition)> {
945 self.hardforks.forks_iter()
946 }
947
948 fn fork_id(&self, head: &Head) -> ForkId {
949 self.fork_id(head)
950 }
951
952 fn latest_fork_id(&self) -> ForkId {
953 self.latest_fork_id()
954 }
955
956 fn fork_filter(&self, head: Head) -> ForkFilter {
957 self.fork_filter(head)
958 }
959}
960
961impl<H: BlockHeader> EthereumHardforks for ChainSpec<H> {
962 fn ethereum_fork_activation(&self, fork: EthereumHardfork) -> ForkCondition {
963 self.fork(fork)
964 }
965}
966
967#[auto_impl::auto_impl(&, Arc)]
969pub trait ChainSpecProvider: Debug + Send {
970 type ChainSpec: EthChainSpec + 'static;
972
973 fn chain_spec(&self) -> Arc<Self::ChainSpec>;
975}
976
977#[derive(Debug, Default, Clone)]
979pub struct ChainSpecBuilder {
980 chain: Option<Chain>,
981 genesis: Option<Genesis>,
982 hardforks: ChainHardforks,
983}
984
985impl ChainSpecBuilder {
986 pub fn mainnet() -> Self {
988 Self {
989 chain: Some(MAINNET.chain),
990 genesis: Some(MAINNET.genesis.clone()),
991 hardforks: MAINNET.hardforks.clone(),
992 }
993 }
994}
995
996impl ChainSpecBuilder {
997 pub const fn chain(mut self, chain: Chain) -> Self {
999 self.chain = Some(chain);
1000 self
1001 }
1002
1003 pub fn reset(mut self) -> Self {
1005 self.hardforks = ChainHardforks::default();
1006 self
1007 }
1008
1009 pub fn genesis(mut self, genesis: Genesis) -> Self {
1011 self.genesis = Some(genesis);
1012 self
1013 }
1014
1015 pub fn with_fork<H: Hardfork>(mut self, fork: H, condition: ForkCondition) -> Self {
1017 self.hardforks.insert(fork, condition);
1018 self
1019 }
1020
1021 pub fn with_forks(mut self, forks: ChainHardforks) -> Self {
1023 self.hardforks = forks;
1024 self
1025 }
1026
1027 pub fn without_fork<H: Hardfork>(mut self, fork: H) -> Self {
1029 self.hardforks.remove(&fork);
1030 self
1031 }
1032
1033 pub fn paris_at_ttd(self, ttd: U256, activation_block_number: BlockNumber) -> Self {
1037 self.with_fork(
1038 EthereumHardfork::Paris,
1039 ForkCondition::TTD { activation_block_number, total_difficulty: ttd, fork_block: None },
1040 )
1041 }
1042
1043 pub fn frontier_activated(mut self) -> Self {
1045 self.hardforks.insert(EthereumHardfork::Frontier, ForkCondition::Block(0));
1046 self
1047 }
1048
1049 pub fn dao_activated(mut self) -> Self {
1051 self = self.frontier_activated();
1052 self.hardforks.insert(EthereumHardfork::Dao, ForkCondition::Block(0));
1053 self
1054 }
1055
1056 pub fn homestead_activated(mut self) -> Self {
1058 self = self.dao_activated();
1059 self.hardforks.insert(EthereumHardfork::Homestead, ForkCondition::Block(0));
1060 self
1061 }
1062
1063 pub fn tangerine_whistle_activated(mut self) -> Self {
1065 self = self.homestead_activated();
1066 self.hardforks.insert(EthereumHardfork::Tangerine, ForkCondition::Block(0));
1067 self
1068 }
1069
1070 pub fn spurious_dragon_activated(mut self) -> Self {
1072 self = self.tangerine_whistle_activated();
1073 self.hardforks.insert(EthereumHardfork::SpuriousDragon, ForkCondition::Block(0));
1074 self
1075 }
1076
1077 pub fn byzantium_activated(mut self) -> Self {
1079 self = self.spurious_dragon_activated();
1080 self.hardforks.insert(EthereumHardfork::Byzantium, ForkCondition::Block(0));
1081 self
1082 }
1083
1084 pub fn constantinople_activated(mut self) -> Self {
1086 self = self.byzantium_activated();
1087 self.hardforks.insert(EthereumHardfork::Constantinople, ForkCondition::Block(0));
1088 self
1089 }
1090
1091 pub fn petersburg_activated(mut self) -> Self {
1093 self = self.constantinople_activated();
1094 self.hardforks.insert(EthereumHardfork::Petersburg, ForkCondition::Block(0));
1095 self
1096 }
1097
1098 pub fn istanbul_activated(mut self) -> Self {
1100 self = self.petersburg_activated();
1101 self.hardforks.insert(EthereumHardfork::Istanbul, ForkCondition::Block(0));
1102 self
1103 }
1104
1105 pub fn muirglacier_activated(mut self) -> Self {
1107 self = self.istanbul_activated();
1108 self.hardforks.insert(EthereumHardfork::MuirGlacier, ForkCondition::Block(0));
1109 self
1110 }
1111
1112 pub fn berlin_activated(mut self) -> Self {
1114 self = self.muirglacier_activated();
1115 self.hardforks.insert(EthereumHardfork::Berlin, ForkCondition::Block(0));
1116 self
1117 }
1118
1119 pub fn london_activated(mut self) -> Self {
1121 self = self.berlin_activated();
1122 self.hardforks.insert(EthereumHardfork::London, ForkCondition::Block(0));
1123 self
1124 }
1125
1126 pub fn arrowglacier_activated(mut self) -> Self {
1128 self = self.london_activated();
1129 self.hardforks.insert(EthereumHardfork::ArrowGlacier, ForkCondition::Block(0));
1130 self
1131 }
1132
1133 pub fn grayglacier_activated(mut self) -> Self {
1135 self = self.arrowglacier_activated();
1136 self.hardforks.insert(EthereumHardfork::GrayGlacier, ForkCondition::Block(0));
1137 self
1138 }
1139
1140 pub fn paris_activated(mut self) -> Self {
1142 self = self.grayglacier_activated();
1143 self.hardforks.insert(
1144 EthereumHardfork::Paris,
1145 ForkCondition::TTD {
1146 activation_block_number: 0,
1147 total_difficulty: U256::ZERO,
1148 fork_block: None,
1149 },
1150 );
1151 self
1152 }
1153
1154 pub fn shanghai_activated(mut self) -> Self {
1156 self = self.paris_activated();
1157 self.hardforks.insert(EthereumHardfork::Shanghai, ForkCondition::Timestamp(0));
1158 self
1159 }
1160
1161 pub fn cancun_activated(mut self) -> Self {
1163 self = self.shanghai_activated();
1164 self.hardforks.insert(EthereumHardfork::Cancun, ForkCondition::Timestamp(0));
1165 self
1166 }
1167
1168 pub fn prague_activated(mut self) -> Self {
1170 self = self.cancun_activated();
1171 self.hardforks.insert(EthereumHardfork::Prague, ForkCondition::Timestamp(0));
1172 self
1173 }
1174
1175 pub fn with_prague_at(mut self, timestamp: u64) -> Self {
1177 self.hardforks.insert(EthereumHardfork::Prague, ForkCondition::Timestamp(timestamp));
1178 self
1179 }
1180
1181 pub fn osaka_activated(mut self) -> Self {
1183 self = self.prague_activated();
1184 self.hardforks.insert(EthereumHardfork::Osaka, ForkCondition::Timestamp(0));
1185 self
1186 }
1187
1188 pub fn with_osaka_at(mut self, timestamp: u64) -> Self {
1190 self.hardforks.insert(EthereumHardfork::Osaka, ForkCondition::Timestamp(timestamp));
1191 self
1192 }
1193
1194 pub fn build(self) -> ChainSpec {
1201 let paris_block_and_final_difficulty = {
1202 self.hardforks.get(EthereumHardfork::Paris).and_then(|cond| {
1203 if let ForkCondition::TTD { total_difficulty, activation_block_number, .. } = cond {
1204 Some((activation_block_number, total_difficulty))
1205 } else {
1206 None
1207 }
1208 })
1209 };
1210 let genesis = self.genesis.expect("The genesis is required");
1211 ChainSpec {
1212 chain: self.chain.expect("The chain is required"),
1213 genesis_header: SealedHeader::new_unhashed(make_genesis_header(
1214 &genesis,
1215 &self.hardforks,
1216 )),
1217 genesis,
1218 hardforks: self.hardforks,
1219 paris_block_and_final_difficulty,
1220 deposit_contract: None,
1221 ..Default::default()
1222 }
1223 }
1224}
1225
1226impl From<&Arc<ChainSpec>> for ChainSpecBuilder {
1227 fn from(value: &Arc<ChainSpec>) -> Self {
1228 Self {
1229 chain: Some(value.chain),
1230 genesis: Some(value.genesis.clone()),
1231 hardforks: value.hardforks.clone(),
1232 }
1233 }
1234}
1235
1236impl<H: BlockHeader> EthExecutorSpec for ChainSpec<H> {
1237 fn deposit_contract_address(&self) -> Option<Address> {
1238 self.deposit_contract.map(|deposit_contract| deposit_contract.address)
1239 }
1240}
1241
1242#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1244pub struct DepositContract {
1245 pub address: Address,
1247 pub block: BlockNumber,
1249 pub topic: B256,
1251}
1252
1253impl DepositContract {
1254 pub const fn new(address: Address, block: BlockNumber, topic: B256) -> Self {
1256 Self { address, block, topic }
1257 }
1258}
1259
1260#[cfg(any(test, feature = "test-utils"))]
1262pub fn test_fork_ids(spec: &ChainSpec, cases: &[(Head, ForkId)]) {
1263 for (block, expected_id) in cases {
1264 let computed_id = spec.fork_id(block);
1265 assert_eq!(
1266 expected_id, &computed_id,
1267 "Expected fork ID {:?}, computed fork ID {:?} at block {}",
1268 expected_id, computed_id, block.number
1269 );
1270 }
1271}
1272
1273#[cfg(test)]
1274mod tests {
1275 use super::*;
1276 use alloy_chains::Chain;
1277 use alloy_consensus::constants::ETH_TO_WEI;
1278 use alloy_eips::{eip4844::BLOB_TX_MIN_BLOB_GASPRICE, eip7840::BlobParams};
1279 use alloy_evm::block::calc::{base_block_reward, block_reward};
1280 use alloy_genesis::{ChainConfig, GenesisAccount};
1281 use alloy_primitives::{b256, hex};
1282 use alloy_trie::{TrieAccount, EMPTY_ROOT_HASH};
1283 use core::ops::Deref;
1284 use reth_ethereum_forks::{ForkCondition, ForkHash, ForkId, Head};
1285 use std::{collections::HashMap, str::FromStr};
1286
1287 fn test_hardfork_fork_ids(spec: &ChainSpec, cases: &[(EthereumHardfork, ForkId)]) {
1288 for (hardfork, expected_id) in cases {
1289 if let Some(computed_id) = spec.hardfork_fork_id(*hardfork) {
1290 assert_eq!(
1291 expected_id, &computed_id,
1292 "Expected fork ID {expected_id:?}, computed fork ID {computed_id:?} for hardfork {hardfork}"
1293 );
1294 if matches!(hardfork, EthereumHardfork::Shanghai) {
1295 if let Some(shanghai_id) = spec.shanghai_fork_id() {
1296 assert_eq!(
1297 expected_id, &shanghai_id,
1298 "Expected fork ID {expected_id:?}, computed fork ID {computed_id:?} for Shanghai hardfork"
1299 );
1300 } else {
1301 panic!("Expected ForkCondition to return Some for Hardfork::Shanghai");
1302 }
1303 }
1304 }
1305 }
1306 }
1307
1308 #[test]
1309 fn test_hardfork_list_display_mainnet() {
1310 assert_eq!(
1311 MAINNET.display_hardforks().to_string(),
1312 "Pre-merge hard forks (block based):
1313- Frontier @0
1314- Homestead @1150000
1315- Dao @1920000
1316- Tangerine @2463000
1317- SpuriousDragon @2675000
1318- Byzantium @4370000
1319- Constantinople @7280000
1320- Petersburg @7280000
1321- Istanbul @9069000
1322- MuirGlacier @9200000
1323- Berlin @12244000
1324- London @12965000
1325- ArrowGlacier @13773000
1326- GrayGlacier @15050000
1327Merge hard forks:
1328- Paris @58750000000000000000000 (network is known to be merged)
1329Post-merge hard forks (timestamp based):
1330- Shanghai @1681338455
1331- Cancun @1710338135 blob: (target: 3, max: 6, fraction: 3338477)
1332- Prague @1746612311 blob: (target: 6, max: 9, fraction: 5007716)
1333- Osaka @1764798551 blob: (target: 6, max: 9, fraction: 5007716)
1334- Bpo1 @1765290071 blob: (target: 10, max: 15, fraction: 8346193)
1335- Bpo2 @1767747671 blob: (target: 14, max: 21, fraction: 11684671)"
1336 );
1337 }
1338
1339 #[test]
1340 fn test_hardfork_list_ignores_disabled_forks() {
1341 let spec = ChainSpec::builder()
1342 .chain(Chain::mainnet())
1343 .genesis(Genesis::default())
1344 .with_fork(EthereumHardfork::Frontier, ForkCondition::Block(0))
1345 .with_fork(EthereumHardfork::Shanghai, ForkCondition::Never)
1346 .build();
1347 assert_eq!(
1348 spec.display_hardforks().to_string(),
1349 "Pre-merge hard forks (block based):
1350- Frontier @0"
1351 );
1352 }
1353
1354 #[test]
1356 fn ignores_genesis_fork_blocks() {
1357 let spec = ChainSpec::builder()
1358 .chain(Chain::mainnet())
1359 .genesis(Genesis::default())
1360 .with_fork(EthereumHardfork::Frontier, ForkCondition::Block(0))
1361 .with_fork(EthereumHardfork::Homestead, ForkCondition::Block(0))
1362 .with_fork(EthereumHardfork::Tangerine, ForkCondition::Block(0))
1363 .with_fork(EthereumHardfork::SpuriousDragon, ForkCondition::Block(0))
1364 .with_fork(EthereumHardfork::Byzantium, ForkCondition::Block(0))
1365 .with_fork(EthereumHardfork::Constantinople, ForkCondition::Block(0))
1366 .with_fork(EthereumHardfork::Istanbul, ForkCondition::Block(0))
1367 .with_fork(EthereumHardfork::MuirGlacier, ForkCondition::Block(0))
1368 .with_fork(EthereumHardfork::Berlin, ForkCondition::Block(0))
1369 .with_fork(EthereumHardfork::London, ForkCondition::Block(0))
1370 .with_fork(EthereumHardfork::ArrowGlacier, ForkCondition::Block(0))
1371 .with_fork(EthereumHardfork::GrayGlacier, ForkCondition::Block(0))
1372 .build();
1373
1374 assert_eq!(spec.deref().len(), 12, "12 forks should be active.");
1375 assert_eq!(
1376 spec.fork_id(&Head { number: 1, ..Default::default() }),
1377 ForkId { hash: ForkHash::from(spec.genesis_hash()), next: 0 },
1378 "the fork ID should be the genesis hash; forks at genesis are ignored for fork filters"
1379 );
1380 }
1381
1382 #[test]
1383 fn ignores_duplicate_fork_blocks() {
1384 let empty_genesis = Genesis::default();
1385 let unique_spec = ChainSpec::builder()
1386 .chain(Chain::mainnet())
1387 .genesis(empty_genesis.clone())
1388 .with_fork(EthereumHardfork::Frontier, ForkCondition::Block(0))
1389 .with_fork(EthereumHardfork::Homestead, ForkCondition::Block(1))
1390 .build();
1391
1392 let duplicate_spec = ChainSpec::builder()
1393 .chain(Chain::mainnet())
1394 .genesis(empty_genesis)
1395 .with_fork(EthereumHardfork::Frontier, ForkCondition::Block(0))
1396 .with_fork(EthereumHardfork::Homestead, ForkCondition::Block(1))
1397 .with_fork(EthereumHardfork::Tangerine, ForkCondition::Block(1))
1398 .build();
1399
1400 assert_eq!(
1401 unique_spec.fork_id(&Head { number: 2, ..Default::default() }),
1402 duplicate_spec.fork_id(&Head { number: 2, ..Default::default() }),
1403 "duplicate fork blocks should be deduplicated for fork filters"
1404 );
1405 }
1406
1407 #[test]
1408 fn test_chainspec_satisfy() {
1409 let empty_genesis = Genesis::default();
1410 let happy_path_case = ChainSpec::builder()
1412 .chain(Chain::mainnet())
1413 .genesis(empty_genesis.clone())
1414 .with_fork(EthereumHardfork::Frontier, ForkCondition::Block(0))
1415 .with_fork(EthereumHardfork::Homestead, ForkCondition::Block(73))
1416 .with_fork(EthereumHardfork::Shanghai, ForkCondition::Timestamp(11313123))
1417 .build();
1418 let happy_path_head = happy_path_case.satisfy(ForkCondition::Timestamp(11313123));
1419 let happy_path_expected = Head { number: 73, timestamp: 11313123, ..Default::default() };
1420 assert_eq!(
1421 happy_path_head, happy_path_expected,
1422 "expected satisfy() to return {happy_path_expected:#?}, but got {happy_path_head:#?} "
1423 );
1424 let multiple_timestamp_fork_case = ChainSpec::builder()
1426 .chain(Chain::mainnet())
1427 .genesis(empty_genesis.clone())
1428 .with_fork(EthereumHardfork::Frontier, ForkCondition::Block(0))
1429 .with_fork(EthereumHardfork::Homestead, ForkCondition::Block(73))
1430 .with_fork(EthereumHardfork::Shanghai, ForkCondition::Timestamp(11313123))
1431 .with_fork(EthereumHardfork::Cancun, ForkCondition::Timestamp(11313398))
1432 .build();
1433 let multi_timestamp_head =
1434 multiple_timestamp_fork_case.satisfy(ForkCondition::Timestamp(11313398));
1435 let mult_timestamp_expected =
1436 Head { number: 73, timestamp: 11313398, ..Default::default() };
1437 assert_eq!(
1438 multi_timestamp_head, mult_timestamp_expected,
1439 "expected satisfy() to return {mult_timestamp_expected:#?}, but got {multi_timestamp_head:#?} "
1440 );
1441 let no_block_fork_case = ChainSpec::builder()
1443 .chain(Chain::mainnet())
1444 .genesis(empty_genesis.clone())
1445 .with_fork(EthereumHardfork::Shanghai, ForkCondition::Timestamp(11313123))
1446 .build();
1447 let no_block_fork_head = no_block_fork_case.satisfy(ForkCondition::Timestamp(11313123));
1448 let no_block_fork_expected = Head { number: 0, timestamp: 11313123, ..Default::default() };
1449 assert_eq!(
1450 no_block_fork_head, no_block_fork_expected,
1451 "expected satisfy() to return {no_block_fork_expected:#?}, but got {no_block_fork_head:#?} ",
1452 );
1453 let fork_cond_ttd_blocknum_case = ChainSpec::builder()
1455 .chain(Chain::mainnet())
1456 .genesis(empty_genesis.clone())
1457 .with_fork(EthereumHardfork::Frontier, ForkCondition::Block(0))
1458 .with_fork(EthereumHardfork::Homestead, ForkCondition::Block(73))
1459 .with_fork(
1460 EthereumHardfork::Paris,
1461 ForkCondition::TTD {
1462 activation_block_number: 101,
1463 fork_block: Some(101),
1464 total_difficulty: U256::from(10_790_000),
1465 },
1466 )
1467 .with_fork(EthereumHardfork::Shanghai, ForkCondition::Timestamp(11313123))
1468 .build();
1469 let fork_cond_ttd_blocknum_head =
1470 fork_cond_ttd_blocknum_case.satisfy(ForkCondition::Timestamp(11313123));
1471 let fork_cond_ttd_blocknum_expected =
1472 Head { number: 101, timestamp: 11313123, ..Default::default() };
1473 assert_eq!(
1474 fork_cond_ttd_blocknum_head, fork_cond_ttd_blocknum_expected,
1475 "expected satisfy() to return {fork_cond_ttd_blocknum_expected:#?}, but got {fork_cond_ttd_blocknum_head:#?} ",
1476 );
1477
1478 let fork_cond_block_only_case = ChainSpec::builder()
1482 .chain(Chain::mainnet())
1483 .genesis(empty_genesis)
1484 .with_fork(EthereumHardfork::Frontier, ForkCondition::Block(0))
1485 .with_fork(EthereumHardfork::Homestead, ForkCondition::Block(73))
1486 .build();
1487 let fork_cond_block_only_head = fork_cond_block_only_case.satisfy(ForkCondition::Block(73));
1488 let fork_cond_block_only_expected = Head { number: 73, ..Default::default() };
1489 assert_eq!(
1490 fork_cond_block_only_head, fork_cond_block_only_expected,
1491 "expected satisfy() to return {fork_cond_block_only_expected:#?}, but got {fork_cond_block_only_head:#?} ",
1492 );
1493 let fork_cond_ttd_no_new_spec = fork_cond_block_only_case.satisfy(ForkCondition::TTD {
1496 activation_block_number: 101,
1497 fork_block: None,
1498 total_difficulty: U256::from(10_790_000),
1499 });
1500 let fork_cond_ttd_no_new_spec_expected =
1501 Head { total_difficulty: U256::from(10_790_000), ..Default::default() };
1502 assert_eq!(
1503 fork_cond_ttd_no_new_spec, fork_cond_ttd_no_new_spec_expected,
1504 "expected satisfy() to return {fork_cond_ttd_no_new_spec_expected:#?}, but got {fork_cond_ttd_no_new_spec:#?} ",
1505 );
1506 }
1507
1508 #[test]
1509 fn mainnet_hardfork_fork_ids() {
1510 test_hardfork_fork_ids(
1511 &MAINNET,
1512 &[
1513 (
1514 EthereumHardfork::Frontier,
1515 ForkId { hash: ForkHash(hex!("0xfc64ec04")), next: 1150000 },
1516 ),
1517 (
1518 EthereumHardfork::Homestead,
1519 ForkId { hash: ForkHash(hex!("0x97c2c34c")), next: 1920000 },
1520 ),
1521 (
1522 EthereumHardfork::Dao,
1523 ForkId { hash: ForkHash(hex!("0x91d1f948")), next: 2463000 },
1524 ),
1525 (
1526 EthereumHardfork::Tangerine,
1527 ForkId { hash: ForkHash(hex!("0x7a64da13")), next: 2675000 },
1528 ),
1529 (
1530 EthereumHardfork::SpuriousDragon,
1531 ForkId { hash: ForkHash(hex!("0x3edd5b10")), next: 4370000 },
1532 ),
1533 (
1534 EthereumHardfork::Byzantium,
1535 ForkId { hash: ForkHash(hex!("0xa00bc324")), next: 7280000 },
1536 ),
1537 (
1538 EthereumHardfork::Constantinople,
1539 ForkId { hash: ForkHash(hex!("0x668db0af")), next: 9069000 },
1540 ),
1541 (
1542 EthereumHardfork::Petersburg,
1543 ForkId { hash: ForkHash(hex!("0x668db0af")), next: 9069000 },
1544 ),
1545 (
1546 EthereumHardfork::Istanbul,
1547 ForkId { hash: ForkHash(hex!("0x879d6e30")), next: 9200000 },
1548 ),
1549 (
1550 EthereumHardfork::MuirGlacier,
1551 ForkId { hash: ForkHash(hex!("0xe029e991")), next: 12244000 },
1552 ),
1553 (
1554 EthereumHardfork::Berlin,
1555 ForkId { hash: ForkHash(hex!("0x0eb440f6")), next: 12965000 },
1556 ),
1557 (
1558 EthereumHardfork::London,
1559 ForkId { hash: ForkHash(hex!("0xb715077d")), next: 13773000 },
1560 ),
1561 (
1562 EthereumHardfork::ArrowGlacier,
1563 ForkId { hash: ForkHash(hex!("0x20c327fc")), next: 15050000 },
1564 ),
1565 (
1566 EthereumHardfork::GrayGlacier,
1567 ForkId { hash: ForkHash(hex!("0xf0afd0e3")), next: 1681338455 },
1568 ),
1569 (
1570 EthereumHardfork::Shanghai,
1571 ForkId { hash: ForkHash(hex!("0xdce96c2d")), next: 1710338135 },
1572 ),
1573 (
1574 EthereumHardfork::Cancun,
1575 ForkId { hash: ForkHash(hex!("0x9f3d2254")), next: 1746612311 },
1576 ),
1577 (
1578 EthereumHardfork::Prague,
1579 ForkId {
1580 hash: ForkHash(hex!("0xc376cf8b")),
1581 next: mainnet::MAINNET_OSAKA_TIMESTAMP,
1582 },
1583 ),
1584 ],
1585 );
1586 }
1587
1588 #[test]
1589 fn sepolia_hardfork_fork_ids() {
1590 test_hardfork_fork_ids(
1591 &SEPOLIA,
1592 &[
1593 (
1594 EthereumHardfork::Frontier,
1595 ForkId { hash: ForkHash(hex!("0xfe3366e7")), next: 1735371 },
1596 ),
1597 (
1598 EthereumHardfork::Homestead,
1599 ForkId { hash: ForkHash(hex!("0xfe3366e7")), next: 1735371 },
1600 ),
1601 (
1602 EthereumHardfork::Tangerine,
1603 ForkId { hash: ForkHash(hex!("0xfe3366e7")), next: 1735371 },
1604 ),
1605 (
1606 EthereumHardfork::SpuriousDragon,
1607 ForkId { hash: ForkHash(hex!("0xfe3366e7")), next: 1735371 },
1608 ),
1609 (
1610 EthereumHardfork::Byzantium,
1611 ForkId { hash: ForkHash(hex!("0xfe3366e7")), next: 1735371 },
1612 ),
1613 (
1614 EthereumHardfork::Constantinople,
1615 ForkId { hash: ForkHash(hex!("0xfe3366e7")), next: 1735371 },
1616 ),
1617 (
1618 EthereumHardfork::Petersburg,
1619 ForkId { hash: ForkHash(hex!("0xfe3366e7")), next: 1735371 },
1620 ),
1621 (
1622 EthereumHardfork::Istanbul,
1623 ForkId { hash: ForkHash(hex!("0xfe3366e7")), next: 1735371 },
1624 ),
1625 (
1626 EthereumHardfork::Berlin,
1627 ForkId { hash: ForkHash(hex!("0xfe3366e7")), next: 1735371 },
1628 ),
1629 (
1630 EthereumHardfork::London,
1631 ForkId { hash: ForkHash(hex!("0xfe3366e7")), next: 1735371 },
1632 ),
1633 (
1634 EthereumHardfork::Paris,
1635 ForkId { hash: ForkHash(hex!("0xb96cbd13")), next: 1677557088 },
1636 ),
1637 (
1638 EthereumHardfork::Shanghai,
1639 ForkId { hash: ForkHash(hex!("0xf7f9bc08")), next: 1706655072 },
1640 ),
1641 (
1642 EthereumHardfork::Cancun,
1643 ForkId { hash: ForkHash(hex!("0x88cf81d9")), next: 1741159776 },
1644 ),
1645 (
1646 EthereumHardfork::Prague,
1647 ForkId {
1648 hash: ForkHash(hex!("0xed88b5fd")),
1649 next: sepolia::SEPOLIA_OSAKA_TIMESTAMP,
1650 },
1651 ),
1652 ],
1653 );
1654 }
1655
1656 #[test]
1657 fn mainnet_fork_ids() {
1658 test_fork_ids(
1659 &MAINNET,
1660 &[
1661 (
1662 Head { number: 0, ..Default::default() },
1663 ForkId { hash: ForkHash(hex!("0xfc64ec04")), next: 1150000 },
1664 ),
1665 (
1666 Head { number: 1150000, ..Default::default() },
1667 ForkId { hash: ForkHash(hex!("0x97c2c34c")), next: 1920000 },
1668 ),
1669 (
1670 Head { number: 1920000, ..Default::default() },
1671 ForkId { hash: ForkHash(hex!("0x91d1f948")), next: 2463000 },
1672 ),
1673 (
1674 Head { number: 2463000, ..Default::default() },
1675 ForkId { hash: ForkHash(hex!("0x7a64da13")), next: 2675000 },
1676 ),
1677 (
1678 Head { number: 2675000, ..Default::default() },
1679 ForkId { hash: ForkHash(hex!("0x3edd5b10")), next: 4370000 },
1680 ),
1681 (
1682 Head { number: 4370000, ..Default::default() },
1683 ForkId { hash: ForkHash(hex!("0xa00bc324")), next: 7280000 },
1684 ),
1685 (
1686 Head { number: 7280000, ..Default::default() },
1687 ForkId { hash: ForkHash(hex!("0x668db0af")), next: 9069000 },
1688 ),
1689 (
1690 Head { number: 9069000, ..Default::default() },
1691 ForkId { hash: ForkHash(hex!("0x879d6e30")), next: 9200000 },
1692 ),
1693 (
1694 Head { number: 9200000, ..Default::default() },
1695 ForkId { hash: ForkHash(hex!("0xe029e991")), next: 12244000 },
1696 ),
1697 (
1698 Head { number: 12244000, ..Default::default() },
1699 ForkId { hash: ForkHash(hex!("0x0eb440f6")), next: 12965000 },
1700 ),
1701 (
1702 Head { number: 12965000, ..Default::default() },
1703 ForkId { hash: ForkHash(hex!("0xb715077d")), next: 13773000 },
1704 ),
1705 (
1706 Head { number: 13773000, ..Default::default() },
1707 ForkId { hash: ForkHash(hex!("0x20c327fc")), next: 15050000 },
1708 ),
1709 (
1710 Head { number: 15050000, ..Default::default() },
1711 ForkId { hash: ForkHash(hex!("0xf0afd0e3")), next: 1681338455 },
1712 ),
1713 (
1715 Head { number: 20000000, timestamp: 1681338455, ..Default::default() },
1716 ForkId { hash: ForkHash(hex!("0xdce96c2d")), next: 1710338135 },
1717 ),
1718 (
1720 Head { number: 20000001, timestamp: 1710338135, ..Default::default() },
1721 ForkId { hash: ForkHash(hex!("0x9f3d2254")), next: 1746612311 },
1722 ),
1723 (
1725 Head { number: 20000004, timestamp: 1746612311, ..Default::default() },
1726 ForkId {
1727 hash: ForkHash(hex!("0xc376cf8b")),
1728 next: mainnet::MAINNET_OSAKA_TIMESTAMP,
1729 },
1730 ),
1731 (
1733 Head {
1734 number: 20000004,
1735 timestamp: mainnet::MAINNET_OSAKA_TIMESTAMP,
1736 ..Default::default()
1737 },
1738 ForkId {
1739 hash: ForkHash(hex!("0x5167e2a6")),
1740 next: mainnet::MAINNET_BPO1_TIMESTAMP,
1741 },
1742 ),
1743 ],
1744 );
1745 }
1746
1747 #[test]
1748 fn hoodi_fork_ids() {
1749 test_fork_ids(
1750 &HOODI,
1751 &[
1752 (
1753 Head { number: 0, ..Default::default() },
1754 ForkId { hash: ForkHash(hex!("0xbef71d30")), next: 1742999832 },
1755 ),
1756 (
1758 Head { number: 0, timestamp: 1742999833, ..Default::default() },
1759 ForkId {
1760 hash: ForkHash(hex!("0x0929e24e")),
1761 next: hoodi::HOODI_OSAKA_TIMESTAMP,
1762 },
1763 ),
1764 (
1766 Head {
1767 number: 0,
1768 timestamp: hoodi::HOODI_OSAKA_TIMESTAMP,
1769 ..Default::default()
1770 },
1771 ForkId {
1772 hash: ForkHash(hex!("0xe7e0e7ff")),
1773 next: hoodi::HOODI_BPO1_TIMESTAMP,
1774 },
1775 ),
1776 ],
1777 )
1778 }
1779
1780 #[test]
1781 fn holesky_fork_ids() {
1782 test_fork_ids(
1783 &HOLESKY,
1784 &[
1785 (
1786 Head { number: 0, ..Default::default() },
1787 ForkId { hash: ForkHash(hex!("0xc61a6098")), next: 1696000704 },
1788 ),
1789 (
1791 Head { number: 123, ..Default::default() },
1792 ForkId { hash: ForkHash(hex!("0xc61a6098")), next: 1696000704 },
1793 ),
1794 (
1796 Head { number: 123, timestamp: 1696000703, ..Default::default() },
1797 ForkId { hash: ForkHash(hex!("0xc61a6098")), next: 1696000704 },
1798 ),
1799 (
1801 Head { number: 123, timestamp: 1696000704, ..Default::default() },
1802 ForkId { hash: ForkHash(hex!("0xfd4f016b")), next: 1707305664 },
1803 ),
1804 (
1806 Head { number: 123, timestamp: 1707305663, ..Default::default() },
1807 ForkId { hash: ForkHash(hex!("0xfd4f016b")), next: 1707305664 },
1808 ),
1809 (
1811 Head { number: 123, timestamp: 1707305664, ..Default::default() },
1812 ForkId { hash: ForkHash(hex!("0x9b192ad0")), next: 1740434112 },
1813 ),
1814 (
1816 Head { number: 123, timestamp: 1740434111, ..Default::default() },
1817 ForkId { hash: ForkHash(hex!("0x9b192ad0")), next: 1740434112 },
1818 ),
1819 (
1821 Head { number: 123, timestamp: 1740434112, ..Default::default() },
1822 ForkId {
1823 hash: ForkHash(hex!("0xdfbd9bed")),
1824 next: holesky::HOLESKY_OSAKA_TIMESTAMP,
1825 },
1826 ),
1827 (
1829 Head {
1830 number: 123,
1831 timestamp: holesky::HOLESKY_OSAKA_TIMESTAMP,
1832 ..Default::default()
1833 },
1834 ForkId {
1835 hash: ForkHash(hex!("0x783def52")),
1836 next: holesky::HOLESKY_BPO1_TIMESTAMP,
1837 },
1838 ),
1839 ],
1840 )
1841 }
1842
1843 #[test]
1844 fn sepolia_fork_ids() {
1845 test_fork_ids(
1846 &SEPOLIA,
1847 &[
1848 (
1849 Head { number: 0, ..Default::default() },
1850 ForkId { hash: ForkHash(hex!("0xfe3366e7")), next: 1735371 },
1851 ),
1852 (
1853 Head { number: 1735370, ..Default::default() },
1854 ForkId { hash: ForkHash(hex!("0xfe3366e7")), next: 1735371 },
1855 ),
1856 (
1857 Head { number: 1735371, ..Default::default() },
1858 ForkId { hash: ForkHash(hex!("0xb96cbd13")), next: 1677557088 },
1859 ),
1860 (
1861 Head { number: 1735372, timestamp: 1677557087, ..Default::default() },
1862 ForkId { hash: ForkHash(hex!("0xb96cbd13")), next: 1677557088 },
1863 ),
1864 (
1866 Head { number: 1735373, timestamp: 1677557088, ..Default::default() },
1867 ForkId { hash: ForkHash(hex!("0xf7f9bc08")), next: 1706655072 },
1868 ),
1869 (
1871 Head { number: 1735374, timestamp: 1706655071, ..Default::default() },
1872 ForkId { hash: ForkHash(hex!("0xf7f9bc08")), next: 1706655072 },
1873 ),
1874 (
1876 Head { number: 1735375, timestamp: 1706655072, ..Default::default() },
1877 ForkId { hash: ForkHash(hex!("0x88cf81d9")), next: 1741159776 },
1878 ),
1879 (
1881 Head { number: 1735376, timestamp: 1741159775, ..Default::default() },
1882 ForkId { hash: ForkHash(hex!("0x88cf81d9")), next: 1741159776 },
1883 ),
1884 (
1886 Head { number: 1735377, timestamp: 1741159776, ..Default::default() },
1887 ForkId {
1888 hash: ForkHash(hex!("0xed88b5fd")),
1889 next: sepolia::SEPOLIA_OSAKA_TIMESTAMP,
1890 },
1891 ),
1892 (
1894 Head {
1895 number: 1735377,
1896 timestamp: sepolia::SEPOLIA_OSAKA_TIMESTAMP,
1897 ..Default::default()
1898 },
1899 ForkId {
1900 hash: ForkHash(hex!("0xe2ae4999")),
1901 next: sepolia::SEPOLIA_BPO1_TIMESTAMP,
1902 },
1903 ),
1904 ],
1905 );
1906 }
1907
1908 #[test]
1909 fn dev_fork_ids() {
1910 test_fork_ids(
1911 &DEV,
1912 &[(
1913 Head { number: 0, ..Default::default() },
1914 ForkId { hash: ForkHash(hex!("0x0b1a4ef7")), next: 0 },
1915 )],
1916 )
1917 }
1918
1919 #[test]
1923 fn timestamped_forks() {
1924 let mainnet_with_timestamps = ChainSpecBuilder::mainnet().build();
1925 test_fork_ids(
1926 &mainnet_with_timestamps,
1927 &[
1928 (
1929 Head { number: 0, timestamp: 0, ..Default::default() },
1930 ForkId { hash: ForkHash(hex!("0xfc64ec04")), next: 1150000 },
1931 ), (
1933 Head { number: 1149999, timestamp: 0, ..Default::default() },
1934 ForkId { hash: ForkHash(hex!("0xfc64ec04")), next: 1150000 },
1935 ), (
1937 Head { number: 1150000, timestamp: 0, ..Default::default() },
1938 ForkId { hash: ForkHash(hex!("0x97c2c34c")), next: 1920000 },
1939 ), (
1941 Head { number: 1919999, timestamp: 0, ..Default::default() },
1942 ForkId { hash: ForkHash(hex!("0x97c2c34c")), next: 1920000 },
1943 ), (
1945 Head { number: 1920000, timestamp: 0, ..Default::default() },
1946 ForkId { hash: ForkHash(hex!("0x91d1f948")), next: 2463000 },
1947 ), (
1949 Head { number: 2462999, timestamp: 0, ..Default::default() },
1950 ForkId { hash: ForkHash(hex!("0x91d1f948")), next: 2463000 },
1951 ), (
1953 Head { number: 2463000, timestamp: 0, ..Default::default() },
1954 ForkId { hash: ForkHash(hex!("0x7a64da13")), next: 2675000 },
1955 ), (
1957 Head { number: 2674999, timestamp: 0, ..Default::default() },
1958 ForkId { hash: ForkHash(hex!("0x7a64da13")), next: 2675000 },
1959 ), (
1961 Head { number: 2675000, timestamp: 0, ..Default::default() },
1962 ForkId { hash: ForkHash(hex!("0x3edd5b10")), next: 4370000 },
1963 ), (
1965 Head { number: 4369999, timestamp: 0, ..Default::default() },
1966 ForkId { hash: ForkHash(hex!("0x3edd5b10")), next: 4370000 },
1967 ), (
1969 Head { number: 4370000, timestamp: 0, ..Default::default() },
1970 ForkId { hash: ForkHash(hex!("0xa00bc324")), next: 7280000 },
1971 ), (
1973 Head { number: 7279999, timestamp: 0, ..Default::default() },
1974 ForkId { hash: ForkHash(hex!("0xa00bc324")), next: 7280000 },
1975 ), (
1977 Head { number: 7280000, timestamp: 0, ..Default::default() },
1978 ForkId { hash: ForkHash(hex!("0x668db0af")), next: 9069000 },
1979 ), (
1981 Head { number: 9068999, timestamp: 0, ..Default::default() },
1982 ForkId { hash: ForkHash(hex!("0x668db0af")), next: 9069000 },
1983 ), (
1985 Head { number: 9069000, timestamp: 0, ..Default::default() },
1986 ForkId { hash: ForkHash(hex!("0x879d6e30")), next: 9200000 },
1987 ), (
1989 Head { number: 9199999, timestamp: 0, ..Default::default() },
1990 ForkId { hash: ForkHash(hex!("0x879d6e30")), next: 9200000 },
1991 ), (
1993 Head { number: 9200000, timestamp: 0, ..Default::default() },
1994 ForkId { hash: ForkHash(hex!("0xe029e991")), next: 12244000 },
1995 ), (
1997 Head { number: 12243999, timestamp: 0, ..Default::default() },
1998 ForkId { hash: ForkHash(hex!("0xe029e991")), next: 12244000 },
1999 ), (
2001 Head { number: 12244000, timestamp: 0, ..Default::default() },
2002 ForkId { hash: ForkHash(hex!("0x0eb440f6")), next: 12965000 },
2003 ), (
2005 Head { number: 12964999, timestamp: 0, ..Default::default() },
2006 ForkId { hash: ForkHash(hex!("0x0eb440f6")), next: 12965000 },
2007 ), (
2009 Head { number: 12965000, timestamp: 0, ..Default::default() },
2010 ForkId { hash: ForkHash(hex!("0xb715077d")), next: 13773000 },
2011 ), (
2013 Head { number: 13772999, timestamp: 0, ..Default::default() },
2014 ForkId { hash: ForkHash(hex!("0xb715077d")), next: 13773000 },
2015 ), (
2017 Head { number: 13773000, timestamp: 0, ..Default::default() },
2018 ForkId { hash: ForkHash(hex!("0x20c327fc")), next: 15050000 },
2019 ), (
2021 Head { number: 15049999, timestamp: 0, ..Default::default() },
2022 ForkId { hash: ForkHash(hex!("0x20c327fc")), next: 15050000 },
2023 ), (
2025 Head { number: 15050000, timestamp: 0, ..Default::default() },
2026 ForkId { hash: ForkHash(hex!("0xf0afd0e3")), next: 1681338455 },
2027 ), (
2029 Head { number: 19999999, timestamp: 1667999999, ..Default::default() },
2030 ForkId { hash: ForkHash(hex!("0xf0afd0e3")), next: 1681338455 },
2031 ), (
2033 Head { number: 20000000, timestamp: 1681338455, ..Default::default() },
2034 ForkId { hash: ForkHash(hex!("0xdce96c2d")), next: 1710338135 },
2035 ), (
2037 Head { number: 20000001, timestamp: 1710338134, ..Default::default() },
2038 ForkId { hash: ForkHash(hex!("0xdce96c2d")), next: 1710338135 },
2039 ), (
2041 Head { number: 20000002, timestamp: 1710338135, ..Default::default() },
2042 ForkId { hash: ForkHash(hex!("0x9f3d2254")), next: 1746612311 },
2043 ), (
2045 Head { number: 20000003, timestamp: 1746612310, ..Default::default() },
2046 ForkId { hash: ForkHash(hex!("0x9f3d2254")), next: 1746612311 },
2047 ), (
2049 Head { number: 20000004, timestamp: 1746612311, ..Default::default() },
2050 ForkId {
2051 hash: ForkHash(hex!("0xc376cf8b")),
2052 next: mainnet::MAINNET_OSAKA_TIMESTAMP,
2053 },
2054 ),
2055 (
2057 Head {
2058 number: 20000004,
2059 timestamp: mainnet::MAINNET_OSAKA_TIMESTAMP,
2060 ..Default::default()
2061 },
2062 ForkId {
2063 hash: ForkHash(hex!("0x5167e2a6")),
2064 next: mainnet::MAINNET_BPO1_TIMESTAMP,
2065 },
2066 ),
2067 ],
2068 );
2069 }
2070
2071 fn construct_chainspec(
2074 builder: ChainSpecBuilder,
2075 shanghai_time: u64,
2076 cancun_time: u64,
2077 ) -> ChainSpec {
2078 builder
2079 .with_fork(EthereumHardfork::Shanghai, ForkCondition::Timestamp(shanghai_time))
2080 .with_fork(EthereumHardfork::Cancun, ForkCondition::Timestamp(cancun_time))
2081 .build()
2082 }
2083
2084 #[test]
2089 fn test_timestamp_fork_in_genesis() {
2090 let timestamp = 1690475657u64;
2091 let default_spec_builder = ChainSpecBuilder::default()
2092 .chain(Chain::from_id(1337))
2093 .genesis(Genesis::default().with_timestamp(timestamp))
2094 .paris_activated();
2095
2096 let tests = [
2099 (
2100 construct_chainspec(default_spec_builder.clone(), timestamp - 1, timestamp + 1),
2101 timestamp + 1,
2102 ),
2103 (
2104 construct_chainspec(default_spec_builder.clone(), timestamp, timestamp + 1),
2105 timestamp + 1,
2106 ),
2107 (
2108 construct_chainspec(default_spec_builder, timestamp + 1, timestamp + 2),
2109 timestamp + 1,
2110 ),
2111 ];
2112
2113 for (spec, expected_timestamp) in tests {
2114 let got_forkid = spec.fork_id(&Head { number: 0, timestamp: 0, ..Default::default() });
2115 let genesis_hash = spec.genesis_hash();
2120 let expected_forkid =
2121 ForkId { hash: ForkHash::from(genesis_hash), next: expected_timestamp };
2122 assert_eq!(got_forkid, expected_forkid);
2123 }
2124 }
2125
2126 #[test]
2128 fn check_terminal_ttd() {
2129 let chainspec = ChainSpecBuilder::mainnet().build();
2130
2131 let terminal_block_ttd = U256::from(58750003716598352816469_u128);
2133 let terminal_block_difficulty = U256::from(11055787484078698_u128);
2134 assert!(!chainspec
2135 .fork(EthereumHardfork::Paris)
2136 .active_at_ttd(terminal_block_ttd, terminal_block_difficulty));
2137
2138 let first_pos_block_ttd = U256::from(58750003716598352816469_u128);
2140 let first_pos_difficulty = U256::ZERO;
2141 assert!(chainspec
2142 .fork(EthereumHardfork::Paris)
2143 .active_at_ttd(first_pos_block_ttd, first_pos_difficulty));
2144 }
2145
2146 #[test]
2147 fn geth_genesis_with_shanghai() {
2148 let geth_genesis = r#"
2149 {
2150 "config": {
2151 "chainId": 1337,
2152 "homesteadBlock": 0,
2153 "eip150Block": 0,
2154 "eip150Hash": "0x0000000000000000000000000000000000000000000000000000000000000000",
2155 "eip155Block": 0,
2156 "eip158Block": 0,
2157 "byzantiumBlock": 0,
2158 "constantinopleBlock": 0,
2159 "petersburgBlock": 0,
2160 "istanbulBlock": 0,
2161 "muirGlacierBlock": 0,
2162 "berlinBlock": 0,
2163 "londonBlock": 0,
2164 "arrowGlacierBlock": 0,
2165 "grayGlacierBlock": 0,
2166 "shanghaiTime": 0,
2167 "cancunTime": 1,
2168 "terminalTotalDifficulty": 0,
2169 "terminalTotalDifficultyPassed": true,
2170 "ethash": {}
2171 },
2172 "nonce": "0x0",
2173 "timestamp": "0x0",
2174 "extraData": "0x",
2175 "gasLimit": "0x4c4b40",
2176 "difficulty": "0x1",
2177 "mixHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
2178 "coinbase": "0x0000000000000000000000000000000000000000",
2179 "alloc": {
2180 "658bdf435d810c91414ec09147daa6db62406379": {
2181 "balance": "0x487a9a304539440000"
2182 },
2183 "aa00000000000000000000000000000000000000": {
2184 "code": "0x6042",
2185 "storage": {
2186 "0x0000000000000000000000000000000000000000000000000000000000000000": "0x0000000000000000000000000000000000000000000000000000000000000000",
2187 "0x0100000000000000000000000000000000000000000000000000000000000000": "0x0100000000000000000000000000000000000000000000000000000000000000",
2188 "0x0200000000000000000000000000000000000000000000000000000000000000": "0x0200000000000000000000000000000000000000000000000000000000000000",
2189 "0x0300000000000000000000000000000000000000000000000000000000000000": "0x0000000000000000000000000000000000000000000000000000000000000303"
2190 },
2191 "balance": "0x1",
2192 "nonce": "0x1"
2193 },
2194 "bb00000000000000000000000000000000000000": {
2195 "code": "0x600154600354",
2196 "storage": {
2197 "0x0000000000000000000000000000000000000000000000000000000000000000": "0x0000000000000000000000000000000000000000000000000000000000000000",
2198 "0x0100000000000000000000000000000000000000000000000000000000000000": "0x0100000000000000000000000000000000000000000000000000000000000000",
2199 "0x0200000000000000000000000000000000000000000000000000000000000000": "0x0200000000000000000000000000000000000000000000000000000000000000",
2200 "0x0300000000000000000000000000000000000000000000000000000000000000": "0x0000000000000000000000000000000000000000000000000000000000000303"
2201 },
2202 "balance": "0x2",
2203 "nonce": "0x1"
2204 }
2205 },
2206 "number": "0x0",
2207 "gasUsed": "0x0",
2208 "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
2209 "baseFeePerGas": "0x3b9aca00"
2210 }
2211 "#;
2212
2213 let genesis: Genesis = serde_json::from_str(geth_genesis).unwrap();
2214 let chainspec = ChainSpec::from(genesis);
2215
2216 assert_eq!(
2218 chainspec.hardforks.get(EthereumHardfork::Homestead).unwrap(),
2219 ForkCondition::Block(0)
2220 );
2221 assert_eq!(
2222 chainspec.hardforks.get(EthereumHardfork::Tangerine).unwrap(),
2223 ForkCondition::Block(0)
2224 );
2225 assert_eq!(
2226 chainspec.hardforks.get(EthereumHardfork::SpuriousDragon).unwrap(),
2227 ForkCondition::Block(0)
2228 );
2229 assert_eq!(
2230 chainspec.hardforks.get(EthereumHardfork::Byzantium).unwrap(),
2231 ForkCondition::Block(0)
2232 );
2233 assert_eq!(
2234 chainspec.hardforks.get(EthereumHardfork::Constantinople).unwrap(),
2235 ForkCondition::Block(0)
2236 );
2237 assert_eq!(
2238 chainspec.hardforks.get(EthereumHardfork::Petersburg).unwrap(),
2239 ForkCondition::Block(0)
2240 );
2241 assert_eq!(
2242 chainspec.hardforks.get(EthereumHardfork::Istanbul).unwrap(),
2243 ForkCondition::Block(0)
2244 );
2245 assert_eq!(
2246 chainspec.hardforks.get(EthereumHardfork::MuirGlacier).unwrap(),
2247 ForkCondition::Block(0)
2248 );
2249 assert_eq!(
2250 chainspec.hardforks.get(EthereumHardfork::Berlin).unwrap(),
2251 ForkCondition::Block(0)
2252 );
2253 assert_eq!(
2254 chainspec.hardforks.get(EthereumHardfork::London).unwrap(),
2255 ForkCondition::Block(0)
2256 );
2257 assert_eq!(
2258 chainspec.hardforks.get(EthereumHardfork::ArrowGlacier).unwrap(),
2259 ForkCondition::Block(0)
2260 );
2261 assert_eq!(
2262 chainspec.hardforks.get(EthereumHardfork::GrayGlacier).unwrap(),
2263 ForkCondition::Block(0)
2264 );
2265
2266 assert_eq!(
2268 chainspec.hardforks.get(EthereumHardfork::Shanghai).unwrap(),
2269 ForkCondition::Timestamp(0)
2270 );
2271
2272 assert_eq!(
2274 chainspec.hardforks.get(EthereumHardfork::Cancun).unwrap(),
2275 ForkCondition::Timestamp(1)
2276 );
2277
2278 let key_rlp = vec![
2280 (
2281 hex!("0x658bdf435d810c91414ec09147daa6db62406379"),
2282 &hex!(
2283 "0xf84d8089487a9a304539440000a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a0c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470"
2284 )[..],
2285 ),
2286 (
2287 hex!("0xaa00000000000000000000000000000000000000"),
2288 &hex!(
2289 "0xf8440101a08afc95b7d18a226944b9c2070b6bda1c3a36afcc3730429d47579c94b9fe5850a0ce92c756baff35fa740c3557c1a971fd24d2d35b7c8e067880d50cd86bb0bc99"
2290 )[..],
2291 ),
2292 (
2293 hex!("0xbb00000000000000000000000000000000000000"),
2294 &hex!(
2295 "0xf8440102a08afc95b7d18a226944b9c2070b6bda1c3a36afcc3730429d47579c94b9fe5850a0e25a53cbb501cec2976b393719c63d832423dd70a458731a0b64e4847bbca7d2"
2296 )[..],
2297 ),
2298 ];
2299
2300 for (key, expected_rlp) in key_rlp {
2301 let account = chainspec.genesis.alloc.get(&key).expect("account should exist");
2302 assert_eq!(&alloy_rlp::encode(TrieAccount::from(account.clone())), expected_rlp);
2303 }
2304
2305 let expected_state_root: B256 =
2306 hex!("0x078dc6061b1d8eaa8493384b59c9c65ceb917201221d08b80c4de6770b6ec7e7").into();
2307 assert_eq!(chainspec.genesis_header().state_root, expected_state_root);
2308
2309 assert_eq!(chainspec.genesis_header().withdrawals_root, Some(EMPTY_ROOT_HASH));
2310
2311 let expected_hash: B256 =
2312 hex!("0x1fc027d65f820d3eef441ebeec139ebe09e471cf98516dce7b5643ccb27f418c").into();
2313 let hash = chainspec.genesis_hash();
2314 assert_eq!(hash, expected_hash);
2315 }
2316
2317 #[test]
2318 fn hive_geth_json() {
2319 let hive_json = r#"
2320 {
2321 "nonce": "0x0000000000000042",
2322 "difficulty": "0x2123456",
2323 "mixHash": "0x123456789abcdef123456789abcdef123456789abcdef123456789abcdef1234",
2324 "coinbase": "0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
2325 "timestamp": "0x123456",
2326 "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
2327 "extraData": "0xfafbfcfd",
2328 "gasLimit": "0x2fefd8",
2329 "alloc": {
2330 "dbdbdb2cbd23b783741e8d7fcf51e459b497e4a6": {
2331 "balance": "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
2332 },
2333 "e6716f9544a56c530d868e4bfbacb172315bdead": {
2334 "balance": "0x11",
2335 "code": "0x12"
2336 },
2337 "b9c015918bdaba24b4ff057a92a3873d6eb201be": {
2338 "balance": "0x21",
2339 "storage": {
2340 "0x0000000000000000000000000000000000000000000000000000000000000001": "0x22"
2341 }
2342 },
2343 "1a26338f0d905e295fccb71fa9ea849ffa12aaf4": {
2344 "balance": "0x31",
2345 "nonce": "0x32"
2346 },
2347 "0000000000000000000000000000000000000001": {
2348 "balance": "0x41"
2349 },
2350 "0000000000000000000000000000000000000002": {
2351 "balance": "0x51"
2352 },
2353 "0000000000000000000000000000000000000003": {
2354 "balance": "0x61"
2355 },
2356 "0000000000000000000000000000000000000004": {
2357 "balance": "0x71"
2358 }
2359 },
2360 "config": {
2361 "ethash": {},
2362 "chainId": 10,
2363 "homesteadBlock": 0,
2364 "eip150Block": 0,
2365 "eip155Block": 0,
2366 "eip158Block": 0,
2367 "byzantiumBlock": 0,
2368 "constantinopleBlock": 0,
2369 "petersburgBlock": 0,
2370 "istanbulBlock": 0
2371 }
2372 }
2373 "#;
2374
2375 let genesis = serde_json::from_str::<Genesis>(hive_json).unwrap();
2376 let chainspec: ChainSpec = genesis.into();
2377 assert_eq!(chainspec.chain, Chain::from_named(NamedChain::Optimism));
2378 let expected_state_root: B256 =
2379 hex!("0x9a6049ac535e3dc7436c189eaa81c73f35abd7f282ab67c32944ff0301d63360").into();
2380 assert_eq!(chainspec.genesis_header().state_root, expected_state_root);
2381 let hard_forks = vec![
2382 EthereumHardfork::Byzantium,
2383 EthereumHardfork::Homestead,
2384 EthereumHardfork::Istanbul,
2385 EthereumHardfork::Petersburg,
2386 EthereumHardfork::Constantinople,
2387 ];
2388 for fork in hard_forks {
2389 assert_eq!(chainspec.hardforks.get(fork).unwrap(), ForkCondition::Block(0));
2390 }
2391
2392 let expected_hash: B256 =
2393 hex!("0x5ae31c6522bd5856129f66be3d582b842e4e9faaa87f21cce547128339a9db3c").into();
2394 let hash = chainspec.genesis_header().hash_slow();
2395 assert_eq!(hash, expected_hash);
2396 }
2397
2398 #[test]
2399 fn test_hive_paris_block_genesis_json() {
2400 let hive_paris = r#"
2403 {
2404 "config": {
2405 "ethash": {},
2406 "chainId": 3503995874084926,
2407 "homesteadBlock": 0,
2408 "eip150Block": 6,
2409 "eip155Block": 12,
2410 "eip158Block": 12,
2411 "byzantiumBlock": 18,
2412 "constantinopleBlock": 24,
2413 "petersburgBlock": 30,
2414 "istanbulBlock": 36,
2415 "muirGlacierBlock": 42,
2416 "berlinBlock": 48,
2417 "londonBlock": 54,
2418 "arrowGlacierBlock": 60,
2419 "grayGlacierBlock": 66,
2420 "mergeNetsplitBlock": 72,
2421 "terminalTotalDifficulty": 9454784,
2422 "shanghaiTime": 780,
2423 "cancunTime": 840
2424 },
2425 "nonce": "0x0",
2426 "timestamp": "0x0",
2427 "extraData": "0x68697665636861696e",
2428 "gasLimit": "0x23f3e20",
2429 "difficulty": "0x20000",
2430 "mixHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
2431 "coinbase": "0x0000000000000000000000000000000000000000",
2432 "alloc": {
2433 "000f3df6d732807ef1319fb7b8bb8522d0beac02": {
2434 "code": "0x3373fffffffffffffffffffffffffffffffffffffffe14604d57602036146024575f5ffd5b5f35801560495762001fff810690815414603c575f5ffd5b62001fff01545f5260205ff35b5f5ffd5b62001fff42064281555f359062001fff015500",
2435 "balance": "0x2a"
2436 },
2437 "0c2c51a0990aee1d73c1228de158688341557508": {
2438 "balance": "0xc097ce7bc90715b34b9f1000000000"
2439 },
2440 "14e46043e63d0e3cdcf2530519f4cfaf35058cb2": {
2441 "balance": "0xc097ce7bc90715b34b9f1000000000"
2442 },
2443 "16c57edf7fa9d9525378b0b81bf8a3ced0620c1c": {
2444 "balance": "0xc097ce7bc90715b34b9f1000000000"
2445 },
2446 "1f4924b14f34e24159387c0a4cdbaa32f3ddb0cf": {
2447 "balance": "0xc097ce7bc90715b34b9f1000000000"
2448 },
2449 "1f5bde34b4afc686f136c7a3cb6ec376f7357759": {
2450 "balance": "0xc097ce7bc90715b34b9f1000000000"
2451 },
2452 "2d389075be5be9f2246ad654ce152cf05990b209": {
2453 "balance": "0xc097ce7bc90715b34b9f1000000000"
2454 },
2455 "3ae75c08b4c907eb63a8960c45b86e1e9ab6123c": {
2456 "balance": "0xc097ce7bc90715b34b9f1000000000"
2457 },
2458 "4340ee1b812acb40a1eb561c019c327b243b92df": {
2459 "balance": "0xc097ce7bc90715b34b9f1000000000"
2460 },
2461 "4a0f1452281bcec5bd90c3dce6162a5995bfe9df": {
2462 "balance": "0xc097ce7bc90715b34b9f1000000000"
2463 },
2464 "4dde844b71bcdf95512fb4dc94e84fb67b512ed8": {
2465 "balance": "0xc097ce7bc90715b34b9f1000000000"
2466 },
2467 "5f552da00dfb4d3749d9e62dcee3c918855a86a0": {
2468 "balance": "0xc097ce7bc90715b34b9f1000000000"
2469 },
2470 "654aa64f5fbefb84c270ec74211b81ca8c44a72e": {
2471 "balance": "0xc097ce7bc90715b34b9f1000000000"
2472 },
2473 "717f8aa2b982bee0e29f573d31df288663e1ce16": {
2474 "balance": "0xc097ce7bc90715b34b9f1000000000"
2475 },
2476 "7435ed30a8b4aeb0877cef0c6e8cffe834eb865f": {
2477 "balance": "0xc097ce7bc90715b34b9f1000000000"
2478 },
2479 "83c7e323d189f18725ac510004fdc2941f8c4a78": {
2480 "balance": "0xc097ce7bc90715b34b9f1000000000"
2481 },
2482 "84e75c28348fb86acea1a93a39426d7d60f4cc46": {
2483 "balance": "0xc097ce7bc90715b34b9f1000000000"
2484 },
2485 "8bebc8ba651aee624937e7d897853ac30c95a067": {
2486 "storage": {
2487 "0x0000000000000000000000000000000000000000000000000000000000000001": "0x0000000000000000000000000000000000000000000000000000000000000001",
2488 "0x0000000000000000000000000000000000000000000000000000000000000002": "0x0000000000000000000000000000000000000000000000000000000000000002",
2489 "0x0000000000000000000000000000000000000000000000000000000000000003": "0x0000000000000000000000000000000000000000000000000000000000000003"
2490 },
2491 "balance": "0x1",
2492 "nonce": "0x1"
2493 },
2494 "c7b99a164efd027a93f147376cc7da7c67c6bbe0": {
2495 "balance": "0xc097ce7bc90715b34b9f1000000000"
2496 },
2497 "d803681e487e6ac18053afc5a6cd813c86ec3e4d": {
2498 "balance": "0xc097ce7bc90715b34b9f1000000000"
2499 },
2500 "e7d13f7aa2a838d24c59b40186a0aca1e21cffcc": {
2501 "balance": "0xc097ce7bc90715b34b9f1000000000"
2502 },
2503 "eda8645ba6948855e3b3cd596bbb07596d59c603": {
2504 "balance": "0xc097ce7bc90715b34b9f1000000000"
2505 }
2506 },
2507 "number": "0x0",
2508 "gasUsed": "0x0",
2509 "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
2510 "baseFeePerGas": null,
2511 "excessBlobGas": null,
2512 "blobGasUsed": null
2513 }
2514 "#;
2515
2516 let genesis: Genesis = serde_json::from_str(hive_paris).unwrap();
2518 let chainspec = ChainSpec::from(genesis);
2519
2520 let expected_forkid = ForkId { hash: ForkHash(hex!("0xbc0c2605")), next: 0 };
2522 let got_forkid =
2523 chainspec.fork_id(&Head { number: 73, timestamp: 840, ..Default::default() });
2524
2525 assert_eq!(got_forkid, expected_forkid);
2527 assert_eq!(chainspec.paris_block_and_final_difficulty, Some((72, U256::from(9454784))));
2529 }
2530
2531 #[test]
2532 fn test_parse_genesis_json() {
2533 let s = r#"{"config":{"ethash":{},"chainId":1337,"homesteadBlock":0,"eip150Block":0,"eip155Block":0,"eip158Block":0,"byzantiumBlock":0,"constantinopleBlock":0,"petersburgBlock":0,"istanbulBlock":0,"berlinBlock":0,"londonBlock":0,"terminalTotalDifficulty":0,"terminalTotalDifficultyPassed":true,"shanghaiTime":0},"nonce":"0x0","timestamp":"0x0","extraData":"0x","gasLimit":"0x4c4b40","difficulty":"0x1","mixHash":"0x0000000000000000000000000000000000000000000000000000000000000000","coinbase":"0x0000000000000000000000000000000000000000","alloc":{"658bdf435d810c91414ec09147daa6db62406379":{"balance":"0x487a9a304539440000"},"aa00000000000000000000000000000000000000":{"code":"0x6042","storage":{"0x0000000000000000000000000000000000000000000000000000000000000000":"0x0000000000000000000000000000000000000000000000000000000000000000","0x0100000000000000000000000000000000000000000000000000000000000000":"0x0100000000000000000000000000000000000000000000000000000000000000","0x0200000000000000000000000000000000000000000000000000000000000000":"0x0200000000000000000000000000000000000000000000000000000000000000","0x0300000000000000000000000000000000000000000000000000000000000000":"0x0000000000000000000000000000000000000000000000000000000000000303"},"balance":"0x1","nonce":"0x1"},"bb00000000000000000000000000000000000000":{"code":"0x600154600354","storage":{"0x0000000000000000000000000000000000000000000000000000000000000000":"0x0000000000000000000000000000000000000000000000000000000000000000","0x0100000000000000000000000000000000000000000000000000000000000000":"0x0100000000000000000000000000000000000000000000000000000000000000","0x0200000000000000000000000000000000000000000000000000000000000000":"0x0200000000000000000000000000000000000000000000000000000000000000","0x0300000000000000000000000000000000000000000000000000000000000000":"0x0000000000000000000000000000000000000000000000000000000000000303"},"balance":"0x2","nonce":"0x1"}},"number":"0x0","gasUsed":"0x0","parentHash":"0x0000000000000000000000000000000000000000000000000000000000000000","baseFeePerGas":"0x1337"}"#;
2534 let genesis: Genesis = serde_json::from_str(s).unwrap();
2535 let acc = genesis
2536 .alloc
2537 .get(&"0xaa00000000000000000000000000000000000000".parse::<Address>().unwrap())
2538 .unwrap();
2539 assert_eq!(acc.balance, U256::from(1));
2540 assert_eq!(genesis.base_fee_per_gas, Some(0x1337));
2541 }
2542
2543 #[test]
2544 fn test_parse_cancun_genesis_json() {
2545 let s = r#"{"config":{"ethash":{},"chainId":1337,"homesteadBlock":0,"eip150Block":0,"eip155Block":0,"eip158Block":0,"byzantiumBlock":0,"constantinopleBlock":0,"petersburgBlock":0,"istanbulBlock":0,"berlinBlock":0,"londonBlock":0,"terminalTotalDifficulty":0,"terminalTotalDifficultyPassed":true,"shanghaiTime":0,"cancunTime":4661},"nonce":"0x0","timestamp":"0x0","extraData":"0x","gasLimit":"0x4c4b40","difficulty":"0x1","mixHash":"0x0000000000000000000000000000000000000000000000000000000000000000","coinbase":"0x0000000000000000000000000000000000000000","alloc":{"658bdf435d810c91414ec09147daa6db62406379":{"balance":"0x487a9a304539440000"},"aa00000000000000000000000000000000000000":{"code":"0x6042","storage":{"0x0000000000000000000000000000000000000000000000000000000000000000":"0x0000000000000000000000000000000000000000000000000000000000000000","0x0100000000000000000000000000000000000000000000000000000000000000":"0x0100000000000000000000000000000000000000000000000000000000000000","0x0200000000000000000000000000000000000000000000000000000000000000":"0x0200000000000000000000000000000000000000000000000000000000000000","0x0300000000000000000000000000000000000000000000000000000000000000":"0x0000000000000000000000000000000000000000000000000000000000000303"},"balance":"0x1","nonce":"0x1"},"bb00000000000000000000000000000000000000":{"code":"0x600154600354","storage":{"0x0000000000000000000000000000000000000000000000000000000000000000":"0x0000000000000000000000000000000000000000000000000000000000000000","0x0100000000000000000000000000000000000000000000000000000000000000":"0x0100000000000000000000000000000000000000000000000000000000000000","0x0200000000000000000000000000000000000000000000000000000000000000":"0x0200000000000000000000000000000000000000000000000000000000000000","0x0300000000000000000000000000000000000000000000000000000000000000":"0x0000000000000000000000000000000000000000000000000000000000000303"},"balance":"0x2","nonce":"0x1"}},"number":"0x0","gasUsed":"0x0","parentHash":"0x0000000000000000000000000000000000000000000000000000000000000000","baseFeePerGas":"0x3b9aca00"}"#;
2546 let genesis: Genesis = serde_json::from_str(s).unwrap();
2547 let acc = genesis
2548 .alloc
2549 .get(&"0xaa00000000000000000000000000000000000000".parse::<Address>().unwrap())
2550 .unwrap();
2551 assert_eq!(acc.balance, U256::from(1));
2552 assert_eq!(genesis.config.cancun_time, Some(4661));
2554 }
2555
2556 #[test]
2557 fn test_parse_prague_genesis_all_formats() {
2558 let s = r#"{"config":{"ethash":{},"chainId":1337,"homesteadBlock":0,"eip150Block":0,"eip155Block":0,"eip158Block":0,"byzantiumBlock":0,"constantinopleBlock":0,"petersburgBlock":0,"istanbulBlock":0,"berlinBlock":0,"londonBlock":0,"terminalTotalDifficulty":0,"terminalTotalDifficultyPassed":true,"shanghaiTime":0,"cancunTime":4661, "pragueTime": 4662},"nonce":"0x0","timestamp":"0x0","extraData":"0x","gasLimit":"0x4c4b40","difficulty":"0x1","mixHash":"0x0000000000000000000000000000000000000000000000000000000000000000","coinbase":"0x0000000000000000000000000000000000000000","alloc":{"658bdf435d810c91414ec09147daa6db62406379":{"balance":"0x487a9a304539440000"},"aa00000000000000000000000000000000000000":{"code":"0x6042","storage":{"0x0000000000000000000000000000000000000000000000000000000000000000":"0x0000000000000000000000000000000000000000000000000000000000000000","0x0100000000000000000000000000000000000000000000000000000000000000":"0x0100000000000000000000000000000000000000000000000000000000000000","0x0200000000000000000000000000000000000000000000000000000000000000":"0x0200000000000000000000000000000000000000000000000000000000000000","0x0300000000000000000000000000000000000000000000000000000000000000":"0x0000000000000000000000000000000000000000000000000000000000000303"},"balance":"0x1","nonce":"0x1"},"bb00000000000000000000000000000000000000":{"code":"0x600154600354","storage":{"0x0000000000000000000000000000000000000000000000000000000000000000":"0x0000000000000000000000000000000000000000000000000000000000000000","0x0100000000000000000000000000000000000000000000000000000000000000":"0x0100000000000000000000000000000000000000000000000000000000000000","0x0200000000000000000000000000000000000000000000000000000000000000":"0x0200000000000000000000000000000000000000000000000000000000000000","0x0300000000000000000000000000000000000000000000000000000000000000":"0x0000000000000000000000000000000000000000000000000000000000000303"},"balance":"0x2","nonce":"0x1"}},"number":"0x0","gasUsed":"0x0","parentHash":"0x0000000000000000000000000000000000000000000000000000000000000000","baseFeePerGas":"0x3b9aca00"}"#;
2559 let genesis: Genesis = serde_json::from_str(s).unwrap();
2560
2561 let acc = genesis
2563 .alloc
2564 .get(&"0xaa00000000000000000000000000000000000000".parse::<Address>().unwrap())
2565 .unwrap();
2566 assert_eq!(acc.balance, U256::from(1));
2567 assert_eq!(genesis.config.cancun_time, Some(4661));
2569 assert_eq!(genesis.config.prague_time, Some(4662));
2571 }
2572
2573 #[test]
2574 fn test_parse_cancun_genesis_all_formats() {
2575 let s = r#"{"config":{"ethash":{},"chainId":1337,"homesteadBlock":0,"eip150Block":0,"eip155Block":0,"eip158Block":0,"byzantiumBlock":0,"constantinopleBlock":0,"petersburgBlock":0,"istanbulBlock":0,"berlinBlock":0,"londonBlock":0,"terminalTotalDifficulty":0,"terminalTotalDifficultyPassed":true,"shanghaiTime":0,"cancunTime":4661},"nonce":"0x0","timestamp":"0x0","extraData":"0x","gasLimit":"0x4c4b40","difficulty":"0x1","mixHash":"0x0000000000000000000000000000000000000000000000000000000000000000","coinbase":"0x0000000000000000000000000000000000000000","alloc":{"658bdf435d810c91414ec09147daa6db62406379":{"balance":"0x487a9a304539440000"},"aa00000000000000000000000000000000000000":{"code":"0x6042","storage":{"0x0000000000000000000000000000000000000000000000000000000000000000":"0x0000000000000000000000000000000000000000000000000000000000000000","0x0100000000000000000000000000000000000000000000000000000000000000":"0x0100000000000000000000000000000000000000000000000000000000000000","0x0200000000000000000000000000000000000000000000000000000000000000":"0x0200000000000000000000000000000000000000000000000000000000000000","0x0300000000000000000000000000000000000000000000000000000000000000":"0x0000000000000000000000000000000000000000000000000000000000000303"},"balance":"0x1","nonce":"0x1"},"bb00000000000000000000000000000000000000":{"code":"0x600154600354","storage":{"0x0000000000000000000000000000000000000000000000000000000000000000":"0x0000000000000000000000000000000000000000000000000000000000000000","0x0100000000000000000000000000000000000000000000000000000000000000":"0x0100000000000000000000000000000000000000000000000000000000000000","0x0200000000000000000000000000000000000000000000000000000000000000":"0x0200000000000000000000000000000000000000000000000000000000000000","0x0300000000000000000000000000000000000000000000000000000000000000":"0x0000000000000000000000000000000000000000000000000000000000000303"},"balance":"0x2","nonce":"0x1"}},"number":"0x0","gasUsed":"0x0","parentHash":"0x0000000000000000000000000000000000000000000000000000000000000000","baseFeePerGas":"0x3b9aca00"}"#;
2576 let genesis: Genesis = serde_json::from_str(s).unwrap();
2577
2578 let acc = genesis
2580 .alloc
2581 .get(&"0xaa00000000000000000000000000000000000000".parse::<Address>().unwrap())
2582 .unwrap();
2583 assert_eq!(acc.balance, U256::from(1));
2584 assert_eq!(genesis.config.cancun_time, Some(4661));
2586 }
2587
2588 #[test]
2589 fn test_paris_block_and_total_difficulty() {
2590 let genesis = Genesis { gas_limit: 0x2fefd8u64, ..Default::default() };
2591 let paris_chainspec = ChainSpecBuilder::default()
2592 .chain(Chain::from_id(1337))
2593 .genesis(genesis)
2594 .paris_activated()
2595 .build();
2596 assert_eq!(paris_chainspec.paris_block_and_final_difficulty, Some((0, U256::ZERO)));
2597 }
2598
2599 #[test]
2600 fn test_default_cancun_header_forkhash() {
2601 let genesis = Genesis { gas_limit: 0x2fefd8u64, ..Default::default() };
2603 let default_chainspec = ChainSpecBuilder::default()
2604 .chain(Chain::from_id(1337))
2605 .genesis(genesis)
2606 .cancun_activated()
2607 .build();
2608 let mut header = default_chainspec.genesis_header().clone();
2609
2610 header.state_root =
2612 B256::from_str("0x62e2595e017f0ca23e08d17221010721a71c3ae932f4ea3cb12117786bb392d4")
2613 .unwrap();
2614
2615 assert_eq!(header.withdrawals_root, Some(EMPTY_WITHDRAWALS));
2617
2618 assert_eq!(header.parent_beacon_block_root, Some(B256::ZERO));
2621 assert_eq!(header.blob_gas_used, Some(0));
2622 assert_eq!(header.excess_blob_gas, Some(0));
2623
2624 let genesis_hash = header.hash_slow();
2626 let expected_hash =
2627 b256!("0x16bb7c59613a5bad3f7c04a852fd056545ade2483968d9a25a1abb05af0c4d37");
2628 assert_eq!(genesis_hash, expected_hash);
2629
2630 let expected_forkhash = ForkHash(hex!("0x8062457a"));
2632 assert_eq!(ForkHash::from(genesis_hash), expected_forkhash);
2633 }
2634
2635 #[test]
2636 fn holesky_paris_activated_at_genesis() {
2637 assert!(HOLESKY
2638 .fork(EthereumHardfork::Paris)
2639 .active_at_ttd(HOLESKY.genesis.difficulty, HOLESKY.genesis.difficulty));
2640 }
2641
2642 #[test]
2643 fn test_genesis_format_deserialization() {
2644 let config = ChainConfig {
2646 chain_id: 2600,
2647 homestead_block: Some(0),
2648 eip150_block: Some(0),
2649 eip155_block: Some(0),
2650 eip158_block: Some(0),
2651 byzantium_block: Some(0),
2652 constantinople_block: Some(0),
2653 petersburg_block: Some(0),
2654 istanbul_block: Some(0),
2655 berlin_block: Some(0),
2656 london_block: Some(0),
2657 shanghai_time: Some(0),
2658 terminal_total_difficulty: Some(U256::ZERO),
2659 terminal_total_difficulty_passed: true,
2660 ..Default::default()
2661 };
2662 let genesis = Genesis {
2664 config,
2665 nonce: 0,
2666 timestamp: 1698688670,
2667 gas_limit: 5000,
2668 difficulty: U256::ZERO,
2669 mix_hash: B256::ZERO,
2670 coinbase: Address::ZERO,
2671 ..Default::default()
2672 };
2673
2674 let address = hex!("0x6Be02d1d3665660d22FF9624b7BE0551ee1Ac91b").into();
2676 let account = GenesisAccount::default().with_balance(U256::from(33));
2677 let genesis = genesis.extend_accounts(HashMap::from([(address, account)]));
2678
2679 let serialized_genesis = serde_json::to_string(&genesis).unwrap();
2681 let deserialized_genesis: Genesis = serde_json::from_str(&serialized_genesis).unwrap();
2682
2683 assert_eq!(genesis, deserialized_genesis);
2684 }
2685
2686 #[test]
2687 fn check_fork_id_chainspec_with_fork_condition_never() {
2688 let spec: ChainSpec = ChainSpec {
2689 chain: Chain::mainnet(),
2690 genesis: Genesis::default(),
2691 hardforks: ChainHardforks::new(vec![(
2692 EthereumHardfork::Frontier.boxed(),
2693 ForkCondition::Never,
2694 )]),
2695 paris_block_and_final_difficulty: None,
2696 deposit_contract: None,
2697 ..Default::default()
2698 };
2699
2700 assert_eq!(spec.hardfork_fork_id(EthereumHardfork::Frontier), None);
2701 }
2702
2703 #[test]
2704 fn check_fork_filter_chainspec_with_fork_condition_never() {
2705 let spec: ChainSpec = ChainSpec {
2706 chain: Chain::mainnet(),
2707 genesis: Genesis::default(),
2708 hardforks: ChainHardforks::new(vec![(
2709 EthereumHardfork::Shanghai.boxed(),
2710 ForkCondition::Never,
2711 )]),
2712 paris_block_and_final_difficulty: None,
2713 deposit_contract: None,
2714 ..Default::default()
2715 };
2716
2717 assert_eq!(spec.hardfork_fork_filter(EthereumHardfork::Shanghai), None);
2718 }
2719
2720 #[test]
2721 fn latest_eth_mainnet_fork_id() {
2722 assert_eq!(ForkId { hash: ForkHash(hex!("0x07c9462e")), next: 0 }, MAINNET.latest_fork_id())
2724 }
2725
2726 #[test]
2727 fn latest_hoodi_mainnet_fork_id() {
2728 assert_eq!(ForkId { hash: ForkHash(hex!("0x23aa1351")), next: 0 }, HOODI.latest_fork_id())
2730 }
2731
2732 #[test]
2733 fn latest_holesky_mainnet_fork_id() {
2734 assert_eq!(ForkId { hash: ForkHash(hex!("0x9bc6cb31")), next: 0 }, HOLESKY.latest_fork_id())
2736 }
2737
2738 #[test]
2739 fn latest_sepolia_mainnet_fork_id() {
2740 assert_eq!(ForkId { hash: ForkHash(hex!("0x268956b6")), next: 0 }, SEPOLIA.latest_fork_id())
2742 }
2743
2744 #[test]
2745 fn test_fork_order_ethereum_mainnet() {
2746 let genesis = Genesis {
2747 config: ChainConfig {
2748 chain_id: 0,
2749 homestead_block: Some(0),
2750 dao_fork_block: Some(0),
2751 dao_fork_support: false,
2752 eip150_block: Some(0),
2753 eip155_block: Some(0),
2754 eip158_block: Some(0),
2755 byzantium_block: Some(0),
2756 constantinople_block: Some(0),
2757 petersburg_block: Some(0),
2758 istanbul_block: Some(0),
2759 muir_glacier_block: Some(0),
2760 berlin_block: Some(0),
2761 london_block: Some(0),
2762 arrow_glacier_block: Some(0),
2763 gray_glacier_block: Some(0),
2764 merge_netsplit_block: Some(0),
2765 shanghai_time: Some(0),
2766 cancun_time: Some(0),
2767 terminal_total_difficulty: Some(U256::ZERO),
2768 ..Default::default()
2769 },
2770 ..Default::default()
2771 };
2772
2773 let chain_spec: ChainSpec = genesis.into();
2774
2775 let hardforks: Vec<_> = chain_spec.hardforks.forks_iter().map(|(h, _)| h).collect();
2776 let expected_hardforks = vec![
2777 EthereumHardfork::Frontier.boxed(),
2778 EthereumHardfork::Homestead.boxed(),
2779 EthereumHardfork::Dao.boxed(),
2780 EthereumHardfork::Tangerine.boxed(),
2781 EthereumHardfork::SpuriousDragon.boxed(),
2782 EthereumHardfork::Byzantium.boxed(),
2783 EthereumHardfork::Constantinople.boxed(),
2784 EthereumHardfork::Petersburg.boxed(),
2785 EthereumHardfork::Istanbul.boxed(),
2786 EthereumHardfork::MuirGlacier.boxed(),
2787 EthereumHardfork::Berlin.boxed(),
2788 EthereumHardfork::London.boxed(),
2789 EthereumHardfork::ArrowGlacier.boxed(),
2790 EthereumHardfork::GrayGlacier.boxed(),
2791 EthereumHardfork::Paris.boxed(),
2792 EthereumHardfork::Shanghai.boxed(),
2793 EthereumHardfork::Cancun.boxed(),
2794 ];
2795
2796 assert!(expected_hardforks
2797 .iter()
2798 .zip(hardforks.iter())
2799 .all(|(expected, actual)| &**expected == *actual));
2800 assert_eq!(expected_hardforks.len(), hardforks.len());
2801 }
2802
2803 #[test]
2804 fn test_calc_base_block_reward() {
2805 let cases = [
2807 ((0, U256::ZERO), Some(ETH_TO_WEI * 5)),
2809 ((4370000, U256::ZERO), Some(ETH_TO_WEI * 3)),
2811 ((7280000, U256::ZERO), Some(ETH_TO_WEI * 2)),
2813 ((15537394, U256::from(58_750_000_000_000_000_000_000_u128)), None),
2815 ];
2816
2817 for ((block_number, _td), expected_reward) in cases {
2818 assert_eq!(base_block_reward(&*MAINNET, block_number), expected_reward);
2819 }
2820 }
2821
2822 #[test]
2823 fn test_calc_full_block_reward() {
2824 let base_reward = ETH_TO_WEI;
2825 let one_thirty_twoth_reward = base_reward >> 5;
2826
2827 let cases = [
2829 (0, base_reward),
2830 (1, base_reward + one_thirty_twoth_reward),
2831 (2, base_reward + one_thirty_twoth_reward * 2),
2832 ];
2833
2834 for (num_ommers, expected_reward) in cases {
2835 assert_eq!(block_reward(base_reward, num_ommers), expected_reward);
2836 }
2837 }
2838
2839 #[test]
2840 fn blob_params_from_genesis() {
2841 let s = r#"{
2842 "blobSchedule": {
2843 "cancun":{
2844 "baseFeeUpdateFraction":3338477,
2845 "max":6,
2846 "target":3
2847 },
2848 "prague":{
2849 "baseFeeUpdateFraction":3338477,
2850 "max":6,
2851 "target":3
2852 }
2853 }
2854 }"#;
2855 let config: ChainConfig = serde_json::from_str(s).unwrap();
2856 let hardfork_params = config.blob_schedule_blob_params();
2857 let expected = BlobScheduleBlobParams {
2858 cancun: BlobParams {
2859 target_blob_count: 3,
2860 max_blob_count: 6,
2861 update_fraction: 3338477,
2862 min_blob_fee: BLOB_TX_MIN_BLOB_GASPRICE,
2863 max_blobs_per_tx: 6,
2864 blob_base_cost: 0,
2865 },
2866 prague: BlobParams {
2867 target_blob_count: 3,
2868 max_blob_count: 6,
2869 update_fraction: 3338477,
2870 min_blob_fee: BLOB_TX_MIN_BLOB_GASPRICE,
2871 max_blobs_per_tx: 6,
2872 blob_base_cost: 0,
2873 },
2874 ..Default::default()
2875 };
2876 assert_eq!(hardfork_params, expected);
2877 }
2878
2879 #[test]
2880 fn parse_perf_net_genesis() {
2881 let s = r#"{
2882 "config": {
2883 "chainId": 1,
2884 "homesteadBlock": 1150000,
2885 "daoForkBlock": 1920000,
2886 "daoForkSupport": true,
2887 "eip150Block": 2463000,
2888 "eip150Hash": "0x2086799aeebeae135c246c65021c82b4e15a2c451340993aacfd2751886514f0",
2889 "eip155Block": 2675000,
2890 "eip158Block": 2675000,
2891 "byzantiumBlock": 4370000,
2892 "constantinopleBlock": 7280000,
2893 "petersburgBlock": 7280000,
2894 "istanbulBlock": 9069000,
2895 "muirGlacierBlock": 9200000,
2896 "berlinBlock": 12244000,
2897 "londonBlock": 12965000,
2898 "arrowGlacierBlock": 13773000,
2899 "grayGlacierBlock": 15050000,
2900 "terminalTotalDifficulty": 58750000000000000000000,
2901 "terminalTotalDifficultyPassed": true,
2902 "shanghaiTime": 1681338455,
2903 "cancunTime": 1710338135,
2904 "pragueTime": 1746612311,
2905 "ethash": {},
2906 "depositContractAddress": "0x00000000219ab540356cBB839Cbe05303d7705Fa",
2907 "blobSchedule": {
2908 "cancun": {
2909 "target": 3,
2910 "max": 6,
2911 "baseFeeUpdateFraction": 3338477
2912 },
2913 "prague": {
2914 "target": 6,
2915 "max": 9,
2916 "baseFeeUpdateFraction": 5007716
2917 }
2918 }
2919 },
2920 "nonce": "0x42",
2921 "timestamp": "0x0",
2922 "extraData": "0x11bbe8db4e347b4e8c937c1c8370e4b5ed33adb3db69cbdb7a38e1e50b1b82fa",
2923 "gasLimit": "0x1388",
2924 "difficulty": "0x400000000",
2925 "mixHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
2926 "coinbase": "0x0000000000000000000000000000000000000000",
2927 "number": "0x0",
2928 "gasUsed": "0x0",
2929 "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
2930 "baseFeePerGas": null
2931}"#;
2932
2933 let genesis = serde_json::from_str::<Genesis>(s).unwrap();
2934 let chainspec = ChainSpec::from_genesis(genesis);
2935 let activation = chainspec.hardforks.fork(EthereumHardfork::Paris);
2936 assert_eq!(
2937 activation,
2938 ForkCondition::TTD {
2939 activation_block_number: MAINNET_PARIS_BLOCK,
2940 total_difficulty: MAINNET_PARIS_TTD,
2941 fork_block: None,
2942 }
2943 )
2944 }
2945}