2018-06-04 16:42:01 -07:00
|
|
|
package slashing
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2018-08-12 00:33:48 -07:00
|
|
|
"time"
|
2018-06-04 16:42:01 -07:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2018-06-28 17:54:47 -07:00
|
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
2018-06-04 16:42:01 -07:00
|
|
|
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2019-07-19 06:43:38 -07:00
|
|
|
slashingkeeper "github.com/cosmos/cosmos-sdk/x/slashing/internal/keeper"
|
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) {
|
2019-07-19 06:43:38 -07:00
|
|
|
ctx, ck, sk, _, keeper := slashingkeeper.CreateTestInput(t, DefaultParams())
|
2019-02-05 21:30:48 -08:00
|
|
|
power := int64(100)
|
2019-06-12 08:57:47 -07:00
|
|
|
amt := sdk.TokensFromConsensusPower(power)
|
2019-07-19 06:43:38 -07:00
|
|
|
addr, pk := slashingkeeper.Addrs[2], slashingkeeper.Pks[2]
|
2018-06-04 16:42:01 -07:00
|
|
|
|
|
|
|
// bond the validator
|
2019-07-19 06:43:38 -07:00
|
|
|
got := staking.NewHandler(sk)(ctx, slashingkeeper.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)
|
2018-11-20 01:22:35 -08:00
|
|
|
require.Equal(
|
|
|
|
t, ck.GetCoins(ctx, sdk.AccAddress(addr)),
|
2019-07-19 06:43:38 -07:00
|
|
|
sdk.NewCoins(sdk.NewCoin(sk.GetParams(ctx).BondDenom, slashingkeeper.InitTokens.Sub(amt))),
|
2018-11-20 01:22:35 -08:00
|
|
|
)
|
2019-02-05 21:30:48 -08:00
|
|
|
require.Equal(t, amt, sk.Validator(ctx, addr).GetBondedTokens())
|
2018-06-04 16:42:01 -07:00
|
|
|
|
|
|
|
val := abci.Validator{
|
2018-08-12 00:33:48 -07:00
|
|
|
Address: pk.Address(),
|
|
|
|
Power: amt.Int64(),
|
2018-06-04 16:42:01 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// mark the validator as having signed
|
|
|
|
req := abci.RequestBeginBlock{
|
2018-08-12 00:33:48 -07:00
|
|
|
LastCommitInfo: abci.LastCommitInfo{
|
2018-10-03 08:48:23 -07:00
|
|
|
Votes: []abci.VoteInfo{{
|
2018-08-12 00:33:48 -07:00
|
|
|
Validator: val,
|
|
|
|
SignedLastBlock: true,
|
|
|
|
}},
|
|
|
|
},
|
2018-06-04 16:42:01 -07:00
|
|
|
}
|
2019-04-05 13:20:58 -07:00
|
|
|
BeginBlocker(ctx, req, keeper)
|
2018-06-04 16:42:01 -07:00
|
|
|
|
2019-07-19 06:43:38 -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)
|
2018-08-12 00:33:48 -07:00
|
|
|
require.Equal(t, time.Unix(0, 0).UTC(), info.JailedUntil)
|
2018-10-11 15:04:44 -07:00
|
|
|
require.Equal(t, int64(0), info.MissedBlocksCounter)
|
2018-06-04 16:42:01 -07:00
|
|
|
|
|
|
|
height := int64(0)
|
|
|
|
|
2018-06-29 20:34:55 -07:00
|
|
|
// for 1000 blocks, mark the validator as having signed
|
2018-07-13 17:12:23 -07:00
|
|
|
for ; height < keeper.SignedBlocksWindow(ctx); height++ {
|
2018-06-04 16:42:01 -07:00
|
|
|
ctx = ctx.WithBlockHeight(height)
|
|
|
|
req = abci.RequestBeginBlock{
|
2018-08-12 00:33:48 -07:00
|
|
|
LastCommitInfo: abci.LastCommitInfo{
|
2018-10-03 08:48:23 -07:00
|
|
|
Votes: []abci.VoteInfo{{
|
2018-08-12 00:33:48 -07:00
|
|
|
Validator: val,
|
|
|
|
SignedLastBlock: true,
|
|
|
|
}},
|
|
|
|
},
|
2018-06-04 16:42:01 -07:00
|
|
|
}
|
2019-04-05 13:20:58 -07:00
|
|
|
BeginBlocker(ctx, req, keeper)
|
2018-06-04 16:42:01 -07:00
|
|
|
}
|
|
|
|
|
2018-06-29 20:34:55 -07:00
|
|
|
// for 500 blocks, mark the validator as having not signed
|
2018-07-13 17:12:23 -07:00
|
|
|
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{
|
2018-08-12 00:33:48 -07:00
|
|
|
LastCommitInfo: abci.LastCommitInfo{
|
2018-10-03 08:48:23 -07:00
|
|
|
Votes: []abci.VoteInfo{{
|
2018-08-12 00:33:48 -07:00
|
|
|
Validator: val,
|
|
|
|
SignedLastBlock: false,
|
|
|
|
}},
|
|
|
|
},
|
2018-06-04 16:42:01 -07:00
|
|
|
}
|
2019-04-05 13:20:58 -07:00
|
|
|
BeginBlocker(ctx, req, keeper)
|
2018-06-04 16:42:01 -07:00
|
|
|
}
|
|
|
|
|
2018-10-03 09:37:06 -07:00
|
|
|
// end block
|
2019-01-11 12:08:01 -08:00
|
|
|
staking.EndBlocker(ctx, sk)
|
2018-10-03 09:37:06 -07:00
|
|
|
|
2018-08-22 08:56:13 -07:00
|
|
|
// validator should be jailed
|
2018-09-24 21:09:31 -07:00
|
|
|
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
|
|
|
}
|