reth_chainspec/
api.rs

1use crate::{ChainSpec, DepositContract};
2use alloc::{boxed::Box, vec::Vec};
3use alloy_chains::Chain;
4use alloy_consensus::Header;
5use alloy_eips::{eip1559::BaseFeeParams, eip7840::BlobParams};
6use alloy_genesis::Genesis;
7use alloy_primitives::{B256, U256};
8use core::fmt::{Debug, Display};
9use reth_ethereum_forks::EthereumHardforks;
10use reth_network_peers::NodeRecord;
11
12/// Trait representing type configuring a chain spec.
13#[auto_impl::auto_impl(&, Arc)]
14pub trait EthChainSpec: Send + Sync + Unpin + Debug {
15    /// The header type of the network.
16    type Header;
17
18    /// Returns the [`Chain`] object this spec targets.
19    fn chain(&self) -> Chain;
20
21    /// Returns the chain id number
22    fn chain_id(&self) -> u64 {
23        self.chain().id()
24    }
25
26    /// Get the [`BaseFeeParams`] for the chain at the given block.
27    fn base_fee_params_at_block(&self, block_number: u64) -> BaseFeeParams;
28
29    /// Get the [`BaseFeeParams`] for the chain at the given timestamp.
30    fn base_fee_params_at_timestamp(&self, timestamp: u64) -> BaseFeeParams;
31
32    /// Get the [`BlobParams`] for the given timestamp
33    fn blob_params_at_timestamp(&self, timestamp: u64) -> Option<BlobParams>;
34
35    /// Returns the deposit contract data for the chain, if it's present
36    fn deposit_contract(&self) -> Option<&DepositContract>;
37
38    /// The genesis hash.
39    fn genesis_hash(&self) -> B256;
40
41    /// The delete limit for pruner, per run.
42    fn prune_delete_limit(&self) -> usize;
43
44    /// Returns a string representation of the hardforks.
45    fn display_hardforks(&self) -> Box<dyn Display>;
46
47    /// The genesis header.
48    fn genesis_header(&self) -> &Self::Header;
49
50    /// The genesis block specification.
51    fn genesis(&self) -> &Genesis;
52
53    /// The bootnodes for the chain, if any.
54    fn bootnodes(&self) -> Option<Vec<NodeRecord>>;
55
56    /// Returns `true` if this chain contains Optimism configuration.
57    fn is_optimism(&self) -> bool {
58        self.chain().is_optimism()
59    }
60
61    /// Returns `true` if this chain contains Ethereum configuration.
62    fn is_ethereum(&self) -> bool {
63        self.chain().is_ethereum()
64    }
65
66    /// Returns the final total difficulty if the Paris hardfork is known.
67    fn final_paris_total_difficulty(&self) -> Option<U256>;
68}
69
70impl EthChainSpec for ChainSpec {
71    type Header = Header;
72
73    fn chain(&self) -> Chain {
74        self.chain
75    }
76
77    fn base_fee_params_at_block(&self, block_number: u64) -> BaseFeeParams {
78        self.base_fee_params_at_block(block_number)
79    }
80
81    fn base_fee_params_at_timestamp(&self, timestamp: u64) -> BaseFeeParams {
82        self.base_fee_params_at_timestamp(timestamp)
83    }
84
85    fn blob_params_at_timestamp(&self, timestamp: u64) -> Option<BlobParams> {
86        if self.is_prague_active_at_timestamp(timestamp) {
87            Some(self.blob_params.prague)
88        } else if self.is_cancun_active_at_timestamp(timestamp) {
89            Some(self.blob_params.cancun)
90        } else {
91            None
92        }
93    }
94
95    fn deposit_contract(&self) -> Option<&DepositContract> {
96        self.deposit_contract.as_ref()
97    }
98
99    fn genesis_hash(&self) -> B256 {
100        self.genesis_hash()
101    }
102
103    fn prune_delete_limit(&self) -> usize {
104        self.prune_delete_limit
105    }
106
107    fn display_hardforks(&self) -> Box<dyn Display> {
108        Box::new(Self::display_hardforks(self))
109    }
110
111    fn genesis_header(&self) -> &Self::Header {
112        self.genesis_header()
113    }
114
115    fn genesis(&self) -> &Genesis {
116        self.genesis()
117    }
118
119    fn bootnodes(&self) -> Option<Vec<NodeRecord>> {
120        self.bootnodes()
121    }
122
123    fn is_optimism(&self) -> bool {
124        false
125    }
126
127    fn final_paris_total_difficulty(&self) -> Option<U256> {
128        self.paris_block_and_final_difficulty.map(|(_, final_difficulty)| final_difficulty)
129    }
130}