-
Notifications
You must be signed in to change notification settings - Fork 166
Validate the ChainConfig struct at construction time #1845
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 8 commits
7a7959e
9e9360b
bab321e
9cf2af0
ec53f94
a9bcf06
e40f143
45dd497
00af883
70f458e
c2c0883
fa26316
5c259bb
2de70b6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,6 +1,5 @@ | ||||||
// Copyright 2019-2022 ChainSafe Systems | ||||||
// SPDX-License-Identifier: Apache-2.0, MIT | ||||||
|
||||||
#[macro_use] | ||||||
extern crate lazy_static; | ||||||
|
||||||
|
@@ -21,75 +20,8 @@ mod mainnet; | |||||
/// Newest network version for all networks | ||||||
pub const NEWEST_NETWORK_VERSION: NetworkVersion = NetworkVersion::V16; | ||||||
|
||||||
const UPGRADE_INFOS: [UpgradeInfo; 16] = [ | ||||||
UpgradeInfo { | ||||||
height: Height::Breeze, | ||||||
version: NetworkVersion::V1, | ||||||
}, | ||||||
UpgradeInfo { | ||||||
height: Height::Smoke, | ||||||
version: NetworkVersion::V2, | ||||||
}, | ||||||
UpgradeInfo { | ||||||
height: Height::Ignition, | ||||||
version: NetworkVersion::V3, | ||||||
}, | ||||||
UpgradeInfo { | ||||||
height: Height::ActorsV2, | ||||||
version: NetworkVersion::V4, | ||||||
}, | ||||||
UpgradeInfo { | ||||||
height: Height::Tape, | ||||||
version: NetworkVersion::V5, | ||||||
}, | ||||||
UpgradeInfo { | ||||||
height: Height::Kumquat, | ||||||
version: NetworkVersion::V6, | ||||||
}, | ||||||
UpgradeInfo { | ||||||
height: Height::Calico, | ||||||
version: NetworkVersion::V7, | ||||||
}, | ||||||
UpgradeInfo { | ||||||
height: Height::Persian, | ||||||
version: NetworkVersion::V8, | ||||||
}, | ||||||
UpgradeInfo { | ||||||
height: Height::Orange, | ||||||
version: NetworkVersion::V9, | ||||||
}, | ||||||
UpgradeInfo { | ||||||
height: Height::Trust, | ||||||
version: NetworkVersion::V10, | ||||||
}, | ||||||
UpgradeInfo { | ||||||
height: Height::Norwegian, | ||||||
version: NetworkVersion::V11, | ||||||
}, | ||||||
UpgradeInfo { | ||||||
height: Height::Turbo, | ||||||
version: NetworkVersion::V12, | ||||||
}, | ||||||
UpgradeInfo { | ||||||
height: Height::Hyperdrive, | ||||||
version: NetworkVersion::V13, | ||||||
}, | ||||||
UpgradeInfo { | ||||||
height: Height::Chocolate, | ||||||
version: NetworkVersion::V14, | ||||||
}, | ||||||
UpgradeInfo { | ||||||
height: Height::OhSnap, | ||||||
version: NetworkVersion::V15, | ||||||
}, | ||||||
UpgradeInfo { | ||||||
height: Height::Skyr, | ||||||
version: NetworkVersion::V16, | ||||||
}, | ||||||
]; | ||||||
|
||||||
/// Defines the meaningful heights of the protocol. | ||||||
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)] | ||||||
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Hash)] | ||||||
pub enum Height { | ||||||
Breeze, | ||||||
Smoke, | ||||||
|
@@ -101,7 +33,6 @@ pub enum Height { | |||||
Calico, | ||||||
Persian, | ||||||
Orange, | ||||||
Claus, | ||||||
Trust, | ||||||
Norwegian, | ||||||
Turbo, | ||||||
|
@@ -117,6 +48,30 @@ impl Default for Height { | |||||
} | ||||||
} | ||||||
|
||||||
impl From<Height> for NetworkVersion { | ||||||
fn from(height: Height) -> NetworkVersion { | ||||||
match height { | ||||||
Height::Breeze => NetworkVersion::V0, | ||||||
Height::Smoke => NetworkVersion::V1, | ||||||
Height::Ignition => NetworkVersion::V2, | ||||||
Height::ActorsV2 => NetworkVersion::V3, | ||||||
Height::Tape => NetworkVersion::V4, | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bump everything such that |
||||||
Height::Liftoff => NetworkVersion::V5, | ||||||
lemmih marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
Height::Kumquat => NetworkVersion::V6, | ||||||
Height::Calico => NetworkVersion::V7, | ||||||
Height::Persian => NetworkVersion::V8, | ||||||
Height::Orange => NetworkVersion::V9, | ||||||
Height::Trust => NetworkVersion::V10, | ||||||
Height::Norwegian => NetworkVersion::V11, | ||||||
Height::Turbo => NetworkVersion::V12, | ||||||
Height::Hyperdrive => NetworkVersion::V13, | ||||||
Height::Chocolate => NetworkVersion::V14, | ||||||
Height::OhSnap => NetworkVersion::V15, | ||||||
Height::Skyr => NetworkVersion::V16, | ||||||
} | ||||||
} | ||||||
} | ||||||
|
||||||
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)] | ||||||
pub struct UpgradeInfo { | ||||||
pub height: Height, | ||||||
|
@@ -131,6 +86,11 @@ pub struct HeightInfo { | |||||
pub epoch: ChainEpoch, | ||||||
} | ||||||
|
||||||
pub fn sort_by_epoch(mut height_info_vec: Vec<HeightInfo>) -> Vec<HeightInfo> { | ||||||
height_info_vec.sort_by(|a, b| a.epoch.cmp(&b.epoch)); | ||||||
height_info_vec | ||||||
} | ||||||
|
||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If this function takes a slice of |
||||||
#[derive(Clone)] | ||||||
struct DrandPoint<'a> { | ||||||
pub height: ChainEpoch, | ||||||
|
@@ -144,7 +104,6 @@ pub struct ChainConfig { | |||||
pub name: String, | ||||||
pub bootstrap_peers: Vec<String>, | ||||||
pub block_delay_secs: u64, | ||||||
pub version_schedule: Vec<UpgradeInfo>, | ||||||
pub height_infos: Vec<HeightInfo>, | ||||||
#[serde(default = "default_policy")] | ||||||
#[serde(with = "serde_policy")] | ||||||
|
@@ -155,11 +114,12 @@ pub struct ChainConfig { | |||||
// https://github.com/filecoin-project/builtin-actors/pull/497 | ||||||
impl PartialEq for ChainConfig { | ||||||
fn eq(&self, other: &Self) -> bool { | ||||||
let height_infos = sort_by_epoch(self.height_infos.clone()); | ||||||
let other_height_infos = sort_by_epoch(other.height_infos.clone()); | ||||||
self.name == other.name | ||||||
&& self.bootstrap_peers == other.bootstrap_peers | ||||||
&& self.block_delay_secs == other.block_delay_secs | ||||||
&& self.version_schedule == other.version_schedule | ||||||
&& self.height_infos == other.height_infos | ||||||
&& height_infos == other_height_infos | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
&& (self.policy.max_aggregated_sectors == other.policy.max_aggregated_sectors | ||||||
&& self.policy.min_aggregated_sectors == other.policy.min_aggregated_sectors | ||||||
&& self.policy.max_aggregated_proof_size == other.policy.max_aggregated_proof_size | ||||||
|
@@ -224,7 +184,6 @@ impl ChainConfig { | |||||
name: "calibnet".to_string(), | ||||||
bootstrap_peers: DEFAULT_BOOTSTRAP.iter().map(|x| x.to_string()).collect(), | ||||||
block_delay_secs: EPOCH_DURATION_SECONDS as u64, | ||||||
version_schedule: UPGRADE_INFOS.to_vec(), | ||||||
height_infos: HEIGHT_INFOS.to_vec(), | ||||||
policy: Policy { | ||||||
valid_post_proof_type: HashSet::<RegisteredPoStProof>::from([ | ||||||
|
@@ -242,19 +201,17 @@ impl ChainConfig { | |||||
} | ||||||
|
||||||
pub fn network_version(&self, epoch: ChainEpoch) -> NetworkVersion { | ||||||
let height = self | ||||||
.height_infos | ||||||
let height_infos = sort_by_epoch(self.height_infos.clone()); | ||||||
let height = height_infos | ||||||
.iter() | ||||||
.rev() | ||||||
.find(|info| epoch > info.epoch) | ||||||
.map(|info| info.height) | ||||||
.unwrap_or(Height::Breeze); | ||||||
|
||||||
self.version_schedule | ||||||
.iter() | ||||||
.find(|info| height == info.height) | ||||||
.map(|info| info.version) | ||||||
.expect("A network version should exist even if not specified in the config (a default exists).") | ||||||
// unfallible as it is guaranteed that number of heights | ||||||
lemmih marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
// is equal to the number of network versions | ||||||
TryFrom::try_from(height as u32).unwrap() | ||||||
lemmih marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
} | ||||||
|
||||||
pub async fn get_beacon_schedule( | ||||||
|
@@ -279,7 +236,8 @@ impl ChainConfig { | |||||
} | ||||||
|
||||||
pub fn epoch(&self, height: Height) -> ChainEpoch { | ||||||
lemmih marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
self.height_infos | ||||||
let height_infos = sort_by_epoch(self.height_infos.clone()); | ||||||
height_infos | ||||||
.iter() | ||||||
.find(|info| height == info.height) | ||||||
.map(|info| info.epoch) | ||||||
|
@@ -308,7 +266,6 @@ impl Default for ChainConfig { | |||||
name: "mainnet".to_string(), | ||||||
bootstrap_peers: DEFAULT_BOOTSTRAP.iter().map(|x| x.to_string()).collect(), | ||||||
block_delay_secs: EPOCH_DURATION_SECONDS as u64, | ||||||
version_schedule: UPGRADE_INFOS.to_vec(), | ||||||
height_infos: HEIGHT_INFOS.to_vec(), | ||||||
policy: Policy { | ||||||
valid_post_proof_type: HashSet::<RegisteredPoStProof>::from([ | ||||||
|
Uh oh!
There was an error while loading. Please reload this page.