Use E for EthSpec globally (#5264)

* Use `E` for `EthSpec` globally

* Fix tests

* Merge branch 'unstable' into e-ethspec

* Merge branch 'unstable' into e-ethspec

# Conflicts:
#	beacon_node/execution_layer/src/engine_api.rs
#	beacon_node/execution_layer/src/engine_api/http.rs
#	beacon_node/execution_layer/src/engine_api/json_structures.rs
#	beacon_node/execution_layer/src/test_utils/handle_rpc.rs
#	beacon_node/store/src/partial_beacon_state.rs
#	consensus/types/src/beacon_block.rs
#	consensus/types/src/beacon_block_body.rs
#	consensus/types/src/beacon_state.rs
#	consensus/types/src/config_and_preset.rs
#	consensus/types/src/execution_payload.rs
#	consensus/types/src/execution_payload_header.rs
#	consensus/types/src/light_client_optimistic_update.rs
#	consensus/types/src/payload.rs
#	lcli/src/parse_ssz.rs
This commit is contained in:
Mac L
2024-04-03 02:12:25 +11:00
committed by GitHub
parent f8fdb71f50
commit 969d12dc6f
230 changed files with 2743 additions and 2792 deletions

View File

@@ -22,10 +22,10 @@ pub mod participation_flag_updates;
pub mod rewards_and_penalties;
pub mod sync_committee_updates;
pub fn process_epoch<T: EthSpec>(
state: &mut BeaconState<T>,
pub fn process_epoch<E: EthSpec>(
state: &mut BeaconState<E>,
spec: &ChainSpec,
) -> Result<EpochProcessingSummary<T>, Error> {
) -> Result<EpochProcessingSummary<E>, Error> {
// Ensure the committee caches are built.
state.build_committee_cache(RelativeEpoch::Previous, spec)?;
state.build_committee_cache(RelativeEpoch::Current, spec)?;
@@ -34,7 +34,7 @@ pub fn process_epoch<T: EthSpec>(
// Pre-compute participating indices and total balances.
let participation_cache = ParticipationCache::new(state, spec)?;
let sync_committee = state.current_sync_committee()?.clone();
initialize_progressive_balances_cache::<T>(state, Some(&participation_cache), spec)?;
initialize_progressive_balances_cache::<E>(state, Some(&participation_cache), spec)?;
// Justification and finalization.
let justification_and_finalization_state =

View File

@@ -7,14 +7,14 @@ use types::chain_spec::ChainSpec;
use types::consts::altair::TIMELY_TARGET_FLAG_INDEX;
use types::eth_spec::EthSpec;
pub fn process_inactivity_updates<T: EthSpec>(
state: &mut BeaconState<T>,
pub fn process_inactivity_updates<E: EthSpec>(
state: &mut BeaconState<E>,
participation_cache: &ParticipationCache,
spec: &ChainSpec,
) -> Result<(), EpochProcessingError> {
let previous_epoch = state.previous_epoch();
// Score updates based on previous epoch participation, skip genesis epoch
if state.current_epoch() == T::genesis_epoch() {
if state.current_epoch() == E::genesis_epoch() {
return Ok(());
}

View File

@@ -8,13 +8,13 @@ use types::consts::altair::TIMELY_TARGET_FLAG_INDEX;
use types::{BeaconState, EthSpec};
/// Update the justified and finalized checkpoints for matching target attestations.
pub fn process_justification_and_finalization<T: EthSpec>(
state: &BeaconState<T>,
pub fn process_justification_and_finalization<E: EthSpec>(
state: &BeaconState<E>,
participation_cache: &ParticipationCache,
) -> Result<JustificationAndFinalizationState<T>, Error> {
) -> Result<JustificationAndFinalizationState<E>, Error> {
let justification_and_finalization_state = JustificationAndFinalizationState::new(state);
if state.current_epoch() <= T::genesis_epoch().safe_add(1)? {
if state.current_epoch() <= E::genesis_epoch().safe_add(1)? {
return Ok(justification_and_finalization_state);
}

View File

@@ -50,7 +50,7 @@ struct SingleEpochParticipationCache {
}
impl SingleEpochParticipationCache {
fn new<T: EthSpec>(state: &BeaconState<T>, spec: &ChainSpec) -> Self {
fn new<E: EthSpec>(state: &BeaconState<E>, spec: &ChainSpec) -> Self {
let num_validators = state.validators().len();
let zero_balance = Balance::zero(spec.effective_balance_increment);
@@ -104,10 +104,10 @@ impl SingleEpochParticipationCache {
/// - The provided `state` **must** be Altair. An error will be returned otherwise.
/// - An error will be returned if the `val_index` validator is inactive at the given
/// `relative_epoch`.
fn process_active_validator<T: EthSpec>(
fn process_active_validator<E: EthSpec>(
&mut self,
val_index: usize,
state: &BeaconState<T>,
state: &BeaconState<E>,
current_epoch: Epoch,
relative_epoch: RelativeEpoch,
) -> Result<(), BeaconStateError> {
@@ -173,8 +173,8 @@ impl ParticipationCache {
/// ## Errors
///
/// - The provided `state` **must** be an Altair state. An error will be returned otherwise.
pub fn new<T: EthSpec>(
state: &BeaconState<T>,
pub fn new<E: EthSpec>(
state: &BeaconState<E>,
spec: &ChainSpec,
) -> Result<Self, BeaconStateError> {
let current_epoch = state.current_epoch();

View File

@@ -4,8 +4,8 @@ use types::eth_spec::EthSpec;
use types::participation_flags::ParticipationFlags;
use types::VariableList;
pub fn process_participation_flag_updates<T: EthSpec>(
state: &mut BeaconState<T>,
pub fn process_participation_flag_updates<E: EthSpec>(
state: &mut BeaconState<E>,
) -> Result<(), EpochProcessingError> {
*state.previous_epoch_participation_mut()? =
std::mem::take(state.current_epoch_participation_mut()?);

View File

@@ -15,12 +15,12 @@ use crate::per_epoch_processing::{Delta, Error};
/// Apply attester and proposer rewards.
///
/// Spec v1.1.0
pub fn process_rewards_and_penalties<T: EthSpec>(
state: &mut BeaconState<T>,
pub fn process_rewards_and_penalties<E: EthSpec>(
state: &mut BeaconState<E>,
participation_cache: &ParticipationCache,
spec: &ChainSpec,
) -> Result<(), Error> {
if state.current_epoch() == T::genesis_epoch() {
if state.current_epoch() == E::genesis_epoch() {
return Ok(());
}
@@ -54,9 +54,9 @@ pub fn process_rewards_and_penalties<T: EthSpec>(
/// Return the deltas for a given flag index by scanning through the participation flags.
///
/// Spec v1.1.0
pub fn get_flag_index_deltas<T: EthSpec>(
pub fn get_flag_index_deltas<E: EthSpec>(
deltas: &mut [Delta],
state: &BeaconState<T>,
state: &BeaconState<E>,
flag_index: usize,
total_active_balance: u64,
participation_cache: &ParticipationCache,
@@ -104,9 +104,9 @@ pub fn get_flag_weight(flag_index: usize) -> Result<u64, Error> {
.ok_or(Error::InvalidFlagIndex(flag_index))
}
pub fn get_inactivity_penalty_deltas<T: EthSpec>(
pub fn get_inactivity_penalty_deltas<E: EthSpec>(
deltas: &mut [Delta],
state: &BeaconState<T>,
state: &BeaconState<E>,
participation_cache: &ParticipationCache,
spec: &ChainSpec,
) -> Result<(), Error> {

View File

@@ -5,8 +5,8 @@ use types::beacon_state::BeaconState;
use types::chain_spec::ChainSpec;
use types::eth_spec::EthSpec;
pub fn process_sync_committee_updates<T: EthSpec>(
state: &mut BeaconState<T>,
pub fn process_sync_committee_updates<E: EthSpec>(
state: &mut BeaconState<E>,
spec: &ChainSpec,
) -> Result<(), EpochProcessingError> {
let next_epoch = state.next_epoch()?;

View File

@@ -15,10 +15,10 @@ pub mod participation_record_updates;
pub mod rewards_and_penalties;
pub mod validator_statuses;
pub fn process_epoch<T: EthSpec>(
state: &mut BeaconState<T>,
pub fn process_epoch<E: EthSpec>(
state: &mut BeaconState<E>,
spec: &ChainSpec,
) -> Result<EpochProcessingSummary<T>, Error> {
) -> Result<EpochProcessingSummary<E>, Error> {
// Ensure the committee caches are built.
state.build_committee_cache(RelativeEpoch::Previous, spec)?;
state.build_committee_cache(RelativeEpoch::Current, spec)?;

View File

@@ -7,14 +7,14 @@ use safe_arith::SafeArith;
use types::{BeaconState, ChainSpec, EthSpec};
/// Update the justified and finalized checkpoints for matching target attestations.
pub fn process_justification_and_finalization<T: EthSpec>(
state: &BeaconState<T>,
pub fn process_justification_and_finalization<E: EthSpec>(
state: &BeaconState<E>,
total_balances: &TotalBalances,
_spec: &ChainSpec,
) -> Result<JustificationAndFinalizationState<T>, Error> {
) -> Result<JustificationAndFinalizationState<E>, Error> {
let justification_and_finalization_state = JustificationAndFinalizationState::new(state);
if state.current_epoch() <= T::genesis_epoch().safe_add(1)? {
if state.current_epoch() <= E::genesis_epoch().safe_add(1)? {
return Ok(justification_and_finalization_state);
}

View File

@@ -2,8 +2,8 @@ use crate::EpochProcessingError;
use types::beacon_state::BeaconState;
use types::eth_spec::EthSpec;
pub fn process_participation_record_updates<T: EthSpec>(
state: &mut BeaconState<T>,
pub fn process_participation_record_updates<E: EthSpec>(
state: &mut BeaconState<E>,
) -> Result<(), EpochProcessingError> {
let base_state = state.as_base_mut()?;
base_state.previous_epoch_attestations =

View File

@@ -43,12 +43,12 @@ impl AttestationDelta {
}
/// Apply attester and proposer rewards.
pub fn process_rewards_and_penalties<T: EthSpec>(
state: &mut BeaconState<T>,
pub fn process_rewards_and_penalties<E: EthSpec>(
state: &mut BeaconState<E>,
validator_statuses: &ValidatorStatuses,
spec: &ChainSpec,
) -> Result<(), Error> {
if state.current_epoch() == T::genesis_epoch() {
if state.current_epoch() == E::genesis_epoch() {
return Ok(());
}
@@ -73,8 +73,8 @@ pub fn process_rewards_and_penalties<T: EthSpec>(
}
/// Apply rewards for participation in attestations during the previous epoch.
pub fn get_attestation_deltas_all<T: EthSpec>(
state: &BeaconState<T>,
pub fn get_attestation_deltas_all<E: EthSpec>(
state: &BeaconState<E>,
validator_statuses: &ValidatorStatuses,
spec: &ChainSpec,
) -> Result<Vec<AttestationDelta>, Error> {
@@ -83,8 +83,8 @@ pub fn get_attestation_deltas_all<T: EthSpec>(
/// Apply rewards for participation in attestations during the previous epoch, and only compute
/// rewards for a subset of validators.
pub fn get_attestation_deltas_subset<T: EthSpec>(
state: &BeaconState<T>,
pub fn get_attestation_deltas_subset<E: EthSpec>(
state: &BeaconState<E>,
validator_statuses: &ValidatorStatuses,
validators_subset: &Vec<usize>,
spec: &ChainSpec,
@@ -103,8 +103,8 @@ pub fn get_attestation_deltas_subset<T: EthSpec>(
/// returned, otherwise deltas for all validators are returned.
///
/// Returns a vec of validator indices to `AttestationDelta`.
fn get_attestation_deltas<T: EthSpec>(
state: &BeaconState<T>,
fn get_attestation_deltas<E: EthSpec>(
state: &BeaconState<E>,
validator_statuses: &ValidatorStatuses,
maybe_validators_subset: Option<&Vec<usize>>,
spec: &ChainSpec,

View File

@@ -188,8 +188,8 @@ impl ValidatorStatuses {
/// - Total balances for the current and previous epochs.
///
/// Spec v0.12.1
pub fn new<T: EthSpec>(
state: &BeaconState<T>,
pub fn new<E: EthSpec>(
state: &BeaconState<E>,
spec: &ChainSpec,
) -> Result<Self, BeaconStateError> {
let mut statuses = Vec::with_capacity(state.validators().len());
@@ -232,9 +232,9 @@ impl ValidatorStatuses {
/// `total_balances` fields.
///
/// Spec v0.12.1
pub fn process_attestations<T: EthSpec>(
pub fn process_attestations<E: EthSpec>(
&mut self,
state: &BeaconState<T>,
state: &BeaconState<E>,
) -> Result<(), BeaconStateError> {
let base_state = state.as_base()?;
for a in base_state
@@ -244,7 +244,7 @@ impl ValidatorStatuses {
{
let committee = state.get_beacon_committee(a.data.slot, a.data.index)?;
let attesting_indices =
get_attesting_indices::<T>(committee.committee, &a.aggregation_bits)?;
get_attesting_indices::<E>(committee.committee, &a.aggregation_bits)?;
let mut status = ValidatorStatus::default();
@@ -326,12 +326,12 @@ impl ValidatorStatuses {
/// beacon block in the given `epoch`.
///
/// Spec v0.12.1
fn target_matches_epoch_start_block<T: EthSpec>(
a: &PendingAttestation<T>,
state: &BeaconState<T>,
fn target_matches_epoch_start_block<E: EthSpec>(
a: &PendingAttestation<E>,
state: &BeaconState<E>,
epoch: Epoch,
) -> Result<bool, BeaconStateError> {
let slot = epoch.start_slot(T::slots_per_epoch());
let slot = epoch.start_slot(E::slots_per_epoch());
let state_boundary_root = *state.get_block_root(slot)?;
Ok(a.data.target.root == state_boundary_root)
@@ -341,9 +341,9 @@ fn target_matches_epoch_start_block<T: EthSpec>(
/// the current slot of the `PendingAttestation`.
///
/// Spec v0.12.1
fn has_common_beacon_block_root<T: EthSpec>(
a: &PendingAttestation<T>,
state: &BeaconState<T>,
fn has_common_beacon_block_root<E: EthSpec>(
a: &PendingAttestation<E>,
state: &BeaconState<E>,
) -> Result<bool, BeaconStateError> {
let state_block_root = *state.get_block_root(a.data.slot)?;

View File

@@ -18,10 +18,10 @@ pub use historical_summaries_update::process_historical_summaries_update;
mod historical_summaries_update;
pub fn process_epoch<T: EthSpec>(
state: &mut BeaconState<T>,
pub fn process_epoch<E: EthSpec>(
state: &mut BeaconState<E>,
spec: &ChainSpec,
) -> Result<EpochProcessingSummary<T>, Error> {
) -> Result<EpochProcessingSummary<E>, Error> {
// Ensure the committee caches are built.
state.build_committee_cache(RelativeEpoch::Previous, spec)?;
state.build_committee_cache(RelativeEpoch::Current, spec)?;

View File

@@ -3,14 +3,14 @@ use safe_arith::SafeArith;
use types::historical_summary::HistoricalSummary;
use types::{BeaconState, EthSpec};
pub fn process_historical_summaries_update<T: EthSpec>(
state: &mut BeaconState<T>,
pub fn process_historical_summaries_update<E: EthSpec>(
state: &mut BeaconState<E>,
) -> Result<(), EpochProcessingError> {
// Set historical block root accumulator.
let next_epoch = state.next_epoch()?;
if next_epoch
.as_u64()
.safe_rem((T::slots_per_historical_root() as u64).safe_div(T::slots_per_epoch())?)?
.safe_rem((E::slots_per_historical_root() as u64).safe_div(E::slots_per_epoch())?)?
== 0
{
let summary = HistoricalSummary::new(state);

View File

@@ -5,8 +5,8 @@ use types::beacon_state::BeaconState;
use types::chain_spec::ChainSpec;
use types::{BeaconStateError, EthSpec, ProgressiveBalancesCache};
pub fn process_effective_balance_updates<T: EthSpec>(
state: &mut BeaconState<T>,
pub fn process_effective_balance_updates<E: EthSpec>(
state: &mut BeaconState<E>,
maybe_participation_cache: Option<&ParticipationCache>,
spec: &ChainSpec,
) -> Result<(), EpochProcessingError> {

View File

@@ -8,18 +8,18 @@ use types::{EthSpec, SyncCommittee};
/// Provides a summary of validator participation during the epoch.
#[derive(PartialEq, Debug)]
pub enum EpochProcessingSummary<T: EthSpec> {
pub enum EpochProcessingSummary<E: EthSpec> {
Base {
total_balances: TotalBalances,
statuses: Vec<ValidatorStatus>,
},
Altair {
participation_cache: ParticipationCache,
sync_committee: Arc<SyncCommittee<T>>,
sync_committee: Arc<SyncCommittee<E>>,
},
}
impl<T: EthSpec> EpochProcessingSummary<T> {
impl<E: EthSpec> EpochProcessingSummary<E> {
/// Updates some Prometheus metrics with some values in `self`.
pub fn observe_metrics(&self) -> Result<(), ParticipationCacheError> {
metrics::set_gauge(
@@ -43,7 +43,7 @@ impl<T: EthSpec> EpochProcessingSummary<T> {
}
/// Returns the sync committee indices for the current epoch for altair.
pub fn sync_committee(&self) -> Option<&SyncCommittee<T>> {
pub fn sync_committee(&self) -> Option<&SyncCommittee<E>> {
match self {
EpochProcessingSummary::Altair { sync_committee, .. } => Some(sync_committee),
EpochProcessingSummary::Base { .. } => None,

View File

@@ -5,13 +5,13 @@ use types::beacon_state::BeaconState;
use types::eth_spec::EthSpec;
use types::Unsigned;
pub fn process_historical_roots_update<T: EthSpec>(
state: &mut BeaconState<T>,
pub fn process_historical_roots_update<E: EthSpec>(
state: &mut BeaconState<E>,
) -> Result<(), EpochProcessingError> {
let next_epoch = state.next_epoch()?;
if next_epoch
.as_u64()
.safe_rem(T::SlotsPerHistoricalRoot::to_u64().safe_div(T::slots_per_epoch())?)?
.safe_rem(E::SlotsPerHistoricalRoot::to_u64().safe_div(E::slots_per_epoch())?)?
== 0
{
let historical_batch = state.historical_batch();

View File

@@ -6,7 +6,7 @@ use types::{BeaconState, BeaconStateError, BitVector, Checkpoint, Epoch, EthSpec
/// A `JustificationAndFinalizationState` can be created from a `BeaconState` to compute
/// justification/finality changes and then applied to a `BeaconState` to enshrine those changes.
#[must_use = "this value must be applied to a state or explicitly dropped"]
pub struct JustificationAndFinalizationState<T: EthSpec> {
pub struct JustificationAndFinalizationState<E: EthSpec> {
/*
* Immutable fields.
*/
@@ -20,11 +20,11 @@ pub struct JustificationAndFinalizationState<T: EthSpec> {
previous_justified_checkpoint: Checkpoint,
current_justified_checkpoint: Checkpoint,
finalized_checkpoint: Checkpoint,
justification_bits: BitVector<T::JustificationBitsLength>,
justification_bits: BitVector<E::JustificationBitsLength>,
}
impl<T: EthSpec> JustificationAndFinalizationState<T> {
pub fn new(state: &BeaconState<T>) -> Self {
impl<E: EthSpec> JustificationAndFinalizationState<E> {
pub fn new(state: &BeaconState<E>) -> Self {
let previous_epoch = state.previous_epoch();
let current_epoch = state.current_epoch();
Self {
@@ -39,7 +39,7 @@ impl<T: EthSpec> JustificationAndFinalizationState<T> {
}
}
pub fn apply_changes_to_state(self, state: &mut BeaconState<T>) {
pub fn apply_changes_to_state(self, state: &mut BeaconState<E>) {
let Self {
/*
* Immutable fields do not need to be used.
@@ -105,11 +105,11 @@ impl<T: EthSpec> JustificationAndFinalizationState<T> {
&mut self.finalized_checkpoint
}
pub fn justification_bits(&self) -> &BitVector<T::JustificationBitsLength> {
pub fn justification_bits(&self) -> &BitVector<E::JustificationBitsLength> {
&self.justification_bits
}
pub fn justification_bits_mut(&mut self) -> &mut BitVector<T::JustificationBitsLength> {
pub fn justification_bits_mut(&mut self) -> &mut BitVector<E::JustificationBitsLength> {
&mut self.justification_bits
}
}

View File

@@ -6,8 +6,8 @@ use types::{BeaconState, ChainSpec, EthSpec, Validator};
/// Performs a validator registry update, if required.
///
/// NOTE: unchanged in Altair
pub fn process_registry_updates<T: EthSpec>(
state: &mut BeaconState<T>,
pub fn process_registry_updates<E: EthSpec>(
state: &mut BeaconState<E>,
spec: &ChainSpec,
) -> Result<(), Error> {
// Process activation eligibility and ejections.

View File

@@ -4,13 +4,13 @@ use types::beacon_state::BeaconState;
use types::eth_spec::EthSpec;
use types::{Unsigned, VariableList};
pub fn process_eth1_data_reset<T: EthSpec>(
state: &mut BeaconState<T>,
pub fn process_eth1_data_reset<E: EthSpec>(
state: &mut BeaconState<E>,
) -> Result<(), EpochProcessingError> {
if state
.slot()
.safe_add(1)?
.safe_rem(T::SlotsPerEth1VotingPeriod::to_u64())?
.safe_rem(E::SlotsPerEth1VotingPeriod::to_u64())?
== 0
{
*state.eth1_data_votes_mut() = VariableList::empty();
@@ -18,16 +18,16 @@ pub fn process_eth1_data_reset<T: EthSpec>(
Ok(())
}
pub fn process_slashings_reset<T: EthSpec>(
state: &mut BeaconState<T>,
pub fn process_slashings_reset<E: EthSpec>(
state: &mut BeaconState<E>,
) -> Result<(), EpochProcessingError> {
let next_epoch = state.next_epoch()?;
state.set_slashings(next_epoch, 0)?;
Ok(())
}
pub fn process_randao_mixes_reset<T: EthSpec>(
state: &mut BeaconState<T>,
pub fn process_randao_mixes_reset<E: EthSpec>(
state: &mut BeaconState<E>,
) -> Result<(), EpochProcessingError> {
let current_epoch = state.current_epoch();
let next_epoch = state.next_epoch()?;

View File

@@ -3,8 +3,8 @@ use safe_arith::{SafeArith, SafeArithIter};
use types::{BeaconState, BeaconStateError, ChainSpec, EthSpec, Unsigned};
/// Process slashings.
pub fn process_slashings<T: EthSpec>(
state: &mut BeaconState<T>,
pub fn process_slashings<E: EthSpec>(
state: &mut BeaconState<E>,
total_balance: u64,
spec: &ChainSpec,
) -> Result<(), Error> {
@@ -19,7 +19,7 @@ pub fn process_slashings<T: EthSpec>(
let (validators, balances, _) = state.validators_and_balances_and_progressive_balances_mut();
for (index, validator) in validators.iter().enumerate() {
if validator.slashed
&& epoch.safe_add(T::EpochsPerSlashingsVector::to_u64().safe_div(2)?)?
&& epoch.safe_add(E::EpochsPerSlashingsVector::to_u64().safe_div(2)?)?
== validator.withdrawable_epoch
{
let increment = spec.effective_balance_increment;

View File

@@ -5,12 +5,12 @@ use types::{Checkpoint, EthSpec};
/// Update the justified and finalized checkpoints for matching target attestations.
#[allow(clippy::if_same_then_else)] // For readability and consistency with spec.
pub fn weigh_justification_and_finalization<T: EthSpec>(
mut state: JustificationAndFinalizationState<T>,
pub fn weigh_justification_and_finalization<E: EthSpec>(
mut state: JustificationAndFinalizationState<E>,
total_active_balance: u64,
previous_target_balance: u64,
current_target_balance: u64,
) -> Result<JustificationAndFinalizationState<T>, Error> {
) -> Result<JustificationAndFinalizationState<E>, Error> {
let previous_epoch = state.previous_epoch();
let current_epoch = state.current_epoch();