88 lines
2.4 KiB
Go
88 lines
2.4 KiB
Go
package slashing
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
|
|
|
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)
|
|
|
|
// 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, sdk.AccAddress(addr)), sdk.Coins{{sk.GetParams(ctx).BondDenom, initCoins.Sub(amt)}})
|
|
require.True(t, sdk.NewDecFromInt(amt).Equal(sk.Validator(ctx, addr).GetPower()))
|
|
|
|
val := abci.Validator{
|
|
Address: pk.Address(),
|
|
Power: amt.Int64(),
|
|
}
|
|
|
|
// mark the validator as having signed
|
|
req := abci.RequestBeginBlock{
|
|
LastCommitInfo: abci.LastCommitInfo{
|
|
Votes: []abci.VoteInfo{{
|
|
Validator: val,
|
|
SignedLastBlock: true,
|
|
}},
|
|
},
|
|
}
|
|
BeginBlocker(ctx, req, keeper)
|
|
|
|
info, found := keeper.getValidatorSigningInfo(ctx, sdk.ConsAddress(pk.Address()))
|
|
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(1), info.SignedBlocksCounter)
|
|
|
|
height := int64(0)
|
|
|
|
// for 1000 blocks, mark the validator as having signed
|
|
for ; height < keeper.SignedBlocksWindow(ctx); height++ {
|
|
ctx = ctx.WithBlockHeight(height)
|
|
req = abci.RequestBeginBlock{
|
|
LastCommitInfo: abci.LastCommitInfo{
|
|
Votes: []abci.VoteInfo{{
|
|
Validator: val,
|
|
SignedLastBlock: true,
|
|
}},
|
|
},
|
|
}
|
|
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++ {
|
|
ctx = ctx.WithBlockHeight(height)
|
|
req = abci.RequestBeginBlock{
|
|
LastCommitInfo: abci.LastCommitInfo{
|
|
Votes: []abci.VoteInfo{{
|
|
Validator: val,
|
|
SignedLastBlock: false,
|
|
}},
|
|
},
|
|
}
|
|
BeginBlocker(ctx, req, keeper)
|
|
}
|
|
|
|
// end block
|
|
stake.EndBlocker(ctx, sk)
|
|
|
|
// validator should be jailed
|
|
validator, found := sk.GetValidatorByConsAddr(ctx, sdk.GetConsAddress(pk))
|
|
require.True(t, found)
|
|
require.Equal(t, sdk.Unbonding, validator.GetStatus())
|
|
}
|