Single-pass epoch processing (#4483)

This commit is contained in:
Michael Sproul
2023-07-18 16:59:55 +10:00
committed by GitHub
parent 079cd67df2
commit 5d2063d262
42 changed files with 1558 additions and 730 deletions

View File

@@ -1,18 +1,19 @@
use super::{process_registry_updates, process_slashings, EpochProcessingSummary, Error};
use super::{EpochProcessingSummary, Error};
use crate::common::update_progressive_balances_cache::{
initialize_progressive_balances_cache, update_progressive_balances_on_epoch_transition,
};
use crate::epoch_cache::initialize_epoch_cache;
use crate::per_epoch_processing::single_pass::{process_epoch_single_pass, SinglePassConfig};
use crate::per_epoch_processing::{
effective_balance_updates::process_effective_balance_updates,
capella::process_historical_summaries_update,
historical_roots_update::process_historical_roots_update,
resets::{process_eth1_data_reset, process_randao_mixes_reset, process_slashings_reset},
};
pub use inactivity_updates::process_inactivity_updates;
pub use inactivity_updates::process_inactivity_updates_slow;
pub use justification_and_finalization::process_justification_and_finalization;
pub use participation_cache::ParticipationCache;
pub use participation_flag_updates::process_participation_flag_updates;
pub use rewards_and_penalties::process_rewards_and_penalties;
pub use rewards_and_penalties::process_rewards_and_penalties_slow;
pub use sync_committee_updates::process_sync_committee_updates;
use types::{BeaconState, ChainSpec, EthSpec, RelativeEpoch};
@@ -27,53 +28,51 @@ pub fn process_epoch<T: EthSpec>(
state: &mut BeaconState<T>,
spec: &ChainSpec,
) -> Result<EpochProcessingSummary<T>, Error> {
// Ensure the committee caches are built.
// Ensure the required caches are built.
state.build_committee_cache(RelativeEpoch::Previous, spec)?;
state.build_committee_cache(RelativeEpoch::Current, spec)?;
state.build_committee_cache(RelativeEpoch::Next, spec)?;
state.build_total_active_balance_cache_at(state.current_epoch(), spec)?;
initialize_epoch_cache(state, state.current_epoch(), spec)?;
initialize_epoch_cache(state, spec)?;
initialize_progressive_balances_cache::<T>(state, None, spec)?;
// Pre-compute participating indices and total balances.
let mut participation_cache = ParticipationCache::new(state, spec)?;
let sync_committee = state.current_sync_committee()?.clone();
initialize_progressive_balances_cache::<T>(state, Some(&participation_cache), spec)?;
// Justification and finalization.
let justification_and_finalization_state =
process_justification_and_finalization(state, &participation_cache)?;
let justification_and_finalization_state = process_justification_and_finalization(state)?;
justification_and_finalization_state.apply_changes_to_state(state);
process_inactivity_updates(state, &mut participation_cache, spec)?;
// Rewards and Penalties.
process_rewards_and_penalties(state, &participation_cache, spec)?;
// Registry Updates.
process_registry_updates(state, spec)?;
// Slashings.
process_slashings(
state,
Some(participation_cache.process_slashings_indices()),
participation_cache.current_epoch_total_active_balance(),
spec,
)?;
// In a single pass:
// - Inactivity updates
// - Rewards and penalties
// - Registry updates
// - Slashings
// - Effective balance updates
//
// The `process_eth1_data_reset` is not covered in the single pass, but happens afterwards
// without loss of correctness.
let current_epoch_progressive_balances = state.progressive_balances_cache().clone();
let current_epoch_total_active_balance = state.get_total_active_balance()?;
let participation_summary =
process_epoch_single_pass(state, spec, SinglePassConfig::default())?;
// Reset eth1 data votes.
process_eth1_data_reset(state)?;
// Update effective balances with hysteresis (lag).
process_effective_balance_updates(state, Some(&participation_cache), spec)?;
// Reset slashings
process_slashings_reset(state)?;
// Set randao mix
process_randao_mixes_reset(state)?;
// Set historical root accumulator
process_historical_roots_update(state)?;
// Set historical summaries accumulator
if state.historical_summaries().is_ok() {
// Post-Capella.
process_historical_summaries_update(state)?;
} else {
// Pre-Capella
process_historical_roots_update(state)?;
}
// Rotate current/previous epoch participation
process_participation_flag_updates(state)?;
@@ -82,12 +81,12 @@ pub fn process_epoch<T: EthSpec>(
// Rotate the epoch caches to suit the epoch transition.
state.advance_caches(spec)?;
initialize_epoch_cache(state, state.next_epoch()?, spec)?;
update_progressive_balances_on_epoch_transition(state, spec)?;
Ok(EpochProcessingSummary::Altair {
participation_cache,
progressive_balances: current_epoch_progressive_balances,
current_epoch_total_active_balance,
participation: participation_summary,
sync_committee,
})
}