From 2c2326f2debd6b181669967aff17ead5e5c02826 Mon Sep 17 00:00:00 2001 From: twoeths Date: Wed, 27 Nov 2024 15:52:43 +0700 Subject: [PATCH] fix: sync cached isCompoundingValidatorArr at epoch transition (#7247) --- .../src/cache/epochTransitionCache.ts | 47 ++++++++++++------- .../epoch/processEffectiveBalanceUpdates.ts | 7 +-- .../src/epoch/processPendingDeposits.ts | 5 +- 3 files changed, 37 insertions(+), 22 deletions(-) diff --git a/packages/state-transition/src/cache/epochTransitionCache.ts b/packages/state-transition/src/cache/epochTransitionCache.ts index 2a35317fbc93..b21f940c28d5 100644 --- a/packages/state-transition/src/cache/epochTransitionCache.ts +++ b/packages/state-transition/src/cache/epochTransitionCache.ts @@ -1,27 +1,32 @@ -import {phase0, Epoch, RootHex, ValidatorIndex} from "@lodestar/types"; -import {intDiv, toRootHex} from "@lodestar/utils"; import { EPOCHS_PER_SLASHINGS_VECTOR, FAR_FUTURE_EPOCH, ForkSeq, - SLOTS_PER_HISTORICAL_ROOT, MIN_ACTIVATION_BALANCE, + SLOTS_PER_HISTORICAL_ROOT, } from "@lodestar/params"; +import {Epoch, RootHex, ValidatorIndex} from "@lodestar/types"; +import {intDiv, toRootHex} from "@lodestar/utils"; +import {processPendingAttestations} from "../epoch/processPendingAttestations.js"; import { - hasMarkers, - FLAG_UNSLASHED, + CachedBeaconStateAllForks, + CachedBeaconStateAltair, + CachedBeaconStatePhase0, + hasCompoundingWithdrawalCredential, +} from "../index.js"; +import {computeBaseRewardPerIncrement} from "../util/altair.js"; +import { + FLAG_CURR_HEAD_ATTESTER, + FLAG_CURR_SOURCE_ATTESTER, + FLAG_CURR_TARGET_ATTESTER, FLAG_ELIGIBLE_ATTESTER, + FLAG_PREV_HEAD_ATTESTER, FLAG_PREV_SOURCE_ATTESTER, FLAG_PREV_TARGET_ATTESTER, - FLAG_PREV_HEAD_ATTESTER, - FLAG_CURR_SOURCE_ATTESTER, - FLAG_CURR_TARGET_ATTESTER, - FLAG_CURR_HEAD_ATTESTER, + FLAG_UNSLASHED, + hasMarkers, } from "../util/attesterStatus.js"; -import {CachedBeaconStateAllForks, CachedBeaconStateAltair, CachedBeaconStatePhase0} from "../index.js"; -import {computeBaseRewardPerIncrement} from "../util/altair.js"; -import {processPendingAttestations} from "../epoch/processPendingAttestations.js"; export type EpochTransitionCacheOpts = { /** @@ -133,11 +138,7 @@ export interface EpochTransitionCache { flags: number[]; - /** - * Validators in the current epoch, should use it for read-only value instead of accessing state.validators directly. - * Note that during epoch processing, validators could be updated so need to use it with care. - */ - validators: phase0.Validator[]; + isCompoundingValidatorArr: boolean[]; /** * balances array will be populated by processRewardsAndPenalties() and consumed by processEffectiveBalanceUpdates(). @@ -209,6 +210,8 @@ const inclusionDelays = new Array(); /** WARNING: reused, never gc'd */ const flags = new Array(); /** WARNING: reused, never gc'd */ +const isCompoundingValidatorArr = new Array(); +/** WARNING: reused, never gc'd */ const nextEpochShufflingActiveValidatorIndices = new Array(); export function beforeProcessEpoch( @@ -262,6 +265,10 @@ export function beforeProcessEpoch( // TODO: optimize by combining the two loops // likely will require splitting into phase0 and post-phase0 versions + if (forkSeq >= ForkSeq.electra) { + isCompoundingValidatorArr.length = validatorCount; + } + // Clone before being mutated in processEffectiveBalanceUpdates epochCtx.beforeEpochTransition(); @@ -298,6 +305,10 @@ export function beforeProcessEpoch( flags[i] = flag; + if (forkSeq >= ForkSeq.electra) { + isCompoundingValidatorArr[i] = hasCompoundingWithdrawalCredential(validator.withdrawalCredentials); + } + if (isActiveCurr) { totalActiveStakeByIncrement += effectiveBalancesByIncrements[i]; } else { @@ -511,7 +522,7 @@ export function beforeProcessEpoch( proposerIndices, inclusionDelays, flags, - validators, + isCompoundingValidatorArr, // Will be assigned in processRewardsAndPenalties() balances: undefined, }; diff --git a/packages/state-transition/src/epoch/processEffectiveBalanceUpdates.ts b/packages/state-transition/src/epoch/processEffectiveBalanceUpdates.ts index 1fe5c92eea1a..6e71d8ccdc73 100644 --- a/packages/state-transition/src/epoch/processEffectiveBalanceUpdates.ts +++ b/packages/state-transition/src/epoch/processEffectiveBalanceUpdates.ts @@ -5,10 +5,11 @@ import { HYSTERESIS_QUOTIENT, HYSTERESIS_UPWARD_MULTIPLIER, MAX_EFFECTIVE_BALANCE, + MAX_EFFECTIVE_BALANCE_ELECTRA, + MIN_ACTIVATION_BALANCE, TIMELY_TARGET_FLAG_INDEX, } from "@lodestar/params"; import {EpochTransitionCache, CachedBeaconStateAllForks, BeaconStateAltair} from "../types.js"; -import {getMaxEffectiveBalance} from "../util/validator.js"; /** Same to https://github.com/ethereum/eth2.0-specs/blob/v1.1.0-alpha.5/specs/altair/beacon-chain.md#has_flag */ const TIMELY_TARGET = 1 << TIMELY_TARGET_FLAG_INDEX; @@ -43,7 +44,7 @@ export function processEffectiveBalanceUpdates( // and updated in processPendingDeposits() and processPendingConsolidations() // so it's recycled here for performance. const balances = cache.balances ?? state.balances.getAll(); - const currentEpochValidators = cache.validators; + const isCompoundingValidatorArr = cache.isCompoundingValidatorArr; let numUpdate = 0; for (let i = 0, len = balances.length; i < len; i++) { @@ -58,7 +59,7 @@ export function processEffectiveBalanceUpdates( effectiveBalanceLimit = MAX_EFFECTIVE_BALANCE; } else { // from electra, effectiveBalanceLimit is per validator - effectiveBalanceLimit = getMaxEffectiveBalance(currentEpochValidators[i].withdrawalCredentials); + effectiveBalanceLimit = isCompoundingValidatorArr[i] ? MAX_EFFECTIVE_BALANCE_ELECTRA : MIN_ACTIVATION_BALANCE; } if ( diff --git a/packages/state-transition/src/epoch/processPendingDeposits.ts b/packages/state-transition/src/epoch/processPendingDeposits.ts index 53af3ab38763..b25cf1d040a4 100644 --- a/packages/state-transition/src/epoch/processPendingDeposits.ts +++ b/packages/state-transition/src/epoch/processPendingDeposits.ts @@ -2,8 +2,9 @@ import {FAR_FUTURE_EPOCH, ForkSeq, GENESIS_SLOT, MAX_PENDING_DEPOSITS_PER_EPOCH} import {PendingDeposit} from "@lodestar/types/lib/electra/types.js"; import {CachedBeaconStateElectra, EpochTransitionCache} from "../types.js"; import {increaseBalance} from "../util/balance.js"; -import {getActivationExitChurnLimit} from "../util/validator.js"; +import {hasCompoundingWithdrawalCredential} from "../util/electra.js"; import {computeStartSlotAtEpoch} from "../util/epoch.js"; +import {getActivationExitChurnLimit} from "../util/validator.js"; import {addValidatorToRegistry, isValidDepositSignature} from "../block/processDeposit.js"; /** @@ -106,6 +107,8 @@ function applyPendingDeposit( // Verify the deposit signature (proof of possession) which is not checked by the deposit contract if (isValidDepositSignature(state.config, pubkey, withdrawalCredentials, amount, signature)) { addValidatorToRegistry(ForkSeq.electra, state, pubkey, withdrawalCredentials, amount); + const newValidatorIndex = state.validators.length - 1; + cache.isCompoundingValidatorArr[newValidatorIndex] = hasCompoundingWithdrawalCredential(withdrawalCredentials); } } else { // Increase balance