Skip to content

Commit

Permalink
Validate the ChainConfig struct at construction time (#1845)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
tyshko5 committed Sep 7, 2022
1 parent 5db55e1 commit aff261b
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ height_infos = [
{ height = "Calico", epoch = -1 },
{ height = "Persian", epoch = -1 },
{ height = "Orange", epoch = -1 },
{ height = "Claus", epoch = -1 },
{ height = "Trust", epoch = -1 },
{ height = "Norwegian", epoch = -1 },
{ height = "Turbo", epoch = -1 },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ height_infos = [
{ height = "Calico", epoch = -1 },
{ height = "Persian", epoch = -1 },
{ height = "Orange", epoch = -1 },
{ height = "Claus", epoch = -1 },
{ height = "Trust", epoch = -1 },
{ height = "Norwegian", epoch = -1 },
{ height = "Turbo", epoch = -1 },
Expand Down
6 changes: 1 addition & 5 deletions types/networks/src/calibnet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub const DEFAULT_BOOTSTRAP: &[&str] = &[
];

/// Height epochs.
pub const HEIGHT_INFOS: [HeightInfo; 18] = [
pub const HEIGHT_INFOS: [HeightInfo; 17] = [
HeightInfo {
height: Height::Breeze,
epoch: -1,
Expand Down Expand Up @@ -58,10 +58,6 @@ pub const HEIGHT_INFOS: [HeightInfo; 18] = [
height: Height::Orange,
epoch: 300,
},
HeightInfo {
height: Height::Claus,
epoch: 270,
},
HeightInfo {
height: Height::Trust,
epoch: 330,
Expand Down
120 changes: 36 additions & 84 deletions types/networks/src/lib.rs
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;

Expand All @@ -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,
Expand All @@ -101,7 +33,6 @@ pub enum Height {
Calico,
Persian,
Orange,
Claus,
Trust,
Norwegian,
Turbo,
Expand All @@ -117,6 +48,30 @@ impl Default for Height {
}
}

impl From<Height> for NetworkVersion {
fn from(height: Height) -> NetworkVersion {
match height {
Height::Breeze => NetworkVersion::V1,
Height::Smoke => NetworkVersion::V2,
Height::Ignition => NetworkVersion::V3,
Height::ActorsV2 => NetworkVersion::V4,
Height::Tape => NetworkVersion::V5,
Height::Liftoff => NetworkVersion::V5,
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,
Expand All @@ -131,6 +86,12 @@ pub struct HeightInfo {
pub epoch: ChainEpoch,
}

pub fn sort_by_epoch(height_info_slice: &[HeightInfo]) -> Vec<HeightInfo> {
let mut height_info_vec = height_info_slice.to_vec();
height_info_vec.sort_by(|a, b| a.epoch.cmp(&b.epoch));
height_info_vec
}

#[derive(Clone)]
struct DrandPoint<'a> {
pub height: ChainEpoch,
Expand All @@ -144,7 +105,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")]
Expand All @@ -158,8 +118,7 @@ impl PartialEq for ChainConfig {
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
&& sort_by_epoch(&self.height_infos) == sort_by_epoch(&other.height_infos)
&& (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
Expand Down Expand Up @@ -224,7 +183,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([
Expand All @@ -242,19 +200,14 @@ impl ChainConfig {
}

pub fn network_version(&self, epoch: ChainEpoch) -> NetworkVersion {
let height = self
.height_infos
let height = sort_by_epoch(&self.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).")
From::from(height)
}

pub async fn get_beacon_schedule(
Expand All @@ -279,11 +232,11 @@ impl ChainConfig {
}

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

pub fn genesis_bytes(&self) -> Option<&[u8]> {
Expand All @@ -308,7 +261,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([
Expand Down
6 changes: 1 addition & 5 deletions types/networks/src/mainnet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub const DEFAULT_BOOTSTRAP: &[&str] = &[
];

/// Height epochs.
pub const HEIGHT_INFOS: [HeightInfo; 18] = [
pub const HEIGHT_INFOS: [HeightInfo; 17] = [
HeightInfo {
height: Height::Breeze,
epoch: 41_280,
Expand Down Expand Up @@ -73,10 +73,6 @@ pub const HEIGHT_INFOS: [HeightInfo; 18] = [
height: Height::Orange,
epoch: 336_458,
},
HeightInfo {
height: Height::Claus,
epoch: 343_200,
},
HeightInfo {
height: Height::Trust,
epoch: 550_321,
Expand Down
2 changes: 0 additions & 2 deletions vm/interpreter/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ pub trait LookbackStateGetter {
#[derive(Clone, Copy)]
pub struct Heights {
pub calico: ChainEpoch,
pub claus: ChainEpoch,
pub turbo: ChainEpoch,
pub hyperdrive: ChainEpoch,
pub chocolate: ChainEpoch,
Expand All @@ -80,7 +79,6 @@ impl Heights {
pub fn new(chain_config: &ChainConfig) -> Self {
Heights {
calico: chain_config.epoch(Height::Calico),
claus: chain_config.epoch(Height::Claus),
turbo: chain_config.epoch(Height::Turbo),
hyperdrive: chain_config.epoch(Height::Hyperdrive),
chocolate: chain_config.epoch(Height::Chocolate),
Expand Down

0 comments on commit aff261b

Please sign in to comment.