Change non-mutating validator accesses to ReadOnly (#5776)

* Change instances of ValidatorAtIndex to ReadOnly where possible

* Use ReadOnly for VerifyExit and Slashings

* Move length check to before lock

* Improve readonly tests

* undo process attester changes

* Fix test
This commit is contained in:
Ivan Martinez
2020-05-07 14:15:51 -04:00
committed by GitHub
parent 574bf3deac
commit 9a1157465e
10 changed files with 234 additions and 105 deletions

View File

@@ -363,11 +363,11 @@ func unslashedAttestingIndices(state *stateTrie.BeaconState, atts []*pb.PendingA
sort.Slice(setIndices, func(i, j int) bool { return setIndices[i] < setIndices[j] })
// Remove the slashed validator indices.
for i := 0; i < len(setIndices); i++ {
v, err := state.ValidatorAtIndex(setIndices[i])
v, err := state.ValidatorAtIndexReadOnly(setIndices[i])
if err != nil {
return nil, errors.Wrap(err, "failed to look up validator")
}
if v != nil && v.Slashed {
if v != nil && v.Slashed() {
setIndices = append(setIndices[:i], setIndices[i+1:]...)
}
}
@@ -391,11 +391,11 @@ func BaseReward(state *stateTrie.BeaconState, index uint64) (uint64, error) {
if err != nil {
return 0, errors.Wrap(err, "could not calculate active balance")
}
val, err := state.ValidatorAtIndex(index)
val, err := state.ValidatorAtIndexReadOnly(index)
if err != nil {
return 0, err
}
effectiveBalance := val.EffectiveBalance
effectiveBalance := val.EffectiveBalance()
baseReward := effectiveBalance * params.BeaconConfig().BaseRewardFactor /
mathutil.IntegerSquareRoot(totalBalance) / params.BeaconConfig().BaseRewardsPerEpoch
return baseReward, nil