Add in Balance Safety Check (#9419)

Co-authored-by: terence tsao <terence@prysmaticlabs.com>
Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com>
This commit is contained in:
Nishant Das
2021-09-29 06:19:10 +08:00
committed by GitHub
parent 8f8ccf11e4
commit 393549ad19
8 changed files with 103 additions and 12 deletions

View File

@@ -180,7 +180,10 @@ func ProcessSlashings(state state.BeaconState, slashingMultiplier uint64) (state
slashings := state.Slashings()
totalSlashing := uint64(0)
for _, slashing := range slashings {
totalSlashing += slashing
totalSlashing, err = math.Add64(totalSlashing, slashing)
if err != nil {
return nil, err
}
}
// a callback is used here to apply the following actions to all validators

View File

@@ -3,6 +3,7 @@ package epoch_test
import (
"context"
"fmt"
"math"
"testing"
types "github.com/prysmaticlabs/eth2-types"
@@ -440,3 +441,16 @@ func buildState(t testing.TB, slot types.Slot, validatorCount uint64) state.Beac
}
return s
}
func TestProcessSlashings_BadValue(t *testing.T) {
base := &ethpb.BeaconState{
Slot: 0,
Validators: []*ethpb.Validator{{Slashed: true}},
Balances: []uint64{params.BeaconConfig().MaxEffectiveBalance},
Slashings: []uint64{math.MaxUint64, 1e9},
}
s, err := v1.InitializeFromProto(base)
require.NoError(t, err)
_, err = epoch.ProcessSlashings(s, params.BeaconConfig().ProportionalSlashingMultiplier)
require.ErrorContains(t, "addition overflows", err)
}

View File

@@ -47,7 +47,10 @@ func ProcessRewardsAndPenaltiesPrecompute(
// Compute the post balance of the validator after accounting for the
// attester and proposer rewards and penalties.
validatorBals[i] = helpers.IncreaseBalanceWithVal(validatorBals[i], attsRewards[i]+proposerRewards[i])
validatorBals[i], err = helpers.IncreaseBalanceWithVal(validatorBals[i], attsRewards[i]+proposerRewards[i])
if err != nil {
return nil, err
}
validatorBals[i] = helpers.DecreaseBalanceWithVal(validatorBals[i], attsPenalties[i])
vp[i].AfterEpochTransitionBalance = validatorBals[i]