cosmos-sdk/x/slashing/tick_test.go

92 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"
2019-01-11 12:08:01 -08:00
"github.com/cosmos/cosmos-sdk/x/staking"
2018-06-04 16:42:01 -07:00
)
func TestBeginBlocker(t *testing.T) {
2018-09-10 04:59:05 -07:00
ctx, ck, sk, _, keeper := createTestInput(t, DefaultParams())
power := int64(100)
amt := sdk.TokensFromTendermintPower(power)
addr, pk := addrs[2], pks[2]
2018-06-04 16:42:01 -07:00
// bond the validator
2019-01-11 12:08:01 -08:00
got := staking.NewHandler(sk)(ctx, NewTestMsgCreateValidator(addr, pk, amt))
2018-06-04 16:42:01 -07:00
require.True(t, got.IsOK())
2019-01-11 12:08:01 -08:00
staking.EndBlocker(ctx, sk)
require.Equal(
t, ck.GetCoins(ctx, sdk.AccAddress(addr)),
sdk.Coins{sdk.NewCoin(sk.GetParams(ctx).BondDenom, initCoins.Sub(amt))},
)
require.Equal(t, amt, sk.Validator(ctx, addr).GetBondedTokens())
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)
2018-06-04 16:42:01 -07:00
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)
2018-06-04 16:42:01 -07:00
}
// 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)
2018-06-04 16:42:01 -07:00
}
// end block
2019-01-11 12:08:01 -08:00
staking.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
}