cosmos-sdk/x/slashing/keeper_test.go

172 lines
6.2 KiB
Go
Raw Normal View History

2018-05-23 13:25:56 -07:00
package slashing
import (
"testing"
"github.com/stretchr/testify/require"
abci "github.com/tendermint/tendermint/abci/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/stake"
)
// Test that a validator is slashed correctly
// when we discover evidence of equivocation
2018-05-23 13:25:56 -07:00
func TestHandleDoubleSign(t *testing.T) {
// initial setup
ctx, ck, sk, keeper := createTestInput(t)
addr, val, amt := addrs[0], pks[0], sdk.NewInt(100)
2018-05-31 12:22:46 -07:00
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()))
// double sign less than max age
keeper.handleDoubleSign(ctx, 0, 0, val)
require.Equal(t, sdk.NewRatFromInt(amt).Mul(sdk.NewRat(19).Quo(sdk.NewRat(20))), sk.Validator(ctx, addr).GetPower())
2018-05-24 16:30:43 -07:00
ctx = ctx.WithBlockHeader(abci.Header{Time: 300})
// double sign past max age
keeper.handleDoubleSign(ctx, 0, 0, val)
require.Equal(t, sdk.NewRatFromInt(amt).Mul(sdk.NewRat(19).Quo(sdk.NewRat(20))), sk.Validator(ctx, addr).GetPower())
2018-05-23 13:25:56 -07:00
}
// Test a validator through uptime, downtime, revocation,
// unrevocation, starting height reset, and revocation again
2018-05-23 13:25:56 -07:00
func TestHandleAbsentValidator(t *testing.T) {
// initial setup
2018-05-24 18:30:17 -07:00
ctx, ck, sk, keeper := createTestInput(t)
addr, val, amt := addrs[0], pks[0], sdk.NewInt(100)
2018-05-25 15:13:29 -07:00
sh := stake.NewHandler(sk)
2018-05-28 12:38:02 -07:00
slh := NewHandler(keeper)
2018-05-31 12:22:46 -07:00
got := sh(ctx, newTestMsgCreateValidator(addr, val, amt))
2018-05-24 18:30:17 -07:00
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()))
2018-05-24 18:30:17 -07:00
info, found := keeper.getValidatorSigningInfo(ctx, val.Address())
require.False(t, found)
require.Equal(t, int64(0), info.StartHeight)
2018-05-28 15:10:52 -07:00
require.Equal(t, int64(0), info.IndexOffset)
2018-05-24 18:30:17 -07:00
require.Equal(t, int64(0), info.SignedBlocksCounter)
2018-05-28 15:10:52 -07:00
require.Equal(t, int64(0), info.JailedUntil)
2018-05-24 18:30:17 -07:00
height := int64(0)
// 1000 first blocks OK
2018-05-24 18:30:17 -07:00
for ; height < 1000; height++ {
ctx = ctx.WithBlockHeight(height)
keeper.handleValidatorSignature(ctx, val, true)
}
info, found = keeper.getValidatorSigningInfo(ctx, val.Address())
require.True(t, found)
require.Equal(t, int64(0), info.StartHeight)
require.Equal(t, SignedBlocksWindow, info.SignedBlocksCounter)
2018-05-24 18:30:17 -07:00
// 50 blocks missed
for ; height < 1050; height++ {
ctx = ctx.WithBlockHeight(height)
keeper.handleValidatorSignature(ctx, val, false)
}
info, found = keeper.getValidatorSigningInfo(ctx, val.Address())
require.True(t, found)
require.Equal(t, int64(0), info.StartHeight)
require.Equal(t, SignedBlocksWindow-50, info.SignedBlocksCounter)
2018-05-28 15:10:52 -07:00
// validator should be bonded still
validator, _ := sk.GetValidatorByPubKey(ctx, val)
2018-05-25 15:13:29 -07:00
require.Equal(t, sdk.Bonded, validator.GetStatus())
2018-05-25 15:27:02 -07:00
pool := sk.GetPool(ctx)
Merge PR #1119: Unbonding, Redelegation * stake/fees spec updates * staking overview.md revisions, moving files * docs reorganization * staking spec state revisions * transaction stake updates * complete staking spec update * WIP adding unbonding/redelegation commands * added msg types for unbonding, redelegation * stake sub-package reorg * working stake reorg * modify lcd tests to not use hardcoded json strings * add description update * index keys * key managment for unbonding redelegation complete * update stake errors * completed handleMsgCompleteUnbonding fn * updated to use begin/complete unbonding/redelegation * fix token shares bug * develop docs into unbonding * got non-tests compiling after merge develop * working fixing tests * PrivlegedKeeper -> PrivilegedKeeper * tests compile * fix some tests * fixing tests * remove PrivilegedKeeper * get unbonding bug * only rpc sig verification failed tests now * move percent unbonding/redelegation to the CLI and out of handler logic * remove min unbonding height * add lcd txs * add pool sanity checks, fix a buncha tests * fix ante. set lcd log to debug (#1322) * redelegation tests, adding query functionality for bonds * add self-delegations at genesis ref #1165 * PR comments (mostly) addressed * cleanup, added Query LCD functionality * test cleanup/fixes * fix governance test * SlashValidatorSet -> ValidatorSet * changelog * stake lcd fix * x/auth: fix chainID in ante * fix lcd test * fix lint, update lint make command for spelling * lowercase error string * don't expose coinkeeper in staking * remove a few duplicate lines in changelog * chain_id in stake lcd tests * added transient redelegation * 'transient' => 'transitive' * Re-add nolint instruction * Fix tiny linter error
2018-06-26 19:00:12 -07:00
require.Equal(t, int64(100), pool.BondedTokens)
2018-05-24 18:30:17 -07:00
// 51st block missed
ctx = ctx.WithBlockHeight(height)
keeper.handleValidatorSignature(ctx, val, false)
info, found = keeper.getValidatorSigningInfo(ctx, val.Address())
require.True(t, found)
require.Equal(t, int64(0), info.StartHeight)
require.Equal(t, SignedBlocksWindow-51, info.SignedBlocksCounter)
2018-05-28 15:10:52 -07:00
// validator should have been revoked
validator, _ = sk.GetValidatorByPubKey(ctx, val)
2018-05-25 15:13:29 -07:00
require.Equal(t, sdk.Unbonded, validator.GetStatus())
2018-05-28 15:10:52 -07:00
// unrevocation should fail prior to jail expiration
2018-05-28 12:38:02 -07:00
got = slh(ctx, NewMsgUnrevoke(addr))
2018-05-28 15:10:52 -07:00
require.False(t, got.IsOK())
2018-05-28 15:10:52 -07:00
// unrevocation should succeed after jail expiration
2018-05-25 15:13:29 -07:00
ctx = ctx.WithBlockHeader(abci.Header{Time: int64(86400 * 2)})
2018-05-28 12:38:02 -07:00
got = slh(ctx, NewMsgUnrevoke(addr))
2018-05-28 15:10:52 -07:00
require.True(t, got.IsOK())
2018-05-28 15:10:52 -07:00
// validator should be rebonded now
validator, _ = sk.GetValidatorByPubKey(ctx, val)
2018-05-25 15:13:29 -07:00
require.Equal(t, sdk.Bonded, validator.GetStatus())
2018-05-28 15:10:52 -07:00
// validator should have been slashed
2018-05-25 15:36:12 -07:00
pool = sk.GetPool(ctx)
Merge PR #1119: Unbonding, Redelegation * stake/fees spec updates * staking overview.md revisions, moving files * docs reorganization * staking spec state revisions * transaction stake updates * complete staking spec update * WIP adding unbonding/redelegation commands * added msg types for unbonding, redelegation * stake sub-package reorg * working stake reorg * modify lcd tests to not use hardcoded json strings * add description update * index keys * key managment for unbonding redelegation complete * update stake errors * completed handleMsgCompleteUnbonding fn * updated to use begin/complete unbonding/redelegation * fix token shares bug * develop docs into unbonding * got non-tests compiling after merge develop * working fixing tests * PrivlegedKeeper -> PrivilegedKeeper * tests compile * fix some tests * fixing tests * remove PrivilegedKeeper * get unbonding bug * only rpc sig verification failed tests now * move percent unbonding/redelegation to the CLI and out of handler logic * remove min unbonding height * add lcd txs * add pool sanity checks, fix a buncha tests * fix ante. set lcd log to debug (#1322) * redelegation tests, adding query functionality for bonds * add self-delegations at genesis ref #1165 * PR comments (mostly) addressed * cleanup, added Query LCD functionality * test cleanup/fixes * fix governance test * SlashValidatorSet -> ValidatorSet * changelog * stake lcd fix * x/auth: fix chainID in ante * fix lcd test * fix lint, update lint make command for spelling * lowercase error string * don't expose coinkeeper in staking * remove a few duplicate lines in changelog * chain_id in stake lcd tests * added transient redelegation * 'transient' => 'transitive' * Re-add nolint instruction * Fix tiny linter error
2018-06-26 19:00:12 -07:00
require.Equal(t, int64(99), pool.BondedTokens)
2018-05-28 15:10:52 -07:00
// validator start height should have been changed
2018-05-28 14:55:39 -07:00
info, found = keeper.getValidatorSigningInfo(ctx, val.Address())
require.True(t, found)
require.Equal(t, height, info.StartHeight)
require.Equal(t, SignedBlocksWindow-51, info.SignedBlocksCounter)
2018-05-28 15:10:52 -07:00
// validator should not be immediately revoked again
2018-05-28 14:55:39 -07:00
height++
ctx = ctx.WithBlockHeight(height)
keeper.handleValidatorSignature(ctx, val, false)
validator, _ = sk.GetValidatorByPubKey(ctx, val)
2018-05-28 14:55:39 -07:00
require.Equal(t, sdk.Bonded, validator.GetStatus())
2018-05-28 15:10:52 -07:00
// validator should be revoked again after 100 unsigned blocks
2018-05-28 14:55:39 -07:00
nextHeight := height + 100
for ; height <= nextHeight; height++ {
ctx = ctx.WithBlockHeight(height)
keeper.handleValidatorSignature(ctx, val, false)
}
validator, _ = sk.GetValidatorByPubKey(ctx, val)
2018-05-28 14:55:39 -07:00
require.Equal(t, sdk.Unbonded, validator.GetStatus())
2018-05-23 13:25:56 -07:00
}
// Test a new validator entering the validator set
// Ensure that SigningInfo.StartHeight is set correctly
// and that they are not immediately revoked
func TestHandleNewValidator(t *testing.T) {
// initial setup
ctx, ck, sk, keeper := createTestInput(t)
addr, val, amt := addrs[0], pks[0], int64(100)
sh := stake.NewHandler(sk)
got := sh(ctx, newTestMsgCreateValidator(addr, val, sdk.NewInt(amt)))
require.True(t, got.IsOK())
stake.EndBlocker(ctx, sk)
require.Equal(t, ck.GetCoins(ctx, addr), sdk.Coins{{sk.GetParams(ctx).BondDenom, initCoins.SubRaw(amt)}})
require.Equal(t, sdk.NewRat(amt), sk.Validator(ctx, addr).GetPower())
// 1000 first blocks not a validator
ctx = ctx.WithBlockHeight(1001)
2018-06-10 18:03:52 -07:00
// Now a validator, for two blocks
keeper.handleValidatorSignature(ctx, val, true)
2018-06-10 18:03:52 -07:00
ctx = ctx.WithBlockHeight(1002)
keeper.handleValidatorSignature(ctx, val, false)
info, found := keeper.getValidatorSigningInfo(ctx, val.Address())
require.True(t, found)
require.Equal(t, int64(1001), info.StartHeight)
2018-06-10 18:03:52 -07:00
require.Equal(t, int64(2), info.IndexOffset)
require.Equal(t, int64(1), info.SignedBlocksCounter)
require.Equal(t, int64(0), info.JailedUntil)
// validator should be bonded still, should not have been revoked or slashed
validator, _ := sk.GetValidatorByPubKey(ctx, val)
require.Equal(t, sdk.Bonded, validator.GetStatus())
pool := sk.GetPool(ctx)
Merge PR #1119: Unbonding, Redelegation * stake/fees spec updates * staking overview.md revisions, moving files * docs reorganization * staking spec state revisions * transaction stake updates * complete staking spec update * WIP adding unbonding/redelegation commands * added msg types for unbonding, redelegation * stake sub-package reorg * working stake reorg * modify lcd tests to not use hardcoded json strings * add description update * index keys * key managment for unbonding redelegation complete * update stake errors * completed handleMsgCompleteUnbonding fn * updated to use begin/complete unbonding/redelegation * fix token shares bug * develop docs into unbonding * got non-tests compiling after merge develop * working fixing tests * PrivlegedKeeper -> PrivilegedKeeper * tests compile * fix some tests * fixing tests * remove PrivilegedKeeper * get unbonding bug * only rpc sig verification failed tests now * move percent unbonding/redelegation to the CLI and out of handler logic * remove min unbonding height * add lcd txs * add pool sanity checks, fix a buncha tests * fix ante. set lcd log to debug (#1322) * redelegation tests, adding query functionality for bonds * add self-delegations at genesis ref #1165 * PR comments (mostly) addressed * cleanup, added Query LCD functionality * test cleanup/fixes * fix governance test * SlashValidatorSet -> ValidatorSet * changelog * stake lcd fix * x/auth: fix chainID in ante * fix lcd test * fix lint, update lint make command for spelling * lowercase error string * don't expose coinkeeper in staking * remove a few duplicate lines in changelog * chain_id in stake lcd tests * added transient redelegation * 'transient' => 'transitive' * Re-add nolint instruction * Fix tiny linter error
2018-06-26 19:00:12 -07:00
require.Equal(t, int64(100), pool.BondedTokens)
}