test(x/slashing): add infractions test (#21387)

This commit is contained in:
Julián Toledano 2024-08-26 12:07:55 +02:00 committed by GitHub
parent 41fa461529
commit 28c792bcd0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 132 additions and 5 deletions

View File

@ -143,7 +143,7 @@ bonded validator. The `SignedBlocksWindow` parameter defines the size
The information stored for tracking validator liveness is as follows:
```protobuf reference
https://github.com/cosmos/cosmos-sdk/blob/v0.47.0-rc1/proto/cosmos/slashing/v1beta1/slashing.proto#L13-L35
https://github.com/cosmos/cosmos-sdk/blob/release/v0.52.x/x/slashing/proto/cosmos/slashing/v1beta1/slashing.proto#L13-L35
```
### Params
@ -154,7 +154,7 @@ it can be updated with governance or the address with authority.
* Params: `0x00 | ProtocolBuffer(Params)`
```protobuf reference
https://github.com/cosmos/cosmos-sdk/blob/v0.47.0-rc1/proto/cosmos/slashing/v1beta1/slashing.proto#L37-L59
https://github.com/cosmos/cosmos-sdk/blob/release/v0.52.x/x/slashing/proto/cosmos/slashing/v1beta1/slashing.proto#L37-L62
```
## Messages

View File

@ -24,7 +24,7 @@ func NewQuerier(keeper Keeper) Querier {
}
// Params returns parameters of x/slashing module
func (k Querier) Params(ctx context.Context, req *types.QueryParamsRequest) (*types.QueryParamsResponse, error) {
func (k Querier) Params(ctx context.Context, _ *types.QueryParamsRequest) (*types.QueryParamsResponse, error) {
params, err := k.Keeper.Params.Get(ctx)
if err != nil {
return nil, err

View File

@ -0,0 +1,127 @@
package keeper_test
import (
"time"
gogoany "github.com/cosmos/gogoproto/types/any"
stakingv1beta1 "cosmossdk.io/api/cosmos/staking/v1beta1"
"cosmossdk.io/core/comet"
"cosmossdk.io/math"
"cosmossdk.io/x/slashing/types"
stakingtypes "cosmossdk.io/x/staking/types"
"github.com/cosmos/cosmos-sdk/testutil/testdata"
sdk "github.com/cosmos/cosmos-sdk/types"
)
func (s *KeeperTestSuite) TestKeeper_HandleValidatorSignature() {
_, edPubKey, valAddr := testdata.KeyTestPubAddrED25519()
valStrAddr, err := s.stakingKeeper.ValidatorAddressCodec().BytesToString(valAddr)
s.Require().NoError(err)
consStrAddr, err := s.stakingKeeper.ConsensusAddressCodec().BytesToString(valAddr)
s.Require().NoError(err)
vpk, err := gogoany.NewAnyWithCacheWithValue(edPubKey)
s.Require().NoError(err)
_, pubKey, _ := testdata.KeyTestPubAddr()
addr := pubKey.Address()
tests := []struct {
name string
height int64
validator stakingtypes.Validator
valSignInfo types.ValidatorSigningInfo
flag comet.BlockIDFlag
wantErr bool
errMsg string
}{
{
name: "ok validator",
validator: stakingtypes.Validator{
OperatorAddress: valStrAddr,
ConsensusPubkey: vpk,
Jailed: false,
Status: stakingtypes.Bonded,
Tokens: math.NewInt(100),
DelegatorShares: math.LegacyNewDec(100),
},
valSignInfo: types.NewValidatorSigningInfo(consStrAddr, int64(0),
time.Now().UTC().Add(100000000000), false, int64(10)),
flag: comet.BlockIDFlagCommit,
},
{
name: "jailed validator",
validator: stakingtypes.Validator{
Jailed: true,
},
flag: comet.BlockIDFlagCommit,
},
{
name: "signingInfo startHeight > height",
validator: stakingtypes.Validator{
OperatorAddress: valStrAddr,
ConsensusPubkey: vpk,
},
valSignInfo: types.NewValidatorSigningInfo(consStrAddr, int64(3),
time.Now().UTC().Add(100000000000), false, int64(10)),
flag: comet.BlockIDFlagCommit,
wantErr: true,
errMsg: "start height 3 , which is greater than the current height 0",
},
{
name: "absent",
validator: stakingtypes.Validator{
OperatorAddress: valStrAddr,
ConsensusPubkey: vpk,
Jailed: false,
Status: stakingtypes.Bonded,
Tokens: math.NewInt(100),
DelegatorShares: math.LegacyNewDec(100),
},
valSignInfo: types.NewValidatorSigningInfo(consStrAddr, int64(0),
time.Now().UTC().Add(100000000000), false, int64(10)),
flag: comet.BlockIDFlagAbsent,
},
{
name: "punish validator",
validator: stakingtypes.Validator{
OperatorAddress: valStrAddr,
ConsensusPubkey: vpk,
Jailed: false,
Status: stakingtypes.Bonded,
Tokens: math.NewInt(100),
DelegatorShares: math.LegacyNewDec(100),
},
valSignInfo: types.NewValidatorSigningInfo(consStrAddr, int64(0),
time.Now().UTC().Add(100000000000), false, int64(501)),
flag: comet.BlockIDFlagAbsent,
height: 2000,
},
}
for _, tt := range tests {
s.Run(tt.name, func() {
headerInfo := s.ctx.HeaderInfo()
headerInfo.Height = tt.height
s.ctx = s.ctx.WithHeaderInfo(headerInfo)
s.Require().NoError(s.slashingKeeper.ValidatorSigningInfo.Set(s.ctx, edPubKey.Address().Bytes(), tt.valSignInfo))
s.stakingKeeper.EXPECT().ValidatorByConsAddr(s.ctx, sdk.ConsAddress(addr)).Return(tt.validator, nil)
s.stakingKeeper.EXPECT().ValidatorIdentifier(s.ctx, sdk.ConsAddress(edPubKey.Address().Bytes())).Return(sdk.ConsAddress(edPubKey.Address().Bytes()), nil).AnyTimes()
s.stakingKeeper.EXPECT().ValidatorByConsAddr(s.ctx, sdk.ConsAddress(edPubKey.Address().Bytes())).Return(tt.validator, nil).AnyTimes()
downTime, err := math.LegacyNewDecFromStr("0.01")
s.Require().NoError(err)
s.stakingKeeper.EXPECT().SlashWithInfractionReason(s.ctx, sdk.ConsAddress(edPubKey.Address().Bytes()), int64(1998), int64(0), downTime, stakingv1beta1.Infraction_INFRACTION_DOWNTIME).Return(math.NewInt(19), nil).AnyTimes()
s.stakingKeeper.EXPECT().Jail(s.ctx, sdk.ConsAddress(edPubKey.Address().Bytes())).Return(nil).AnyTimes()
err = s.slashingKeeper.HandleValidatorSignature(s.ctx, addr, 0, tt.flag)
if tt.wantErr {
s.Require().Error(err)
s.Require().Contains(err.Error(), tt.errMsg)
} else {
s.Require().NoError(err)
}
})
}
}

View File

@ -24,7 +24,7 @@ func NewMigrator(keeper Keeper, valCodec address.ValidatorAddressCodec) Migrator
}
// Migrate1to2 migrates from version 1 to 2.
func (m Migrator) Migrate1to2(ctx context.Context) error {
func (m Migrator) Migrate1to2(_ context.Context) error {
return nil
}
@ -32,7 +32,7 @@ func (m Migrator) Migrate1to2(ctx context.Context) error {
// version 2 to version 3. Specifically, it takes the parameters that are currently stored
// and managed by the x/params modules and stores them directly into the x/slashing
// module state.
func (m Migrator) Migrate2to3(ctx context.Context) error {
func (m Migrator) Migrate2to3(_ context.Context) error {
return nil
}