Check for array out of bounds when calculating proposer delta (#6629)

* Check for array out of bounds when calculating proposer delta
* Merge refs/heads/master into proposer-delta-bug
This commit is contained in:
Preston Van Loon 2020-07-17 21:31:03 -07:00 committed by GitHub
parent 0e4cb68249
commit 5336a167af
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 0 deletions

View File

@ -162,6 +162,10 @@ func ProposersDelta(state *stateTrie.BeaconState, pBal *Balance, vp []*Validator
baseRewardsPerEpoch := params.BeaconConfig().BaseRewardsPerEpoch
proposerRewardQuotient := params.BeaconConfig().ProposerRewardQuotient
for _, v := range vp {
if v.ProposerIndex > uint64(len(rewards)) {
// This should never happen with a valid state / validator.
return nil, errors.New("proposer index out of range")
}
// Only apply inclusion rewards to proposer only if the attested hasn't been slashed.
if v.IsPrevEpochAttester && !v.IsSlashed {
vBalance := v.CurrentEpochEffectiveBalance

View File

@ -359,6 +359,26 @@ func TestProposerDeltaPrecompute_HappyCase(t *testing.T) {
}
}
func TestProposerDeltaPrecompute_ValidatorIndexOutOfRange(t *testing.T) {
e := params.BeaconConfig().SlotsPerEpoch
validatorCount := uint64(10)
base := buildState(e, validatorCount)
state, err := state.InitializeFromProto(base)
if err != nil {
t.Fatal(err)
}
proposerIndex := validatorCount + 1
b := &Balance{ActiveCurrentEpoch: 1000}
v := []*Validator{
{IsPrevEpochAttester: true, CurrentEpochEffectiveBalance: 32, ProposerIndex: proposerIndex},
}
_, err = ProposersDelta(state, b, v)
if err == nil {
t.Fatal("Expected an error with invalid proposer index")
}
}
func TestProposerDeltaPrecompute_SlashedCase(t *testing.T) {
e := params.BeaconConfig().SlotsPerEpoch
validatorCount := uint64(10)