-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
hardfork.rs
187 lines (171 loc) · 5.23 KB
/
hardfork.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
use serde::{Deserialize, Serialize};
use crate::{ChainSpec, ForkCondition, ForkFilter, ForkId};
use std::{fmt::Display, str::FromStr};
/// The name of an Ethereum hardfork.
#[derive(Debug, Copy, Clone, Eq, PartialEq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[non_exhaustive]
pub enum Hardfork {
/// Frontier.
Frontier,
/// Homestead.
Homestead,
/// The DAO fork.
Dao,
/// Tangerine.
Tangerine,
/// Spurious Dragon.
SpuriousDragon,
/// Byzantium.
Byzantium,
/// Constantinople.
Constantinople,
/// Petersburg.
Petersburg,
/// Istanbul.
Istanbul,
/// Muir Glacier.
MuirGlacier,
/// Berlin.
Berlin,
/// London.
London,
/// Arrow Glacier.
ArrowGlacier,
/// Gray Glacier.
GrayGlacier,
/// Paris.
Paris,
/// Shanghai.
Shanghai,
/// Cancun.
Cancun,
}
impl Hardfork {
/// Get the [ForkId] for this hardfork in the given spec, if the fork is activated at any point.
pub fn fork_id(&self, spec: &ChainSpec) -> Option<ForkId> {
match spec.fork(*self) {
ForkCondition::Never => None,
_ => Some(spec.fork_id(&spec.fork(*self).satisfy())),
}
}
/// Get the [ForkFilter] for this hardfork in the given spec, if the fork is activated at any
/// point.
pub fn fork_filter(&self, spec: &ChainSpec) -> Option<ForkFilter> {
match spec.fork(*self) {
ForkCondition::Never => None,
_ => Some(spec.fork_filter(spec.fork(*self).satisfy())),
}
}
}
impl FromStr for Hardfork {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let s = s.to_lowercase();
let hardfork = match s.as_str() {
"frontier" => Hardfork::Frontier,
"homestead" => Hardfork::Homestead,
"dao" => Hardfork::Dao,
"tangerine" => Hardfork::Tangerine,
"spuriousdragon" => Hardfork::SpuriousDragon,
"byzantium" => Hardfork::Byzantium,
"constantinople" => Hardfork::Constantinople,
"petersburg" => Hardfork::Petersburg,
"istanbul" => Hardfork::Istanbul,
"muirglacier" => Hardfork::MuirGlacier,
"berlin" => Hardfork::Berlin,
"london" => Hardfork::London,
"arrowglacier" => Hardfork::ArrowGlacier,
"grayglacier" => Hardfork::GrayGlacier,
"paris" => Hardfork::Paris,
"shanghai" => Hardfork::Shanghai,
"cancun" => Hardfork::Cancun,
_ => return Err(format!("Unknown hardfork: {s}")),
};
Ok(hardfork)
}
}
impl Display for Hardfork {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{self:?}")
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{Chain, Genesis};
use std::collections::BTreeMap;
#[test]
fn check_hardfork_from_str() {
let hardfork_str = [
"frOntier",
"homEstead",
"dao",
"tAngerIne",
"spurIousdrAgon",
"byzAntium",
"constantinople",
"petersburg",
"istanbul",
"muirglacier",
"bErlin",
"lonDon",
"arrowglacier",
"grayglacier",
"PARIS",
"ShAnGhAI",
"CaNcUn",
];
let expected_hardforks = [
Hardfork::Frontier,
Hardfork::Homestead,
Hardfork::Dao,
Hardfork::Tangerine,
Hardfork::SpuriousDragon,
Hardfork::Byzantium,
Hardfork::Constantinople,
Hardfork::Petersburg,
Hardfork::Istanbul,
Hardfork::MuirGlacier,
Hardfork::Berlin,
Hardfork::London,
Hardfork::ArrowGlacier,
Hardfork::GrayGlacier,
Hardfork::Paris,
Hardfork::Shanghai,
Hardfork::Cancun,
];
let hardforks: Vec<Hardfork> =
hardfork_str.iter().map(|h| Hardfork::from_str(h).unwrap()).collect();
assert_eq!(hardforks, expected_hardforks);
}
#[test]
fn check_nonexistent_hardfork_from_str() {
assert!(Hardfork::from_str("not a hardfork").is_err());
}
#[test]
fn check_fork_id_chainspec_with_fork_condition_never() {
let spec = ChainSpec {
chain: Chain::mainnet(),
genesis: Genesis::default(),
genesis_hash: None,
hardforks: BTreeMap::from([(Hardfork::Frontier, ForkCondition::Never)]),
paris_block_and_final_difficulty: None,
deposit_contract: None,
..Default::default()
};
assert_eq!(Hardfork::Frontier.fork_id(&spec), None);
}
#[test]
fn check_fork_filter_chainspec_with_fork_condition_never() {
let spec = ChainSpec {
chain: Chain::mainnet(),
genesis: Genesis::default(),
genesis_hash: None,
hardforks: BTreeMap::from([(Hardfork::Shanghai, ForkCondition::Never)]),
paris_block_and_final_difficulty: None,
deposit_contract: None,
..Default::default()
};
assert_eq!(Hardfork::Shanghai.fork_filter(&spec), None);
}
}