cosmos-sdk/x/slashing/tick_test.go

85 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) {
ctx, ck, sk, _, keeper := createTestInput(t)
addr, pk, amt := addrs[2], pks[2], sdk.NewInt(100)
2018-06-04 16:42:01 -07:00
// bond the validator
got := stake.NewHandler(sk)(ctx, newTestMsgCreateValidator(addr, pk, amt))
require.True(t, got.IsOK())
validatorUpdates := stake.EndBlocker(ctx, sk)
keeper.AddValidators(ctx, validatorUpdates)
require.Equal(t, ck.GetCoins(ctx, addr), sdk.Coins{{sk.GetParams(ctx).BondDenom, initCoins.Sub(amt)}})
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{
Validators: []abci.SigningValidator{{
Validator: val,
SignedLastBlock: true,
}},
},
2018-06-04 16:42:01 -07:00
}
BeginBlocker(ctx, req, keeper)
2018-07-06 00:06:53 -07:00
info, found := keeper.getValidatorSigningInfo(ctx, sdk.ValAddress(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)
2018-06-04 16:42:01 -07:00
require.Equal(t, int64(1), info.SignedBlocksCounter)
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{
Validators: []abci.SigningValidator{{
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{
Validators: []abci.SigningValidator{{
Validator: val,
SignedLastBlock: false,
}},
},
2018-06-04 16:42:01 -07:00
}
BeginBlocker(ctx, req, keeper)
}
2018-08-22 08:56:13 -07:00
// validator should be jailed
2018-06-04 16:42:01 -07:00
validator, found := sk.GetValidatorByPubKey(ctx, pk)
require.True(t, found)
require.Equal(t, sdk.Unbonded, validator.GetStatus())
}