<!-- The default pull request template is for types feat, fix, or refactor. For other templates, add one of the following parameters to the url: - template=docs.md - template=other.md --> ## Description ref: #8961 <!-- Add a description of the changes that this PR introduces and the files that are the most critical to review. --> Following up on [#9697](https://github.com/cosmos/cosmos-sdk/pull/9697#pullrequestreview-727295733), this PR is the first step for the #8961. --- ### Author Checklist *All items are required. Please add a note to the item if the item is not applicable and please add links to any relevant follow up issues.* I have... - [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [x] added `!` to the type prefix if API or client breaking change - [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting)) - [x] provided a link to the relevant issue or specification - [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules) - [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing) - [x] added a changelog entry to `CHANGELOG.md` - [ ] included comments for [documenting Go code](https://blog.golang.org/godoc) - [ ] updated the relevant documentation or specification - [ ] reviewed "Files changed" and left comments if necessary - [ ] confirmed all CI checks have passed ### Reviewers Checklist *All items are required. Please add a note if the item is not applicable and please add your handle next to the items reviewed if you only reviewed selected items.* I have... - [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] confirmed `!` in the type prefix if API or client breaking change - [ ] confirmed all author checklist items have been addressed - [ ] reviewed state machine logic - [ ] reviewed API design and naming - [ ] reviewed documentation is accurate - [ ] reviewed tests and test coverage - [ ] manually tested (if applicable)
102 lines
3.0 KiB
Go
102 lines
3.0 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"
|
|
|
|
"github.com/cosmos/cosmos-sdk/simapp"
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
"github.com/cosmos/cosmos-sdk/x/slashing"
|
|
"github.com/cosmos/cosmos-sdk/x/staking"
|
|
"github.com/cosmos/cosmos-sdk/x/staking/teststaking"
|
|
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
|
|
)
|
|
|
|
func TestBeginBlocker(t *testing.T) {
|
|
app := simapp.Setup(t, false)
|
|
ctx := app.BaseApp.NewContext(false, tmproto.Header{})
|
|
|
|
pks := simapp.CreateTestPubKeys(1)
|
|
simapp.AddTestAddrsFromPubKeys(app, ctx, pks, app.StakingKeeper.TokensFromConsensusPower(ctx, 200))
|
|
addr, pk := sdk.ValAddress(pks[0].Address()), pks[0]
|
|
tstaking := teststaking.NewHelper(t, ctx, app.StakingKeeper)
|
|
|
|
// bond the validator
|
|
power := int64(100)
|
|
amt := tstaking.CreateValidatorWithValPower(addr, pk, power, true)
|
|
staking.EndBlocker(ctx, app.StakingKeeper)
|
|
require.Equal(
|
|
t, app.BankKeeper.GetAllBalances(ctx, sdk.AccAddress(addr)),
|
|
sdk.NewCoins(sdk.NewCoin(app.StakingKeeper.GetParams(ctx).BondDenom, InitTokens.Sub(amt))),
|
|
)
|
|
require.Equal(t, amt, app.StakingKeeper.Validator(ctx, addr).GetBondedTokens())
|
|
|
|
val := abci.Validator{
|
|
Address: pk.Address(),
|
|
Power: power,
|
|
}
|
|
|
|
// mark the validator as having signed
|
|
req := abci.RequestBeginBlock{
|
|
LastCommitInfo: abci.LastCommitInfo{
|
|
Votes: []abci.VoteInfo{{
|
|
Validator: val,
|
|
SignedLastBlock: true,
|
|
}},
|
|
},
|
|
}
|
|
|
|
slashing.BeginBlocker(ctx, req, app.SlashingKeeper)
|
|
|
|
info, found := app.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 < app.SlashingKeeper.SignedBlocksWindow(ctx); height++ {
|
|
ctx = ctx.WithBlockHeight(height)
|
|
req = abci.RequestBeginBlock{
|
|
LastCommitInfo: abci.LastCommitInfo{
|
|
Votes: []abci.VoteInfo{{
|
|
Validator: val,
|
|
SignedLastBlock: true,
|
|
}},
|
|
},
|
|
}
|
|
|
|
slashing.BeginBlocker(ctx, req, app.SlashingKeeper)
|
|
}
|
|
|
|
// for 500 blocks, mark the validator as having not signed
|
|
for ; height < ((app.SlashingKeeper.SignedBlocksWindow(ctx) * 2) - app.SlashingKeeper.MinSignedPerWindow(ctx) + 1); height++ {
|
|
ctx = ctx.WithBlockHeight(height)
|
|
req = abci.RequestBeginBlock{
|
|
LastCommitInfo: abci.LastCommitInfo{
|
|
Votes: []abci.VoteInfo{{
|
|
Validator: val,
|
|
SignedLastBlock: false,
|
|
}},
|
|
},
|
|
}
|
|
|
|
slashing.BeginBlocker(ctx, req, app.SlashingKeeper)
|
|
}
|
|
|
|
// end block
|
|
staking.EndBlocker(ctx, app.StakingKeeper)
|
|
|
|
// validator should be jailed
|
|
validator, found := app.StakingKeeper.GetValidatorByConsAddr(ctx, sdk.GetConsAddress(pk))
|
|
require.True(t, found)
|
|
require.Equal(t, stakingtypes.Unbonding, validator.GetStatus())
|
|
}
|