120 lines
3.6 KiB
Go
120 lines
3.6 KiB
Go
package slashing_test
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
|
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
|
|
|
|
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
|
|
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
|
|
"github.com/cosmos/cosmos-sdk/x/slashing"
|
|
slashingkeeper "github.com/cosmos/cosmos-sdk/x/slashing/keeper"
|
|
"github.com/cosmos/cosmos-sdk/x/slashing/testutil"
|
|
"github.com/cosmos/cosmos-sdk/x/staking"
|
|
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
|
|
stakingtestutil "github.com/cosmos/cosmos-sdk/x/staking/testutil"
|
|
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
|
|
)
|
|
|
|
func TestBeginBlocker(t *testing.T) {
|
|
var interfaceRegistry codectypes.InterfaceRegistry
|
|
var bankKeeper bankkeeper.Keeper
|
|
var stakingKeeper *stakingkeeper.Keeper
|
|
var slashingKeeper slashingkeeper.Keeper
|
|
|
|
app, err := simtestutil.Setup(
|
|
testutil.AppConfig,
|
|
&interfaceRegistry,
|
|
&bankKeeper,
|
|
&stakingKeeper,
|
|
&slashingKeeper,
|
|
)
|
|
require.NoError(t, err)
|
|
|
|
ctx := app.BaseApp.NewContext(false, tmproto.Header{})
|
|
|
|
pks := simtestutil.CreateTestPubKeys(1)
|
|
simtestutil.AddTestAddrsFromPubKeys(bankKeeper, stakingKeeper, ctx, pks, stakingKeeper.TokensFromConsensusPower(ctx, 200))
|
|
addr, pk := sdk.ValAddress(pks[0].Address()), pks[0]
|
|
tstaking := stakingtestutil.NewHelper(t, ctx, stakingKeeper)
|
|
|
|
// bond the validator
|
|
power := int64(100)
|
|
amt := tstaking.CreateValidatorWithValPower(addr, pk, power, true)
|
|
staking.EndBlocker(ctx, stakingKeeper)
|
|
require.Equal(
|
|
t, bankKeeper.GetAllBalances(ctx, sdk.AccAddress(addr)),
|
|
sdk.NewCoins(sdk.NewCoin(stakingKeeper.GetParams(ctx).BondDenom, InitTokens.Sub(amt))),
|
|
)
|
|
require.Equal(t, amt, stakingKeeper.Validator(ctx, addr).GetBondedTokens())
|
|
|
|
val := abci.Validator{
|
|
Address: pk.Address(),
|
|
Power: power,
|
|
}
|
|
|
|
// mark the validator as having signed
|
|
req := abci.RequestBeginBlock{
|
|
LastCommitInfo: abci.CommitInfo{
|
|
Votes: []abci.VoteInfo{{
|
|
Validator: val,
|
|
SignedLastBlock: true,
|
|
}},
|
|
},
|
|
}
|
|
|
|
slashing.BeginBlocker(ctx, req, slashingKeeper)
|
|
|
|
info, found := slashingKeeper.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(0), info.MissedBlocksCounter)
|
|
|
|
height := int64(0)
|
|
|
|
// for 1000 blocks, mark the validator as having signed
|
|
for ; height < slashingKeeper.SignedBlocksWindow(ctx); height++ {
|
|
ctx = ctx.WithBlockHeight(height)
|
|
req = abci.RequestBeginBlock{
|
|
LastCommitInfo: abci.CommitInfo{
|
|
Votes: []abci.VoteInfo{{
|
|
Validator: val,
|
|
SignedLastBlock: true,
|
|
}},
|
|
},
|
|
}
|
|
|
|
slashing.BeginBlocker(ctx, req, slashingKeeper)
|
|
}
|
|
|
|
// for 500 blocks, mark the validator as having not signed
|
|
for ; height < ((slashingKeeper.SignedBlocksWindow(ctx) * 2) - slashingKeeper.MinSignedPerWindow(ctx) + 1); height++ {
|
|
ctx = ctx.WithBlockHeight(height)
|
|
req = abci.RequestBeginBlock{
|
|
LastCommitInfo: abci.CommitInfo{
|
|
Votes: []abci.VoteInfo{{
|
|
Validator: val,
|
|
SignedLastBlock: false,
|
|
}},
|
|
},
|
|
}
|
|
|
|
slashing.BeginBlocker(ctx, req, slashingKeeper)
|
|
}
|
|
|
|
// end block
|
|
staking.EndBlocker(ctx, stakingKeeper)
|
|
|
|
// validator should be jailed
|
|
validator, found := stakingKeeper.GetValidatorByConsAddr(ctx, sdk.GetConsAddress(pk))
|
|
require.True(t, found)
|
|
require.Equal(t, stakingtypes.Unbonding, validator.GetStatus())
|
|
}
|