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 EthChainSpec,
7};
8use alloc::{boxed::Box, sync::Arc, vec::Vec};
9use alloy_chains::{Chain, NamedChain};
10use alloy_consensus::{
11 constants::{
12 DEV_GENESIS_HASH, EMPTY_WITHDRAWALS, HOLESKY_GENESIS_HASH, HOODI_GENESIS_HASH,
13 MAINNET_GENESIS_HASH, SEPOLIA_GENESIS_HASH,
14 },
15 Header,
16};
17use alloy_eips::{
18 eip1559::INITIAL_BASE_FEE, eip7685::EMPTY_REQUESTS_HASH, eip7892::BlobScheduleBlobParams,
19};
20use alloy_genesis::Genesis;
21use alloy_primitives::{address, b256, Address, BlockNumber, B256, U256};
22use alloy_seismic_evm::hardfork::{SeismicHardfork, SeismicHardforks};
23use alloy_trie::root::state_root_ref_unhashed;
24use core::fmt::Debug;
25use derive_more::From;
26use reth_ethereum_forks::{
27 ChainHardforks, DisplayHardforks, EthereumHardfork, EthereumHardforks, ForkCondition,
28 ForkFilter, ForkFilterKey, ForkHash, ForkId, Hardfork, Hardforks, Head, DEV_HARDFORKS,
29};
30use reth_network_peers::{
31 holesky_nodes, hoodi_nodes, mainnet_nodes, op_nodes, op_testnet_nodes, sepolia_nodes,
32 NodeRecord,
33};
34use reth_primitives_traits::{sync::LazyLock, SealedHeader};
35
36pub fn make_genesis_header(genesis: &Genesis, hardforks: &ChainHardforks) -> Header {
38 let base_fee_per_gas = hardforks
40 .fork(EthereumHardfork::London)
41 .active_at_block(0)
42 .then(|| genesis.base_fee_per_gas.map(|fee| fee as u64).unwrap_or(INITIAL_BASE_FEE));
43
44 let withdrawals_root = hardforks
47 .fork(EthereumHardfork::Shanghai)
48 .active_at_timestamp(genesis.timestamp)
49 .then_some(EMPTY_WITHDRAWALS);
50
51 let (parent_beacon_block_root, blob_gas_used, excess_blob_gas) =
56 if hardforks.fork(EthereumHardfork::Cancun).active_at_timestamp(genesis.timestamp) {
57 let blob_gas_used = genesis.blob_gas_used.unwrap_or(0);
58 let excess_blob_gas = genesis.excess_blob_gas.unwrap_or(0);
59 (Some(B256::ZERO), Some(blob_gas_used), Some(excess_blob_gas))
60 } else {
61 (None, None, None)
62 };
63
64 let requests_hash = hardforks
66 .fork(EthereumHardfork::Prague)
67 .active_at_timestamp(genesis.timestamp)
68 .then_some(EMPTY_REQUESTS_HASH);
69
70 Header {
71 gas_limit: genesis.gas_limit,
72 difficulty: genesis.difficulty,
73 nonce: genesis.nonce.into(),
74 extra_data: genesis.extra_data.clone(),
75 state_root: state_root_ref_unhashed(&genesis.alloc),
76 timestamp: genesis.timestamp,
77 mix_hash: genesis.mix_hash,
78 beneficiary: genesis.coinbase,
79 base_fee_per_gas,
80 withdrawals_root,
81 parent_beacon_block_root,
82 blob_gas_used,
83 excess_blob_gas,
84 requests_hash,
85 ..Default::default()
86 }
87}
88
89pub static MAINNET: LazyLock<Arc<ChainSpec>> = LazyLock::new(|| {
91 let genesis = serde_json::from_str(include_str!("../res/genesis/mainnet.json"))
92 .expect("Can't deserialize Mainnet genesis json");
93 let hardforks = EthereumHardfork::mainnet().into();
94 let mut spec = ChainSpec {
95 chain: Chain::mainnet(),
96 genesis_header: SealedHeader::new(
97 make_genesis_header(&genesis, &hardforks),
98 MAINNET_GENESIS_HASH,
99 ),
100 genesis,
101 paris_block_and_final_difficulty: Some((
103 15537394,
104 U256::from(58_750_003_716_598_352_816_469u128),
105 )),
106 hardforks,
107 deposit_contract: Some(MAINNET_DEPOSIT_CONTRACT),
109 base_fee_params: BaseFeeParamsKind::Constant(BaseFeeParams::ethereum()),
110 prune_delete_limit: MAINNET_PRUNE_DELETE_LIMIT,
111 blob_params: BlobScheduleBlobParams::default(),
112 };
113 spec.genesis.config.dao_fork_support = true;
114 spec.into()
115});
116
117pub static SEPOLIA: LazyLock<Arc<ChainSpec>> = LazyLock::new(|| {
119 let genesis = serde_json::from_str(include_str!("../res/genesis/sepolia.json"))
120 .expect("Can't deserialize Sepolia genesis json");
121 let hardforks = EthereumHardfork::sepolia().into();
122 let mut spec = ChainSpec {
123 chain: Chain::sepolia(),
124 genesis_header: SealedHeader::new(
125 make_genesis_header(&genesis, &hardforks),
126 SEPOLIA_GENESIS_HASH,
127 ),
128 genesis,
129 paris_block_and_final_difficulty: Some((1450409, U256::from(17_000_018_015_853_232u128))),
131 hardforks,
132 deposit_contract: Some(DepositContract::new(
134 address!("0x7f02c3e3c98b133055b8b348b2ac625669ed295d"),
135 1273020,
136 b256!("0x649bbc62d0e31342afea4e5cd82d4049e7e1ee912fc0889aa790803be39038c5"),
137 )),
138 base_fee_params: BaseFeeParamsKind::Constant(BaseFeeParams::ethereum()),
139 prune_delete_limit: 10000,
140 blob_params: BlobScheduleBlobParams::default(),
141 };
142 spec.genesis.config.dao_fork_support = true;
143 spec.into()
144});
145
146pub static HOLESKY: LazyLock<Arc<ChainSpec>> = LazyLock::new(|| {
148 let genesis = serde_json::from_str(include_str!("../res/genesis/holesky.json"))
149 .expect("Can't deserialize Holesky genesis json");
150 let hardforks = EthereumHardfork::holesky().into();
151 let mut spec = ChainSpec {
152 chain: Chain::holesky(),
153 genesis_header: SealedHeader::new(
154 make_genesis_header(&genesis, &hardforks),
155 HOLESKY_GENESIS_HASH,
156 ),
157 genesis,
158 paris_block_and_final_difficulty: Some((0, U256::from(1))),
159 hardforks,
160 deposit_contract: Some(DepositContract::new(
161 address!("0x4242424242424242424242424242424242424242"),
162 0,
163 b256!("0x649bbc62d0e31342afea4e5cd82d4049e7e1ee912fc0889aa790803be39038c5"),
164 )),
165 base_fee_params: BaseFeeParamsKind::Constant(BaseFeeParams::ethereum()),
166 prune_delete_limit: 10000,
167 blob_params: BlobScheduleBlobParams::default(),
168 };
169 spec.genesis.config.dao_fork_support = true;
170 spec.into()
171});
172
173pub static HOODI: LazyLock<Arc<ChainSpec>> = LazyLock::new(|| {
177 let genesis = serde_json::from_str(include_str!("../res/genesis/hoodi.json"))
178 .expect("Can't deserialize Hoodi genesis json");
179 let hardforks = EthereumHardfork::hoodi().into();
180 let mut spec = ChainSpec {
181 chain: Chain::hoodi(),
182 genesis_header: SealedHeader::new(
183 make_genesis_header(&genesis, &hardforks),
184 HOODI_GENESIS_HASH,
185 ),
186 genesis,
187 paris_block_and_final_difficulty: Some((0, U256::from(0))),
188 hardforks,
189 deposit_contract: Some(DepositContract::new(
190 address!("0x00000000219ab540356cBB839Cbe05303d7705Fa"),
191 0,
192 b256!("0x649bbc62d0e31342afea4e5cd82d4049e7e1ee912fc0889aa790803be39038c5"),
193 )),
194 base_fee_params: BaseFeeParamsKind::Constant(BaseFeeParams::ethereum()),
195 prune_delete_limit: 10000,
196 blob_params: BlobScheduleBlobParams::default(),
197 };
198 spec.genesis.config.dao_fork_support = true;
199 spec.into()
200});
201
202pub static DEV: LazyLock<Arc<ChainSpec>> = LazyLock::new(|| {
207 let genesis = serde_json::from_str(include_str!("../res/genesis/dev.json"))
208 .expect("Can't deserialize Dev testnet genesis json");
209 let hardforks = DEV_HARDFORKS.clone();
210 ChainSpec {
211 chain: Chain::dev(),
212 genesis_header: SealedHeader::new(
213 make_genesis_header(&genesis, &hardforks),
214 DEV_GENESIS_HASH,
215 ),
216 genesis,
217 paris_block_and_final_difficulty: Some((0, U256::from(0))),
218 hardforks: DEV_HARDFORKS.clone(),
219 base_fee_params: BaseFeeParamsKind::Constant(BaseFeeParams::ethereum()),
220 deposit_contract: None, ..Default::default()
222 }
223 .into()
224});
225
226#[derive(Clone, Debug, PartialEq, Eq)]
229pub enum BaseFeeParamsKind {
230 Constant(BaseFeeParams),
232 Variable(ForkBaseFeeParams),
235}
236
237impl Default for BaseFeeParamsKind {
238 fn default() -> Self {
239 BaseFeeParams::ethereum().into()
240 }
241}
242
243impl From<BaseFeeParams> for BaseFeeParamsKind {
244 fn from(params: BaseFeeParams) -> Self {
245 Self::Constant(params)
246 }
247}
248
249impl From<ForkBaseFeeParams> for BaseFeeParamsKind {
250 fn from(params: ForkBaseFeeParams) -> Self {
251 Self::Variable(params)
252 }
253}
254
255#[derive(Clone, Debug, PartialEq, Eq, From)]
258pub struct ForkBaseFeeParams(Vec<(Box<dyn Hardfork>, BaseFeeParams)>);
259
260impl core::ops::Deref for ChainSpec {
261 type Target = ChainHardforks;
262
263 fn deref(&self) -> &Self::Target {
264 &self.hardforks
265 }
266}
267
268#[derive(Debug, Clone, PartialEq, Eq)]
276pub struct ChainSpec {
277 pub chain: Chain,
279
280 pub genesis: Genesis,
282
283 pub genesis_header: SealedHeader,
285
286 pub paris_block_and_final_difficulty: Option<(u64, U256)>,
289
290 pub hardforks: ChainHardforks,
292
293 pub deposit_contract: Option<DepositContract>,
295
296 pub base_fee_params: BaseFeeParamsKind,
298
299 pub prune_delete_limit: usize,
301
302 pub blob_params: BlobScheduleBlobParams,
304}
305
306impl Default for ChainSpec {
307 fn default() -> Self {
308 Self {
309 chain: Default::default(),
310 genesis: Default::default(),
311 genesis_header: Default::default(),
312 paris_block_and_final_difficulty: Default::default(),
313 hardforks: Default::default(),
314 deposit_contract: Default::default(),
315 base_fee_params: BaseFeeParamsKind::Constant(BaseFeeParams::ethereum()),
316 prune_delete_limit: MAINNET_PRUNE_DELETE_LIMIT,
317 blob_params: Default::default(),
318 }
319 }
320}
321
322impl ChainSpec {
323 pub fn from_genesis(genesis: Genesis) -> Self {
325 genesis.into()
326 }
327
328 pub const fn chain(&self) -> Chain {
330 self.chain
331 }
332
333 #[inline]
335 pub const fn is_ethereum(&self) -> bool {
336 self.chain.is_ethereum()
337 }
338
339 #[inline]
341 pub fn is_optimism_mainnet(&self) -> bool {
342 self.chain == Chain::optimism_mainnet()
343 }
344
345 #[inline]
347 pub fn paris_block(&self) -> Option<u64> {
348 self.paris_block_and_final_difficulty.map(|(block, _)| block)
349 }
350
351 pub const fn genesis(&self) -> &Genesis {
355 &self.genesis
356 }
357
358 pub fn genesis_header(&self) -> &Header {
360 &self.genesis_header
361 }
362
363 pub fn sealed_genesis_header(&self) -> SealedHeader {
365 SealedHeader::new(self.genesis_header().clone(), self.genesis_hash())
366 }
367
368 pub fn initial_base_fee(&self) -> Option<u64> {
370 let genesis_base_fee =
372 self.genesis.base_fee_per_gas.map(|fee| fee as u64).unwrap_or(INITIAL_BASE_FEE);
373
374 self.hardforks.fork(EthereumHardfork::London).active_at_block(0).then_some(genesis_base_fee)
376 }
377
378 pub fn base_fee_params_at_timestamp(&self, timestamp: u64) -> BaseFeeParams {
380 match self.base_fee_params {
381 BaseFeeParamsKind::Constant(bf_params) => bf_params,
382 BaseFeeParamsKind::Variable(ForkBaseFeeParams(ref bf_params)) => {
383 for (fork, params) in bf_params.iter().rev() {
387 if self.hardforks.is_fork_active_at_timestamp(fork.clone(), timestamp) {
388 return *params
389 }
390 }
391
392 bf_params.first().map(|(_, params)| *params).unwrap_or(BaseFeeParams::ethereum())
393 }
394 }
395 }
396
397 pub fn base_fee_params_at_block(&self, block_number: u64) -> BaseFeeParams {
399 match self.base_fee_params {
400 BaseFeeParamsKind::Constant(bf_params) => bf_params,
401 BaseFeeParamsKind::Variable(ForkBaseFeeParams(ref bf_params)) => {
402 for (fork, params) in bf_params.iter().rev() {
406 if self.hardforks.is_fork_active_at_block(fork.clone(), block_number) {
407 return *params
408 }
409 }
410
411 bf_params.first().map(|(_, params)| *params).unwrap_or(BaseFeeParams::ethereum())
412 }
413 }
414 }
415
416 pub fn genesis_hash(&self) -> B256 {
418 self.genesis_header.hash()
419 }
420
421 pub const fn genesis_timestamp(&self) -> u64 {
423 self.genesis.timestamp
424 }
425
426 pub fn get_final_paris_total_difficulty(&self) -> Option<U256> {
428 self.paris_block_and_final_difficulty.map(|(_, final_difficulty)| final_difficulty)
429 }
430
431 pub fn hardfork_fork_filter<H: Hardfork + Clone>(&self, fork: H) -> Option<ForkFilter> {
433 match self.hardforks.fork(fork.clone()) {
434 ForkCondition::Never => None,
435 _ => Some(self.fork_filter(self.satisfy(self.hardforks.fork(fork)))),
436 }
437 }
438
439 pub fn display_hardforks(&self) -> DisplayHardforks {
441 DisplayHardforks::new(self.hardforks.forks_iter())
442 }
443
444 #[inline]
446 pub fn hardfork_fork_id<H: Hardfork + Clone>(&self, fork: H) -> Option<ForkId> {
447 let condition = self.hardforks.fork(fork);
448 match condition {
449 ForkCondition::Never => None,
450 _ => Some(self.fork_id(&self.satisfy(condition))),
451 }
452 }
453
454 #[inline]
457 pub fn shanghai_fork_id(&self) -> Option<ForkId> {
458 self.hardfork_fork_id(EthereumHardfork::Shanghai)
459 }
460
461 #[inline]
464 pub fn cancun_fork_id(&self) -> Option<ForkId> {
465 self.hardfork_fork_id(EthereumHardfork::Cancun)
466 }
467
468 #[inline]
471 pub fn latest_fork_id(&self) -> ForkId {
472 self.hardfork_fork_id(self.hardforks.last().unwrap().0).unwrap()
473 }
474
475 pub fn fork_filter(&self, head: Head) -> ForkFilter {
477 let forks = self.hardforks.forks_iter().filter_map(|(_, condition)| {
478 Some(match condition {
481 ForkCondition::Block(block) |
482 ForkCondition::TTD { fork_block: Some(block), .. } => ForkFilterKey::Block(block),
483 ForkCondition::Timestamp(time) => ForkFilterKey::Time(time),
484 _ => return None,
485 })
486 });
487
488 ForkFilter::new(head, self.genesis_hash(), self.genesis_timestamp(), forks)
489 }
490
491 pub fn fork_id(&self, head: &Head) -> ForkId {
496 let mut forkhash = ForkHash::from(self.genesis_hash());
497
498 let mut current_applied = 0;
504
505 for (_, cond) in self.hardforks.forks_iter() {
507 if let ForkCondition::Block(block) |
510 ForkCondition::TTD { fork_block: Some(block), .. } = cond
511 {
512 if head.number >= block {
513 if block != current_applied {
515 forkhash += block;
516 current_applied = block;
517 }
518 } else {
519 return ForkId { hash: forkhash, next: block }
522 }
523 }
524 }
525
526 for timestamp in self.hardforks.forks_iter().filter_map(|(_, cond)| {
530 cond.as_timestamp().filter(|time| time > &self.genesis.timestamp)
532 }) {
533 if head.timestamp >= timestamp {
534 if timestamp != current_applied {
536 forkhash += timestamp;
537 current_applied = timestamp;
538 }
539 } else {
540 return ForkId { hash: forkhash, next: timestamp }
544 }
545 }
546
547 ForkId { hash: forkhash, next: 0 }
548 }
549
550 pub(crate) fn satisfy(&self, cond: ForkCondition) -> Head {
552 match cond {
553 ForkCondition::Block(number) => Head { number, ..Default::default() },
554 ForkCondition::Timestamp(timestamp) => {
555 Head {
558 timestamp,
559 number: self.last_block_fork_before_merge_or_timestamp().unwrap_or_default(),
560 ..Default::default()
561 }
562 }
563 ForkCondition::TTD { total_difficulty, fork_block, .. } => Head {
564 total_difficulty,
565 number: fork_block.unwrap_or_default(),
566 ..Default::default()
567 },
568 ForkCondition::Never => unreachable!(),
569 }
570 }
571
572 pub(crate) fn last_block_fork_before_merge_or_timestamp(&self) -> Option<u64> {
584 let mut hardforks_iter = self.hardforks.forks_iter().peekable();
585 while let Some((_, curr_cond)) = hardforks_iter.next() {
586 if let Some((_, next_cond)) = hardforks_iter.peek() {
587 match next_cond {
591 ForkCondition::TTD { fork_block: Some(block), .. } => return Some(*block),
594
595 ForkCondition::TTD { .. } | ForkCondition::Timestamp(_) => {
598 if let ForkCondition::Block(block_num) = curr_cond {
601 return Some(block_num);
602 }
603 }
604 ForkCondition::Block(_) | ForkCondition::Never => {}
605 }
606 }
607 }
608 None
609 }
610
611 pub fn builder() -> ChainSpecBuilder {
613 ChainSpecBuilder::default()
614 }
615
616 pub fn bootnodes(&self) -> Option<Vec<NodeRecord>> {
618 use NamedChain as C;
619
620 match self.chain.try_into().ok()? {
621 C::Mainnet => Some(mainnet_nodes()),
622 C::Sepolia => Some(sepolia_nodes()),
623 C::Holesky => Some(holesky_nodes()),
624 C::Hoodi => Some(hoodi_nodes()),
625 C::Base | C::Optimism | C::Unichain | C::World => Some(op_nodes()),
627 C::OptimismSepolia | C::BaseSepolia | C::UnichainSepolia | C::WorldSepolia => {
628 Some(op_testnet_nodes())
629 }
630
631 chain if chain.is_optimism() && chain.is_testnet() => Some(op_testnet_nodes()),
633 chain if chain.is_optimism() => Some(op_nodes()),
634 _ => None,
635 }
636 }
637}
638
639impl From<Genesis> for ChainSpec {
640 fn from(genesis: Genesis) -> Self {
641 let hardfork_opts = [
643 (EthereumHardfork::Frontier.boxed(), Some(0)),
644 (EthereumHardfork::Homestead.boxed(), genesis.config.homestead_block),
645 (EthereumHardfork::Dao.boxed(), genesis.config.dao_fork_block),
646 (EthereumHardfork::Tangerine.boxed(), genesis.config.eip150_block),
647 (EthereumHardfork::SpuriousDragon.boxed(), genesis.config.eip155_block),
648 (EthereumHardfork::Byzantium.boxed(), genesis.config.byzantium_block),
649 (EthereumHardfork::Constantinople.boxed(), genesis.config.constantinople_block),
650 (EthereumHardfork::Petersburg.boxed(), genesis.config.petersburg_block),
651 (EthereumHardfork::Istanbul.boxed(), genesis.config.istanbul_block),
652 (EthereumHardfork::MuirGlacier.boxed(), genesis.config.muir_glacier_block),
653 (EthereumHardfork::Berlin.boxed(), genesis.config.berlin_block),
654 (EthereumHardfork::London.boxed(), genesis.config.london_block),
655 (EthereumHardfork::ArrowGlacier.boxed(), genesis.config.arrow_glacier_block),
656 (EthereumHardfork::GrayGlacier.boxed(), genesis.config.gray_glacier_block),
657 ];
658 let mut hardforks = hardfork_opts
659 .into_iter()
660 .filter_map(|(hardfork, opt)| opt.map(|block| (hardfork, ForkCondition::Block(block))))
661 .collect::<Vec<_>>();
662
663 let paris_block_and_final_difficulty =
667 if let Some(ttd) = genesis.config.terminal_total_difficulty {
668 hardforks.push((
669 EthereumHardfork::Paris.boxed(),
670 ForkCondition::TTD {
671 activation_block_number: genesis
674 .config
675 .merge_netsplit_block
676 .unwrap_or_default(),
677 total_difficulty: ttd,
678 fork_block: genesis.config.merge_netsplit_block,
679 },
680 ));
681
682 genesis.config.merge_netsplit_block.map(|block| (block, ttd))
683 } else {
684 None
685 };
686
687 let time_hardfork_opts = [
689 (EthereumHardfork::Shanghai.boxed(), genesis.config.shanghai_time),
690 (EthereumHardfork::Cancun.boxed(), genesis.config.cancun_time),
691 (EthereumHardfork::Prague.boxed(), genesis.config.prague_time),
692 (EthereumHardfork::Osaka.boxed(), genesis.config.osaka_time),
693 ];
694
695 let mut time_hardforks = time_hardfork_opts
696 .into_iter()
697 .filter_map(|(hardfork, opt)| {
698 opt.map(|time| (hardfork, ForkCondition::Timestamp(time)))
699 })
700 .collect::<Vec<_>>();
701
702 hardforks.append(&mut time_hardforks);
703
704 let mainnet_hardforks: ChainHardforks = EthereumHardfork::mainnet().into();
706 let mainnet_order = mainnet_hardforks.forks_iter();
707
708 let mut ordered_hardforks = Vec::with_capacity(hardforks.len());
709 for (hardfork, _) in mainnet_order {
710 if let Some(pos) = hardforks.iter().position(|(e, _)| **e == *hardfork) {
711 ordered_hardforks.push(hardforks.remove(pos));
712 }
713 }
714
715 ordered_hardforks.append(&mut hardforks);
717
718 let blob_params = genesis.config.blob_schedule_blob_params();
720
721 let deposit_contract = genesis.config.deposit_contract_address.map(|address| {
726 DepositContract { address, block: 0, topic: MAINNET_DEPOSIT_CONTRACT.topic }
727 });
728
729 let hardforks = ChainHardforks::new(ordered_hardforks);
730
731 Self {
732 chain: genesis.config.chain_id.into(),
733 genesis_header: SealedHeader::new_unhashed(make_genesis_header(&genesis, &hardforks)),
734 genesis,
735 hardforks,
736 paris_block_and_final_difficulty,
737 deposit_contract,
738 blob_params,
739 ..Default::default()
740 }
741 }
742}
743
744impl Hardforks for ChainSpec {
745 fn fork<H: Hardfork>(&self, fork: H) -> ForkCondition {
746 self.hardforks.fork(fork)
747 }
748
749 fn forks_iter(&self) -> impl Iterator<Item = (&dyn Hardfork, ForkCondition)> {
750 self.hardforks.forks_iter()
751 }
752
753 fn fork_id(&self, head: &Head) -> ForkId {
754 self.fork_id(head)
755 }
756
757 fn latest_fork_id(&self) -> ForkId {
758 self.latest_fork_id()
759 }
760
761 fn fork_filter(&self, head: Head) -> ForkFilter {
762 self.fork_filter(head)
763 }
764}
765
766impl EthereumHardforks for ChainSpec {
767 fn ethereum_fork_activation(&self, fork: EthereumHardfork) -> ForkCondition {
768 self.fork(fork)
769 }
770}
771
772impl SeismicHardforks for ChainSpec {
773 fn seismic_fork_activation(&self, fork: SeismicHardfork) -> ForkCondition {
774 self.fork(fork)
775 }
776}
777
778#[auto_impl::auto_impl(&, Arc)]
780pub trait ChainSpecProvider: Debug + Send + Sync {
781 type ChainSpec: EthChainSpec + 'static;
783
784 fn chain_spec(&self) -> Arc<Self::ChainSpec>;
786}
787
788#[derive(Debug, Default, Clone)]
790pub struct ChainSpecBuilder {
791 chain: Option<Chain>,
792 genesis: Option<Genesis>,
793 hardforks: ChainHardforks,
794}
795
796impl ChainSpecBuilder {
797 pub fn mainnet() -> Self {
799 Self {
800 chain: Some(MAINNET.chain),
801 genesis: Some(MAINNET.genesis.clone()),
802 hardforks: MAINNET.hardforks.clone(),
803 }
804 }
805}
806
807impl ChainSpecBuilder {
808 pub const fn chain(mut self, chain: Chain) -> Self {
810 self.chain = Some(chain);
811 self
812 }
813
814 pub fn genesis(mut self, genesis: Genesis) -> Self {
816 self.genesis = Some(genesis);
817 self
818 }
819
820 pub fn with_fork<H: Hardfork>(mut self, fork: H, condition: ForkCondition) -> Self {
822 self.hardforks.insert(fork, condition);
823 self
824 }
825
826 pub fn with_forks(mut self, forks: ChainHardforks) -> Self {
828 self.hardforks = forks;
829 self
830 }
831
832 pub fn without_fork<H: Hardfork>(mut self, fork: H) -> Self {
834 self.hardforks.remove(fork);
835 self
836 }
837
838 pub fn paris_at_ttd(self, ttd: U256, activation_block_number: BlockNumber) -> Self {
842 self.with_fork(
843 EthereumHardfork::Paris,
844 ForkCondition::TTD { activation_block_number, total_difficulty: ttd, fork_block: None },
845 )
846 }
847
848 pub fn frontier_activated(mut self) -> Self {
850 self.hardforks.insert(EthereumHardfork::Frontier, ForkCondition::Block(0));
851 self
852 }
853
854 pub fn homestead_activated(mut self) -> Self {
856 self = self.frontier_activated();
857 self.hardforks.insert(EthereumHardfork::Homestead, ForkCondition::Block(0));
858 self
859 }
860
861 pub fn tangerine_whistle_activated(mut self) -> Self {
863 self = self.homestead_activated();
864 self.hardforks.insert(EthereumHardfork::Tangerine, ForkCondition::Block(0));
865 self
866 }
867
868 pub fn spurious_dragon_activated(mut self) -> Self {
870 self = self.tangerine_whistle_activated();
871 self.hardforks.insert(EthereumHardfork::SpuriousDragon, ForkCondition::Block(0));
872 self
873 }
874
875 pub fn byzantium_activated(mut self) -> Self {
877 self = self.spurious_dragon_activated();
878 self.hardforks.insert(EthereumHardfork::Byzantium, ForkCondition::Block(0));
879 self
880 }
881
882 pub fn constantinople_activated(mut self) -> Self {
884 self = self.byzantium_activated();
885 self.hardforks.insert(EthereumHardfork::Constantinople, ForkCondition::Block(0));
886 self
887 }
888
889 pub fn petersburg_activated(mut self) -> Self {
891 self = self.constantinople_activated();
892 self.hardforks.insert(EthereumHardfork::Petersburg, ForkCondition::Block(0));
893 self
894 }
895
896 pub fn istanbul_activated(mut self) -> Self {
898 self = self.petersburg_activated();
899 self.hardforks.insert(EthereumHardfork::Istanbul, ForkCondition::Block(0));
900 self
901 }
902
903 pub fn berlin_activated(mut self) -> Self {
905 self = self.istanbul_activated();
906 self.hardforks.insert(EthereumHardfork::Berlin, ForkCondition::Block(0));
907 self
908 }
909
910 pub fn london_activated(mut self) -> Self {
912 self = self.berlin_activated();
913 self.hardforks.insert(EthereumHardfork::London, ForkCondition::Block(0));
914 self
915 }
916
917 pub fn paris_activated(mut self) -> Self {
919 self = self.london_activated();
920 self.hardforks.insert(
921 EthereumHardfork::Paris,
922 ForkCondition::TTD {
923 activation_block_number: 0,
924 total_difficulty: U256::ZERO,
925 fork_block: None,
926 },
927 );
928 self
929 }
930
931 pub fn shanghai_activated(mut self) -> Self {
933 self = self.paris_activated();
934 self.hardforks.insert(EthereumHardfork::Shanghai, ForkCondition::Timestamp(0));
935 self
936 }
937
938 pub fn cancun_activated(mut self) -> Self {
940 self = self.shanghai_activated();
941 self.hardforks.insert(EthereumHardfork::Cancun, ForkCondition::Timestamp(0));
942 self
943 }
944
945 pub fn prague_activated(mut self) -> Self {
947 self = self.cancun_activated();
948 self.hardforks.insert(EthereumHardfork::Prague, ForkCondition::Timestamp(0));
949 self
950 }
951
952 pub fn osaka_activated(mut self) -> Self {
954 self = self.prague_activated();
955 self.hardforks.insert(EthereumHardfork::Osaka, ForkCondition::Timestamp(0));
956 self
957 }
958
959 pub fn build(self) -> ChainSpec {
966 let paris_block_and_final_difficulty = {
967 self.hardforks.get(EthereumHardfork::Paris).and_then(|cond| {
968 if let ForkCondition::TTD { total_difficulty, activation_block_number, .. } = cond {
969 Some((activation_block_number, total_difficulty))
970 } else {
971 None
972 }
973 })
974 };
975 let genesis = self.genesis.expect("The genesis is required");
976 ChainSpec {
977 chain: self.chain.expect("The chain is required"),
978 genesis_header: SealedHeader::new_unhashed(make_genesis_header(
979 &genesis,
980 &self.hardforks,
981 )),
982 genesis,
983 hardforks: self.hardforks,
984 paris_block_and_final_difficulty,
985 deposit_contract: None,
986 ..Default::default()
987 }
988 }
989}
990
991impl From<&Arc<ChainSpec>> for ChainSpecBuilder {
992 fn from(value: &Arc<ChainSpec>) -> Self {
993 Self {
994 chain: Some(value.chain),
995 genesis: Some(value.genesis.clone()),
996 hardforks: value.hardforks.clone(),
997 }
998 }
999}
1000
1001impl EthExecutorSpec for ChainSpec {
1002 fn deposit_contract_address(&self) -> Option<Address> {
1003 self.deposit_contract.map(|deposit_contract| deposit_contract.address)
1004 }
1005}
1006
1007#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1009pub struct DepositContract {
1010 pub address: Address,
1012 pub block: BlockNumber,
1014 pub topic: B256,
1016}
1017
1018impl DepositContract {
1019 pub const fn new(address: Address, block: BlockNumber, topic: B256) -> Self {
1021 Self { address, block, topic }
1022 }
1023}
1024
1025#[cfg(any(test, feature = "test-utils"))]
1027pub fn test_fork_ids(spec: &ChainSpec, cases: &[(Head, ForkId)]) {
1028 for (block, expected_id) in cases {
1029 let computed_id = spec.fork_id(block);
1030 assert_eq!(
1031 expected_id, &computed_id,
1032 "Expected fork ID {:?}, computed fork ID {:?} at block {}",
1033 expected_id, computed_id, block.number
1034 );
1035 }
1036}
1037
1038#[cfg(test)]
1039mod tests {
1040 use super::*;
1041 use alloy_chains::Chain;
1042 use alloy_consensus::constants::ETH_TO_WEI;
1043 use alloy_eips::{eip4844::BLOB_TX_MIN_BLOB_GASPRICE, eip7840::BlobParams};
1044 use alloy_evm::block::calc::{base_block_reward, block_reward};
1045 use alloy_genesis::{ChainConfig, GenesisAccount};
1046 use alloy_primitives::{b256, hex};
1047 use alloy_trie::{TrieAccount, EMPTY_ROOT_HASH};
1048 use core::ops::Deref;
1049 use reth_ethereum_forks::{ForkCondition, ForkHash, ForkId, Head};
1050 use std::{
1051 collections::{BTreeMap, HashMap},
1052 str::FromStr,
1053 string::String,
1054 };
1055
1056 fn test_hardfork_fork_ids(spec: &ChainSpec, cases: &[(EthereumHardfork, ForkId)]) {
1057 for (hardfork, expected_id) in cases {
1058 if let Some(computed_id) = spec.hardfork_fork_id(*hardfork) {
1059 assert_eq!(
1060 expected_id, &computed_id,
1061 "Expected fork ID {expected_id:?}, computed fork ID {computed_id:?} for hardfork {hardfork}"
1062 );
1063 if matches!(hardfork, EthereumHardfork::Shanghai) {
1064 if let Some(shangai_id) = spec.shanghai_fork_id() {
1065 assert_eq!(
1066 expected_id, &shangai_id,
1067 "Expected fork ID {expected_id:?}, computed fork ID {computed_id:?} for Shanghai hardfork"
1068 );
1069 } else {
1070 panic!("Expected ForkCondition to return Some for Hardfork::Shanghai");
1071 }
1072 }
1073 }
1074 }
1075 }
1076
1077 #[test]
1078 fn test_hardfork_list_display_mainnet() {
1079 assert_eq!(
1080 MAINNET.display_hardforks().to_string(),
1081 "Pre-merge hard forks (block based):
1082- Frontier @0
1083- Homestead @1150000
1084- Dao @1920000
1085- Tangerine @2463000
1086- SpuriousDragon @2675000
1087- Byzantium @4370000
1088- Constantinople @7280000
1089- Petersburg @7280000
1090- Istanbul @9069000
1091- MuirGlacier @9200000
1092- Berlin @12244000
1093- London @12965000
1094- ArrowGlacier @13773000
1095- GrayGlacier @15050000
1096Merge hard forks:
1097- Paris @58750000000000000000000 (network is known to be merged)
1098Post-merge hard forks (timestamp based):
1099- Shanghai @1681338455
1100- Cancun @1710338135
1101- Prague @1746612311"
1102 );
1103 }
1104
1105 #[test]
1106 fn test_hardfork_list_ignores_disabled_forks() {
1107 let spec = ChainSpec::builder()
1108 .chain(Chain::mainnet())
1109 .genesis(Genesis::default())
1110 .with_fork(EthereumHardfork::Frontier, ForkCondition::Block(0))
1111 .with_fork(EthereumHardfork::Shanghai, ForkCondition::Never)
1112 .build();
1113 assert_eq!(
1114 spec.display_hardforks().to_string(),
1115 "Pre-merge hard forks (block based):
1116- Frontier @0"
1117 );
1118 }
1119
1120 #[test]
1122 fn ignores_genesis_fork_blocks() {
1123 let spec = ChainSpec::builder()
1124 .chain(Chain::mainnet())
1125 .genesis(Genesis::default())
1126 .with_fork(EthereumHardfork::Frontier, ForkCondition::Block(0))
1127 .with_fork(EthereumHardfork::Homestead, ForkCondition::Block(0))
1128 .with_fork(EthereumHardfork::Tangerine, ForkCondition::Block(0))
1129 .with_fork(EthereumHardfork::SpuriousDragon, ForkCondition::Block(0))
1130 .with_fork(EthereumHardfork::Byzantium, ForkCondition::Block(0))
1131 .with_fork(EthereumHardfork::Constantinople, ForkCondition::Block(0))
1132 .with_fork(EthereumHardfork::Istanbul, ForkCondition::Block(0))
1133 .with_fork(EthereumHardfork::MuirGlacier, ForkCondition::Block(0))
1134 .with_fork(EthereumHardfork::Berlin, ForkCondition::Block(0))
1135 .with_fork(EthereumHardfork::London, ForkCondition::Block(0))
1136 .with_fork(EthereumHardfork::ArrowGlacier, ForkCondition::Block(0))
1137 .with_fork(EthereumHardfork::GrayGlacier, ForkCondition::Block(0))
1138 .build();
1139
1140 assert_eq!(spec.deref().len(), 12, "12 forks should be active.");
1141 assert_eq!(
1142 spec.fork_id(&Head { number: 1, ..Default::default() }),
1143 ForkId { hash: ForkHash::from(spec.genesis_hash()), next: 0 },
1144 "the fork ID should be the genesis hash; forks at genesis are ignored for fork filters"
1145 );
1146 }
1147
1148 #[test]
1149 fn ignores_duplicate_fork_blocks() {
1150 let empty_genesis = Genesis::default();
1151 let unique_spec = ChainSpec::builder()
1152 .chain(Chain::mainnet())
1153 .genesis(empty_genesis.clone())
1154 .with_fork(EthereumHardfork::Frontier, ForkCondition::Block(0))
1155 .with_fork(EthereumHardfork::Homestead, ForkCondition::Block(1))
1156 .build();
1157
1158 let duplicate_spec = ChainSpec::builder()
1159 .chain(Chain::mainnet())
1160 .genesis(empty_genesis)
1161 .with_fork(EthereumHardfork::Frontier, ForkCondition::Block(0))
1162 .with_fork(EthereumHardfork::Homestead, ForkCondition::Block(1))
1163 .with_fork(EthereumHardfork::Tangerine, ForkCondition::Block(1))
1164 .build();
1165
1166 assert_eq!(
1167 unique_spec.fork_id(&Head { number: 2, ..Default::default() }),
1168 duplicate_spec.fork_id(&Head { number: 2, ..Default::default() }),
1169 "duplicate fork blocks should be deduplicated for fork filters"
1170 );
1171 }
1172
1173 #[test]
1174 fn test_chainspec_satisfy() {
1175 let empty_genesis = Genesis::default();
1176 let happy_path_case = ChainSpec::builder()
1178 .chain(Chain::mainnet())
1179 .genesis(empty_genesis.clone())
1180 .with_fork(EthereumHardfork::Frontier, ForkCondition::Block(0))
1181 .with_fork(EthereumHardfork::Homestead, ForkCondition::Block(73))
1182 .with_fork(EthereumHardfork::Shanghai, ForkCondition::Timestamp(11313123))
1183 .build();
1184 let happy_path_head = happy_path_case.satisfy(ForkCondition::Timestamp(11313123));
1185 let happy_path_expected = Head { number: 73, timestamp: 11313123, ..Default::default() };
1186 assert_eq!(
1187 happy_path_head, happy_path_expected,
1188 "expected satisfy() to return {happy_path_expected:#?}, but got {happy_path_head:#?} "
1189 );
1190 let multiple_timestamp_fork_case = ChainSpec::builder()
1192 .chain(Chain::mainnet())
1193 .genesis(empty_genesis.clone())
1194 .with_fork(EthereumHardfork::Frontier, ForkCondition::Block(0))
1195 .with_fork(EthereumHardfork::Homestead, ForkCondition::Block(73))
1196 .with_fork(EthereumHardfork::Shanghai, ForkCondition::Timestamp(11313123))
1197 .with_fork(EthereumHardfork::Cancun, ForkCondition::Timestamp(11313398))
1198 .build();
1199 let multi_timestamp_head =
1200 multiple_timestamp_fork_case.satisfy(ForkCondition::Timestamp(11313398));
1201 let mult_timestamp_expected =
1202 Head { number: 73, timestamp: 11313398, ..Default::default() };
1203 assert_eq!(
1204 multi_timestamp_head, mult_timestamp_expected,
1205 "expected satisfy() to return {mult_timestamp_expected:#?}, but got {multi_timestamp_head:#?} "
1206 );
1207 let no_block_fork_case = ChainSpec::builder()
1209 .chain(Chain::mainnet())
1210 .genesis(empty_genesis.clone())
1211 .with_fork(EthereumHardfork::Shanghai, ForkCondition::Timestamp(11313123))
1212 .build();
1213 let no_block_fork_head = no_block_fork_case.satisfy(ForkCondition::Timestamp(11313123));
1214 let no_block_fork_expected = Head { number: 0, timestamp: 11313123, ..Default::default() };
1215 assert_eq!(
1216 no_block_fork_head, no_block_fork_expected,
1217 "expected satisfy() to return {no_block_fork_expected:#?}, but got {no_block_fork_head:#?} ",
1218 );
1219 let fork_cond_ttd_blocknum_case = ChainSpec::builder()
1221 .chain(Chain::mainnet())
1222 .genesis(empty_genesis.clone())
1223 .with_fork(EthereumHardfork::Frontier, ForkCondition::Block(0))
1224 .with_fork(EthereumHardfork::Homestead, ForkCondition::Block(73))
1225 .with_fork(
1226 EthereumHardfork::Paris,
1227 ForkCondition::TTD {
1228 activation_block_number: 101,
1229 fork_block: Some(101),
1230 total_difficulty: U256::from(10_790_000),
1231 },
1232 )
1233 .with_fork(EthereumHardfork::Shanghai, ForkCondition::Timestamp(11313123))
1234 .build();
1235 let fork_cond_ttd_blocknum_head =
1236 fork_cond_ttd_blocknum_case.satisfy(ForkCondition::Timestamp(11313123));
1237 let fork_cond_ttd_blocknum_expected =
1238 Head { number: 101, timestamp: 11313123, ..Default::default() };
1239 assert_eq!(
1240 fork_cond_ttd_blocknum_head, fork_cond_ttd_blocknum_expected,
1241 "expected satisfy() to return {fork_cond_ttd_blocknum_expected:#?}, but got {fork_cond_ttd_blocknum_expected:#?} ",
1242 );
1243
1244 let fork_cond_block_only_case = ChainSpec::builder()
1248 .chain(Chain::mainnet())
1249 .genesis(empty_genesis)
1250 .with_fork(EthereumHardfork::Frontier, ForkCondition::Block(0))
1251 .with_fork(EthereumHardfork::Homestead, ForkCondition::Block(73))
1252 .build();
1253 let fork_cond_block_only_head = fork_cond_block_only_case.satisfy(ForkCondition::Block(73));
1254 let fork_cond_block_only_expected = Head { number: 73, ..Default::default() };
1255 assert_eq!(
1256 fork_cond_block_only_head, fork_cond_block_only_expected,
1257 "expected satisfy() to return {fork_cond_block_only_expected:#?}, but got {fork_cond_block_only_head:#?} ",
1258 );
1259 let fork_cond_ttd_no_new_spec = fork_cond_block_only_case.satisfy(ForkCondition::TTD {
1262 activation_block_number: 101,
1263 fork_block: None,
1264 total_difficulty: U256::from(10_790_000),
1265 });
1266 let fork_cond_ttd_no_new_spec_expected =
1267 Head { total_difficulty: U256::from(10_790_000), ..Default::default() };
1268 assert_eq!(
1269 fork_cond_ttd_no_new_spec, fork_cond_ttd_no_new_spec_expected,
1270 "expected satisfy() to return {fork_cond_ttd_blocknum_expected:#?}, but got {fork_cond_ttd_blocknum_expected:#?} ",
1271 );
1272 }
1273
1274 #[test]
1275 fn mainnet_hardfork_fork_ids() {
1276 test_hardfork_fork_ids(
1277 &MAINNET,
1278 &[
1279 (
1280 EthereumHardfork::Frontier,
1281 ForkId { hash: ForkHash([0xfc, 0x64, 0xec, 0x04]), next: 1150000 },
1282 ),
1283 (
1284 EthereumHardfork::Homestead,
1285 ForkId { hash: ForkHash([0x97, 0xc2, 0xc3, 0x4c]), next: 1920000 },
1286 ),
1287 (
1288 EthereumHardfork::Dao,
1289 ForkId { hash: ForkHash([0x91, 0xd1, 0xf9, 0x48]), next: 2463000 },
1290 ),
1291 (
1292 EthereumHardfork::Tangerine,
1293 ForkId { hash: ForkHash([0x7a, 0x64, 0xda, 0x13]), next: 2675000 },
1294 ),
1295 (
1296 EthereumHardfork::SpuriousDragon,
1297 ForkId { hash: ForkHash([0x3e, 0xdd, 0x5b, 0x10]), next: 4370000 },
1298 ),
1299 (
1300 EthereumHardfork::Byzantium,
1301 ForkId { hash: ForkHash([0xa0, 0x0b, 0xc3, 0x24]), next: 7280000 },
1302 ),
1303 (
1304 EthereumHardfork::Constantinople,
1305 ForkId { hash: ForkHash([0x66, 0x8d, 0xb0, 0xaf]), next: 9069000 },
1306 ),
1307 (
1308 EthereumHardfork::Petersburg,
1309 ForkId { hash: ForkHash([0x66, 0x8d, 0xb0, 0xaf]), next: 9069000 },
1310 ),
1311 (
1312 EthereumHardfork::Istanbul,
1313 ForkId { hash: ForkHash([0x87, 0x9d, 0x6e, 0x30]), next: 9200000 },
1314 ),
1315 (
1316 EthereumHardfork::MuirGlacier,
1317 ForkId { hash: ForkHash([0xe0, 0x29, 0xe9, 0x91]), next: 12244000 },
1318 ),
1319 (
1320 EthereumHardfork::Berlin,
1321 ForkId { hash: ForkHash([0x0e, 0xb4, 0x40, 0xf6]), next: 12965000 },
1322 ),
1323 (
1324 EthereumHardfork::London,
1325 ForkId { hash: ForkHash([0xb7, 0x15, 0x07, 0x7d]), next: 13773000 },
1326 ),
1327 (
1328 EthereumHardfork::ArrowGlacier,
1329 ForkId { hash: ForkHash([0x20, 0xc3, 0x27, 0xfc]), next: 15050000 },
1330 ),
1331 (
1332 EthereumHardfork::GrayGlacier,
1333 ForkId { hash: ForkHash([0xf0, 0xaf, 0xd0, 0xe3]), next: 1681338455 },
1334 ),
1335 (
1336 EthereumHardfork::Shanghai,
1337 ForkId { hash: ForkHash([0xdc, 0xe9, 0x6c, 0x2d]), next: 1710338135 },
1338 ),
1339 (
1340 EthereumHardfork::Cancun,
1341 ForkId { hash: ForkHash([0x9f, 0x3d, 0x22, 0x54]), next: 1746612311 },
1342 ),
1343 (
1344 EthereumHardfork::Prague,
1345 ForkId { hash: ForkHash([0xc3, 0x76, 0xcf, 0x8b]), next: 0 },
1346 ),
1347 ],
1348 );
1349 }
1350
1351 #[test]
1352 fn sepolia_hardfork_fork_ids() {
1353 test_hardfork_fork_ids(
1354 &SEPOLIA,
1355 &[
1356 (
1357 EthereumHardfork::Frontier,
1358 ForkId { hash: ForkHash([0xfe, 0x33, 0x66, 0xe7]), next: 1735371 },
1359 ),
1360 (
1361 EthereumHardfork::Homestead,
1362 ForkId { hash: ForkHash([0xfe, 0x33, 0x66, 0xe7]), next: 1735371 },
1363 ),
1364 (
1365 EthereumHardfork::Tangerine,
1366 ForkId { hash: ForkHash([0xfe, 0x33, 0x66, 0xe7]), next: 1735371 },
1367 ),
1368 (
1369 EthereumHardfork::SpuriousDragon,
1370 ForkId { hash: ForkHash([0xfe, 0x33, 0x66, 0xe7]), next: 1735371 },
1371 ),
1372 (
1373 EthereumHardfork::Byzantium,
1374 ForkId { hash: ForkHash([0xfe, 0x33, 0x66, 0xe7]), next: 1735371 },
1375 ),
1376 (
1377 EthereumHardfork::Constantinople,
1378 ForkId { hash: ForkHash([0xfe, 0x33, 0x66, 0xe7]), next: 1735371 },
1379 ),
1380 (
1381 EthereumHardfork::Petersburg,
1382 ForkId { hash: ForkHash([0xfe, 0x33, 0x66, 0xe7]), next: 1735371 },
1383 ),
1384 (
1385 EthereumHardfork::Istanbul,
1386 ForkId { hash: ForkHash([0xfe, 0x33, 0x66, 0xe7]), next: 1735371 },
1387 ),
1388 (
1389 EthereumHardfork::Berlin,
1390 ForkId { hash: ForkHash([0xfe, 0x33, 0x66, 0xe7]), next: 1735371 },
1391 ),
1392 (
1393 EthereumHardfork::London,
1394 ForkId { hash: ForkHash([0xfe, 0x33, 0x66, 0xe7]), next: 1735371 },
1395 ),
1396 (
1397 EthereumHardfork::Paris,
1398 ForkId { hash: ForkHash([0xb9, 0x6c, 0xbd, 0x13]), next: 1677557088 },
1399 ),
1400 (
1401 EthereumHardfork::Shanghai,
1402 ForkId { hash: ForkHash([0xf7, 0xf9, 0xbc, 0x08]), next: 1706655072 },
1403 ),
1404 (
1405 EthereumHardfork::Cancun,
1406 ForkId { hash: ForkHash([0x88, 0xcf, 0x81, 0xd9]), next: 1741159776 },
1407 ),
1408 (
1409 EthereumHardfork::Prague,
1410 ForkId { hash: ForkHash([0xed, 0x88, 0xb5, 0xfd]), next: 0 },
1411 ),
1412 ],
1413 );
1414 }
1415
1416 #[test]
1417 fn mainnet_fork_ids() {
1418 test_fork_ids(
1419 &MAINNET,
1420 &[
1421 (
1422 Head { number: 0, ..Default::default() },
1423 ForkId { hash: ForkHash([0xfc, 0x64, 0xec, 0x04]), next: 1150000 },
1424 ),
1425 (
1426 Head { number: 1150000, ..Default::default() },
1427 ForkId { hash: ForkHash([0x97, 0xc2, 0xc3, 0x4c]), next: 1920000 },
1428 ),
1429 (
1430 Head { number: 1920000, ..Default::default() },
1431 ForkId { hash: ForkHash([0x91, 0xd1, 0xf9, 0x48]), next: 2463000 },
1432 ),
1433 (
1434 Head { number: 2463000, ..Default::default() },
1435 ForkId { hash: ForkHash([0x7a, 0x64, 0xda, 0x13]), next: 2675000 },
1436 ),
1437 (
1438 Head { number: 2675000, ..Default::default() },
1439 ForkId { hash: ForkHash([0x3e, 0xdd, 0x5b, 0x10]), next: 4370000 },
1440 ),
1441 (
1442 Head { number: 4370000, ..Default::default() },
1443 ForkId { hash: ForkHash([0xa0, 0x0b, 0xc3, 0x24]), next: 7280000 },
1444 ),
1445 (
1446 Head { number: 7280000, ..Default::default() },
1447 ForkId { hash: ForkHash([0x66, 0x8d, 0xb0, 0xaf]), next: 9069000 },
1448 ),
1449 (
1450 Head { number: 9069000, ..Default::default() },
1451 ForkId { hash: ForkHash([0x87, 0x9d, 0x6e, 0x30]), next: 9200000 },
1452 ),
1453 (
1454 Head { number: 9200000, ..Default::default() },
1455 ForkId { hash: ForkHash([0xe0, 0x29, 0xe9, 0x91]), next: 12244000 },
1456 ),
1457 (
1458 Head { number: 12244000, ..Default::default() },
1459 ForkId { hash: ForkHash([0x0e, 0xb4, 0x40, 0xf6]), next: 12965000 },
1460 ),
1461 (
1462 Head { number: 12965000, ..Default::default() },
1463 ForkId { hash: ForkHash([0xb7, 0x15, 0x07, 0x7d]), next: 13773000 },
1464 ),
1465 (
1466 Head { number: 13773000, ..Default::default() },
1467 ForkId { hash: ForkHash([0x20, 0xc3, 0x27, 0xfc]), next: 15050000 },
1468 ),
1469 (
1470 Head { number: 15050000, ..Default::default() },
1471 ForkId { hash: ForkHash([0xf0, 0xaf, 0xd0, 0xe3]), next: 1681338455 },
1472 ),
1473 (
1475 Head { number: 20000000, timestamp: 1681338455, ..Default::default() },
1476 ForkId { hash: ForkHash([0xdc, 0xe9, 0x6c, 0x2d]), next: 1710338135 },
1477 ),
1478 (
1480 Head { number: 20000001, timestamp: 1710338135, ..Default::default() },
1481 ForkId { hash: ForkHash([0x9f, 0x3d, 0x22, 0x54]), next: 1746612311 },
1482 ),
1483 (
1485 Head { number: 20000002, timestamp: 1746612311, ..Default::default() },
1486 ForkId { hash: ForkHash([0xc3, 0x76, 0xcf, 0x8b]), next: 0 },
1487 ),
1488 (
1490 Head { number: 20000002, timestamp: 2000000000, ..Default::default() },
1491 ForkId { hash: ForkHash([0xc3, 0x76, 0xcf, 0x8b]), next: 0 },
1492 ),
1493 ],
1494 );
1495 }
1496
1497 #[test]
1498 fn hoodi_fork_ids() {
1499 test_fork_ids(
1500 &HOODI,
1501 &[
1502 (
1503 Head { number: 0, ..Default::default() },
1504 ForkId { hash: ForkHash([0xbe, 0xf7, 0x1d, 0x30]), next: 1742999832 },
1505 ),
1506 (
1508 Head { number: 0, timestamp: 1742999833, ..Default::default() },
1509 ForkId { hash: ForkHash([0x09, 0x29, 0xe2, 0x4e]), next: 0 },
1510 ),
1511 ],
1512 )
1513 }
1514
1515 #[test]
1516 fn holesky_fork_ids() {
1517 test_fork_ids(
1518 &HOLESKY,
1519 &[
1520 (
1521 Head { number: 0, ..Default::default() },
1522 ForkId { hash: ForkHash([0xc6, 0x1a, 0x60, 0x98]), next: 1696000704 },
1523 ),
1524 (
1526 Head { number: 123, ..Default::default() },
1527 ForkId { hash: ForkHash([0xc6, 0x1a, 0x60, 0x98]), next: 1696000704 },
1528 ),
1529 (
1531 Head { number: 123, timestamp: 1696000703, ..Default::default() },
1532 ForkId { hash: ForkHash([0xc6, 0x1a, 0x60, 0x98]), next: 1696000704 },
1533 ),
1534 (
1536 Head { number: 123, timestamp: 1696000704, ..Default::default() },
1537 ForkId { hash: ForkHash([0xfd, 0x4f, 0x01, 0x6b]), next: 1707305664 },
1538 ),
1539 (
1541 Head { number: 123, timestamp: 1707305663, ..Default::default() },
1542 ForkId { hash: ForkHash([0xfd, 0x4f, 0x01, 0x6b]), next: 1707305664 },
1543 ),
1544 (
1546 Head { number: 123, timestamp: 1707305664, ..Default::default() },
1547 ForkId { hash: ForkHash([0x9b, 0x19, 0x2a, 0xd0]), next: 1740434112 },
1548 ),
1549 (
1551 Head { number: 123, timestamp: 1740434111, ..Default::default() },
1552 ForkId { hash: ForkHash([0x9b, 0x19, 0x2a, 0xd0]), next: 1740434112 },
1553 ),
1554 (
1556 Head { number: 123, timestamp: 1740434112, ..Default::default() },
1557 ForkId { hash: ForkHash([0xdf, 0xbd, 0x9b, 0xed]), next: 0 },
1558 ),
1559 ],
1560 )
1561 }
1562
1563 #[test]
1564 fn sepolia_fork_ids() {
1565 test_fork_ids(
1566 &SEPOLIA,
1567 &[
1568 (
1569 Head { number: 0, ..Default::default() },
1570 ForkId { hash: ForkHash([0xfe, 0x33, 0x66, 0xe7]), next: 1735371 },
1571 ),
1572 (
1573 Head { number: 1735370, ..Default::default() },
1574 ForkId { hash: ForkHash([0xfe, 0x33, 0x66, 0xe7]), next: 1735371 },
1575 ),
1576 (
1577 Head { number: 1735371, ..Default::default() },
1578 ForkId { hash: ForkHash([0xb9, 0x6c, 0xbd, 0x13]), next: 1677557088 },
1579 ),
1580 (
1581 Head { number: 1735372, timestamp: 1677557087, ..Default::default() },
1582 ForkId { hash: ForkHash([0xb9, 0x6c, 0xbd, 0x13]), next: 1677557088 },
1583 ),
1584 (
1586 Head { number: 1735373, timestamp: 1677557088, ..Default::default() },
1587 ForkId { hash: ForkHash([0xf7, 0xf9, 0xbc, 0x08]), next: 1706655072 },
1588 ),
1589 (
1591 Head { number: 1735374, timestamp: 1706655071, ..Default::default() },
1592 ForkId { hash: ForkHash([0xf7, 0xf9, 0xbc, 0x08]), next: 1706655072 },
1593 ),
1594 (
1596 Head { number: 1735375, timestamp: 1706655072, ..Default::default() },
1597 ForkId { hash: ForkHash([0x88, 0xcf, 0x81, 0xd9]), next: 1741159776 },
1598 ),
1599 (
1601 Head { number: 1735376, timestamp: 1741159775, ..Default::default() },
1602 ForkId { hash: ForkHash([0x88, 0xcf, 0x81, 0xd9]), next: 1741159776 },
1603 ),
1604 (
1606 Head { number: 1735377, timestamp: 1741159776, ..Default::default() },
1607 ForkId { hash: ForkHash([0xed, 0x88, 0xb5, 0xfd]), next: 0 },
1608 ),
1609 ],
1610 );
1611 }
1612
1613 #[test]
1614 fn dev_fork_ids() {
1615 test_fork_ids(
1616 &DEV,
1617 &[(
1618 Head { number: 0, ..Default::default() },
1619 ForkId { hash: ForkHash([0x45, 0xb8, 0x36, 0x12]), next: 0 },
1620 )],
1621 )
1622 }
1623
1624 #[test]
1628 fn timestamped_forks() {
1629 let mainnet_with_timestamps = ChainSpecBuilder::mainnet().build();
1630 test_fork_ids(
1631 &mainnet_with_timestamps,
1632 &[
1633 (
1634 Head { number: 0, timestamp: 0, ..Default::default() },
1635 ForkId { hash: ForkHash([0xfc, 0x64, 0xec, 0x04]), next: 1150000 },
1636 ), (
1638 Head { number: 1149999, timestamp: 0, ..Default::default() },
1639 ForkId { hash: ForkHash([0xfc, 0x64, 0xec, 0x04]), next: 1150000 },
1640 ), (
1642 Head { number: 1150000, timestamp: 0, ..Default::default() },
1643 ForkId { hash: ForkHash([0x97, 0xc2, 0xc3, 0x4c]), next: 1920000 },
1644 ), (
1646 Head { number: 1919999, timestamp: 0, ..Default::default() },
1647 ForkId { hash: ForkHash([0x97, 0xc2, 0xc3, 0x4c]), next: 1920000 },
1648 ), (
1650 Head { number: 1920000, timestamp: 0, ..Default::default() },
1651 ForkId { hash: ForkHash([0x91, 0xd1, 0xf9, 0x48]), next: 2463000 },
1652 ), (
1654 Head { number: 2462999, timestamp: 0, ..Default::default() },
1655 ForkId { hash: ForkHash([0x91, 0xd1, 0xf9, 0x48]), next: 2463000 },
1656 ), (
1658 Head { number: 2463000, timestamp: 0, ..Default::default() },
1659 ForkId { hash: ForkHash([0x7a, 0x64, 0xda, 0x13]), next: 2675000 },
1660 ), (
1662 Head { number: 2674999, timestamp: 0, ..Default::default() },
1663 ForkId { hash: ForkHash([0x7a, 0x64, 0xda, 0x13]), next: 2675000 },
1664 ), (
1666 Head { number: 2675000, timestamp: 0, ..Default::default() },
1667 ForkId { hash: ForkHash([0x3e, 0xdd, 0x5b, 0x10]), next: 4370000 },
1668 ), (
1670 Head { number: 4369999, timestamp: 0, ..Default::default() },
1671 ForkId { hash: ForkHash([0x3e, 0xdd, 0x5b, 0x10]), next: 4370000 },
1672 ), (
1674 Head { number: 4370000, timestamp: 0, ..Default::default() },
1675 ForkId { hash: ForkHash([0xa0, 0x0b, 0xc3, 0x24]), next: 7280000 },
1676 ), (
1678 Head { number: 7279999, timestamp: 0, ..Default::default() },
1679 ForkId { hash: ForkHash([0xa0, 0x0b, 0xc3, 0x24]), next: 7280000 },
1680 ), (
1682 Head { number: 7280000, timestamp: 0, ..Default::default() },
1683 ForkId { hash: ForkHash([0x66, 0x8d, 0xb0, 0xaf]), next: 9069000 },
1684 ), (
1686 Head { number: 9068999, timestamp: 0, ..Default::default() },
1687 ForkId { hash: ForkHash([0x66, 0x8d, 0xb0, 0xaf]), next: 9069000 },
1688 ), (
1690 Head { number: 9069000, timestamp: 0, ..Default::default() },
1691 ForkId { hash: ForkHash([0x87, 0x9d, 0x6e, 0x30]), next: 9200000 },
1692 ), (
1694 Head { number: 9199999, timestamp: 0, ..Default::default() },
1695 ForkId { hash: ForkHash([0x87, 0x9d, 0x6e, 0x30]), next: 9200000 },
1696 ), (
1698 Head { number: 9200000, timestamp: 0, ..Default::default() },
1699 ForkId { hash: ForkHash([0xe0, 0x29, 0xe9, 0x91]), next: 12244000 },
1700 ), (
1702 Head { number: 12243999, timestamp: 0, ..Default::default() },
1703 ForkId { hash: ForkHash([0xe0, 0x29, 0xe9, 0x91]), next: 12244000 },
1704 ), (
1706 Head { number: 12244000, timestamp: 0, ..Default::default() },
1707 ForkId { hash: ForkHash([0x0e, 0xb4, 0x40, 0xf6]), next: 12965000 },
1708 ), (
1710 Head { number: 12964999, timestamp: 0, ..Default::default() },
1711 ForkId { hash: ForkHash([0x0e, 0xb4, 0x40, 0xf6]), next: 12965000 },
1712 ), (
1714 Head { number: 12965000, timestamp: 0, ..Default::default() },
1715 ForkId { hash: ForkHash([0xb7, 0x15, 0x07, 0x7d]), next: 13773000 },
1716 ), (
1718 Head { number: 13772999, timestamp: 0, ..Default::default() },
1719 ForkId { hash: ForkHash([0xb7, 0x15, 0x07, 0x7d]), next: 13773000 },
1720 ), (
1722 Head { number: 13773000, timestamp: 0, ..Default::default() },
1723 ForkId { hash: ForkHash([0x20, 0xc3, 0x27, 0xfc]), next: 15050000 },
1724 ), (
1726 Head { number: 15049999, timestamp: 0, ..Default::default() },
1727 ForkId { hash: ForkHash([0x20, 0xc3, 0x27, 0xfc]), next: 15050000 },
1728 ), (
1730 Head { number: 15050000, timestamp: 0, ..Default::default() },
1731 ForkId { hash: ForkHash([0xf0, 0xaf, 0xd0, 0xe3]), next: 1681338455 },
1732 ), (
1734 Head { number: 19999999, timestamp: 1667999999, ..Default::default() },
1735 ForkId { hash: ForkHash([0xf0, 0xaf, 0xd0, 0xe3]), next: 1681338455 },
1736 ), (
1738 Head { number: 20000000, timestamp: 1681338455, ..Default::default() },
1739 ForkId { hash: ForkHash([0xdc, 0xe9, 0x6c, 0x2d]), next: 1710338135 },
1740 ), (
1742 Head { number: 20000001, timestamp: 1710338134, ..Default::default() },
1743 ForkId { hash: ForkHash([0xdc, 0xe9, 0x6c, 0x2d]), next: 1710338135 },
1744 ), (
1746 Head { number: 20000002, timestamp: 1710338135, ..Default::default() },
1747 ForkId { hash: ForkHash([0x9f, 0x3d, 0x22, 0x54]), next: 1746612311 },
1748 ), (
1750 Head { number: 20000003, timestamp: 1746612310, ..Default::default() },
1751 ForkId { hash: ForkHash([0x9f, 0x3d, 0x22, 0x54]), next: 1746612311 },
1752 ), (
1754 Head { number: 20000004, timestamp: 1746612311, ..Default::default() },
1755 ForkId { hash: ForkHash([0xc3, 0x76, 0xcf, 0x8b]), next: 0 },
1756 ), (
1758 Head { number: 20000004, timestamp: 2000000000, ..Default::default() },
1759 ForkId { hash: ForkHash([0xc3, 0x76, 0xcf, 0x8b]), next: 0 },
1760 ),
1761 ],
1762 );
1763 }
1764
1765 fn construct_chainspec(
1768 builder: ChainSpecBuilder,
1769 shanghai_time: u64,
1770 cancun_time: u64,
1771 ) -> ChainSpec {
1772 builder
1773 .with_fork(EthereumHardfork::Shanghai, ForkCondition::Timestamp(shanghai_time))
1774 .with_fork(EthereumHardfork::Cancun, ForkCondition::Timestamp(cancun_time))
1775 .build()
1776 }
1777
1778 #[test]
1783 fn test_timestamp_fork_in_genesis() {
1784 let timestamp = 1690475657u64;
1785 let default_spec_builder = ChainSpecBuilder::default()
1786 .chain(Chain::from_id(1337))
1787 .genesis(Genesis::default().with_timestamp(timestamp))
1788 .paris_activated();
1789
1790 let tests = [
1793 (
1794 construct_chainspec(default_spec_builder.clone(), timestamp - 1, timestamp + 1),
1795 timestamp + 1,
1796 ),
1797 (
1798 construct_chainspec(default_spec_builder.clone(), timestamp, timestamp + 1),
1799 timestamp + 1,
1800 ),
1801 (
1802 construct_chainspec(default_spec_builder, timestamp + 1, timestamp + 2),
1803 timestamp + 1,
1804 ),
1805 ];
1806
1807 for (spec, expected_timestamp) in tests {
1808 let got_forkid = spec.fork_id(&Head { number: 0, timestamp: 0, ..Default::default() });
1809 let genesis_hash = spec.genesis_hash();
1814 let expected_forkid =
1815 ForkId { hash: ForkHash::from(genesis_hash), next: expected_timestamp };
1816 assert_eq!(got_forkid, expected_forkid);
1817 }
1818 }
1819
1820 #[test]
1822 fn check_terminal_ttd() {
1823 let chainspec = ChainSpecBuilder::mainnet().build();
1824
1825 let terminal_block_ttd = U256::from(58750003716598352816469_u128);
1827 let terminal_block_difficulty = U256::from(11055787484078698_u128);
1828 assert!(!chainspec
1829 .fork(EthereumHardfork::Paris)
1830 .active_at_ttd(terminal_block_ttd, terminal_block_difficulty));
1831
1832 let first_pos_block_ttd = U256::from(58750003716598352816469_u128);
1834 let first_pos_difficulty = U256::ZERO;
1835 assert!(chainspec
1836 .fork(EthereumHardfork::Paris)
1837 .active_at_ttd(first_pos_block_ttd, first_pos_difficulty));
1838 }
1839
1840 #[test]
1841 fn geth_genesis_with_shanghai() {
1842 let geth_genesis = r#"
1843 {
1844 "config": {
1845 "chainId": 1337,
1846 "homesteadBlock": 0,
1847 "eip150Block": 0,
1848 "eip150Hash": "0x0000000000000000000000000000000000000000000000000000000000000000",
1849 "eip155Block": 0,
1850 "eip158Block": 0,
1851 "byzantiumBlock": 0,
1852 "constantinopleBlock": 0,
1853 "petersburgBlock": 0,
1854 "istanbulBlock": 0,
1855 "muirGlacierBlock": 0,
1856 "berlinBlock": 0,
1857 "londonBlock": 0,
1858 "arrowGlacierBlock": 0,
1859 "grayGlacierBlock": 0,
1860 "shanghaiTime": 0,
1861 "cancunTime": 1,
1862 "terminalTotalDifficulty": 0,
1863 "terminalTotalDifficultyPassed": true,
1864 "ethash": {}
1865 },
1866 "nonce": "0x0",
1867 "timestamp": "0x0",
1868 "extraData": "0x",
1869 "gasLimit": "0x4c4b40",
1870 "difficulty": "0x1",
1871 "mixHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
1872 "coinbase": "0x0000000000000000000000000000000000000000",
1873 "alloc": {
1874 "658bdf435d810c91414ec09147daa6db62406379": {
1875 "balance": "0x487a9a304539440000"
1876 },
1877 "aa00000000000000000000000000000000000000": {
1878 "code": "0x6042",
1879 "storage": {
1880 "0x0000000000000000000000000000000000000000000000000000000000000000": "0x0000000000000000000000000000000000000000000000000000000000000000",
1881 "0x0100000000000000000000000000000000000000000000000000000000000000": "0x0100000000000000000000000000000000000000000000000000000000000000",
1882 "0x0200000000000000000000000000000000000000000000000000000000000000": "0x0200000000000000000000000000000000000000000000000000000000000000",
1883 "0x0300000000000000000000000000000000000000000000000000000000000000": "0x0000000000000000000000000000000000000000000000000000000000000303"
1884 },
1885 "balance": "0x1",
1886 "nonce": "0x1"
1887 },
1888 "bb00000000000000000000000000000000000000": {
1889 "code": "0x600154600354",
1890 "storage": {
1891 "0x0000000000000000000000000000000000000000000000000000000000000000": "0x0000000000000000000000000000000000000000000000000000000000000000",
1892 "0x0100000000000000000000000000000000000000000000000000000000000000": "0x0100000000000000000000000000000000000000000000000000000000000000",
1893 "0x0200000000000000000000000000000000000000000000000000000000000000": "0x0200000000000000000000000000000000000000000000000000000000000000",
1894 "0x0300000000000000000000000000000000000000000000000000000000000000": "0x0000000000000000000000000000000000000000000000000000000000000303"
1895 },
1896 "balance": "0x2",
1897 "nonce": "0x1"
1898 }
1899 },
1900 "number": "0x0",
1901 "gasUsed": "0x0",
1902 "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
1903 "baseFeePerGas": "0x3b9aca00"
1904 }
1905 "#;
1906
1907 let genesis: Genesis = serde_json::from_str(geth_genesis).unwrap();
1908 let chainspec = ChainSpec::from(genesis);
1909
1910 assert_eq!(
1912 chainspec.hardforks.get(EthereumHardfork::Homestead).unwrap(),
1913 ForkCondition::Block(0)
1914 );
1915 assert_eq!(
1916 chainspec.hardforks.get(EthereumHardfork::Tangerine).unwrap(),
1917 ForkCondition::Block(0)
1918 );
1919 assert_eq!(
1920 chainspec.hardforks.get(EthereumHardfork::SpuriousDragon).unwrap(),
1921 ForkCondition::Block(0)
1922 );
1923 assert_eq!(
1924 chainspec.hardforks.get(EthereumHardfork::Byzantium).unwrap(),
1925 ForkCondition::Block(0)
1926 );
1927 assert_eq!(
1928 chainspec.hardforks.get(EthereumHardfork::Constantinople).unwrap(),
1929 ForkCondition::Block(0)
1930 );
1931 assert_eq!(
1932 chainspec.hardforks.get(EthereumHardfork::Petersburg).unwrap(),
1933 ForkCondition::Block(0)
1934 );
1935 assert_eq!(
1936 chainspec.hardforks.get(EthereumHardfork::Istanbul).unwrap(),
1937 ForkCondition::Block(0)
1938 );
1939 assert_eq!(
1940 chainspec.hardforks.get(EthereumHardfork::MuirGlacier).unwrap(),
1941 ForkCondition::Block(0)
1942 );
1943 assert_eq!(
1944 chainspec.hardforks.get(EthereumHardfork::Berlin).unwrap(),
1945 ForkCondition::Block(0)
1946 );
1947 assert_eq!(
1948 chainspec.hardforks.get(EthereumHardfork::London).unwrap(),
1949 ForkCondition::Block(0)
1950 );
1951 assert_eq!(
1952 chainspec.hardforks.get(EthereumHardfork::ArrowGlacier).unwrap(),
1953 ForkCondition::Block(0)
1954 );
1955 assert_eq!(
1956 chainspec.hardforks.get(EthereumHardfork::GrayGlacier).unwrap(),
1957 ForkCondition::Block(0)
1958 );
1959
1960 assert_eq!(
1962 chainspec.hardforks.get(EthereumHardfork::Shanghai).unwrap(),
1963 ForkCondition::Timestamp(0)
1964 );
1965
1966 assert_eq!(
1968 chainspec.hardforks.get(EthereumHardfork::Cancun).unwrap(),
1969 ForkCondition::Timestamp(1)
1970 );
1971
1972 let key_rlp = vec![
1974 (
1975 hex!("0x658bdf435d810c91414ec09147daa6db62406379"),
1976 &hex!(
1977 "0xf84d8089487a9a304539440000a056e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421a0c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470"
1978 )[..],
1979 ),
1980 (
1981 hex!("0xaa00000000000000000000000000000000000000"),
1982 &hex!(
1983 "0xf8440101a08afc95b7d18a226944b9c2070b6bda1c3a36afcc3730429d47579c94b9fe5850a0ce92c756baff35fa740c3557c1a971fd24d2d35b7c8e067880d50cd86bb0bc99"
1984 )[..],
1985 ),
1986 (
1987 hex!("0xbb00000000000000000000000000000000000000"),
1988 &hex!(
1989 "0xf8440102a08afc95b7d18a226944b9c2070b6bda1c3a36afcc3730429d47579c94b9fe5850a0e25a53cbb501cec2976b393719c63d832423dd70a458731a0b64e4847bbca7d2"
1990 )[..],
1991 ),
1992 ];
1993
1994 for (key, expected_rlp) in key_rlp {
1995 let account = chainspec.genesis.alloc.get(&key).expect("account should exist");
1996 assert_eq!(&alloy_rlp::encode(TrieAccount::from(account.clone())), expected_rlp);
1997 }
1998
1999 let expected_state_root: B256 =
2000 hex!("0x078dc6061b1d8eaa8493384b59c9c65ceb917201221d08b80c4de6770b6ec7e7").into();
2001 assert_eq!(chainspec.genesis_header().state_root, expected_state_root);
2002
2003 assert_eq!(chainspec.genesis_header().withdrawals_root, Some(EMPTY_ROOT_HASH));
2004
2005 let expected_hash: B256 =
2006 hex!("0x1fc027d65f820d3eef441ebeec139ebe09e471cf98516dce7b5643ccb27f418c").into();
2007 let hash = chainspec.genesis_hash();
2008 assert_eq!(hash, expected_hash);
2009 }
2010
2011 #[test]
2012 fn hive_geth_json() {
2013 let hive_json = r#"
2014 {
2015 "nonce": "0x0000000000000042",
2016 "difficulty": "0x2123456",
2017 "mixHash": "0x123456789abcdef123456789abcdef123456789abcdef123456789abcdef1234",
2018 "coinbase": "0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
2019 "timestamp": "0x123456",
2020 "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
2021 "extraData": "0xfafbfcfd",
2022 "gasLimit": "0x2fefd8",
2023 "alloc": {
2024 "dbdbdb2cbd23b783741e8d7fcf51e459b497e4a6": {
2025 "balance": "0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
2026 },
2027 "e6716f9544a56c530d868e4bfbacb172315bdead": {
2028 "balance": "0x11",
2029 "code": "0x12"
2030 },
2031 "b9c015918bdaba24b4ff057a92a3873d6eb201be": {
2032 "balance": "0x21",
2033 "storage": {
2034 "0x0000000000000000000000000000000000000000000000000000000000000001": "0x22"
2035 }
2036 },
2037 "1a26338f0d905e295fccb71fa9ea849ffa12aaf4": {
2038 "balance": "0x31",
2039 "nonce": "0x32"
2040 },
2041 "0000000000000000000000000000000000000001": {
2042 "balance": "0x41"
2043 },
2044 "0000000000000000000000000000000000000002": {
2045 "balance": "0x51"
2046 },
2047 "0000000000000000000000000000000000000003": {
2048 "balance": "0x61"
2049 },
2050 "0000000000000000000000000000000000000004": {
2051 "balance": "0x71"
2052 }
2053 },
2054 "config": {
2055 "ethash": {},
2056 "chainId": 10,
2057 "homesteadBlock": 0,
2058 "eip150Block": 0,
2059 "eip155Block": 0,
2060 "eip158Block": 0,
2061 "byzantiumBlock": 0,
2062 "constantinopleBlock": 0,
2063 "petersburgBlock": 0,
2064 "istanbulBlock": 0
2065 }
2066 }
2067 "#;
2068
2069 let genesis = serde_json::from_str::<Genesis>(hive_json).unwrap();
2070 let chainspec: ChainSpec = genesis.into();
2071 assert_eq!(chainspec.chain, Chain::from_named(NamedChain::Optimism));
2072 let expected_state_root: B256 =
2073 hex!("0x9a6049ac535e3dc7436c189eaa81c73f35abd7f282ab67c32944ff0301d63360").into();
2074 assert_eq!(chainspec.genesis_header().state_root, expected_state_root);
2075 let hard_forks = vec![
2076 EthereumHardfork::Byzantium,
2077 EthereumHardfork::Homestead,
2078 EthereumHardfork::Istanbul,
2079 EthereumHardfork::Petersburg,
2080 EthereumHardfork::Constantinople,
2081 ];
2082 for fork in hard_forks {
2083 assert_eq!(chainspec.hardforks.get(fork).unwrap(), ForkCondition::Block(0));
2084 }
2085
2086 let expected_hash: B256 =
2087 hex!("0x5ae31c6522bd5856129f66be3d582b842e4e9faaa87f21cce547128339a9db3c").into();
2088 let hash = chainspec.genesis_header().hash_slow();
2089 assert_eq!(hash, expected_hash);
2090 }
2091
2092 #[test]
2093 fn test_hive_paris_block_genesis_json() {
2094 let hive_paris = r#"
2097 {
2098 "config": {
2099 "ethash": {},
2100 "chainId": 3503995874084926,
2101 "homesteadBlock": 0,
2102 "eip150Block": 6,
2103 "eip155Block": 12,
2104 "eip158Block": 12,
2105 "byzantiumBlock": 18,
2106 "constantinopleBlock": 24,
2107 "petersburgBlock": 30,
2108 "istanbulBlock": 36,
2109 "muirGlacierBlock": 42,
2110 "berlinBlock": 48,
2111 "londonBlock": 54,
2112 "arrowGlacierBlock": 60,
2113 "grayGlacierBlock": 66,
2114 "mergeNetsplitBlock": 72,
2115 "terminalTotalDifficulty": 9454784,
2116 "shanghaiTime": 780,
2117 "cancunTime": 840
2118 },
2119 "nonce": "0x0",
2120 "timestamp": "0x0",
2121 "extraData": "0x68697665636861696e",
2122 "gasLimit": "0x23f3e20",
2123 "difficulty": "0x20000",
2124 "mixHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
2125 "coinbase": "0x0000000000000000000000000000000000000000",
2126 "alloc": {
2127 "000f3df6d732807ef1319fb7b8bb8522d0beac02": {
2128 "code": "0x3373fffffffffffffffffffffffffffffffffffffffe14604d57602036146024575f5ffd5b5f35801560495762001fff810690815414603c575f5ffd5b62001fff01545f5260205ff35b5f5ffd5b62001fff42064281555f359062001fff015500",
2129 "balance": "0x2a"
2130 },
2131 "0c2c51a0990aee1d73c1228de158688341557508": {
2132 "balance": "0xc097ce7bc90715b34b9f1000000000"
2133 },
2134 "14e46043e63d0e3cdcf2530519f4cfaf35058cb2": {
2135 "balance": "0xc097ce7bc90715b34b9f1000000000"
2136 },
2137 "16c57edf7fa9d9525378b0b81bf8a3ced0620c1c": {
2138 "balance": "0xc097ce7bc90715b34b9f1000000000"
2139 },
2140 "1f4924b14f34e24159387c0a4cdbaa32f3ddb0cf": {
2141 "balance": "0xc097ce7bc90715b34b9f1000000000"
2142 },
2143 "1f5bde34b4afc686f136c7a3cb6ec376f7357759": {
2144 "balance": "0xc097ce7bc90715b34b9f1000000000"
2145 },
2146 "2d389075be5be9f2246ad654ce152cf05990b209": {
2147 "balance": "0xc097ce7bc90715b34b9f1000000000"
2148 },
2149 "3ae75c08b4c907eb63a8960c45b86e1e9ab6123c": {
2150 "balance": "0xc097ce7bc90715b34b9f1000000000"
2151 },
2152 "4340ee1b812acb40a1eb561c019c327b243b92df": {
2153 "balance": "0xc097ce7bc90715b34b9f1000000000"
2154 },
2155 "4a0f1452281bcec5bd90c3dce6162a5995bfe9df": {
2156 "balance": "0xc097ce7bc90715b34b9f1000000000"
2157 },
2158 "4dde844b71bcdf95512fb4dc94e84fb67b512ed8": {
2159 "balance": "0xc097ce7bc90715b34b9f1000000000"
2160 },
2161 "5f552da00dfb4d3749d9e62dcee3c918855a86a0": {
2162 "balance": "0xc097ce7bc90715b34b9f1000000000"
2163 },
2164 "654aa64f5fbefb84c270ec74211b81ca8c44a72e": {
2165 "balance": "0xc097ce7bc90715b34b9f1000000000"
2166 },
2167 "717f8aa2b982bee0e29f573d31df288663e1ce16": {
2168 "balance": "0xc097ce7bc90715b34b9f1000000000"
2169 },
2170 "7435ed30a8b4aeb0877cef0c6e8cffe834eb865f": {
2171 "balance": "0xc097ce7bc90715b34b9f1000000000"
2172 },
2173 "83c7e323d189f18725ac510004fdc2941f8c4a78": {
2174 "balance": "0xc097ce7bc90715b34b9f1000000000"
2175 },
2176 "84e75c28348fb86acea1a93a39426d7d60f4cc46": {
2177 "balance": "0xc097ce7bc90715b34b9f1000000000"
2178 },
2179 "8bebc8ba651aee624937e7d897853ac30c95a067": {
2180 "storage": {
2181 "0x0000000000000000000000000000000000000000000000000000000000000001": "0x0000000000000000000000000000000000000000000000000000000000000001",
2182 "0x0000000000000000000000000000000000000000000000000000000000000002": "0x0000000000000000000000000000000000000000000000000000000000000002",
2183 "0x0000000000000000000000000000000000000000000000000000000000000003": "0x0000000000000000000000000000000000000000000000000000000000000003"
2184 },
2185 "balance": "0x1",
2186 "nonce": "0x1"
2187 },
2188 "c7b99a164efd027a93f147376cc7da7c67c6bbe0": {
2189 "balance": "0xc097ce7bc90715b34b9f1000000000"
2190 },
2191 "d803681e487e6ac18053afc5a6cd813c86ec3e4d": {
2192 "balance": "0xc097ce7bc90715b34b9f1000000000"
2193 },
2194 "e7d13f7aa2a838d24c59b40186a0aca1e21cffcc": {
2195 "balance": "0xc097ce7bc90715b34b9f1000000000"
2196 },
2197 "eda8645ba6948855e3b3cd596bbb07596d59c603": {
2198 "balance": "0xc097ce7bc90715b34b9f1000000000"
2199 }
2200 },
2201 "number": "0x0",
2202 "gasUsed": "0x0",
2203 "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
2204 "baseFeePerGas": null,
2205 "excessBlobGas": null,
2206 "blobGasUsed": null
2207 }
2208 "#;
2209
2210 let genesis: Genesis = serde_json::from_str(hive_paris).unwrap();
2212 let chainspec = ChainSpec::from(genesis);
2213
2214 let expected_forkid = ForkId { hash: ForkHash([0xbc, 0x0c, 0x26, 0x05]), next: 0 };
2216 let got_forkid =
2217 chainspec.fork_id(&Head { number: 73, timestamp: 840, ..Default::default() });
2218
2219 assert_eq!(got_forkid, expected_forkid);
2221 assert_eq!(chainspec.paris_block_and_final_difficulty, Some((72, U256::from(9454784))));
2223 }
2224
2225 #[test]
2226 fn test_parse_genesis_json() {
2227 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"}"#;
2228 let genesis: Genesis = serde_json::from_str(s).unwrap();
2229 let acc = genesis
2230 .alloc
2231 .get(&"0xaa00000000000000000000000000000000000000".parse::<Address>().unwrap())
2232 .unwrap();
2233 assert_eq!(acc.balance, U256::from(1));
2234 assert_eq!(genesis.base_fee_per_gas, Some(0x1337));
2235 }
2236
2237 #[test]
2238 fn test_parse_cancun_genesis_json() {
2239 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"}"#;
2240 let genesis: Genesis = serde_json::from_str(s).unwrap();
2241 let acc = genesis
2242 .alloc
2243 .get(&"0xaa00000000000000000000000000000000000000".parse::<Address>().unwrap())
2244 .unwrap();
2245 assert_eq!(acc.balance, U256::from(1));
2246 assert_eq!(genesis.config.cancun_time, Some(4661));
2248 }
2249
2250 #[test]
2251 fn test_parse_prague_genesis_all_formats() {
2252 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"}"#;
2253 let genesis: Genesis = serde_json::from_str(s).unwrap();
2254
2255 let acc = genesis
2257 .alloc
2258 .get(&"0xaa00000000000000000000000000000000000000".parse::<Address>().unwrap())
2259 .unwrap();
2260 assert_eq!(acc.balance, U256::from(1));
2261 assert_eq!(genesis.config.cancun_time, Some(4661));
2263 assert_eq!(genesis.config.prague_time, Some(4662));
2265 }
2266
2267 #[test]
2268 fn test_parse_cancun_genesis_all_formats() {
2269 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"}"#;
2270 let genesis: Genesis = serde_json::from_str(s).unwrap();
2271
2272 let acc = genesis
2274 .alloc
2275 .get(&"0xaa00000000000000000000000000000000000000".parse::<Address>().unwrap())
2276 .unwrap();
2277 assert_eq!(acc.balance, U256::from(1));
2278 assert_eq!(genesis.config.cancun_time, Some(4661));
2280 }
2281
2282 #[test]
2283 fn test_paris_block_and_total_difficulty() {
2284 let genesis = Genesis { gas_limit: 0x2fefd8u64, ..Default::default() };
2285 let paris_chainspec = ChainSpecBuilder::default()
2286 .chain(Chain::from_id(1337))
2287 .genesis(genesis)
2288 .paris_activated()
2289 .build();
2290 assert_eq!(paris_chainspec.paris_block_and_final_difficulty, Some((0, U256::ZERO)));
2291 }
2292
2293 #[test]
2294 fn test_default_cancun_header_forkhash() {
2295 let genesis = Genesis { gas_limit: 0x2fefd8u64, ..Default::default() };
2297 let default_chainspec = ChainSpecBuilder::default()
2298 .chain(Chain::from_id(1337))
2299 .genesis(genesis)
2300 .cancun_activated()
2301 .build();
2302 let mut header = default_chainspec.genesis_header().clone();
2303
2304 header.state_root =
2306 B256::from_str("0x62e2595e017f0ca23e08d17221010721a71c3ae932f4ea3cb12117786bb392d4")
2307 .unwrap();
2308
2309 assert_eq!(header.withdrawals_root, Some(EMPTY_WITHDRAWALS));
2311
2312 assert_eq!(header.parent_beacon_block_root, Some(B256::ZERO));
2315 assert_eq!(header.blob_gas_used, Some(0));
2316 assert_eq!(header.excess_blob_gas, Some(0));
2317
2318 let genesis_hash = header.hash_slow();
2320 let expected_hash =
2321 b256!("0x16bb7c59613a5bad3f7c04a852fd056545ade2483968d9a25a1abb05af0c4d37");
2322 assert_eq!(genesis_hash, expected_hash);
2323
2324 let expected_forkhash = ForkHash(hex!("8062457a"));
2326 assert_eq!(ForkHash::from(genesis_hash), expected_forkhash);
2327 }
2328
2329 #[test]
2330 fn holesky_paris_activated_at_genesis() {
2331 assert!(HOLESKY
2332 .fork(EthereumHardfork::Paris)
2333 .active_at_ttd(HOLESKY.genesis.difficulty, HOLESKY.genesis.difficulty));
2334 }
2335
2336 #[test]
2337 fn test_genesis_format_deserialization() {
2338 let config = ChainConfig {
2340 chain_id: 2600,
2341 homestead_block: Some(0),
2342 eip150_block: Some(0),
2343 eip155_block: Some(0),
2344 eip158_block: Some(0),
2345 byzantium_block: Some(0),
2346 constantinople_block: Some(0),
2347 petersburg_block: Some(0),
2348 istanbul_block: Some(0),
2349 berlin_block: Some(0),
2350 london_block: Some(0),
2351 shanghai_time: Some(0),
2352 terminal_total_difficulty: Some(U256::ZERO),
2353 terminal_total_difficulty_passed: true,
2354 ..Default::default()
2355 };
2356 let genesis = Genesis {
2358 config,
2359 nonce: 0,
2360 timestamp: 1698688670,
2361 gas_limit: 5000,
2362 difficulty: U256::ZERO,
2363 mix_hash: B256::ZERO,
2364 coinbase: Address::ZERO,
2365 ..Default::default()
2366 };
2367
2368 let address = hex!("0x6Be02d1d3665660d22FF9624b7BE0551ee1Ac91b").into();
2370 let account = GenesisAccount::default().with_balance(U256::from(33));
2371 let genesis = genesis.extend_accounts(HashMap::from([(address, account)]));
2372
2373 let serialized_genesis = serde_json::to_string(&genesis).unwrap();
2375 let deserialized_genesis: Genesis = serde_json::from_str(&serialized_genesis).unwrap();
2376
2377 assert_eq!(genesis, deserialized_genesis);
2378 }
2379
2380 #[test]
2381 fn check_fork_id_chainspec_with_fork_condition_never() {
2382 let spec = ChainSpec {
2383 chain: Chain::mainnet(),
2384 genesis: Genesis::default(),
2385 hardforks: ChainHardforks::new(vec![(
2386 EthereumHardfork::Frontier.boxed(),
2387 ForkCondition::Never,
2388 )]),
2389 paris_block_and_final_difficulty: None,
2390 deposit_contract: None,
2391 ..Default::default()
2392 };
2393
2394 assert_eq!(spec.hardfork_fork_id(EthereumHardfork::Frontier), None);
2395 }
2396
2397 #[test]
2398 fn check_fork_filter_chainspec_with_fork_condition_never() {
2399 let spec = ChainSpec {
2400 chain: Chain::mainnet(),
2401 genesis: Genesis::default(),
2402 hardforks: ChainHardforks::new(vec![(
2403 EthereumHardfork::Shanghai.boxed(),
2404 ForkCondition::Never,
2405 )]),
2406 paris_block_and_final_difficulty: None,
2407 deposit_contract: None,
2408 ..Default::default()
2409 };
2410
2411 assert_eq!(spec.hardfork_fork_filter(EthereumHardfork::Shanghai), None);
2412 }
2413
2414 #[test]
2415 fn latest_eth_mainnet_fork_id() {
2416 assert_eq!(
2417 ForkId { hash: ForkHash([0xc3, 0x76, 0xcf, 0x8b]), next: 0 },
2418 MAINNET.latest_fork_id()
2419 )
2420 }
2421
2422 #[test]
2423 fn test_fork_order_ethereum_mainnet() {
2424 let genesis = Genesis {
2425 config: ChainConfig {
2426 chain_id: 0,
2427 homestead_block: Some(0),
2428 dao_fork_block: Some(0),
2429 dao_fork_support: false,
2430 eip150_block: Some(0),
2431 eip155_block: Some(0),
2432 eip158_block: Some(0),
2433 byzantium_block: Some(0),
2434 constantinople_block: Some(0),
2435 petersburg_block: Some(0),
2436 istanbul_block: Some(0),
2437 muir_glacier_block: Some(0),
2438 berlin_block: Some(0),
2439 london_block: Some(0),
2440 arrow_glacier_block: Some(0),
2441 gray_glacier_block: Some(0),
2442 merge_netsplit_block: Some(0),
2443 shanghai_time: Some(0),
2444 cancun_time: Some(0),
2445 terminal_total_difficulty: Some(U256::ZERO),
2446 ..Default::default()
2447 },
2448 ..Default::default()
2449 };
2450
2451 let chain_spec: ChainSpec = genesis.into();
2452
2453 let hardforks: Vec<_> = chain_spec.hardforks.forks_iter().map(|(h, _)| h).collect();
2454 let expected_hardforks = vec![
2455 EthereumHardfork::Frontier.boxed(),
2456 EthereumHardfork::Homestead.boxed(),
2457 EthereumHardfork::Dao.boxed(),
2458 EthereumHardfork::Tangerine.boxed(),
2459 EthereumHardfork::SpuriousDragon.boxed(),
2460 EthereumHardfork::Byzantium.boxed(),
2461 EthereumHardfork::Constantinople.boxed(),
2462 EthereumHardfork::Petersburg.boxed(),
2463 EthereumHardfork::Istanbul.boxed(),
2464 EthereumHardfork::MuirGlacier.boxed(),
2465 EthereumHardfork::Berlin.boxed(),
2466 EthereumHardfork::London.boxed(),
2467 EthereumHardfork::ArrowGlacier.boxed(),
2468 EthereumHardfork::GrayGlacier.boxed(),
2469 EthereumHardfork::Paris.boxed(),
2470 EthereumHardfork::Shanghai.boxed(),
2471 EthereumHardfork::Cancun.boxed(),
2472 ];
2473
2474 assert!(expected_hardforks
2475 .iter()
2476 .zip(hardforks.iter())
2477 .all(|(expected, actual)| &**expected == *actual));
2478 assert_eq!(expected_hardforks.len(), hardforks.len());
2479 }
2480
2481 #[test]
2482 fn test_calc_base_block_reward() {
2483 let cases = [
2485 ((0, U256::ZERO), Some(ETH_TO_WEI * 5)),
2487 ((4370000, U256::ZERO), Some(ETH_TO_WEI * 3)),
2489 ((7280000, U256::ZERO), Some(ETH_TO_WEI * 2)),
2491 ((15537394, U256::from(58_750_000_000_000_000_000_000_u128)), None),
2493 ];
2494
2495 for ((block_number, _td), expected_reward) in cases {
2496 assert_eq!(base_block_reward(&*MAINNET, block_number), expected_reward);
2497 }
2498 }
2499
2500 #[test]
2501 fn test_calc_full_block_reward() {
2502 let base_reward = ETH_TO_WEI;
2503 let one_thirty_twoth_reward = base_reward >> 5;
2504
2505 let cases = [
2507 (0, base_reward),
2508 (1, base_reward + one_thirty_twoth_reward),
2509 (2, base_reward + one_thirty_twoth_reward * 2),
2510 ];
2511
2512 for (num_ommers, expected_reward) in cases {
2513 assert_eq!(block_reward(base_reward, num_ommers), expected_reward);
2514 }
2515 }
2516
2517 #[test]
2518 fn blob_params_from_genesis() {
2519 let s = r#"{
2520 "cancun":{
2521 "baseFeeUpdateFraction":3338477,
2522 "max":6,
2523 "target":3
2524 },
2525 "prague":{
2526 "baseFeeUpdateFraction":3338477,
2527 "max":6,
2528 "target":3
2529 }
2530 }"#;
2531 let schedule: BTreeMap<String, BlobParams> = serde_json::from_str(s).unwrap();
2532 let hardfork_params = BlobScheduleBlobParams::from_schedule(&schedule);
2533 let expected = BlobScheduleBlobParams {
2534 cancun: BlobParams {
2535 target_blob_count: 3,
2536 max_blob_count: 6,
2537 update_fraction: 3338477,
2538 min_blob_fee: BLOB_TX_MIN_BLOB_GASPRICE,
2539 },
2540 prague: BlobParams {
2541 target_blob_count: 3,
2542 max_blob_count: 6,
2543 update_fraction: 3338477,
2544 min_blob_fee: BLOB_TX_MIN_BLOB_GASPRICE,
2545 },
2546 ..Default::default()
2547 };
2548 assert_eq!(hardfork_params, expected);
2549 }
2550}