11use alloy_rpc_types:: BlockNumberOrTag ;
2+ use eyre:: bail;
23use foundry_evm:: revm:: primitives:: SpecId ;
34use std:: str:: FromStr ;
45
6+ #[ derive( Clone , Copy , Debug , PartialEq , Eq , PartialOrd , Ord , Hash ) ]
7+ pub enum ChainHardfork {
8+ Ethereum ( EthereumHardfork ) ,
9+ Optimism ( OptimismHardfork ) ,
10+ }
11+
12+ impl From < EthereumHardfork > for ChainHardfork {
13+ fn from ( value : EthereumHardfork ) -> Self {
14+ Self :: Ethereum ( value)
15+ }
16+ }
17+
18+ impl From < OptimismHardfork > for ChainHardfork {
19+ fn from ( value : OptimismHardfork ) -> Self {
20+ Self :: Optimism ( value)
21+ }
22+ }
23+
24+ impl From < ChainHardfork > for SpecId {
25+ fn from ( fork : ChainHardfork ) -> Self {
26+ match fork {
27+ ChainHardfork :: Ethereum ( hardfork) => hardfork. into ( ) ,
28+ ChainHardfork :: Optimism ( hardfork) => hardfork. into ( ) ,
29+ }
30+ }
31+ }
32+
533#[ derive( Clone , Copy , Debug , Default , PartialEq , Eq , PartialOrd , Ord , Hash ) ]
6- pub enum Hardfork {
34+ pub enum EthereumHardfork {
735 Frontier ,
836 Homestead ,
937 Dao ,
@@ -27,7 +55,7 @@ pub enum Hardfork {
2755 Latest ,
2856}
2957
30- impl Hardfork {
58+ impl EthereumHardfork {
3159 /// Get the first block number of the hardfork.
3260 pub fn fork_block ( & self ) -> u64 {
3361 match * self {
@@ -53,8 +81,8 @@ impl Hardfork {
5381 }
5482}
5583
56- impl FromStr for Hardfork {
57- type Err = String ;
84+ impl FromStr for EthereumHardfork {
85+ type Err = eyre :: Report ;
5886
5987 fn from_str ( s : & str ) -> Result < Self , Self :: Err > {
6088 let s = s. to_lowercase ( ) ;
@@ -79,40 +107,40 @@ impl FromStr for Hardfork {
79107 "prague" | "18" => Self :: Prague ,
80108 "pragueeof" | "19" | "prague-eof" => Self :: PragueEOF ,
81109 "latest" => Self :: Latest ,
82- _ => return Err ( format ! ( "Unknown hardfork {s}" ) ) ,
110+ _ => bail ! ( "Unknown hardfork {s}" ) ,
83111 } ;
84112 Ok ( hardfork)
85113 }
86114}
87115
88- impl From < Hardfork > for SpecId {
89- fn from ( fork : Hardfork ) -> Self {
116+ impl From < EthereumHardfork > for SpecId {
117+ fn from ( fork : EthereumHardfork ) -> Self {
90118 match fork {
91- Hardfork :: Frontier => Self :: FRONTIER ,
92- Hardfork :: Homestead => Self :: HOMESTEAD ,
93- Hardfork :: Dao => Self :: HOMESTEAD ,
94- Hardfork :: Tangerine => Self :: TANGERINE ,
95- Hardfork :: SpuriousDragon => Self :: SPURIOUS_DRAGON ,
96- Hardfork :: Byzantium => Self :: BYZANTIUM ,
97- Hardfork :: Constantinople => Self :: CONSTANTINOPLE ,
98- Hardfork :: Petersburg => Self :: PETERSBURG ,
99- Hardfork :: Istanbul => Self :: ISTANBUL ,
100- Hardfork :: Muirglacier => Self :: MUIR_GLACIER ,
101- Hardfork :: Berlin => Self :: BERLIN ,
102- Hardfork :: London => Self :: LONDON ,
103- Hardfork :: ArrowGlacier => Self :: LONDON ,
104- Hardfork :: GrayGlacier => Self :: GRAY_GLACIER ,
105- Hardfork :: Paris => Self :: MERGE ,
106- Hardfork :: Shanghai => Self :: SHANGHAI ,
107- Hardfork :: Cancun | Hardfork :: Latest => Self :: CANCUN ,
108- Hardfork :: Prague => Self :: PRAGUE ,
119+ EthereumHardfork :: Frontier => Self :: FRONTIER ,
120+ EthereumHardfork :: Homestead => Self :: HOMESTEAD ,
121+ EthereumHardfork :: Dao => Self :: HOMESTEAD ,
122+ EthereumHardfork :: Tangerine => Self :: TANGERINE ,
123+ EthereumHardfork :: SpuriousDragon => Self :: SPURIOUS_DRAGON ,
124+ EthereumHardfork :: Byzantium => Self :: BYZANTIUM ,
125+ EthereumHardfork :: Constantinople => Self :: CONSTANTINOPLE ,
126+ EthereumHardfork :: Petersburg => Self :: PETERSBURG ,
127+ EthereumHardfork :: Istanbul => Self :: ISTANBUL ,
128+ EthereumHardfork :: Muirglacier => Self :: MUIR_GLACIER ,
129+ EthereumHardfork :: Berlin => Self :: BERLIN ,
130+ EthereumHardfork :: London => Self :: LONDON ,
131+ EthereumHardfork :: ArrowGlacier => Self :: LONDON ,
132+ EthereumHardfork :: GrayGlacier => Self :: GRAY_GLACIER ,
133+ EthereumHardfork :: Paris => Self :: MERGE ,
134+ EthereumHardfork :: Shanghai => Self :: SHANGHAI ,
135+ EthereumHardfork :: Cancun | EthereumHardfork :: Latest => Self :: CANCUN ,
136+ EthereumHardfork :: Prague => Self :: PRAGUE ,
109137 // TODO: switch to latest after activation
110- Hardfork :: PragueEOF => Self :: PRAGUE_EOF ,
138+ EthereumHardfork :: PragueEOF => Self :: PRAGUE_EOF ,
111139 }
112140 }
113141}
114142
115- impl < T : Into < BlockNumberOrTag > > From < T > for Hardfork {
143+ impl < T : Into < BlockNumberOrTag > > From < T > for EthereumHardfork {
116144 fn from ( block : T ) -> Self {
117145 let num = match block. into ( ) {
118146 BlockNumberOrTag :: Earliest => 0 ,
@@ -140,19 +168,64 @@ impl<T: Into<BlockNumberOrTag>> From<T> for Hardfork {
140168 }
141169}
142170
171+ #[ derive( Clone , Copy , Debug , Default , PartialEq , Eq , PartialOrd , Ord , Hash ) ]
172+ pub enum OptimismHardfork {
173+ Bedrock ,
174+ Regolith ,
175+ Canyon ,
176+ Ecotone ,
177+ Fjord ,
178+ Granite ,
179+ #[ default]
180+ Latest ,
181+ }
182+
183+ impl FromStr for OptimismHardfork {
184+ type Err = eyre:: Report ;
185+
186+ fn from_str ( s : & str ) -> Result < Self , Self :: Err > {
187+ let s = s. to_lowercase ( ) ;
188+ let hardfork = match s. as_str ( ) {
189+ "bedrock" => Self :: Bedrock ,
190+ "regolith" => Self :: Regolith ,
191+ "canyon" => Self :: Canyon ,
192+ "ecotone" => Self :: Ecotone ,
193+ "fjord" => Self :: Fjord ,
194+ "granite" => Self :: Granite ,
195+ "latest" => Self :: Latest ,
196+ _ => bail ! ( "Unknown hardfork {s}" ) ,
197+ } ;
198+ Ok ( hardfork)
199+ }
200+ }
201+
202+ impl From < OptimismHardfork > for SpecId {
203+ fn from ( fork : OptimismHardfork ) -> Self {
204+ match fork {
205+ OptimismHardfork :: Bedrock => Self :: BEDROCK ,
206+ OptimismHardfork :: Regolith => Self :: REGOLITH ,
207+ OptimismHardfork :: Canyon => Self :: CANYON ,
208+ OptimismHardfork :: Ecotone => Self :: ECOTONE ,
209+ OptimismHardfork :: Fjord => Self :: FJORD ,
210+ OptimismHardfork :: Granite => Self :: GRANITE ,
211+ OptimismHardfork :: Latest => Self :: LATEST ,
212+ }
213+ }
214+ }
215+
143216#[ cfg( test) ]
144217mod tests {
145- use crate :: Hardfork ;
218+ use crate :: EthereumHardfork ;
146219
147220 #[ test]
148221 fn test_hardfork_blocks ( ) {
149- let hf: Hardfork = 12_965_000u64 . into ( ) ;
150- assert_eq ! ( hf, Hardfork :: London ) ;
222+ let hf: EthereumHardfork = 12_965_000u64 . into ( ) ;
223+ assert_eq ! ( hf, EthereumHardfork :: London ) ;
151224
152- let hf: Hardfork = 4370000u64 . into ( ) ;
153- assert_eq ! ( hf, Hardfork :: Byzantium ) ;
225+ let hf: EthereumHardfork = 4370000u64 . into ( ) ;
226+ assert_eq ! ( hf, EthereumHardfork :: Byzantium ) ;
154227
155- let hf: Hardfork = 12244000u64 . into ( ) ;
156- assert_eq ! ( hf, Hardfork :: Berlin ) ;
228+ let hf: EthereumHardfork = 12244000u64 . into ( ) ;
229+ assert_eq ! ( hf, EthereumHardfork :: Berlin ) ;
157230 }
158231}
0 commit comments