more initialize_validator_exit optimization (#6146)
This commit is contained in:
parent
ad2299cd8c
commit
7a3edb6961
|
@ -11,13 +11,11 @@ import
|
||||||
stew/assign2,
|
stew/assign2,
|
||||||
json_serialization/std/sets,
|
json_serialization/std/sets,
|
||||||
chronicles,
|
chronicles,
|
||||||
../extras,
|
|
||||||
./datatypes/[phase0, altair, bellatrix],
|
./datatypes/[phase0, altair, bellatrix],
|
||||||
"."/[eth2_merkleization, forks, signatures, validator]
|
"."/[eth2_merkleization, forks, signatures, validator]
|
||||||
|
|
||||||
from std/algorithm import fill
|
from std/algorithm import fill
|
||||||
from std/sequtils import anyIt, mapIt, toSeq
|
from std/sequtils import anyIt, mapIt, toSeq
|
||||||
|
|
||||||
from ./datatypes/capella import BeaconState, ExecutionPayloadHeader, Withdrawal
|
from ./datatypes/capella import BeaconState, ExecutionPayloadHeader, Withdrawal
|
||||||
|
|
||||||
export extras, forks, validator, chronicles
|
export extras, forks, validator, chronicles
|
||||||
|
@ -104,25 +102,31 @@ func initiate_validator_exit*(
|
||||||
# Return if validator already initiated exit
|
# Return if validator already initiated exit
|
||||||
let validator = addr state.validators.mitem(index)
|
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
|
# Compute max exit epoch
|
||||||
for idx in 0..<state.validators.len:
|
for idx in 0..<state.validators.len:
|
||||||
let exit_epoch = state.validators.item(idx).exit_epoch
|
let exit_epoch = state.validators.item(idx).exit_epoch
|
||||||
if exit_epoch != FAR_FUTURE_EPOCH and exit_epoch > exit_queue_epoch:
|
if exit_epoch != FAR_FUTURE_EPOCH and exit_epoch > exit_queue_epoch:
|
||||||
exit_queue_epoch = exit_epoch
|
exit_queue_epoch = exit_epoch
|
||||||
|
|
||||||
var exit_queue_churn: uint64
|
# Reset exit queue churn counter as the expected exit_queue_epoch updates
|
||||||
let validator_churn_limit = get_validator_churn_limit(cfg, state, cache)
|
# via this essentially max()-but-not-FAR_FUTURE_EPOCH loop to restart the
|
||||||
for idx in 0..<state.validators.len:
|
# counting the second for loop in spec version does. Only the last count,
|
||||||
if state.validators.item(idx).exit_epoch == exit_queue_epoch:
|
# 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
|
inc exit_queue_churn
|
||||||
|
|
||||||
# In spec version, this check occurs only after looping through all
|
if exit_queue_churn >= get_validator_churn_limit(cfg, state, cache):
|
||||||
# validators, but it is ultimately a binary decision. Once matching
|
inc exit_queue_epoch
|
||||||
# get_validator_churn_limit(state), it can't validly become less.
|
|
||||||
if exit_queue_churn >= validator_churn_limit:
|
|
||||||
inc exit_queue_epoch
|
|
||||||
break
|
|
||||||
|
|
||||||
# Set validator exit epoch and withdrawable epoch
|
# Set validator exit epoch and withdrawable epoch
|
||||||
validator.exit_epoch = exit_queue_epoch
|
validator.exit_epoch = exit_queue_epoch
|
||||||
|
|
Loading…
Reference in New Issue