diff --git a/x/slashing/README.md b/x/slashing/README.md index 1a620c8c7d57..004b112f42ce 100644 --- a/x/slashing/README.md +++ b/x/slashing/README.md @@ -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 diff --git a/x/slashing/keeper/grpc_query.go b/x/slashing/keeper/grpc_query.go index 9ac25cf625b2..12f0b9bf5c91 100644 --- a/x/slashing/keeper/grpc_query.go +++ b/x/slashing/keeper/grpc_query.go @@ -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 diff --git a/x/slashing/keeper/infractions_test.go b/x/slashing/keeper/infractions_test.go new file mode 100644 index 000000000000..1cef0f8269fd --- /dev/null +++ b/x/slashing/keeper/infractions_test.go @@ -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) + } + }) + } +} diff --git a/x/slashing/keeper/migrations.go b/x/slashing/keeper/migrations.go index 6c84523aebaa..dfc57b0d3125 100644 --- a/x/slashing/keeper/migrations.go +++ b/x/slashing/keeper/migrations.go @@ -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 }