cosmos-sdk/x/slashing/tick_test.go

88 lines
2.4 KiB
Go
Raw Normal View History

2018-06-04 16:42:01 -07:00
package slashing
import (
"testing"
"time"
2018-06-04 16:42:01 -07:00
"github.com/stretchr/testify/require"
abci "github.com/tendermint/tendermint/abci/types"
2018-06-04 16:42:01 -07:00
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/stake"
)
func TestBeginBlocker(t *testing.T) {
2018-09-10 04:59:05 -07:00
ctx, ck, sk, _, keeper := createTestInput(t, DefaultParams())
addr, pk, amt := addrs[2], pks[2], sdk.NewInt(100)
2018-06-04 16:42:01 -07:00
// bond the validator
2018-10-09 10:58:59 -07:00
got := stake.NewHandler(sk)(ctx, NewTestMsgCreateValidator(addr, pk, amt))
2018-06-04 16:42:01 -07:00
require.True(t, got.IsOK())
validatorUpdates := stake.EndBlocker(ctx, sk)
keeper.AddValidators(ctx, validatorUpdates)
require.Equal(t, ck.GetCoins(ctx, sdk.AccAddress(addr)), sdk.Coins{{sk.GetParams(ctx).BondDenom, initCoins.Sub(amt)}})
2018-09-03 09:47:24 -07:00
require.True(t, sdk.NewDecFromInt(amt).Equal(sk.Validator(ctx, addr).GetPower()))
2018-06-04 16:42:01 -07:00
val := abci.Validator{
Address: pk.Address(),
Power: amt.Int64(),
2018-06-04 16:42:01 -07:00
}
// mark the validator as having signed
req := abci.RequestBeginBlock{
LastCommitInfo: abci.LastCommitInfo{
Votes: []abci.VoteInfo{{
Validator: val,
SignedLastBlock: true,
}},
},
2018-06-04 16:42:01 -07:00
}
BeginBlocker(ctx, req, keeper)
info, found := keeper.getValidatorSigningInfo(ctx, sdk.ConsAddress(pk.Address()))
2018-06-04 16:42:01 -07:00
require.True(t, found)
require.Equal(t, ctx.BlockHeight(), info.StartHeight)
require.Equal(t, int64(1), info.IndexOffset)
require.Equal(t, time.Unix(0, 0).UTC(), info.JailedUntil)
require.Equal(t, int64(0), info.MissedBlocksCounter)
2018-06-04 16:42:01 -07:00
height := int64(0)
// for 1000 blocks, mark the validator as having signed
for ; height < keeper.SignedBlocksWindow(ctx); height++ {
2018-06-04 16:42:01 -07:00
ctx = ctx.WithBlockHeight(height)
req = abci.RequestBeginBlock{
LastCommitInfo: abci.LastCommitInfo{
Votes: []abci.VoteInfo{{
Validator: val,
SignedLastBlock: true,
}},
},
2018-06-04 16:42:01 -07:00
}
BeginBlocker(ctx, req, keeper)
}
// for 500 blocks, mark the validator as having not signed
for ; height < ((keeper.SignedBlocksWindow(ctx) * 2) - keeper.MinSignedPerWindow(ctx) + 1); height++ {
2018-06-04 16:42:01 -07:00
ctx = ctx.WithBlockHeight(height)
req = abci.RequestBeginBlock{
LastCommitInfo: abci.LastCommitInfo{
Votes: []abci.VoteInfo{{
Validator: val,
SignedLastBlock: false,
}},
},
2018-06-04 16:42:01 -07:00
}
BeginBlocker(ctx, req, keeper)
}
// end block
stake.EndBlocker(ctx, sk)
2018-08-22 08:56:13 -07:00
// validator should be jailed
validator, found := sk.GetValidatorByConsAddr(ctx, sdk.GetConsAddress(pk))
2018-06-04 16:42:01 -07:00
require.True(t, found)
2018-08-26 21:20:40 -07:00
require.Equal(t, sdk.Unbonding, validator.GetStatus())
2018-06-04 16:42:01 -07:00
}