more initialize_validator_exit optimization (#6146)

This commit is contained in:
tersec 2024-03-27 08:18:50 +00:00 committed by GitHub
parent ad2299cd8c
commit 7a3edb6961
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 17 additions and 13 deletions

View File

@ -11,13 +11,11 @@ import
stew/assign2,
json_serialization/std/sets,
chronicles,
../extras,
./datatypes/[phase0, altair, bellatrix],
"."/[eth2_merkleization, forks, signatures, validator]
from std/algorithm import fill
from std/sequtils import anyIt, mapIt, toSeq
from ./datatypes/capella import BeaconState, ExecutionPayloadHeader, Withdrawal
export extras, forks, validator, chronicles
@ -104,25 +102,31 @@ func initiate_validator_exit*(
# Return if validator already initiated exit
let validator = addr state.validators.mitem(index)
var exit_queue_epoch = compute_activation_exit_epoch(get_current_epoch(state))
var
exit_queue_epoch = compute_activation_exit_epoch(get_current_epoch(state))
exit_queue_churn: uint64
# Compute max exit epoch
for idx in 0..<state.validators.len:
let exit_epoch = state.validators.item(idx).exit_epoch
if exit_epoch != FAR_FUTURE_EPOCH and exit_epoch > exit_queue_epoch:
exit_queue_epoch = exit_epoch
var exit_queue_churn: uint64
let validator_churn_limit = get_validator_churn_limit(cfg, state, cache)
for idx in 0..<state.validators.len:
if state.validators.item(idx).exit_epoch == exit_queue_epoch:
# Reset exit queue churn counter as the expected exit_queue_epoch updates
# via this essentially max()-but-not-FAR_FUTURE_EPOCH loop to restart the
# counting the second for loop in spec version does. Only the last count,
# the one corresponding to the ultimately correct exit_queue_epoch, won't
# be reset.
exit_queue_churn = 0
# Second spec loop body, which there is responsible for taking the already
# known exit_queue_epoch, scanning for all validators with that exit epoch
# and checking if they'll reach validator_churn_limit(state). Do that here
# incrementally to fuse the two loops and save an all-validator iteration.
if exit_epoch == exit_queue_epoch:
inc exit_queue_churn
# In spec version, this check occurs only after looping through all
# validators, but it is ultimately a binary decision. Once matching
# get_validator_churn_limit(state), it can't validly become less.
if exit_queue_churn >= validator_churn_limit:
inc exit_queue_epoch
break
if exit_queue_churn >= get_validator_churn_limit(cfg, state, cache):
inc exit_queue_epoch
# Set validator exit epoch and withdrawable epoch
validator.exit_epoch = exit_queue_epoch