Skip to content

Commit 80b39b1

Browse files
committed
Validate the ChainConfig struct at construction time (#1845)
* most changes * requested change * fix fmt * new ways: delete claus, version_schedule, sort * fix lint * requested changes * compile time check * from implementation * requested changes * requested changes * requested changes * requested changes * requested changes * delete Claus from deleg cns config
1 parent 695af91 commit 80b39b1

File tree

6 files changed

+38
-97
lines changed

6 files changed

+38
-97
lines changed

blockchain/consensus/deleg_cns/configs/delegator-config.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ height_infos = [
1616
{ height = "Calico", epoch = -1 },
1717
{ height = "Persian", epoch = -1 },
1818
{ height = "Orange", epoch = -1 },
19-
{ height = "Claus", epoch = -1 },
2019
{ height = "Trust", epoch = -1 },
2120
{ height = "Norwegian", epoch = -1 },
2221
{ height = "Turbo", epoch = -1 },

blockchain/consensus/deleg_cns/configs/proposer-config.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ height_infos = [
1616
{ height = "Calico", epoch = -1 },
1717
{ height = "Persian", epoch = -1 },
1818
{ height = "Orange", epoch = -1 },
19-
{ height = "Claus", epoch = -1 },
2019
{ height = "Trust", epoch = -1 },
2120
{ height = "Norwegian", epoch = -1 },
2221
{ height = "Turbo", epoch = -1 },

types/networks/src/calibnet/mod.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub const DEFAULT_BOOTSTRAP: &[&str] = &[
1818
];
1919

2020
/// Height epochs.
21-
pub const HEIGHT_INFOS: [HeightInfo; 18] = [
21+
pub const HEIGHT_INFOS: [HeightInfo; 17] = [
2222
HeightInfo {
2323
height: Height::Breeze,
2424
epoch: -1,
@@ -59,10 +59,6 @@ pub const HEIGHT_INFOS: [HeightInfo; 18] = [
5959
height: Height::Orange,
6060
epoch: 300,
6161
},
62-
HeightInfo {
63-
height: Height::Claus,
64-
epoch: 270,
65-
},
6662
HeightInfo {
6763
height: Height::Trust,
6864
epoch: 330,

types/networks/src/lib.rs

Lines changed: 36 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -18,75 +18,8 @@ mod mainnet;
1818
/// Newest network version for all networks
1919
pub const NEWEST_NETWORK_VERSION: NetworkVersion = NetworkVersion::V16;
2020

21-
const UPGRADE_INFOS: [UpgradeInfo; 16] = [
22-
UpgradeInfo {
23-
height: Height::Breeze,
24-
version: NetworkVersion::V1,
25-
},
26-
UpgradeInfo {
27-
height: Height::Smoke,
28-
version: NetworkVersion::V2,
29-
},
30-
UpgradeInfo {
31-
height: Height::Ignition,
32-
version: NetworkVersion::V3,
33-
},
34-
UpgradeInfo {
35-
height: Height::ActorsV2,
36-
version: NetworkVersion::V4,
37-
},
38-
UpgradeInfo {
39-
height: Height::Tape,
40-
version: NetworkVersion::V5,
41-
},
42-
UpgradeInfo {
43-
height: Height::Kumquat,
44-
version: NetworkVersion::V6,
45-
},
46-
UpgradeInfo {
47-
height: Height::Calico,
48-
version: NetworkVersion::V7,
49-
},
50-
UpgradeInfo {
51-
height: Height::Persian,
52-
version: NetworkVersion::V8,
53-
},
54-
UpgradeInfo {
55-
height: Height::Orange,
56-
version: NetworkVersion::V9,
57-
},
58-
UpgradeInfo {
59-
height: Height::Trust,
60-
version: NetworkVersion::V10,
61-
},
62-
UpgradeInfo {
63-
height: Height::Norwegian,
64-
version: NetworkVersion::V11,
65-
},
66-
UpgradeInfo {
67-
height: Height::Turbo,
68-
version: NetworkVersion::V12,
69-
},
70-
UpgradeInfo {
71-
height: Height::Hyperdrive,
72-
version: NetworkVersion::V13,
73-
},
74-
UpgradeInfo {
75-
height: Height::Chocolate,
76-
version: NetworkVersion::V14,
77-
},
78-
UpgradeInfo {
79-
height: Height::OhSnap,
80-
version: NetworkVersion::V15,
81-
},
82-
UpgradeInfo {
83-
height: Height::Skyr,
84-
version: NetworkVersion::V16,
85-
},
86-
];
87-
8821
/// Defines the meaningful heights of the protocol.
89-
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
22+
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Hash)]
9023
pub enum Height {
9124
Breeze,
9225
Smoke,
@@ -98,7 +31,6 @@ pub enum Height {
9831
Calico,
9932
Persian,
10033
Orange,
101-
Claus,
10234
Trust,
10335
Norwegian,
10436
Turbo,
@@ -114,6 +46,30 @@ impl Default for Height {
11446
}
11547
}
11648

49+
impl From<Height> for NetworkVersion {
50+
fn from(height: Height) -> NetworkVersion {
51+
match height {
52+
Height::Breeze => NetworkVersion::V1,
53+
Height::Smoke => NetworkVersion::V2,
54+
Height::Ignition => NetworkVersion::V3,
55+
Height::ActorsV2 => NetworkVersion::V4,
56+
Height::Tape => NetworkVersion::V5,
57+
Height::Liftoff => NetworkVersion::V5,
58+
Height::Kumquat => NetworkVersion::V6,
59+
Height::Calico => NetworkVersion::V7,
60+
Height::Persian => NetworkVersion::V8,
61+
Height::Orange => NetworkVersion::V9,
62+
Height::Trust => NetworkVersion::V10,
63+
Height::Norwegian => NetworkVersion::V11,
64+
Height::Turbo => NetworkVersion::V12,
65+
Height::Hyperdrive => NetworkVersion::V13,
66+
Height::Chocolate => NetworkVersion::V14,
67+
Height::OhSnap => NetworkVersion::V15,
68+
Height::Skyr => NetworkVersion::V16,
69+
}
70+
}
71+
}
72+
11773
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)]
11874
pub struct UpgradeInfo {
11975
pub height: Height,
@@ -128,6 +84,12 @@ pub struct HeightInfo {
12884
pub epoch: ChainEpoch,
12985
}
13086

87+
pub fn sort_by_epoch(height_info_slice: &[HeightInfo]) -> Vec<HeightInfo> {
88+
let mut height_info_vec = height_info_slice.to_vec();
89+
height_info_vec.sort_by(|a, b| a.epoch.cmp(&b.epoch));
90+
height_info_vec
91+
}
92+
13193
#[derive(Clone)]
13294
struct DrandPoint<'a> {
13395
pub height: ChainEpoch,
@@ -141,7 +103,6 @@ pub struct ChainConfig {
141103
pub name: String,
142104
pub bootstrap_peers: Vec<String>,
143105
pub block_delay_secs: u64,
144-
pub version_schedule: Vec<UpgradeInfo>,
145106
pub height_infos: Vec<HeightInfo>,
146107
#[serde(default = "default_policy")]
147108
#[serde(with = "serde_policy")]
@@ -155,8 +116,7 @@ impl PartialEq for ChainConfig {
155116
self.name == other.name
156117
&& self.bootstrap_peers == other.bootstrap_peers
157118
&& self.block_delay_secs == other.block_delay_secs
158-
&& self.version_schedule == other.version_schedule
159-
&& self.height_infos == other.height_infos
119+
&& sort_by_epoch(&self.height_infos) == sort_by_epoch(&other.height_infos)
160120
&& (self.policy.max_aggregated_sectors == other.policy.max_aggregated_sectors
161121
&& self.policy.min_aggregated_sectors == other.policy.min_aggregated_sectors
162122
&& self.policy.max_aggregated_proof_size == other.policy.max_aggregated_proof_size
@@ -221,7 +181,6 @@ impl ChainConfig {
221181
name: "calibnet".to_string(),
222182
bootstrap_peers: DEFAULT_BOOTSTRAP.iter().map(|x| x.to_string()).collect(),
223183
block_delay_secs: EPOCH_DURATION_SECONDS as u64,
224-
version_schedule: UPGRADE_INFOS.to_vec(),
225184
height_infos: HEIGHT_INFOS.to_vec(),
226185
policy: Policy {
227186
valid_post_proof_type: HashSet::<RegisteredPoStProof>::from([
@@ -239,19 +198,14 @@ impl ChainConfig {
239198
}
240199

241200
pub fn network_version(&self, epoch: ChainEpoch) -> NetworkVersion {
242-
let height = self
243-
.height_infos
201+
let height = sort_by_epoch(&self.height_infos)
244202
.iter()
245203
.rev()
246204
.find(|info| epoch > info.epoch)
247205
.map(|info| info.height)
248206
.unwrap_or(Height::Breeze);
249207

250-
self.version_schedule
251-
.iter()
252-
.find(|info| height == info.height)
253-
.map(|info| info.version)
254-
.expect("A network version should exist even if not specified in the config (a default exists).")
208+
From::from(height)
255209
}
256210

257211
pub async fn get_beacon_schedule(
@@ -276,11 +230,11 @@ impl ChainConfig {
276230
}
277231

278232
pub fn epoch(&self, height: Height) -> ChainEpoch {
279-
self.height_infos
233+
sort_by_epoch(&self.height_infos)
280234
.iter()
281235
.find(|info| height == info.height)
282236
.map(|info| info.epoch)
283-
.expect("Internal error: Protocol height not found in map. Please report to https://github.com/ChainSafe/forest/issues")
237+
.unwrap_or(0)
284238
}
285239

286240
pub fn genesis_bytes(&self) -> Option<&[u8]> {
@@ -305,7 +259,6 @@ impl Default for ChainConfig {
305259
name: "mainnet".to_string(),
306260
bootstrap_peers: DEFAULT_BOOTSTRAP.iter().map(|x| x.to_string()).collect(),
307261
block_delay_secs: EPOCH_DURATION_SECONDS as u64,
308-
version_schedule: UPGRADE_INFOS.to_vec(),
309262
height_infos: HEIGHT_INFOS.to_vec(),
310263
policy: Policy {
311264
valid_post_proof_type: HashSet::<RegisteredPoStProof>::from([

types/networks/src/mainnet/mod.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ pub const DEFAULT_BOOTSTRAP: &[&str] = &[
3333
];
3434

3535
/// Height epochs.
36-
pub const HEIGHT_INFOS: [HeightInfo; 18] = [
36+
pub const HEIGHT_INFOS: [HeightInfo; 17] = [
3737
HeightInfo {
3838
height: Height::Breeze,
3939
epoch: 41_280,
@@ -74,10 +74,6 @@ pub const HEIGHT_INFOS: [HeightInfo; 18] = [
7474
height: Height::Orange,
7575
epoch: 336_458,
7676
},
77-
HeightInfo {
78-
height: Height::Claus,
79-
epoch: 343_200,
80-
},
8177
HeightInfo {
8278
height: Height::Trust,
8379
epoch: 550_321,

vm/interpreter/src/vm.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ pub trait LookbackStateGetter {
7070
#[derive(Clone, Copy)]
7171
pub struct Heights {
7272
pub calico: ChainEpoch,
73-
pub claus: ChainEpoch,
7473
pub turbo: ChainEpoch,
7574
pub hyperdrive: ChainEpoch,
7675
pub chocolate: ChainEpoch,
@@ -80,7 +79,6 @@ impl Heights {
8079
pub fn new(chain_config: &ChainConfig) -> Self {
8180
Heights {
8281
calico: chain_config.epoch(Height::Calico),
83-
claus: chain_config.epoch(Height::Claus),
8482
turbo: chain_config.epoch(Height::Turbo),
8583
hyperdrive: chain_config.epoch(Height::Hyperdrive),
8684
chocolate: chain_config.epoch(Height::Chocolate),

0 commit comments

Comments
 (0)