Remove state dependency from core module in consensus/types (#8653)

#8652


  - This removes instances of `BeaconStateError` from `eth_spec.rs`, and replaces them directly with `ArithError` which can be trivially converted back to `BeaconStateError` at the call site.
- Also moves the state related methods on `ChainSpec` to be methods on `BeaconState` instead. I think this might be a more natural place for them to exist anyway.


Co-Authored-By: Mac L <mjladson@pm.me>
This commit is contained in:
Mac L
2026-01-15 06:16:40 +04:00
committed by GitHub
parent f584521e85
commit 605ef8e8e6
9 changed files with 53 additions and 66 deletions

View File

@@ -19,7 +19,6 @@ use crate::{
data::{BlobIdentifier, DataColumnSubnetId, DataColumnsByRootIdentifier},
execution::ExecutionBlockHash,
fork::{Fork, ForkData, ForkName},
state::BeaconState,
};
/// Each of the BLS signature domains.
@@ -418,51 +417,6 @@ impl ChainSpec {
}
}
/// For a given `BeaconState`, return the proportional slashing multiplier associated with its variant.
pub fn proportional_slashing_multiplier_for_state<E: EthSpec>(
&self,
state: &BeaconState<E>,
) -> u64 {
let fork_name = state.fork_name_unchecked();
if fork_name >= ForkName::Bellatrix {
self.proportional_slashing_multiplier_bellatrix
} else if fork_name >= ForkName::Altair {
self.proportional_slashing_multiplier_altair
} else {
self.proportional_slashing_multiplier
}
}
/// For a given `BeaconState`, return the minimum slashing penalty quotient associated with its variant.
pub fn min_slashing_penalty_quotient_for_state<E: EthSpec>(
&self,
state: &BeaconState<E>,
) -> u64 {
let fork_name = state.fork_name_unchecked();
if fork_name.electra_enabled() {
self.min_slashing_penalty_quotient_electra
} else if fork_name >= ForkName::Bellatrix {
self.min_slashing_penalty_quotient_bellatrix
} else if fork_name >= ForkName::Altair {
self.min_slashing_penalty_quotient_altair
} else {
self.min_slashing_penalty_quotient
}
}
/// For a given `BeaconState`, return the whistleblower reward quotient associated with its variant.
pub fn whistleblower_reward_quotient_for_state<E: EthSpec>(
&self,
state: &BeaconState<E>,
) -> u64 {
let fork_name = state.fork_name_unchecked();
if fork_name.electra_enabled() {
self.whistleblower_reward_quotient_electra
} else {
self.whistleblower_reward_quotient
}
}
pub fn max_effective_balance_for_fork(&self, fork_name: ForkName) -> u64 {
if fork_name.electra_enabled() {
self.max_effective_balance_electra

View File

@@ -3,7 +3,7 @@ use std::{
str::FromStr,
};
use safe_arith::SafeArith;
use safe_arith::{ArithError, SafeArith};
use serde::{Deserialize, Serialize};
use typenum::{
U0, U1, U2, U4, U8, U16, U17, U32, U64, U128, U256, U512, U625, U1024, U2048, U4096, U8192,
@@ -11,10 +11,7 @@ use typenum::{
U1099511627776, UInt, Unsigned, bit::B0,
};
use crate::{
core::{ChainSpec, Epoch},
state::BeaconStateError,
};
use crate::core::{ChainSpec, Epoch};
type U5000 = UInt<UInt<UInt<U625, B0>, B0>, B0>; // 625 * 8 = 5000
@@ -196,7 +193,7 @@ pub trait EthSpec: 'static + Default + Sync + Send + Clone + Debug + PartialEq +
fn get_committee_count_per_slot(
active_validator_count: usize,
spec: &ChainSpec,
) -> Result<usize, BeaconStateError> {
) -> Result<usize, ArithError> {
Self::get_committee_count_per_slot_with(
active_validator_count,
spec.max_committees_per_slot,
@@ -208,7 +205,7 @@ pub trait EthSpec: 'static + Default + Sync + Send + Clone + Debug + PartialEq +
active_validator_count: usize,
max_committees_per_slot: usize,
target_committee_size: usize,
) -> Result<usize, BeaconStateError> {
) -> Result<usize, ArithError> {
let slots_per_epoch = Self::SlotsPerEpoch::to_usize();
Ok(std::cmp::max(

View File

@@ -2528,6 +2528,42 @@ impl<E: EthSpec> BeaconState<E> {
self.epoch_cache().get_base_reward(validator_index)
}
/// Get the proportional slashing multiplier for the current fork.
pub fn get_proportional_slashing_multiplier(&self, spec: &ChainSpec) -> u64 {
let fork_name = self.fork_name_unchecked();
if fork_name >= ForkName::Bellatrix {
spec.proportional_slashing_multiplier_bellatrix
} else if fork_name >= ForkName::Altair {
spec.proportional_slashing_multiplier_altair
} else {
spec.proportional_slashing_multiplier
}
}
/// Get the minimum slashing penalty quotient for the current fork.
pub fn get_min_slashing_penalty_quotient(&self, spec: &ChainSpec) -> u64 {
let fork_name = self.fork_name_unchecked();
if fork_name.electra_enabled() {
spec.min_slashing_penalty_quotient_electra
} else if fork_name >= ForkName::Bellatrix {
spec.min_slashing_penalty_quotient_bellatrix
} else if fork_name >= ForkName::Altair {
spec.min_slashing_penalty_quotient_altair
} else {
spec.min_slashing_penalty_quotient
}
}
/// Get the whistleblower reward quotient for the current fork.
pub fn get_whistleblower_reward_quotient(&self, spec: &ChainSpec) -> u64 {
let fork_name = self.fork_name_unchecked();
if fork_name.electra_enabled() {
spec.whistleblower_reward_quotient_electra
} else {
spec.whistleblower_reward_quotient
}
}
// ******* Electra accessors *******
/// Return the churn limit for the current epoch.

View File

@@ -100,7 +100,8 @@ impl CommitteeCache {
}
let committees_per_slot =
E::get_committee_count_per_slot(active_validator_indices.len(), spec)? as u64;
E::get_committee_count_per_slot(active_validator_indices.len(), spec)
.map_err(BeaconStateError::ArithError)? as u64;
let seed = state.get_seed(epoch, Domain::BeaconAttester, spec)?;