diff --git a/x/slashing/keeper_test.go b/x/slashing/keeper_test.go index c14e063151..3d6da8c232 100644 --- a/x/slashing/keeper_test.go +++ b/x/slashing/keeper_test.go @@ -12,6 +12,8 @@ import ( ) func TestHandleDoubleSign(t *testing.T) { + + // initial setup ctx, ck, sk, keeper := createTestInput(t) addr, val, amt := addrs[0], pks[0], int64(100) got := stake.NewHandler(sk)(ctx, newTestMsgDeclareCandidacy(addr, val, amt)) @@ -19,14 +21,20 @@ func TestHandleDoubleSign(t *testing.T) { sk.Tick(ctx) require.Equal(t, ck.GetCoins(ctx, addr), sdk.Coins{{sk.GetParams(ctx).BondDenom, initCoins - amt}}) require.Equal(t, sdk.NewRat(amt), sk.Validator(ctx, addr).GetPower()) - keeper.handleDoubleSign(ctx, 0, 0, val) // double sign less than max age + + // double sign less than max age + keeper.handleDoubleSign(ctx, 0, 0, val) require.Equal(t, sdk.NewRat(amt).Mul(sdk.NewRat(19).Quo(sdk.NewRat(20))), sk.Validator(ctx, addr).GetPower()) ctx = ctx.WithBlockHeader(abci.Header{Time: 300}) - keeper.handleDoubleSign(ctx, 0, 0, val) // double sign past max age + + // double sign past max age + keeper.handleDoubleSign(ctx, 0, 0, val) require.Equal(t, sdk.NewRat(amt).Mul(sdk.NewRat(19).Quo(sdk.NewRat(20))), sk.Validator(ctx, addr).GetPower()) } func TestHandleAbsentValidator(t *testing.T) { + + // initial setup ctx, ck, sk, keeper := createTestInput(t) addr, val, amt := addrs[0], pks[0], int64(100) sh := stake.NewHandler(sk) @@ -43,7 +51,8 @@ func TestHandleAbsentValidator(t *testing.T) { require.Equal(t, int64(0), info.SignedBlocksCounter) require.Equal(t, int64(0), info.JailedUntil) height := int64(0) - // 1000 blocks OK + + // 1000 first blocks OK for ; height < 1000; height++ { ctx = ctx.WithBlockHeight(height) keeper.handleValidatorSignature(ctx, val, true) @@ -52,6 +61,7 @@ func TestHandleAbsentValidator(t *testing.T) { require.True(t, found) require.Equal(t, int64(0), info.StartHeight) require.Equal(t, SignedBlocksWindow, info.SignedBlocksCounter) + // 50 blocks missed for ; height < 1050; height++ { ctx = ctx.WithBlockHeight(height) @@ -61,11 +71,13 @@ func TestHandleAbsentValidator(t *testing.T) { require.True(t, found) require.Equal(t, int64(0), info.StartHeight) require.Equal(t, SignedBlocksWindow-50, info.SignedBlocksCounter) + // validator should be bonded still validator, _ := sk.GetValidatorByPubKey(ctx, val) require.Equal(t, sdk.Bonded, validator.GetStatus()) pool := sk.GetPool(ctx) require.Equal(t, int64(100), pool.BondedTokens) + // 51st block missed ctx = ctx.WithBlockHeight(height) keeper.handleValidatorSignature(ctx, val, false) @@ -73,33 +85,41 @@ func TestHandleAbsentValidator(t *testing.T) { require.True(t, found) require.Equal(t, int64(0), info.StartHeight) require.Equal(t, SignedBlocksWindow-51, info.SignedBlocksCounter) + // validator should have been revoked validator, _ = sk.GetValidatorByPubKey(ctx, val) require.Equal(t, sdk.Unbonded, validator.GetStatus()) + // unrevocation should fail prior to jail expiration got = slh(ctx, NewMsgUnrevoke(addr)) require.False(t, got.IsOK()) + // unrevocation should succeed after jail expiration ctx = ctx.WithBlockHeader(abci.Header{Time: int64(86400 * 2)}) got = slh(ctx, NewMsgUnrevoke(addr)) require.True(t, got.IsOK()) + // validator should be rebonded now validator, _ = sk.GetValidatorByPubKey(ctx, val) require.Equal(t, sdk.Bonded, validator.GetStatus()) + // validator should have been slashed pool = sk.GetPool(ctx) require.Equal(t, int64(99), pool.BondedTokens) + // validator start height should have been changed info, found = keeper.getValidatorSigningInfo(ctx, val.Address()) require.True(t, found) require.Equal(t, height, info.StartHeight) require.Equal(t, SignedBlocksWindow-51, info.SignedBlocksCounter) + // validator should not be immediately revoked again height++ ctx = ctx.WithBlockHeight(height) keeper.handleValidatorSignature(ctx, val, false) validator, _ = sk.GetValidatorByPubKey(ctx, val) require.Equal(t, sdk.Bonded, validator.GetStatus()) + // validator should be revoked again after 100 unsigned blocks nextHeight := height + 100 for ; height <= nextHeight; height++ {