Fix three consensus bugs!

This commit is contained in:
Michael Sproul
2023-10-11 11:18:22 +11:00
parent ca1abfeb2d
commit d4f87ef1a1
4 changed files with 23 additions and 7 deletions

View File

@@ -79,22 +79,23 @@ pub fn initialize_epoch_cache<E: EthSpec>(
state: &mut BeaconState<E>,
spec: &ChainSpec,
) -> Result<(), EpochCacheError> {
let epoch = state.current_epoch();
let current_epoch = state.current_epoch();
let next_epoch = state.next_epoch().map_err(EpochCacheError::BeaconState)?;
let epoch_cache: &EpochCache = state.epoch_cache();
let decision_block_root = state
.proposer_shuffling_decision_root(Hash256::zero())
.map_err(EpochCacheError::BeaconState)?;
if epoch_cache
.check_validity::<E>(epoch, decision_block_root)
.check_validity::<E>(current_epoch, decision_block_root)
.is_ok()
{
// `EpochCache` has already been initialized and is valid, no need to initialize.
return Ok(());
}
state.build_total_active_balance_cache_at(epoch, spec)?;
let total_active_balance = state.get_total_active_balance_at_epoch(epoch)?;
state.build_total_active_balance_cache_at(current_epoch, spec)?;
let total_active_balance = state.get_total_active_balance_at_epoch(current_epoch)?;
// Collect effective balances and compute activation queue.
let mut effective_balances = Vec::with_capacity(state.validators().len());
@@ -104,13 +105,14 @@ pub fn initialize_epoch_cache<E: EthSpec>(
effective_balances.push(validator.effective_balance());
// Add to speculative activation queue.
activation_queue.add_if_could_be_eligible_for_activation(index, validator, epoch, spec);
activation_queue
.add_if_could_be_eligible_for_activation(index, validator, next_epoch, spec);
}
// Compute base rewards.
let pre_epoch_cache = PreEpochCache {
epoch_key: EpochCacheKey {
epoch,
epoch: current_epoch,
decision_block_root,
},
effective_balances,

View File

@@ -120,6 +120,7 @@ pub fn process_epoch_single_pass<E: EthSpec>(
let is_in_inactivity_leak = state.is_in_inactivity_leak(previous_epoch, spec)?;
let total_active_balance = state.get_total_active_balance()?;
let churn_limit = state.get_churn_limit(spec)?;
let activation_churn_limit = state.get_activation_churn_limit(spec)?;
let finalized_checkpoint = state.finalized_checkpoint();
let fork_name = state.fork_name_unchecked();
@@ -164,7 +165,10 @@ pub fn process_epoch_single_pass<E: EthSpec>(
let rewards_ctxt = &RewardsAndPenaltiesContext::new(progressive_balances, state_ctxt, spec)?;
let activation_queue = &epoch_cache
.activation_queue()?
.get_validators_eligible_for_activation(finalized_checkpoint.epoch, churn_limit as usize);
.get_validators_eligible_for_activation(
finalized_checkpoint.epoch,
activation_churn_limit as usize,
);
let effective_balances_ctxt = &EffectiveBalancesContext::new(spec)?;
// Iterate over the validators and related fields in one pass.
@@ -619,6 +623,7 @@ fn process_single_effective_balance_update(
// Update progressive balances cache for the *current* epoch, which will soon become the
// previous epoch once the epoch transition completes.
progressive_balances.on_effective_balance_change(
validator.slashed(),
validator_info.current_epoch_participation,
old_effective_balance,
new_effective_balance,