1#![doc(
4 html_logo_url = "https://raw.githubusercontent.com/paradigmxyz/reth/main/assets/reth-docs.png",
5 html_favicon_url = "https://avatars0.githubusercontent.com/u/97369466?s=256",
6 issue_tracker_base_url = "https://github.com/paradigmxyz/reth/issues/"
7)]
8#![cfg_attr(not(test), warn(unused_crate_dependencies))]
9#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
10#![cfg_attr(not(feature = "std"), no_std)]
11
12extern crate alloc;
13
14mod constants;
16pub use constants::*;
17
18mod api;
19mod info;
21mod spec;
23
24pub use alloy_chains::{Chain, ChainKind, NamedChain};
25pub use reth_ethereum_forks::*;
27
28pub use api::EthChainSpec;
29pub use info::ChainInfo;
30#[cfg(any(test, feature = "test-utils"))]
31pub use spec::test_fork_ids;
32pub use spec::{
33 make_genesis_header, BaseFeeParams, BaseFeeParamsKind, ChainSpec, ChainSpecBuilder,
34 ChainSpecProvider, DepositContract, ForkBaseFeeParams, DEV, HOLESKY, HOODI, MAINNET, SEPOLIA,
35};
36
37use reth_primitives_traits::sync::OnceLock;
38
39pub fn once_cell_set<T>(value: T) -> OnceLock<T> {
41 let once = OnceLock::new();
42 let _ = once.set(value);
43 once
44}
45
46#[cfg(test)]
47mod tests {
48 use super::*;
49 use alloy_primitives::U256;
50 use alloy_rlp::Encodable;
51 use std::str::FromStr;
52
53 #[test]
54 fn test_id() {
55 let chain = Chain::from(1234);
56 assert_eq!(chain.id(), 1234);
57 }
58
59 #[test]
60 fn test_named_id() {
61 let chain = Chain::from_named(NamedChain::Holesky);
62 assert_eq!(chain.id(), 17000);
63 }
64
65 #[test]
66 fn test_display_named_chain() {
67 let chain = Chain::from_named(NamedChain::Mainnet);
68 assert_eq!(format!("{chain}"), "mainnet");
69 }
70
71 #[test]
72 fn test_display_id_chain() {
73 let chain = Chain::from(1234);
74 assert_eq!(format!("{chain}"), "1234");
75 }
76
77 #[test]
78 fn test_from_u256() {
79 let n = U256::from(1234);
80 let chain = Chain::from(n.to::<u64>());
81 let expected = Chain::from(1234);
82
83 assert_eq!(chain, expected);
84 }
85
86 #[test]
87 fn test_into_u256() {
88 let chain = Chain::from_named(NamedChain::Holesky);
89 let n: U256 = U256::from(chain.id());
90 let expected = U256::from(17000);
91
92 assert_eq!(n, expected);
93 }
94
95 #[test]
96 fn test_from_str_named_chain() {
97 let result = Chain::from_str("mainnet");
98 let expected = Chain::from_named(NamedChain::Mainnet);
99
100 assert!(result.is_ok());
101 assert_eq!(result.unwrap(), expected);
102 }
103
104 #[test]
105 fn test_from_str_named_chain_error() {
106 let result = Chain::from_str("chain");
107
108 assert!(result.is_err());
109 }
110
111 #[test]
112 fn test_from_str_id_chain() {
113 let result = Chain::from_str("1234");
114 let expected = Chain::from(1234);
115
116 assert!(result.is_ok());
117 assert_eq!(result.unwrap(), expected);
118 }
119
120 #[test]
121 fn test_default() {
122 let default = Chain::default();
123 let expected = Chain::from_named(NamedChain::Mainnet);
124
125 assert_eq!(default, expected);
126 }
127
128 #[test]
129 fn test_id_chain_encodable_length() {
130 let chain = Chain::from(1234);
131
132 assert_eq!(chain.length(), 3);
133 }
134
135 #[test]
136 fn test_dns_main_network() {
137 let s = "enrtree://AKA3AM6LPBYEUDMVNU3BSVQJ5AD45Y7YPOHJLEF6W26QOE4VTUDPE@all.mainnet.ethdisco.net";
138 let chain: Chain = NamedChain::Mainnet.into();
139 assert_eq!(s, chain.public_dns_network_protocol().unwrap().as_str());
140 }
141
142 #[test]
143 fn test_dns_holesky_network() {
144 let s = "enrtree://AKA3AM6LPBYEUDMVNU3BSVQJ5AD45Y7YPOHJLEF6W26QOE4VTUDPE@all.holesky.ethdisco.net";
145 let chain: Chain = NamedChain::Holesky.into();
146 assert_eq!(s, chain.public_dns_network_protocol().unwrap().as_str());
147 }
148
149 #[test]
150 fn test_centralized_base_fee_calculation() {
151 use crate::{ChainSpec, EthChainSpec};
152 use alloy_consensus::Header;
153 use alloy_eips::eip1559::INITIAL_BASE_FEE;
154
155 fn parent_header() -> Header {
156 Header {
157 gas_used: 15_000_000,
158 gas_limit: 30_000_000,
159 base_fee_per_gas: Some(INITIAL_BASE_FEE),
160 timestamp: 1_000,
161 ..Default::default()
162 }
163 }
164
165 let spec = ChainSpec::default();
166 let parent = parent_header();
167
168 let next_timestamp = parent.timestamp + 12;
170
171 let expected = parent
172 .next_block_base_fee(spec.base_fee_params_at_timestamp(next_timestamp))
173 .unwrap_or_default();
174
175 let got = spec.next_block_base_fee(&parent, next_timestamp).unwrap_or_default();
176 assert_eq!(expected, got, "Base fee calculation does not match expected value");
177 }
178}