Impl Ord on ForkName for ChainSpec usage (#5531)

* Impl Ord on ForkName for ChainSpec usage

* add fork ord consistent test
This commit is contained in:
Lion - dapplion
2024-04-10 18:02:23 +09:00
committed by GitHub
parent d527d124dd
commit ced653873e
2 changed files with 41 additions and 33 deletions

View File

@@ -334,12 +334,12 @@ impl ChainSpec {
} }
pub fn inactivity_penalty_quotient_for_fork(&self, fork_name: ForkName) -> u64 { pub fn inactivity_penalty_quotient_for_fork(&self, fork_name: ForkName) -> u64 {
match fork_name { if fork_name >= ForkName::Merge {
ForkName::Base => self.inactivity_penalty_quotient, self.inactivity_penalty_quotient_bellatrix
ForkName::Altair => self.inactivity_penalty_quotient_altair, } else if fork_name >= ForkName::Altair {
ForkName::Merge => self.inactivity_penalty_quotient_bellatrix, self.inactivity_penalty_quotient_altair
ForkName::Capella => self.inactivity_penalty_quotient_bellatrix, } else {
ForkName::Deneb | ForkName::Electra => self.inactivity_penalty_quotient_bellatrix, self.inactivity_penalty_quotient
} }
} }
@@ -348,13 +348,13 @@ impl ChainSpec {
&self, &self,
state: &BeaconState<E>, state: &BeaconState<E>,
) -> u64 { ) -> u64 {
match state { let fork_name = state.fork_name_unchecked();
BeaconState::Base(_) => self.proportional_slashing_multiplier, if fork_name >= ForkName::Merge {
BeaconState::Altair(_) => self.proportional_slashing_multiplier_altair, self.proportional_slashing_multiplier_bellatrix
BeaconState::Merge(_) => self.proportional_slashing_multiplier_bellatrix, } else if fork_name >= ForkName::Altair {
BeaconState::Capella(_) => self.proportional_slashing_multiplier_bellatrix, self.proportional_slashing_multiplier_altair
BeaconState::Deneb(_) => self.proportional_slashing_multiplier_bellatrix, } else {
BeaconState::Electra(_) => self.proportional_slashing_multiplier_bellatrix, self.proportional_slashing_multiplier
} }
} }
@@ -363,13 +363,13 @@ impl ChainSpec {
&self, &self,
state: &BeaconState<E>, state: &BeaconState<E>,
) -> u64 { ) -> u64 {
match state { let fork_name = state.fork_name_unchecked();
BeaconState::Base(_) => self.min_slashing_penalty_quotient, if fork_name >= ForkName::Merge {
BeaconState::Altair(_) => self.min_slashing_penalty_quotient_altair, self.min_slashing_penalty_quotient_bellatrix
BeaconState::Merge(_) => self.min_slashing_penalty_quotient_bellatrix, } else if fork_name >= ForkName::Altair {
BeaconState::Capella(_) => self.min_slashing_penalty_quotient_bellatrix, self.min_slashing_penalty_quotient_altair
BeaconState::Deneb(_) => self.min_slashing_penalty_quotient_bellatrix, } else {
BeaconState::Electra(_) => self.min_slashing_penalty_quotient_bellatrix, self.min_slashing_penalty_quotient
} }
} }
@@ -531,22 +531,19 @@ impl ChainSpec {
} }
pub fn max_blocks_by_root_request(&self, fork_name: ForkName) -> usize { pub fn max_blocks_by_root_request(&self, fork_name: ForkName) -> usize {
match fork_name { if fork_name >= ForkName::Deneb {
ForkName::Base | ForkName::Altair | ForkName::Merge | ForkName::Capella => { self.max_blocks_by_root_request_deneb
self.max_blocks_by_root_request } else {
} self.max_blocks_by_root_request
ForkName::Deneb | ForkName::Electra => self.max_blocks_by_root_request_deneb,
} }
} }
pub fn max_request_blocks(&self, fork_name: ForkName) -> usize { pub fn max_request_blocks(&self, fork_name: ForkName) -> usize {
let max_request_blocks = match fork_name { if fork_name >= ForkName::Deneb {
ForkName::Base | ForkName::Altair | ForkName::Merge | ForkName::Capella => { self.max_request_blocks_deneb as usize
self.max_request_blocks } else {
} self.max_request_blocks as usize
ForkName::Deneb | ForkName::Electra => self.max_request_blocks_deneb, }
};
max_request_blocks as usize
} }
/// Returns a `ChainSpec` compatible with the Ethereum Foundation specification. /// Returns a `ChainSpec` compatible with the Ethereum Foundation specification.

View File

@@ -4,7 +4,9 @@ use ssz_derive::{Decode, Encode};
use std::fmt::{self, Display, Formatter}; use std::fmt::{self, Display, Formatter};
use std::str::FromStr; use std::str::FromStr;
#[derive(Debug, Clone, Copy, Decode, Encode, PartialEq, Eq, Hash, Serialize, Deserialize)] #[derive(
Debug, Clone, Copy, Decode, Encode, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize,
)]
#[serde(try_from = "String")] #[serde(try_from = "String")]
#[serde(into = "String")] #[serde(into = "String")]
#[ssz(enum_behaviour = "tag")] #[ssz(enum_behaviour = "tag")]
@@ -272,4 +274,13 @@ mod test {
} }
assert_eq!(ForkName::latest(), fork); assert_eq!(ForkName::latest(), fork);
} }
#[test]
fn fork_ord_consistent() {
for (prev_fork, fork) in ForkName::list_all().into_iter().tuple_windows() {
assert_eq!(prev_fork.next_fork(), Some(fork));
assert_eq!(fork.previous_fork(), Some(prev_fork));
assert!(prev_fork < fork);
}
}
} }