Skip to content

Commit

Permalink
test(x/slashing): add infractions test (#21387)
Browse files Browse the repository at this point in the history
  • Loading branch information
JulianToledano authored Aug 26, 2024
1 parent 41fa461 commit 28c792b
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 5 deletions.
4 changes: 2 additions & 2 deletions x/slashing/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion x/slashing/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
127 changes: 127 additions & 0 deletions x/slashing/keeper/infractions_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}
4 changes: 2 additions & 2 deletions x/slashing/keeper/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ 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
}

// Migrate2to3 migrates the x/slashing module state from the consensus
// 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
}

Expand Down

0 comments on commit 28c792b

Please sign in to comment.