-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
x/Slashing: gRPC query service (#6597)
* Add grpc methods - slashing * Add slashing grpc queries * Add tests * Remove duplicate declarations * Add signing infos * Update query test * Use suite for grpc tests * Update godoc Co-authored-by: Alexander Bezobchuk <alexanderbez@users.noreply.github.com> Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
- Loading branch information
1 parent
3c76084
commit a0daec2
Showing
7 changed files
with
2,096 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
syntax = "proto3"; | ||
package cosmos.slashing; | ||
|
||
import "cosmos/query/pagination.proto"; | ||
import "gogoproto/gogo.proto"; | ||
import "cosmos/slashing/slashing.proto"; | ||
|
||
option go_package = "github.com/cosmos/cosmos-sdk/x/slashing/types"; | ||
|
||
// Query provides defines the gRPC querier service | ||
service Query { | ||
// Params queries the parameters of slashing module | ||
rpc Params (QueryParamsRequest) returns (QueryParamsResponse){} | ||
|
||
// SigningInfo queries the signing info of given cons address | ||
rpc SigningInfo (QuerySigningInfoRequest) returns (QuerySigningInfoResponse) {} | ||
|
||
// SigningInfos queries signing info of all validators | ||
rpc SigningInfos (QuerySigningInfosRequest) returns (QuerySigningInfosResponse) {} | ||
} | ||
|
||
// QueryParamsRequest is the request type for the Query/Parameters RPC method | ||
message QueryParamsRequest{} | ||
|
||
// QueryParamsResponse is the response type for the Query/Parameters RPC method | ||
message QueryParamsResponse{ | ||
cosmos.slashing.Params params = 1[(gogoproto.nullable) = false]; | ||
} | ||
|
||
// QuerySigningInfoRequest is the request type for the Query/SigningInfo RPC method | ||
message QuerySigningInfoRequest{ | ||
// cons_address is the address to query signing info of | ||
bytes cons_address = 1 [(gogoproto.casttype) = "github.com/cosmos/cosmos-sdk/types.ConsAddress"]; | ||
} | ||
|
||
// QuerySigningInfoResponse is the response type for the Query/SigningInfo RPC method | ||
message QuerySigningInfoResponse{ | ||
// val_signing_info is the signing info of requested val cons address | ||
cosmos.slashing.ValidatorSigningInfo val_signing_info = 1[(gogoproto.nullable)= false]; | ||
} | ||
|
||
// QuerySigningInfosRequest is the request type for the Query/SigningInfos RPC method | ||
message QuerySigningInfosRequest{ | ||
cosmos.query.PageRequest req = 1; | ||
} | ||
|
||
// QuerySigningInfosResponse is the response type for the Query/SigningInfos RPC method | ||
message QuerySigningInfosResponse{ | ||
// info is the signing info of all validators | ||
repeated cosmos.slashing.ValidatorSigningInfo info = 1[(gogoproto.nullable)= false]; | ||
cosmos.query.PageResponse res =2; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package keeper | ||
|
||
import ( | ||
"context" | ||
|
||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/status" | ||
|
||
"github.com/cosmos/cosmos-sdk/store/prefix" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/cosmos/cosmos-sdk/types/query" | ||
"github.com/cosmos/cosmos-sdk/x/slashing/types" | ||
) | ||
|
||
var _ types.QueryServer = Keeper{} | ||
|
||
func (k Keeper) Params(c context.Context, req *types.QueryParamsRequest) (*types.QueryParamsResponse, error) { | ||
if req == nil { | ||
return nil, status.Errorf(codes.InvalidArgument, "empty request") | ||
} | ||
|
||
ctx := sdk.UnwrapSDKContext(c) | ||
params := k.GetParams(ctx) | ||
|
||
return &types.QueryParamsResponse{Params: params}, nil | ||
} | ||
|
||
func (k Keeper) SigningInfo(c context.Context, req *types.QuerySigningInfoRequest) (*types.QuerySigningInfoResponse, error) { | ||
if req == nil { | ||
return nil, status.Errorf(codes.InvalidArgument, "empty request") | ||
} | ||
|
||
if req.ConsAddress == nil { | ||
return nil, status.Errorf(codes.InvalidArgument, "invalid request") | ||
} | ||
|
||
ctx := sdk.UnwrapSDKContext(c) | ||
signingInfo, found := k.GetValidatorSigningInfo(ctx, req.ConsAddress) | ||
if !found { | ||
return nil, status.Errorf(codes.NotFound, "SigningInfo not found for validator %s", req.ConsAddress) | ||
} | ||
|
||
return &types.QuerySigningInfoResponse{ValSigningInfo: signingInfo}, nil | ||
} | ||
|
||
func (k Keeper) SigningInfos(c context.Context, req *types.QuerySigningInfosRequest) (*types.QuerySigningInfosResponse, error) { | ||
if req == nil { | ||
return nil, status.Errorf(codes.InvalidArgument, "empty request") | ||
} | ||
|
||
ctx := sdk.UnwrapSDKContext(c) | ||
store := ctx.KVStore(k.storeKey) | ||
var signInfos []types.ValidatorSigningInfo | ||
|
||
sigInfoStore := prefix.NewStore(store, types.ValidatorSigningInfoKeyPrefix) | ||
res, err := query.Paginate(sigInfoStore, req.Req, func(key []byte, value []byte) error { | ||
var info types.ValidatorSigningInfo | ||
err := k.cdc.UnmarshalBinaryBare(value, &info) | ||
if err != nil { | ||
return err | ||
} | ||
signInfos = append(signInfos, info) | ||
return nil | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &types.QuerySigningInfosResponse{Info: signInfos, Res: res}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package keeper_test | ||
|
||
import ( | ||
gocontext "context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/suite" | ||
abci "github.com/tendermint/tendermint/abci/types" | ||
|
||
"github.com/cosmos/cosmos-sdk/baseapp" | ||
"github.com/cosmos/cosmos-sdk/simapp" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/cosmos/cosmos-sdk/types/query" | ||
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" | ||
"github.com/cosmos/cosmos-sdk/x/slashing/keeper" | ||
"github.com/cosmos/cosmos-sdk/x/slashing/types" | ||
) | ||
|
||
type SlashingTestSuite struct { | ||
suite.Suite | ||
|
||
app *simapp.SimApp | ||
ctx sdk.Context | ||
queryClient types.QueryClient | ||
addrDels []sdk.AccAddress | ||
} | ||
|
||
func (suite *SlashingTestSuite) SetupTest() { | ||
app := simapp.Setup(false) | ||
ctx := app.BaseApp.NewContext(false, abci.Header{}) | ||
|
||
app.AccountKeeper.SetParams(ctx, authtypes.DefaultParams()) | ||
app.BankKeeper.SetSendEnabled(ctx, true) | ||
app.SlashingKeeper.SetParams(ctx, keeper.TestParams()) | ||
|
||
addrDels := simapp.AddTestAddrsIncremental(app, ctx, 2, sdk.TokensFromConsensusPower(200)) | ||
|
||
info1 := types.NewValidatorSigningInfo(sdk.ConsAddress(addrDels[0]), int64(4), int64(3), | ||
time.Unix(2, 0), false, int64(10)) | ||
info2 := types.NewValidatorSigningInfo(sdk.ConsAddress(addrDels[1]), int64(5), int64(4), | ||
time.Unix(2, 0), false, int64(10)) | ||
|
||
app.SlashingKeeper.SetValidatorSigningInfo(ctx, sdk.ConsAddress(addrDels[0]), info1) | ||
app.SlashingKeeper.SetValidatorSigningInfo(ctx, sdk.ConsAddress(addrDels[1]), info2) | ||
|
||
suite.app = app | ||
suite.ctx = ctx | ||
suite.addrDels = addrDels | ||
|
||
queryHelper := baseapp.NewQueryServerTestHelper(ctx, app.InterfaceRegistry()) | ||
types.RegisterQueryServer(queryHelper, app.SlashingKeeper) | ||
queryClient := types.NewQueryClient(queryHelper) | ||
suite.queryClient = queryClient | ||
} | ||
|
||
func (suite *SlashingTestSuite) TestGRPCQueryParams() { | ||
queryClient := suite.queryClient | ||
paramsResp, err := queryClient.Params(gocontext.Background(), &types.QueryParamsRequest{}) | ||
|
||
suite.NoError(err) | ||
suite.Equal(keeper.TestParams(), paramsResp.Params) | ||
} | ||
|
||
func (suite *SlashingTestSuite) TestGRPCSigningInfo() { | ||
queryClient := suite.queryClient | ||
|
||
infoResp, err := queryClient.SigningInfo(gocontext.Background(), &types.QuerySigningInfoRequest{ConsAddress: nil}) | ||
suite.Error(err) | ||
suite.Nil(infoResp) | ||
|
||
consAddr := sdk.ConsAddress(suite.addrDels[0]) | ||
info, found := suite.app.SlashingKeeper.GetValidatorSigningInfo(suite.ctx, consAddr) | ||
suite.True(found) | ||
|
||
infoResp, err = queryClient.SigningInfo(gocontext.Background(), | ||
&types.QuerySigningInfoRequest{ConsAddress: consAddr}) | ||
suite.NoError(err) | ||
suite.Equal(info, infoResp.ValSigningInfo) | ||
} | ||
|
||
func (suite *SlashingTestSuite) TestGRPCSigningInfos() { | ||
queryClient := suite.queryClient | ||
|
||
var signingInfos []types.ValidatorSigningInfo | ||
|
||
suite.app.SlashingKeeper.IterateValidatorSigningInfos(suite.ctx, func(consAddr sdk.ConsAddress, info types.ValidatorSigningInfo) (stop bool) { | ||
signingInfos = append(signingInfos, info) | ||
return false | ||
}) | ||
|
||
// verify all values are returned without pagination | ||
var infoResp, err = queryClient.SigningInfos(gocontext.Background(), | ||
&types.QuerySigningInfosRequest{Req: nil}) | ||
suite.NoError(err) | ||
suite.Equal(signingInfos, infoResp.Info) | ||
|
||
infoResp, err = queryClient.SigningInfos(gocontext.Background(), | ||
&types.QuerySigningInfosRequest{Req: &query.PageRequest{Limit: 1, CountTotal: true}}) | ||
suite.NoError(err) | ||
suite.Len(infoResp.Info, 1) | ||
suite.Equal(signingInfos[0], infoResp.Info[0]) | ||
suite.NotNil(infoResp.Res.NextKey) | ||
suite.Equal(uint64(2), infoResp.Res.Total) | ||
} | ||
|
||
func TestSlashingTestSuite(t *testing.T) { | ||
suite.Run(t, new(SlashingTestSuite)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.