-
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/params: query gRPC service (#6585)
* Add grpc queries in x/params * Modify grpc query tests format * Modify query request and revert changes in querier * Fix protobuf lint issues
- Loading branch information
1 parent
3fc5a4d
commit 674845c
Showing
6 changed files
with
830 additions
and
0 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,25 @@ | ||
syntax = "proto3"; | ||
package cosmos.params; | ||
|
||
import "gogoproto/gogo.proto"; | ||
import "cosmos/params/params.proto"; | ||
|
||
option go_package = "github.com/cosmos/cosmos-sdk/x/params/types/proposal"; | ||
|
||
// Query creates service with Parameters as rpc | ||
service Query{ | ||
// Parameters queries all params | ||
rpc Parameters (QueryParametersRequest) returns (QueryParametersResponse) {} | ||
} | ||
|
||
// QueryParametersRequest is request type for the Query/Parameters RPC method | ||
message QueryParametersRequest{ | ||
string subspace = 1; | ||
|
||
string key = 2; | ||
} | ||
|
||
// QueryParametersResponse is response type for the Query/Parameters RPC method | ||
message QueryParametersResponse{ | ||
cosmos.params.ParamChange params = 1 [(gogoproto.nullable) = false]; | ||
} |
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,36 @@ | ||
package keeper | ||
|
||
import ( | ||
"context" | ||
|
||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/status" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" | ||
"github.com/cosmos/cosmos-sdk/x/params/types/proposal" | ||
) | ||
|
||
var _ proposal.QueryServer = Keeper{} | ||
|
||
// Parameters returns subspace params | ||
func (k Keeper) Parameters(c context.Context, req *proposal.QueryParametersRequest) (*proposal.QueryParametersResponse, error) { | ||
if req == nil { | ||
return nil, status.Errorf(codes.InvalidArgument, "empty request") | ||
} | ||
|
||
if req.Subspace == "" || req.Key == "" { | ||
return nil, status.Errorf(codes.InvalidArgument, "invalid request") | ||
} | ||
|
||
ss, ok := k.GetSubspace(req.Subspace) | ||
if !ok { | ||
return nil, sdkerrors.Wrap(proposal.ErrUnknownSubspace, req.Subspace) | ||
} | ||
|
||
ctx := sdk.UnwrapSDKContext(c) | ||
rawValue := ss.GetRaw(ctx, []byte(req.Key)) | ||
params := proposal.NewParamChange(req.Subspace, req.Key, string(rawValue)) | ||
|
||
return &proposal.QueryParametersResponse{Params: params}, 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,86 @@ | ||
package keeper_test | ||
|
||
import ( | ||
"fmt" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/cosmos/cosmos-sdk/x/params/types" | ||
"github.com/cosmos/cosmos-sdk/x/params/types/proposal" | ||
) | ||
|
||
func (suite *KeeperTestSuite) TestGRPCQueryParams() { | ||
var ( | ||
req *proposal.QueryParametersRequest | ||
expValue string | ||
space types.Subspace | ||
) | ||
key := []byte("key") | ||
|
||
testCases := []struct { | ||
msg string | ||
malleate func() | ||
expPass bool | ||
}{ | ||
{ | ||
"empty request", | ||
func() { | ||
req = &proposal.QueryParametersRequest{} | ||
}, | ||
false, | ||
}, | ||
{ | ||
"invalid request with subspace not found", | ||
func() { | ||
req = &proposal.QueryParametersRequest{Subspace: "test"} | ||
}, | ||
false, | ||
}, | ||
{ | ||
"invalid request with subspace and key not found", | ||
func() { | ||
req = &proposal.QueryParametersRequest{Subspace: "test", Key: "key"} | ||
}, | ||
false, | ||
}, | ||
{ | ||
"success", | ||
func() { | ||
space = suite.app.ParamsKeeper.Subspace("test"). | ||
WithKeyTable(types.NewKeyTable(types.NewParamSetPair(key, paramJSON{}, validateNoOp))) | ||
req = &proposal.QueryParametersRequest{Subspace: "test", Key: "key"} | ||
expValue = "" | ||
}, | ||
true, | ||
}, | ||
{ | ||
"update value success", | ||
func() { | ||
err := space.Update(suite.ctx, key, []byte(`{"param1":"10241024"}`)) | ||
suite.Require().NoError(err) | ||
req = &proposal.QueryParametersRequest{Subspace: "test", Key: "key"} | ||
expValue = `{"param1":"10241024"}` | ||
}, | ||
true, | ||
}, | ||
} | ||
|
||
suite.SetupTest() | ||
ctx := sdk.WrapSDKContext(suite.ctx) | ||
|
||
for _, tc := range testCases { | ||
suite.Run(fmt.Sprintf("Case %s", tc.msg), func() { | ||
tc.malleate() | ||
|
||
res, err := suite.queryClient.Parameters(ctx, req) | ||
|
||
if tc.expPass { | ||
suite.Require().NoError(err) | ||
suite.Require().NotNil(res) | ||
suite.Require().Equal(expValue, res.Params.Value) | ||
} else { | ||
suite.Require().Error(err) | ||
suite.Require().Nil(res) | ||
} | ||
}) | ||
} | ||
} |
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
Oops, something went wrong.