2020-02-14 07:30:51 -08:00
|
|
|
package slashing_test
|
2018-06-04 16:42:01 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2018-08-12 00:33:48 -07:00
|
|
|
"time"
|
2018-06-04 16:42:01 -07:00
|
|
|
|
2020-03-20 10:14:14 -07:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
2020-08-14 10:58:53 -07:00
|
|
|
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
|
2020-03-20 10:14:14 -07:00
|
|
|
|
2020-03-03 02:25:32 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/simapp"
|
2018-06-04 16:42:01 -07:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2020-02-14 07:30:51 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/slashing"
|
2019-01-11 12:08:01 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/staking"
|
2020-10-19 06:04:55 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/staking/teststaking"
|
2020-10-12 06:56:02 -07:00
|
|
|
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
|
2018-06-04 16:42:01 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestBeginBlocker(t *testing.T) {
|
2021-08-16 17:52:06 -07:00
|
|
|
app := simapp.Setup(t, false)
|
2020-08-14 10:58:53 -07:00
|
|
|
ctx := app.BaseApp.NewContext(false, tmproto.Header{})
|
2020-03-03 02:25:32 -08:00
|
|
|
|
|
|
|
pks := simapp.CreateTestPubKeys(1)
|
2021-01-28 05:54:39 -08:00
|
|
|
simapp.AddTestAddrsFromPubKeys(app, ctx, pks, app.StakingKeeper.TokensFromConsensusPower(ctx, 200))
|
2020-03-03 02:25:32 -08:00
|
|
|
addr, pk := sdk.ValAddress(pks[0].Address()), pks[0]
|
2020-10-19 06:04:55 -07:00
|
|
|
tstaking := teststaking.NewHelper(t, ctx, app.StakingKeeper)
|
2018-06-04 16:42:01 -07:00
|
|
|
|
|
|
|
// bond the validator
|
2020-11-10 08:07:40 -08:00
|
|
|
power := int64(100)
|
|
|
|
amt := tstaking.CreateValidatorWithValPower(addr, pk, power, true)
|
2020-03-03 02:25:32 -08:00
|
|
|
staking.EndBlocker(ctx, app.StakingKeeper)
|
2018-11-20 01:22:35 -08:00
|
|
|
require.Equal(
|
2020-03-03 02:25:32 -08:00
|
|
|
t, app.BankKeeper.GetAllBalances(ctx, sdk.AccAddress(addr)),
|
2020-10-19 06:04:55 -07:00
|
|
|
sdk.NewCoins(sdk.NewCoin(app.StakingKeeper.GetParams(ctx).BondDenom, InitTokens.Sub(amt))),
|
2018-11-20 01:22:35 -08:00
|
|
|
)
|
2020-03-03 02:25:32 -08:00
|
|
|
require.Equal(t, amt, app.StakingKeeper.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(),
|
2020-11-10 08:07:40 -08:00
|
|
|
Power: power,
|
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
|
|
|
}
|
2020-02-14 07:30:51 -08:00
|
|
|
|
2020-03-03 02:25:32 -08:00
|
|
|
slashing.BeginBlocker(ctx, req, app.SlashingKeeper)
|
2018-06-04 16:42:01 -07:00
|
|
|
|
2020-03-03 02:25:32 -08:00
|
|
|
info, found := app.SlashingKeeper.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
|
2020-03-03 02:25:32 -08:00
|
|
|
for ; height < app.SlashingKeeper.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
|
|
|
}
|
2020-02-14 07:30:51 -08:00
|
|
|
|
2020-03-03 02:25:32 -08:00
|
|
|
slashing.BeginBlocker(ctx, req, app.SlashingKeeper)
|
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
|
2020-03-03 02:25:32 -08:00
|
|
|
for ; height < ((app.SlashingKeeper.SignedBlocksWindow(ctx) * 2) - app.SlashingKeeper.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
|
|
|
}
|
2020-02-14 07:30:51 -08:00
|
|
|
|
2020-03-03 02:25:32 -08:00
|
|
|
slashing.BeginBlocker(ctx, req, app.SlashingKeeper)
|
2018-06-04 16:42:01 -07:00
|
|
|
}
|
|
|
|
|
2018-10-03 09:37:06 -07:00
|
|
|
// end block
|
2020-03-03 02:25:32 -08:00
|
|
|
staking.EndBlocker(ctx, app.StakingKeeper)
|
2018-10-03 09:37:06 -07:00
|
|
|
|
2018-08-22 08:56:13 -07:00
|
|
|
// validator should be jailed
|
2020-03-03 02:25:32 -08:00
|
|
|
validator, found := app.StakingKeeper.GetValidatorByConsAddr(ctx, sdk.GetConsAddress(pk))
|
2018-06-04 16:42:01 -07:00
|
|
|
require.True(t, found)
|
2020-10-12 06:56:02 -07:00
|
|
|
require.Equal(t, stakingtypes.Unbonding, validator.GetStatus())
|
2018-06-04 16:42:01 -07:00
|
|
|
}
|