Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exit early if there's no validator slashing #7587

Merged
merged 9 commits into from
Oct 21, 2020
21 changes: 21 additions & 0 deletions beacon-chain/core/epoch/precompute/slashing.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package precompute

import (
"errors"

ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
"github.com/prysmaticlabs/prysm/beacon-chain/core/helpers"
stateTrie "github.com/prysmaticlabs/prysm/beacon-chain/state"
Expand All @@ -23,6 +25,25 @@ func ProcessSlashingsPrecompute(state *stateTrie.BeaconState, pBal *Balance) err

minSlashing := mathutil.Min(totalSlashing*params.BeaconConfig().ProportionalSlashingMultiplier, pBal.ActiveCurrentEpoch)
epochToWithdraw := currentEpoch + exitLength/2

vs := state.ValidatorsReadOnly()
var hasSlashing bool
// Iterate through validator list in state, stop until a validator satisfies slashing condition of current epoch.
for _, v := range vs {
if v == nil {
return errors.New("nil validator in state")
}
correctEpoch := epochToWithdraw == v.WithdrawableEpoch()
if v.Slashed() && correctEpoch {
hasSlashing = true
break
}
}
// Exit early if there's no meaningful slashing to process.
if !hasSlashing {
return nil
}

increment := params.BeaconConfig().EffectiveBalanceIncrement
validatorFunc := func(idx int, val *ethpb.Validator) (bool, error) {
correctEpoch := epochToWithdraw == val.WithdrawableEpoch
Expand Down
17 changes: 16 additions & 1 deletion beacon-chain/core/epoch/precompute/slashing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/prysmaticlabs/prysm/shared/testutil/require"
)

func TestProcessSlashingsPrecompute_NotSlashed(t *testing.T) {
func TestProcessSlashingsPrecompute_NotSlashedWithSlashedTrue(t *testing.T) {
s, err := beaconstate.InitializeFromProto(&pb.BeaconState{
Slot: 0,
Validators: []*ethpb.Validator{{Slashed: true}},
Expand All @@ -28,6 +28,21 @@ func TestProcessSlashingsPrecompute_NotSlashed(t *testing.T) {
assert.Equal(t, wanted, s.Balances()[0], "Unexpected slashed balance")
}

func TestProcessSlashingsPrecompute_NotSlashedWithSlashedFalse(t *testing.T) {
s, err := beaconstate.InitializeFromProto(&pb.BeaconState{
Slot: 0,
Validators: []*ethpb.Validator{{}},
Balances: []uint64{params.BeaconConfig().MaxEffectiveBalance},
Slashings: []uint64{0, 1e9},
})
require.NoError(t, err)
pBal := &precompute.Balance{ActiveCurrentEpoch: params.BeaconConfig().MaxEffectiveBalance}
require.NoError(t, precompute.ProcessSlashingsPrecompute(s, pBal))

wanted := params.BeaconConfig().MaxEffectiveBalance
assert.Equal(t, wanted, s.Balances()[0], "Unexpected slashed balance")
}

func TestProcessSlashingsPrecompute_SlashedLess(t *testing.T) {
tests := []struct {
state *pb.BeaconState
Expand Down