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

Prevent unrevoked validators from unrevoking #1511

Merged
merged 9 commits into from
Jul 3, 2018
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ IMPROVEMENTS
* added contributing guidelines

BUG FIXES
* [x/slashing] \#1510 Unrevoked validators cannot un-revoke themselves
* [gaia] Added self delegation for validators in the genesis creation
* [lcd] tests now don't depend on raw json text
* [stake] error strings lower case
Expand Down
8 changes: 6 additions & 2 deletions x/slashing/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ const (
// Default slashing codespace
DefaultCodespace sdk.CodespaceType = 10

CodeInvalidValidator CodeType = 101
CodeValidatorJailed CodeType = 102
CodeInvalidValidator CodeType = 101
CodeValidatorJailed CodeType = 102
CodeValidatorNotRevoked CodeType = 103
)

func ErrNoValidatorForAddress(codespace sdk.CodespaceType) sdk.Error {
Expand All @@ -25,3 +26,6 @@ func ErrBadValidatorAddr(codespace sdk.CodespaceType) sdk.Error {
func ErrValidatorJailed(codespace sdk.CodespaceType) sdk.Error {
return sdk.NewError(codespace, CodeValidatorJailed, "validator jailed, cannot yet be unrevoked")
}
func ErrValidatorNotRevoked(codespace sdk.CodespaceType) sdk.Error {
return sdk.NewError(codespace, CodeValidatorNotRevoked, "validator not revoked, cannot be unrevoked")
}
4 changes: 4 additions & 0 deletions x/slashing/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ func handleMsgUnrevoke(ctx sdk.Context, msg MsgUnrevoke, k Keeper) sdk.Result {
return ErrNoValidatorForAddress(k.codespace).Result()
}

if !validator.GetRevoked() {
return ErrValidatorNotRevoked(k.codespace).Result()
}

addr := validator.GetPubKey().Address()

// Signing info must exist
Expand Down
28 changes: 28 additions & 0 deletions x/slashing/handler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package slashing

import (
"testing"

"github.com/stretchr/testify/require"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/stake"
)

func TestCannotUnrevokeUnlessRevoked(t *testing.T) {
// initial setup
ctx, ck, sk, keeper := createTestInput(t)
slh := NewHandler(keeper)
amtInt := int64(100)
addr, val, amt := addrs[0], pks[0], sdk.NewInt(amtInt)
got := stake.NewHandler(sk)(ctx, newTestMsgCreateValidator(addr, val, amt))
require.True(t, got.IsOK())
stake.EndBlocker(ctx, sk)
require.Equal(t, ck.GetCoins(ctx, addr), sdk.Coins{{sk.GetParams(ctx).BondDenom, initCoins.Sub(amt)}})
require.True(t, sdk.NewRatFromInt(amt).Equal(sk.Validator(ctx, addr).GetPower()))

// assert non-revoked validator can't be unrevoked
got = slh(ctx, NewMsgUnrevoke(addr))
require.False(t, got.IsOK(), "allowed unrevoke of non-revoked validator")
require.Equal(t, sdk.ToABCICode(DefaultCodespace, CodeValidatorNotRevoked), got.Code)
}