Skip to content

Commit

Permalink
add check not to jail validator if its the last one
Browse files Browse the repository at this point in the history
  • Loading branch information
Vizualni committed Aug 11, 2022
1 parent 14dd34e commit d5569a4
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
2 changes: 2 additions & 0 deletions x/valset/keeper/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ const (
ErrSigningKeyNotFound = whoops.Errorf("signing key for valAddr %s, chainType %s and chainReferenceID %s not found")
ErrExternalChainAlreadyRegistered = whoops.Errorf("external account already registered: %s, %s, %s. Existing owner: %s, New owner: %s")
ErrExternalAddressNotFound = whoops.Errorf("external address (%s, %s, %s) for validator %s was not founds")

ErrCannotJailValidator = whoops.Errorf("cannot jail validator: %s")
)
22 changes: 16 additions & 6 deletions x/valset/keeper/keep_alive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"time"

sdk "github.com/cosmos/cosmos-sdk/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/palomachain/paloma/x/valset/types/mocks"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
Expand All @@ -15,7 +16,7 @@ func TestJailingInactiveValidators(t *testing.T) {
k, ms, ctx := newValsetKeeper(t)
ctx = ctx.WithBlockTime(time.Unix(1000000000, 0))

valBuild := func(id int, alive bool) sdk.ValAddress {
valBuild := func(id int, alive bool) *mocks.StakingValidatorI {
val := sdk.ValAddress(fmt.Sprintf("validator_%d", id))
vali := mocks.NewStakingValidatorI(t)
ms.StakingKeeper.On("Validator", mock.Anything, val).Return(vali)
Expand All @@ -32,12 +33,21 @@ func TestJailingInactiveValidators(t *testing.T) {
err := k.KeepValidatorAlive(ctx.WithBlockTime(ctx.BlockTime().Add(-defaultKeepAliveDuration/2)), val)
require.NoError(t, err)
}
return val
return vali
}
valBuild(1, false)
valBuild(2, false)
valBuild(3, true)
valBuild(4, true)

v1 := valBuild(1, false)
v2 := valBuild(2, false)
v3 := valBuild(3, true)
v4 := valBuild(4, true)

ms.StakingKeeper.On("IterateValidators", mock.Anything, mock.Anything).Run(func(args mock.Arguments) {
callback := args.Get(1).(func(int64, stakingtypes.ValidatorI) bool)
callback(0, v1)
callback(0, v2)
callback(0, v3)
callback(0, v4)
}).Return(false)

err := k.JailInactiveValidators(ctx)
require.NoError(t, err)
Expand Down
10 changes: 10 additions & 0 deletions x/valset/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,16 @@ func (k Keeper) IsJailed(ctx sdk.Context, val sdk.ValAddress) bool {

func (k Keeper) Jail(ctx sdk.Context, valAddr sdk.ValAddress, reason string) error {
val := k.staking.Validator(ctx, valAddr)
count := 0
k.staking.IterateValidators(ctx, func(_ int64, val stakingtypes.ValidatorI) bool {
if val.IsBonded() && !val.IsJailed() {
count++
}
return false
})
if count == 1 {
return ErrCannotJailValidator.Format(valAddr).WrapS("number of active validators would be zero then")
}
cons, err := val.GetConsAddr()
if err != nil {
return err
Expand Down

0 comments on commit d5569a4

Please sign in to comment.