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"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/stake"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestBeginBlocker(t *testing.T) {
|
2018-07-13 17:12:23 -07:00
|
|
|
ctx, ck, sk, _, keeper := createTestInput(t)
|
2018-06-15 14:16:45 -07:00
|
|
|
addr, pk, amt := addrs[2], pks[2], sdk.NewInt(100)
|
2018-06-04 16:42:01 -07:00
|
|
|
|
|
|
|
// bond the validator
|
2018-09-03 09:47:24 -07:00
|
|
|
got := stake.NewHandler(sk)(ctx, newTestMsgCreateValidator(addr, pk, amt))
|
2018-06-04 16:42:01 -07:00
|
|
|
require.True(t, got.IsOK())
|
2018-08-12 00:33:48 -07:00
|
|
|
validatorUpdates := stake.EndBlocker(ctx, sk)
|
|
|
|
keeper.AddValidators(ctx, validatorUpdates)
|
2018-08-31 17:01:23 -07:00
|
|
|
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{
|
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{
|
|
|
|
Validators: []abci.SigningValidator{{
|
|
|
|
Validator: val,
|
|
|
|
SignedLastBlock: true,
|
|
|
|
}},
|
|
|
|
},
|
2018-06-04 16:42:01 -07:00
|
|
|
}
|
|
|
|
BeginBlocker(ctx, req, keeper)
|
|
|
|
|
2018-08-31 17:01:23 -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-06-04 16:42:01 -07:00
|
|
|
require.Equal(t, int64(1), info.SignedBlocksCounter)
|
|
|
|
|
|
|
|
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{
|
|
|
|
Validators: []abci.SigningValidator{{
|
|
|
|
Validator: val,
|
|
|
|
SignedLastBlock: true,
|
|
|
|
}},
|
|
|
|
},
|
2018-06-04 16:42:01 -07:00
|
|
|
}
|
|
|
|
BeginBlocker(ctx, req, keeper)
|
|
|
|
}
|
|
|
|
|
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{
|
|
|
|
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-09-20 17:30:18 -07:00
|
|
|
validator, found := sk.GetValidatorByConsPubKey(ctx, 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
|
|
|
}
|