Skip to content

Commit f264680

Browse files
saolynjames-prysm
andauthored
Simplify ExitedValidatorIndices (#14587)
* fix * add to comment * modify test * remove unused parameter * changelog * exclude ejected from exited * fix linter --------- Co-authored-by: james-prysm <[email protected]>
1 parent 8fe024f commit f264680

File tree

4 files changed

+18
-53
lines changed

4 files changed

+18
-53
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ The format is based on Keep a Changelog, and this project adheres to Semantic Ve
1818
- Updated the `beacon-chain/monitor` package to Electra. [PR](https://github.com/prysmaticlabs/prysm/pull/14562)
1919
- Added ListAttestationsV2 endpoint.
2020
- Add ability to rollback node's internal state during processing.
21-
- Simplified `EjectedValidatorIndices`.
2221

2322
### Changed
2423

@@ -36,6 +35,8 @@ The format is based on Keep a Changelog, and this project adheres to Semantic Ve
3635
- Updated pgo profile for beacon chain with holesky data. This improves the profile guided
3736
optimizations in the go compiler.
3837
- Use read only state when computing the active validator list.
38+
- Simplified `ExitedValidatorIndices`.
39+
- Simplified `EjectedValidatorIndices`.
3940

4041
### Deprecated
4142

beacon-chain/core/validators/validator.go

Lines changed: 8 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -241,38 +241,16 @@ func SlashedValidatorIndices(epoch primitives.Epoch, validators []*ethpb.Validat
241241
return slashed
242242
}
243243

244-
// ExitedValidatorIndices determines the indices exited during the current epoch.
245-
func ExitedValidatorIndices(epoch primitives.Epoch, validators []*ethpb.Validator, activeValidatorCount uint64) ([]primitives.ValidatorIndex, error) {
244+
// ExitedValidatorIndices returns the indices of validators who exited during the specified epoch.
245+
//
246+
// A validator is considered to have exited during an epoch if their ExitEpoch equals the epoch and
247+
// excludes validators that have been ejected.
248+
// This function simplifies the exit determination by directly checking the validator's ExitEpoch,
249+
// avoiding the complexities and potential inaccuracies of calculating withdrawable epochs.
250+
func ExitedValidatorIndices(epoch primitives.Epoch, validators []*ethpb.Validator) ([]primitives.ValidatorIndex, error) {
246251
exited := make([]primitives.ValidatorIndex, 0)
247-
exitEpochs := make([]primitives.Epoch, 0)
248-
for i := 0; i < len(validators); i++ {
249-
val := validators[i]
250-
if val.ExitEpoch != params.BeaconConfig().FarFutureEpoch {
251-
exitEpochs = append(exitEpochs, val.ExitEpoch)
252-
}
253-
}
254-
exitQueueEpoch := primitives.Epoch(0)
255-
for _, i := range exitEpochs {
256-
if exitQueueEpoch < i {
257-
exitQueueEpoch = i
258-
}
259-
}
260-
261-
// We use the exit queue churn to determine if we have passed a churn limit.
262-
exitQueueChurn := uint64(0)
263-
for _, val := range validators {
264-
if val.ExitEpoch == exitQueueEpoch {
265-
exitQueueChurn++
266-
}
267-
}
268-
churn := helpers.ValidatorExitChurnLimit(activeValidatorCount)
269-
if churn < exitQueueChurn {
270-
exitQueueEpoch++
271-
}
272-
withdrawableEpoch := exitQueueEpoch + params.BeaconConfig().MinValidatorWithdrawabilityDelay
273252
for i, val := range validators {
274-
if val.ExitEpoch == epoch && val.WithdrawableEpoch == withdrawableEpoch &&
275-
val.EffectiveBalance > params.BeaconConfig().EjectionBalance {
253+
if val.ExitEpoch == epoch && val.EffectiveBalance > params.BeaconConfig().EjectionBalance {
276254
exited = append(exited, primitives.ValidatorIndex(i))
277255
}
278256
}

beacon-chain/core/validators/validator_test.go

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -389,19 +389,16 @@ func TestExitedValidatorIndices(t *testing.T) {
389389
state: &ethpb.BeaconState{
390390
Validators: []*ethpb.Validator{
391391
{
392-
EffectiveBalance: params.BeaconConfig().MaxEffectiveBalance,
393-
ExitEpoch: 0,
394-
WithdrawableEpoch: params.BeaconConfig().MinValidatorWithdrawabilityDelay,
392+
EffectiveBalance: params.BeaconConfig().MaxEffectiveBalance,
393+
ExitEpoch: 0,
395394
},
396395
{
397-
EffectiveBalance: params.BeaconConfig().MaxEffectiveBalance,
398-
ExitEpoch: 0,
399-
WithdrawableEpoch: 10,
396+
EffectiveBalance: params.BeaconConfig().MaxEffectiveBalance,
397+
ExitEpoch: 10,
400398
},
401399
{
402-
EffectiveBalance: params.BeaconConfig().MaxEffectiveBalance,
403-
ExitEpoch: 0,
404-
WithdrawableEpoch: params.BeaconConfig().MinValidatorWithdrawabilityDelay,
400+
EffectiveBalance: params.BeaconConfig().MaxEffectiveBalance,
401+
ExitEpoch: 0,
405402
},
406403
},
407404
},
@@ -433,11 +430,7 @@ func TestExitedValidatorIndices(t *testing.T) {
433430
},
434431
}
435432
for _, tt := range tests {
436-
s, err := state_native.InitializeFromProtoPhase0(tt.state)
437-
require.NoError(t, err)
438-
activeCount, err := helpers.ActiveValidatorCount(context.Background(), s, time.PrevEpoch(s))
439-
require.NoError(t, err)
440-
exitedIndices, err := validators.ExitedValidatorIndices(0, tt.state.Validators, activeCount)
433+
exitedIndices, err := validators.ExitedValidatorIndices(0, tt.state.Validators)
441434
require.NoError(t, err)
442435
assert.DeepEqual(t, tt.wanted, exitedIndices)
443436
}

beacon-chain/rpc/core/validator.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -869,16 +869,9 @@ func (s *Service) ValidatorActiveSetChanges(
869869
}
870870
}
871871

872-
activeValidatorCount, err := helpers.ActiveValidatorCount(ctx, requestedState, coreTime.CurrentEpoch(requestedState))
873-
if err != nil {
874-
return nil, &RpcError{
875-
Err: errors.Wrap(err, "could not get active validator count"),
876-
Reason: Internal,
877-
}
878-
}
879872
vs := requestedState.Validators()
880873
activatedIndices := validators.ActivatedValidatorIndices(coreTime.CurrentEpoch(requestedState), vs)
881-
exitedIndices, err := validators.ExitedValidatorIndices(coreTime.CurrentEpoch(requestedState), vs, activeValidatorCount)
874+
exitedIndices, err := validators.ExitedValidatorIndices(coreTime.CurrentEpoch(requestedState), vs)
882875
if err != nil {
883876
return nil, &RpcError{
884877
Err: errors.Wrap(err, "could not determine exited validator indices"),

0 commit comments

Comments
 (0)