Start replacing boilerplate

This commit is contained in:
Mac L
2024-05-01 18:12:38 +10:00
parent 4e3839ec98
commit b68d08c847
9 changed files with 155 additions and 84 deletions

View File

@@ -337,9 +337,10 @@ impl ChainSpec {
}
pub fn inactivity_penalty_quotient_for_fork(&self, fork_name: ForkName) -> u64 {
if fork_name >= ForkName::Bellatrix {
// TODO(superstruct_features) Is this a better pattern?
if fork_name.is_feature_enabled(FeatureName::Bellatrix) {
self.inactivity_penalty_quotient_bellatrix
} else if fork_name >= ForkName::Altair {
} else if fork_name.is_feature_enabled(FeatureName::Altair) {
self.inactivity_penalty_quotient_altair
} else {
self.inactivity_penalty_quotient

View File

@@ -3,7 +3,7 @@
//
// For now, older Forks have a single "super-feature" which contains all features associated with
// that Fork. It may be worth splitting these up at a later time.
#[derive(PartialEq)]
#[derive(PartialEq, Clone, Copy, Debug)]
pub enum FeatureName {
// Altair.
Altair,

View File

@@ -1,5 +1,5 @@
use crate::fork_order::FORK_ORDER;
use crate::{ChainSpec, Epoch};
use crate::{ChainSpec, Epoch, FeatureName};
use serde::{Deserialize, Serialize};
use ssz_derive::{Decode, Encode};
use std::cmp::{Ord, Ordering};
@@ -49,6 +49,20 @@ impl ForkName {
FORK_ORDER[FORK_ORDER.len() - 1].0
}
pub fn list_all_enabled_features(self) -> Vec<FeatureName> {
let mut res = vec![];
for (fork, features) in FORK_ORDER {
if *fork <= self {
res.extend(features.iter());
}
}
res
}
pub fn is_feature_enabled(self, feature: FeatureName) -> bool {
self.list_all_enabled_features().contains(&feature)
}
/// Set the activation slots in the given `ChainSpec` so that the fork named by `self`
/// is the only fork in effect from genesis.
pub fn make_genesis_spec(&self, mut spec: ChainSpec) -> ChainSpec {
@@ -296,4 +310,51 @@ mod test {
assert!(prev_fork < fork);
}
}
#[test]
fn check_fork_name_enabled_features() {
let base = ForkName::Base;
let altair = ForkName::Altair;
let bellatrix = ForkName::Bellatrix;
let capella = ForkName::Capella;
let deneb = ForkName::Deneb;
let electra = ForkName::Electra;
assert_eq!(base.list_all_enabled_features(), vec![]);
assert_eq!(
altair.list_all_enabled_features(),
vec![FeatureName::Altair]
);
assert_eq!(
bellatrix.list_all_enabled_features(),
vec![FeatureName::Altair, FeatureName::Bellatrix]
);
assert_eq!(
capella.list_all_enabled_features(),
vec![
FeatureName::Altair,
FeatureName::Bellatrix,
FeatureName::Capella
]
);
assert_eq!(
deneb.list_all_enabled_features(),
vec![
FeatureName::Altair,
FeatureName::Bellatrix,
FeatureName::Capella,
FeatureName::Deneb
]
);
assert_eq!(
electra.list_all_enabled_features(),
vec![
FeatureName::Altair,
FeatureName::Bellatrix,
FeatureName::Capella,
FeatureName::Deneb,
FeatureName::Electra
]
);
}
}

View File

@@ -1,6 +1,6 @@
use crate::{
test_utils::TestRandom, ChainSpec, Domain, Epoch, ForkName, Hash256, SecretKey, SignedRoot,
SignedVoluntaryExit,
test_utils::TestRandom, ChainSpec, Domain, Epoch, FeatureName, ForkName, Hash256, SecretKey,
SignedRoot, SignedVoluntaryExit,
};
use serde::{Deserialize, Serialize};
@@ -41,12 +41,11 @@ impl VoluntaryExit {
spec: &ChainSpec,
) -> SignedVoluntaryExit {
let fork_name = spec.fork_name_at_epoch(self.epoch);
let fork_version = match fork_name {
ForkName::Base | ForkName::Altair | ForkName::Bellatrix | ForkName::Capella => {
spec.fork_version_for_name(fork_name)
}
// EIP-7044
ForkName::Deneb | ForkName::Electra => spec.fork_version_for_name(ForkName::Capella),
// EIP-7044
let fork_version = if fork_name.is_feature_enabled(FeatureName::Deneb) {
spec.fork_version_for_name(ForkName::Capella)
} else {
spec.fork_version_for_name(fork_name)
};
let domain =
spec.compute_domain(Domain::VoluntaryExit, fork_version, genesis_validators_root);