Skip to content

Commit

Permalink
migrate TestJailedValidatorDelegations to simapp
Browse files Browse the repository at this point in the history
  • Loading branch information
jgimeno committed Mar 3, 2020
1 parent 210d78b commit c97a1b6
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 74 deletions.
81 changes: 81 additions & 0 deletions x/slashing/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package slashing_test

import (
"errors"
"strings"
"testing"
"time"

"github.com/cosmos/cosmos-sdk/simapp"
abci "github.com/tendermint/tendermint/abci/types"
Expand Down Expand Up @@ -84,3 +86,82 @@ func TestCannotUnjailUnlessMeetMinSelfDelegation(t *testing.T) {
require.Nil(t, res)
require.True(t, errors.Is(slashing.ErrSelfDelegationTooLowToUnjail, err))
}

func TestJailedValidatorDelegations(t *testing.T) {
// initial setup
app := simapp.Setup(false)
ctx := app.BaseApp.NewContext(false, abci.Header{Time: time.Unix(0, 0)})

pks := simapp.CreateTestPubKeys(3)
simapp.AddTestAddrsFromPubKeys(app, ctx, pks, sdk.TokensFromConsensusPower(20))
app.SlashingKeeper.SetParams(ctx, slashingkeeper.TestParams())

stakingParams := app.StakingKeeper.GetParams(ctx)
app.StakingKeeper.SetParams(ctx, stakingParams)

// create a validator
bondAmount := sdk.TokensFromConsensusPower(10)
valPubKey := pks[1]
valAddr, consAddr := sdk.ValAddress(pks[1].Address()), sdk.ConsAddress(pks[0].Address())

msgCreateVal := slashingkeeper.NewTestMsgCreateValidator(valAddr, valPubKey, bondAmount)
res, err := staking.NewHandler(app.StakingKeeper)(ctx, msgCreateVal)
require.NoError(t, err)
require.NotNil(t, res)

// end block
staking.EndBlocker(ctx, app.StakingKeeper)

// set dummy signing info
newInfo := slashing.NewValidatorSigningInfo(consAddr, 0, 0, time.Unix(0, 0), false, 0)
app.SlashingKeeper.SetValidatorSigningInfo(ctx, consAddr, newInfo)

// delegate tokens to the validator
delAddr := sdk.AccAddress(pks[2].Address())
msgDelegate := slashingkeeper.NewTestMsgDelegate(delAddr, valAddr, bondAmount)
res, err = staking.NewHandler(app.StakingKeeper)(ctx, msgDelegate)
require.NoError(t, err)
require.NotNil(t, res)

unbondAmt := sdk.NewCoin(app.StakingKeeper.GetParams(ctx).BondDenom, bondAmount)

// unbond validator total self-delegations (which should jail the validator)
msgUndelegate := staking.NewMsgUndelegate(sdk.AccAddress(valAddr), valAddr, unbondAmt)
res, err = staking.NewHandler(app.StakingKeeper)(ctx, msgUndelegate)
require.NoError(t, err)
require.NotNil(t, res)

err = app.StakingKeeper.CompleteUnbonding(ctx, sdk.AccAddress(valAddr), valAddr)
require.Nil(t, err, "expected complete unbonding validator to be ok, got: %v", err)

// verify validator still exists and is jailed
validator, found := app.StakingKeeper.GetValidator(ctx, valAddr)
require.True(t, found)
require.True(t, validator.IsJailed())

// verify the validator cannot unjail itself
res, err = slashing.NewHandler(app.SlashingKeeper)(ctx, slashing.NewMsgUnjail(valAddr))
require.Error(t, err)
require.Nil(t, res)

// self-delegate to validator
msgSelfDelegate := slashingkeeper.NewTestMsgDelegate(sdk.AccAddress(valAddr), valAddr, bondAmount)
res, err = staking.NewHandler(app.StakingKeeper)(ctx, msgSelfDelegate)
require.NoError(t, err)
require.NotNil(t, res)

// verify the validator can now unjail itself
res, err = slashing.NewHandler(app.SlashingKeeper)(ctx, slashing.NewMsgUnjail(valAddr))
require.NoError(t, err)
require.NotNil(t, res)
}

func TestInvalidMsg(t *testing.T) {
k := slashing.Keeper{}
h := slashing.NewHandler(k)

res, err := h(sdk.NewContext(nil, abci.Header{}, false, nil), sdk.NewTestMsg())
require.Error(t, err)
require.Nil(t, res)
require.True(t, strings.Contains(err.Error(), "unrecognized slashing message type"))
}
74 changes: 0 additions & 74 deletions x/slashing/old_handler_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package slashing_test

import (
"strings"
"testing"
"time"

Expand All @@ -15,79 +14,6 @@ import (
"github.com/cosmos/cosmos-sdk/x/staking"
)

func TestJailedValidatorDelegations(t *testing.T) {
ctx, _, stakingKeeper, _, slashingKeeper := slashingkeeper.CreateTestInput(t, slashing.DefaultParams())

stakingParams := stakingKeeper.GetParams(ctx)
stakingKeeper.SetParams(ctx, stakingParams)

// create a validator
bondAmount := sdk.TokensFromConsensusPower(10)
valPubKey := slashingkeeper.Pks[0]
valAddr, consAddr := slashingkeeper.Addrs[1], sdk.ConsAddress(slashingkeeper.Addrs[0])

msgCreateVal := slashingkeeper.NewTestMsgCreateValidator(valAddr, valPubKey, bondAmount)
res, err := staking.NewHandler(stakingKeeper)(ctx, msgCreateVal)
require.NoError(t, err)
require.NotNil(t, res)

// end block
staking.EndBlocker(ctx, stakingKeeper)

// set dummy signing info
newInfo := slashing.NewValidatorSigningInfo(consAddr, 0, 0, time.Unix(0, 0), false, 0)
slashingKeeper.SetValidatorSigningInfo(ctx, consAddr, newInfo)

// delegate tokens to the validator
delAddr := sdk.AccAddress(slashingkeeper.Addrs[2])
msgDelegate := slashingkeeper.NewTestMsgDelegate(delAddr, valAddr, bondAmount)
res, err = staking.NewHandler(stakingKeeper)(ctx, msgDelegate)
require.NoError(t, err)
require.NotNil(t, res)

unbondAmt := sdk.NewCoin(stakingKeeper.GetParams(ctx).BondDenom, bondAmount)

// unbond validator total self-delegations (which should jail the validator)
msgUndelegate := staking.NewMsgUndelegate(sdk.AccAddress(valAddr), valAddr, unbondAmt)
res, err = staking.NewHandler(stakingKeeper)(ctx, msgUndelegate)
require.NoError(t, err)
require.NotNil(t, res)

err = stakingKeeper.CompleteUnbonding(ctx, sdk.AccAddress(valAddr), valAddr)
require.Nil(t, err, "expected complete unbonding validator to be ok, got: %v", err)

// verify validator still exists and is jailed
validator, found := stakingKeeper.GetValidator(ctx, valAddr)
require.True(t, found)
require.True(t, validator.IsJailed())

// verify the validator cannot unjail itself
res, err = slashing.NewHandler(slashingKeeper)(ctx, slashing.NewMsgUnjail(valAddr))
require.Error(t, err)
require.Nil(t, res)

// self-delegate to validator
msgSelfDelegate := slashingkeeper.NewTestMsgDelegate(sdk.AccAddress(valAddr), valAddr, bondAmount)
res, err = staking.NewHandler(stakingKeeper)(ctx, msgSelfDelegate)
require.NoError(t, err)
require.NotNil(t, res)

// verify the validator can now unjail itself
res, err = slashing.NewHandler(slashingKeeper)(ctx, slashing.NewMsgUnjail(valAddr))
require.NoError(t, err)
require.NotNil(t, res)
}

func TestInvalidMsg(t *testing.T) {
k := slashing.Keeper{}
h := slashing.NewHandler(k)

res, err := h(sdk.NewContext(nil, abci.Header{}, false, nil), sdk.NewTestMsg())
require.Error(t, err)
require.Nil(t, res)
require.True(t, strings.Contains(err.Error(), "unrecognized slashing message type"))
}

// Test a validator through uptime, downtime, revocation,
// unrevocation, starting height reset, and revocation again
func TestHandleAbsentValidator(t *testing.T) {
Expand Down

0 comments on commit c97a1b6

Please sign in to comment.