mirror of
https://github.com/sigp/lighthouse.git
synced 2026-03-03 00:31:50 +00:00
Cache target attester balances for unrealized FFG progression calculation (#4362)
## Issue Addressed #4118 ## Proposed Changes This PR introduces a "progressive balances" cache on the `BeaconState`, which keeps track of the accumulated target attestation balance for the current & previous epochs. The cached values are utilised by fork choice to calculate unrealized justification and finalization (instead of converting epoch participation arrays to balances for each block we receive). This optimization will be rolled out gradually to allow for more testing. A new `--progressive-balances disabled|checked|strict|fast` flag is introduced to support this: - `checked`: enabled with checks against participation cache, and falls back to the existing epoch processing calculation if there is a total target attester balance mismatch. There is no performance gain from this as the participation cache still needs to be computed. **This is the default mode for now.** - `strict`: enabled with checks against participation cache, returns error if there is a mismatch. **Used for testing only**. - `fast`: enabled with no comparative checks and without computing the participation cache. This mode gives us the performance gains from the optimization. This is still experimental and not currently recommended for production usage, but will become the default mode in a future release. - `disabled`: disable the usage of progressive cache, and use the existing method for FFG progression calculation. This mode may be useful if we find a bug and want to stop the frequent error logs. ### Tasks - [x] Initial cache implementation in `BeaconState` - [x] Perform checks in fork choice to compare the progressive balances cache against results from `ParticipationCache` - [x] Add CLI flag, and disable the optimization by default - [x] Testing on Goerli & Benchmarking - [x] Move caching logic from state processing to the `ProgressiveBalancesCache` (see [this comment](https://github.com/sigp/lighthouse/pull/4362#discussion_r1230877001)) - [x] Add attesting balance metrics Co-authored-by: Jimmy Chen <jimmy@sigmaprime.io>
This commit is contained in:
@@ -7,6 +7,7 @@ mod slash_validator;
|
||||
|
||||
pub mod altair;
|
||||
pub mod base;
|
||||
pub mod update_progressive_balances_cache;
|
||||
|
||||
pub use deposit_data_tree::DepositDataTree;
|
||||
pub use get_attestation_participation::get_attestation_participation_flag_indices;
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
use crate::common::update_progressive_balances_cache::update_progressive_balances_on_slashing;
|
||||
use crate::{
|
||||
common::{decrease_balance, increase_balance, initiate_validator_exit},
|
||||
per_block_processing::errors::BlockProcessingError,
|
||||
@@ -43,6 +44,8 @@ pub fn slash_validator<T: EthSpec>(
|
||||
.safe_div(spec.min_slashing_penalty_quotient_for_state(state))?,
|
||||
)?;
|
||||
|
||||
update_progressive_balances_on_slashing(state, slashed_index)?;
|
||||
|
||||
// Apply proposer and whistleblower rewards
|
||||
let proposer_index = ctxt.get_proposer_index(state, spec)? as usize;
|
||||
let whistleblower_index = opt_whistleblower_index.unwrap_or(proposer_index);
|
||||
|
||||
@@ -0,0 +1,142 @@
|
||||
/// A collection of all functions that mutates the `ProgressiveBalancesCache`.
|
||||
use crate::metrics::{
|
||||
PARTICIPATION_CURR_EPOCH_TARGET_ATTESTING_GWEI_PROGRESSIVE_TOTAL,
|
||||
PARTICIPATION_PREV_EPOCH_TARGET_ATTESTING_GWEI_PROGRESSIVE_TOTAL,
|
||||
};
|
||||
use crate::per_epoch_processing::altair::ParticipationCache;
|
||||
use crate::{BlockProcessingError, EpochProcessingError};
|
||||
use lighthouse_metrics::set_gauge;
|
||||
use ssz_types::VariableList;
|
||||
use std::borrow::Cow;
|
||||
use types::consts::altair::TIMELY_TARGET_FLAG_INDEX;
|
||||
use types::{
|
||||
is_progressive_balances_enabled, BeaconState, BeaconStateError, ChainSpec, Epoch, EthSpec,
|
||||
ParticipationFlags, ProgressiveBalancesCache,
|
||||
};
|
||||
|
||||
/// Initializes the `ProgressiveBalancesCache` cache using balance values from the
|
||||
/// `ParticipationCache`. If the optional `&ParticipationCache` is not supplied, it will be computed
|
||||
/// from the `BeaconState`.
|
||||
pub fn initialize_progressive_balances_cache<E: EthSpec>(
|
||||
state: &mut BeaconState<E>,
|
||||
maybe_participation_cache: Option<&ParticipationCache>,
|
||||
spec: &ChainSpec,
|
||||
) -> Result<(), BeaconStateError> {
|
||||
if !is_progressive_balances_enabled(state)
|
||||
|| state.progressive_balances_cache().is_initialized()
|
||||
{
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let participation_cache = match maybe_participation_cache {
|
||||
Some(cache) => Cow::Borrowed(cache),
|
||||
None => Cow::Owned(ParticipationCache::new(state, spec)?),
|
||||
};
|
||||
|
||||
let previous_epoch_target_attesting_balance = participation_cache
|
||||
.previous_epoch_target_attesting_balance_raw()
|
||||
.map_err(|e| BeaconStateError::ParticipationCacheError(format!("{e:?}")))?;
|
||||
|
||||
let current_epoch_target_attesting_balance = participation_cache
|
||||
.current_epoch_target_attesting_balance_raw()
|
||||
.map_err(|e| BeaconStateError::ParticipationCacheError(format!("{e:?}")))?;
|
||||
|
||||
let current_epoch = state.current_epoch();
|
||||
state.progressive_balances_cache_mut().initialize(
|
||||
current_epoch,
|
||||
previous_epoch_target_attesting_balance,
|
||||
current_epoch_target_attesting_balance,
|
||||
);
|
||||
|
||||
update_progressive_balances_metrics(state.progressive_balances_cache())?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Updates the `ProgressiveBalancesCache` when a new target attestation has been processed.
|
||||
pub fn update_progressive_balances_on_attestation<T: EthSpec>(
|
||||
state: &mut BeaconState<T>,
|
||||
epoch: Epoch,
|
||||
validator_index: usize,
|
||||
) -> Result<(), BlockProcessingError> {
|
||||
if is_progressive_balances_enabled(state) {
|
||||
let validator = state.get_validator(validator_index)?;
|
||||
if !validator.slashed {
|
||||
let validator_effective_balance = validator.effective_balance;
|
||||
state
|
||||
.progressive_balances_cache_mut()
|
||||
.on_new_target_attestation(epoch, validator_effective_balance)?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Updates the `ProgressiveBalancesCache` when a target attester has been slashed.
|
||||
pub fn update_progressive_balances_on_slashing<T: EthSpec>(
|
||||
state: &mut BeaconState<T>,
|
||||
validator_index: usize,
|
||||
) -> Result<(), BlockProcessingError> {
|
||||
if is_progressive_balances_enabled(state) {
|
||||
let previous_epoch_participation = state.previous_epoch_participation()?;
|
||||
let is_previous_epoch_target_attester =
|
||||
is_target_attester_in_epoch::<T>(previous_epoch_participation, validator_index)?;
|
||||
|
||||
let current_epoch_participation = state.current_epoch_participation()?;
|
||||
let is_current_epoch_target_attester =
|
||||
is_target_attester_in_epoch::<T>(current_epoch_participation, validator_index)?;
|
||||
|
||||
let validator_effective_balance = state.get_effective_balance(validator_index)?;
|
||||
|
||||
state.progressive_balances_cache_mut().on_slashing(
|
||||
is_previous_epoch_target_attester,
|
||||
is_current_epoch_target_attester,
|
||||
validator_effective_balance,
|
||||
)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Updates the `ProgressiveBalancesCache` on epoch transition.
|
||||
pub fn update_progressive_balances_on_epoch_transition<T: EthSpec>(
|
||||
state: &mut BeaconState<T>,
|
||||
spec: &ChainSpec,
|
||||
) -> Result<(), EpochProcessingError> {
|
||||
if is_progressive_balances_enabled(state) {
|
||||
state
|
||||
.progressive_balances_cache_mut()
|
||||
.on_epoch_transition(spec)?;
|
||||
|
||||
update_progressive_balances_metrics(state.progressive_balances_cache())?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn update_progressive_balances_metrics(
|
||||
cache: &ProgressiveBalancesCache,
|
||||
) -> Result<(), BeaconStateError> {
|
||||
set_gauge(
|
||||
&PARTICIPATION_PREV_EPOCH_TARGET_ATTESTING_GWEI_PROGRESSIVE_TOTAL,
|
||||
cache.previous_epoch_target_attesting_balance()? as i64,
|
||||
);
|
||||
|
||||
set_gauge(
|
||||
&PARTICIPATION_CURR_EPOCH_TARGET_ATTESTING_GWEI_PROGRESSIVE_TOTAL,
|
||||
cache.current_epoch_target_attesting_balance()? as i64,
|
||||
);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn is_target_attester_in_epoch<T: EthSpec>(
|
||||
epoch_participation: &VariableList<ParticipationFlags, T::ValidatorRegistryLimit>,
|
||||
validator_index: usize,
|
||||
) -> Result<bool, BlockProcessingError> {
|
||||
let participation_flags = epoch_participation
|
||||
.get(validator_index)
|
||||
.ok_or(BeaconStateError::UnknownValidator(validator_index))?;
|
||||
participation_flags
|
||||
.has_flag(TIMELY_TARGET_FLAG_INDEX)
|
||||
.map_err(|e| e.into())
|
||||
}
|
||||
@@ -92,7 +92,7 @@ pub fn initialize_beacon_state_from_eth1<T: EthSpec>(
|
||||
}
|
||||
|
||||
// Now that we have our validators, initialize the caches (including the committees)
|
||||
state.build_all_caches(spec)?;
|
||||
state.build_caches(spec)?;
|
||||
|
||||
// Set genesis validators root for domain separation and chain versioning
|
||||
*state.genesis_validators_root_mut() = state.update_validators_tree_hash_cache()?;
|
||||
@@ -115,7 +115,7 @@ pub fn process_activations<T: EthSpec>(
|
||||
state: &mut BeaconState<T>,
|
||||
spec: &ChainSpec,
|
||||
) -> Result<(), Error> {
|
||||
let (validators, balances) = state.validators_and_balances_mut();
|
||||
let (validators, balances, _) = state.validators_and_balances_and_progressive_balances_mut();
|
||||
for (index, validator) in validators.iter_mut().enumerate() {
|
||||
let balance = balances
|
||||
.get(index)
|
||||
|
||||
@@ -23,4 +23,15 @@ lazy_static! {
|
||||
"beacon_participation_prev_epoch_active_gwei_total",
|
||||
"Total effective balance (gwei) of validators active in the previous epoch"
|
||||
);
|
||||
/*
|
||||
* Participation Metrics (progressive balances)
|
||||
*/
|
||||
pub static ref PARTICIPATION_PREV_EPOCH_TARGET_ATTESTING_GWEI_PROGRESSIVE_TOTAL: Result<IntGauge> = try_create_int_gauge(
|
||||
"beacon_participation_prev_epoch_target_attesting_gwei_progressive_total",
|
||||
"Progressive total effective balance (gwei) of validators who attested to the target in the previous epoch"
|
||||
);
|
||||
pub static ref PARTICIPATION_CURR_EPOCH_TARGET_ATTESTING_GWEI_PROGRESSIVE_TOTAL: Result<IntGauge> = try_create_int_gauge(
|
||||
"beacon_participation_curr_epoch_target_attesting_gwei_progressive_total",
|
||||
"Progressive total effective balance (gwei) of validators who attested to the target in the current epoch"
|
||||
);
|
||||
}
|
||||
|
||||
@@ -41,6 +41,9 @@ mod verify_proposer_slashing;
|
||||
use crate::common::decrease_balance;
|
||||
use crate::StateProcessingStrategy;
|
||||
|
||||
use crate::common::update_progressive_balances_cache::{
|
||||
initialize_progressive_balances_cache, update_progressive_balances_metrics,
|
||||
};
|
||||
#[cfg(feature = "arbitrary-fuzz")]
|
||||
use arbitrary::Arbitrary;
|
||||
|
||||
@@ -114,6 +117,8 @@ pub fn per_block_processing<T: EthSpec, Payload: AbstractExecPayload<T>>(
|
||||
.fork_name(spec)
|
||||
.map_err(BlockProcessingError::InconsistentStateFork)?;
|
||||
|
||||
initialize_progressive_balances_cache(state, None, spec)?;
|
||||
|
||||
let verify_signatures = match block_signature_strategy {
|
||||
BlockSignatureStrategy::VerifyBulk => {
|
||||
// Verify all signatures in the block at once.
|
||||
@@ -182,6 +187,10 @@ pub fn per_block_processing<T: EthSpec, Payload: AbstractExecPayload<T>>(
|
||||
)?;
|
||||
}
|
||||
|
||||
if is_progressive_balances_enabled(state) {
|
||||
update_progressive_balances_metrics(state.progressive_balances_cache())?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
use super::signature_sets::Error as SignatureSetError;
|
||||
use crate::per_epoch_processing::altair::participation_cache;
|
||||
use crate::ContextError;
|
||||
use merkle_proof::MerkleTreeError;
|
||||
use participation_cache::Error as ParticipationCacheError;
|
||||
use safe_arith::ArithError;
|
||||
use ssz::DecodeError;
|
||||
use types::*;
|
||||
@@ -83,6 +85,7 @@ pub enum BlockProcessingError {
|
||||
found: Hash256,
|
||||
},
|
||||
WithdrawalCredentialsInvalid,
|
||||
ParticipationCacheError(ParticipationCacheError),
|
||||
}
|
||||
|
||||
impl From<BeaconStateError> for BlockProcessingError {
|
||||
@@ -140,6 +143,12 @@ impl From<BlockOperationError<HeaderInvalid>> for BlockProcessingError {
|
||||
}
|
||||
}
|
||||
|
||||
impl From<ParticipationCacheError> for BlockProcessingError {
|
||||
fn from(e: ParticipationCacheError) -> Self {
|
||||
BlockProcessingError::ParticipationCacheError(e)
|
||||
}
|
||||
}
|
||||
|
||||
/// A conversion that consumes `self` and adds an `index` variable to resulting struct.
|
||||
///
|
||||
/// Used here to allow converting an error into an upstream error that points to the object that
|
||||
|
||||
@@ -97,6 +97,8 @@ pub mod base {
|
||||
|
||||
pub mod altair {
|
||||
use super::*;
|
||||
use crate::common::update_progressive_balances_cache::update_progressive_balances_on_attestation;
|
||||
use types::consts::altair::TIMELY_TARGET_FLAG_INDEX;
|
||||
|
||||
pub fn process_attestations<T: EthSpec>(
|
||||
state: &mut BeaconState<T>,
|
||||
@@ -163,6 +165,14 @@ pub mod altair {
|
||||
get_base_reward(state, index, base_reward_per_increment, spec)?
|
||||
.safe_mul(weight)?,
|
||||
)?;
|
||||
|
||||
if flag_index == TIMELY_TARGET_FLAG_INDEX {
|
||||
update_progressive_balances_on_attestation(
|
||||
state,
|
||||
data.target.epoch,
|
||||
index,
|
||||
)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -235,6 +245,7 @@ pub fn process_attester_slashings<T: EthSpec>(
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Wrapper function to handle calling the correct version of `process_attestations` based on
|
||||
/// the fork.
|
||||
pub fn process_attestations<T: EthSpec, Payload: AbstractExecPayload<T>>(
|
||||
|
||||
@@ -1,4 +1,7 @@
|
||||
use super::{process_registry_updates, process_slashings, EpochProcessingSummary, Error};
|
||||
use crate::common::update_progressive_balances_cache::{
|
||||
initialize_progressive_balances_cache, update_progressive_balances_on_epoch_transition,
|
||||
};
|
||||
use crate::per_epoch_processing::{
|
||||
effective_balance_updates::process_effective_balance_updates,
|
||||
historical_roots_update::process_historical_roots_update,
|
||||
@@ -31,6 +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)?;
|
||||
|
||||
// Justification and finalization.
|
||||
let justification_and_finalization_state =
|
||||
@@ -56,7 +60,7 @@ pub fn process_epoch<T: EthSpec>(
|
||||
process_eth1_data_reset(state)?;
|
||||
|
||||
// Update effective balances with hysteresis (lag).
|
||||
process_effective_balance_updates(state, spec)?;
|
||||
process_effective_balance_updates(state, Some(&participation_cache), spec)?;
|
||||
|
||||
// Reset slashings
|
||||
process_slashings_reset(state)?;
|
||||
@@ -75,6 +79,8 @@ pub fn process_epoch<T: EthSpec>(
|
||||
// Rotate the epoch caches to suit the epoch transition.
|
||||
state.advance_caches(spec)?;
|
||||
|
||||
update_progressive_balances_on_epoch_transition(state, spec)?;
|
||||
|
||||
Ok(EpochProcessingSummary::Altair {
|
||||
participation_cache,
|
||||
sync_committee,
|
||||
|
||||
@@ -11,49 +11,23 @@
|
||||
//! Additionally, this cache is returned from the `altair::process_epoch` function and can be used
|
||||
//! to get useful summaries about the validator participation in an epoch.
|
||||
|
||||
use safe_arith::{ArithError, SafeArith};
|
||||
use types::{
|
||||
consts::altair::{
|
||||
NUM_FLAG_INDICES, TIMELY_HEAD_FLAG_INDEX, TIMELY_SOURCE_FLAG_INDEX,
|
||||
TIMELY_TARGET_FLAG_INDEX,
|
||||
},
|
||||
BeaconState, BeaconStateError, ChainSpec, Epoch, EthSpec, ParticipationFlags, RelativeEpoch,
|
||||
Balance, BeaconState, BeaconStateError, ChainSpec, Epoch, EthSpec, ParticipationFlags,
|
||||
RelativeEpoch,
|
||||
};
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
#[derive(Debug, PartialEq, Clone)]
|
||||
pub enum Error {
|
||||
InvalidFlagIndex(usize),
|
||||
InvalidValidatorIndex(usize),
|
||||
}
|
||||
|
||||
/// A balance which will never be below the specified `minimum`.
|
||||
///
|
||||
/// This is an effort to ensure the `EFFECTIVE_BALANCE_INCREMENT` minimum is always respected.
|
||||
#[derive(PartialEq, Debug, Clone, Copy)]
|
||||
struct Balance {
|
||||
raw: u64,
|
||||
minimum: u64,
|
||||
}
|
||||
|
||||
impl Balance {
|
||||
/// Initialize the balance to `0`, or the given `minimum`.
|
||||
pub fn zero(minimum: u64) -> Self {
|
||||
Self { raw: 0, minimum }
|
||||
}
|
||||
|
||||
/// Returns the balance with respect to the initialization `minimum`.
|
||||
pub fn get(&self) -> u64 {
|
||||
std::cmp::max(self.raw, self.minimum)
|
||||
}
|
||||
|
||||
/// Add-assign to the balance.
|
||||
pub fn safe_add_assign(&mut self, other: u64) -> Result<(), ArithError> {
|
||||
self.raw.safe_add_assign(other)
|
||||
}
|
||||
}
|
||||
|
||||
/// Caches the participation values for one epoch (either the previous or current).
|
||||
#[derive(PartialEq, Debug)]
|
||||
#[derive(PartialEq, Debug, Clone)]
|
||||
struct SingleEpochParticipationCache {
|
||||
/// Maps an active validator index to their participation flags.
|
||||
///
|
||||
@@ -95,6 +69,14 @@ impl SingleEpochParticipationCache {
|
||||
.ok_or(Error::InvalidFlagIndex(flag_index))
|
||||
}
|
||||
|
||||
/// Returns the raw total balance of attesters who have `flag_index` set.
|
||||
fn total_flag_balance_raw(&self, flag_index: usize) -> Result<Balance, Error> {
|
||||
self.total_flag_balances
|
||||
.get(flag_index)
|
||||
.copied()
|
||||
.ok_or(Error::InvalidFlagIndex(flag_index))
|
||||
}
|
||||
|
||||
/// Returns `true` if `val_index` is active, unslashed and has `flag_index` set.
|
||||
///
|
||||
/// ## Errors
|
||||
@@ -173,7 +155,7 @@ impl SingleEpochParticipationCache {
|
||||
}
|
||||
|
||||
/// Maintains a cache to be used during `altair::process_epoch`.
|
||||
#[derive(PartialEq, Debug)]
|
||||
#[derive(PartialEq, Debug, Clone)]
|
||||
pub struct ParticipationCache {
|
||||
current_epoch: Epoch,
|
||||
/// Caches information about active validators pertaining to `self.current_epoch`.
|
||||
@@ -291,6 +273,11 @@ impl ParticipationCache {
|
||||
.total_flag_balance(TIMELY_TARGET_FLAG_INDEX)
|
||||
}
|
||||
|
||||
pub fn current_epoch_target_attesting_balance_raw(&self) -> Result<Balance, Error> {
|
||||
self.current_epoch_participation
|
||||
.total_flag_balance_raw(TIMELY_TARGET_FLAG_INDEX)
|
||||
}
|
||||
|
||||
pub fn previous_epoch_total_active_balance(&self) -> u64 {
|
||||
self.previous_epoch_participation.total_active_balance.get()
|
||||
}
|
||||
@@ -300,6 +287,11 @@ impl ParticipationCache {
|
||||
.total_flag_balance(TIMELY_TARGET_FLAG_INDEX)
|
||||
}
|
||||
|
||||
pub fn previous_epoch_target_attesting_balance_raw(&self) -> Result<Balance, Error> {
|
||||
self.previous_epoch_participation
|
||||
.total_flag_balance_raw(TIMELY_TARGET_FLAG_INDEX)
|
||||
}
|
||||
|
||||
pub fn previous_epoch_source_attesting_balance(&self) -> Result<u64, Error> {
|
||||
self.previous_epoch_participation
|
||||
.total_flag_balance(TIMELY_SOURCE_FLAG_INDEX)
|
||||
|
||||
@@ -52,7 +52,7 @@ pub fn process_epoch<T: EthSpec>(
|
||||
process_eth1_data_reset(state)?;
|
||||
|
||||
// Update effective balances with hysteresis (lag).
|
||||
process_effective_balance_updates(state, spec)?;
|
||||
process_effective_balance_updates(state, None, spec)?;
|
||||
|
||||
// Reset slashings
|
||||
process_slashings_reset(state)?;
|
||||
|
||||
@@ -11,6 +11,9 @@ use crate::per_epoch_processing::{
|
||||
};
|
||||
use types::{BeaconState, ChainSpec, EthSpec, RelativeEpoch};
|
||||
|
||||
use crate::common::update_progressive_balances_cache::{
|
||||
initialize_progressive_balances_cache, update_progressive_balances_on_epoch_transition,
|
||||
};
|
||||
pub use historical_summaries_update::process_historical_summaries_update;
|
||||
|
||||
mod historical_summaries_update;
|
||||
@@ -27,6 +30,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(state, Some(&participation_cache), spec)?;
|
||||
|
||||
// Justification and finalization.
|
||||
let justification_and_finalization_state =
|
||||
@@ -52,7 +56,7 @@ pub fn process_epoch<T: EthSpec>(
|
||||
process_eth1_data_reset(state)?;
|
||||
|
||||
// Update effective balances with hysteresis (lag).
|
||||
process_effective_balance_updates(state, spec)?;
|
||||
process_effective_balance_updates(state, Some(&participation_cache), spec)?;
|
||||
|
||||
// Reset slashings
|
||||
process_slashings_reset(state)?;
|
||||
@@ -71,6 +75,8 @@ pub fn process_epoch<T: EthSpec>(
|
||||
// Rotate the epoch caches to suit the epoch transition.
|
||||
state.advance_caches(spec)?;
|
||||
|
||||
update_progressive_balances_on_epoch_transition(state, spec)?;
|
||||
|
||||
Ok(EpochProcessingSummary::Altair {
|
||||
participation_cache,
|
||||
sync_committee,
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
use super::errors::EpochProcessingError;
|
||||
use crate::per_epoch_processing::altair::ParticipationCache;
|
||||
use safe_arith::SafeArith;
|
||||
use types::beacon_state::BeaconState;
|
||||
use types::chain_spec::ChainSpec;
|
||||
use types::{BeaconStateError, EthSpec};
|
||||
use types::{BeaconStateError, EthSpec, ProgressiveBalancesCache};
|
||||
|
||||
pub fn process_effective_balance_updates<T: EthSpec>(
|
||||
state: &mut BeaconState<T>,
|
||||
maybe_participation_cache: Option<&ParticipationCache>,
|
||||
spec: &ChainSpec,
|
||||
) -> Result<(), EpochProcessingError> {
|
||||
let hysteresis_increment = spec
|
||||
@@ -13,7 +15,8 @@ pub fn process_effective_balance_updates<T: EthSpec>(
|
||||
.safe_div(spec.hysteresis_quotient)?;
|
||||
let downward_threshold = hysteresis_increment.safe_mul(spec.hysteresis_downward_multiplier)?;
|
||||
let upward_threshold = hysteresis_increment.safe_mul(spec.hysteresis_upward_multiplier)?;
|
||||
let (validators, balances) = state.validators_and_balances_mut();
|
||||
let (validators, balances, progressive_balances_cache) =
|
||||
state.validators_and_balances_and_progressive_balances_mut();
|
||||
for (index, validator) in validators.iter_mut().enumerate() {
|
||||
let balance = balances
|
||||
.get(index)
|
||||
@@ -23,11 +26,43 @@ pub fn process_effective_balance_updates<T: EthSpec>(
|
||||
if balance.safe_add(downward_threshold)? < validator.effective_balance
|
||||
|| validator.effective_balance.safe_add(upward_threshold)? < balance
|
||||
{
|
||||
validator.effective_balance = std::cmp::min(
|
||||
let old_effective_balance = validator.effective_balance;
|
||||
let new_effective_balance = std::cmp::min(
|
||||
balance.safe_sub(balance.safe_rem(spec.effective_balance_increment)?)?,
|
||||
spec.max_effective_balance,
|
||||
);
|
||||
|
||||
if let Some(participation_cache) = maybe_participation_cache {
|
||||
update_progressive_balances(
|
||||
participation_cache,
|
||||
progressive_balances_cache,
|
||||
index,
|
||||
old_effective_balance,
|
||||
new_effective_balance,
|
||||
)?;
|
||||
}
|
||||
|
||||
validator.effective_balance = new_effective_balance;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn update_progressive_balances(
|
||||
participation_cache: &ParticipationCache,
|
||||
progressive_balances_cache: &mut ProgressiveBalancesCache,
|
||||
index: usize,
|
||||
old_effective_balance: u64,
|
||||
new_effective_balance: u64,
|
||||
) -> Result<(), EpochProcessingError> {
|
||||
if old_effective_balance != new_effective_balance {
|
||||
let is_current_epoch_target_attester =
|
||||
participation_cache.is_current_epoch_timely_target_attester(index)?;
|
||||
progressive_balances_cache.on_effective_balance_change(
|
||||
is_current_epoch_target_attester,
|
||||
old_effective_balance,
|
||||
new_effective_balance,
|
||||
)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -16,7 +16,7 @@ pub fn process_slashings<T: EthSpec>(
|
||||
total_balance,
|
||||
);
|
||||
|
||||
let (validators, balances) = state.validators_and_balances_mut();
|
||||
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)?)?
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
use crate::common::update_progressive_balances_cache::initialize_progressive_balances_cache;
|
||||
use crate::common::{get_attestation_participation_flag_indices, get_attesting_indices};
|
||||
use std::mem;
|
||||
use std::sync::Arc;
|
||||
@@ -101,6 +102,7 @@ pub fn upgrade_to_altair<E: EthSpec>(
|
||||
next_sync_committee: temp_sync_committee, // not read
|
||||
// Caches
|
||||
total_active_balance: pre.total_active_balance,
|
||||
progressive_balances_cache: mem::take(&mut pre.progressive_balances_cache),
|
||||
committee_caches: mem::take(&mut pre.committee_caches),
|
||||
pubkey_cache: mem::take(&mut pre.pubkey_cache),
|
||||
exit_cache: mem::take(&mut pre.exit_cache),
|
||||
@@ -110,6 +112,8 @@ pub fn upgrade_to_altair<E: EthSpec>(
|
||||
// Fill in previous epoch participation from the pre state's pending attestations.
|
||||
translate_participation(&mut post, &pre.previous_epoch_attestations, spec)?;
|
||||
|
||||
initialize_progressive_balances_cache(&mut post, None, spec)?;
|
||||
|
||||
// Fill in sync committees
|
||||
// Note: A duplicate committee is assigned for the current and next committee at the fork
|
||||
// boundary
|
||||
|
||||
@@ -62,6 +62,7 @@ pub fn upgrade_to_capella<E: EthSpec>(
|
||||
historical_summaries: VariableList::default(),
|
||||
// Caches
|
||||
total_active_balance: pre.total_active_balance,
|
||||
progressive_balances_cache: mem::take(&mut pre.progressive_balances_cache),
|
||||
committee_caches: mem::take(&mut pre.committee_caches),
|
||||
pubkey_cache: mem::take(&mut pre.pubkey_cache),
|
||||
exit_cache: mem::take(&mut pre.exit_cache),
|
||||
|
||||
@@ -60,6 +60,7 @@ pub fn upgrade_to_bellatrix<E: EthSpec>(
|
||||
latest_execution_payload_header: <ExecutionPayloadHeaderMerge<E>>::default(),
|
||||
// Caches
|
||||
total_active_balance: pre.total_active_balance,
|
||||
progressive_balances_cache: mem::take(&mut pre.progressive_balances_cache),
|
||||
committee_caches: mem::take(&mut pre.committee_caches),
|
||||
pubkey_cache: mem::take(&mut pre.pubkey_cache),
|
||||
exit_cache: mem::take(&mut pre.exit_cache),
|
||||
|
||||
Reference in New Issue
Block a user