forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
x/auth: gRPC query service (cosmos#6565)
Co-authored-by: sahith-narahari <[email protected]> Co-authored-by: Anil Kumar Kammari <[email protected]>
- Loading branch information
1 parent
e6bb2e7
commit 4536ca2
Showing
10 changed files
with
1,216 additions
and
11 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,36 @@ | ||
syntax = "proto3"; | ||
package cosmos.auth; | ||
|
||
import "gogoproto/gogo.proto"; | ||
import "google/protobuf/any.proto"; | ||
import "cosmos/auth/auth.proto"; | ||
import "cosmos_proto/cosmos.proto"; | ||
|
||
option go_package = "github.com/cosmos/cosmos-sdk/x/auth/types"; | ||
|
||
// Query creates service with Account and Parameters as rpc | ||
service Query{ | ||
// Account returns account details based on address | ||
rpc Account (QueryAccountRequest) returns (QueryAccountResponse) {} | ||
|
||
// Parameters queries all params | ||
rpc Parameters (QueryParametersRequest) returns (QueryParametersResponse) {} | ||
} | ||
|
||
// QueryAccountRequest is request type for the Query/Account RPC method | ||
message QueryAccountRequest{ | ||
bytes address = 1 [(gogoproto.casttype) = "github.com/cosmos/cosmos-sdk/types.AccAddress"]; | ||
} | ||
|
||
// QueryAccountResponse is response type for the Query/Account RPC method | ||
message QueryAccountResponse{ | ||
google.protobuf.Any account = 1 [(cosmos_proto.accepts_interface) = "AccountI"]; | ||
} | ||
|
||
// QueryParametersRequest is request type for the Query/Parameters RPC method | ||
message QueryParametersRequest{ } | ||
|
||
// QueryParametersResponse is response type for the Query/Parameters RPC method | ||
message QueryParametersResponse{ | ||
cosmos.auth.Params 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,67 @@ | ||
package keeper | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/status" | ||
|
||
proto "github.com/gogo/protobuf/proto" | ||
|
||
codectypes "github.com/cosmos/cosmos-sdk/codec/types" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/cosmos/cosmos-sdk/x/auth/types" | ||
) | ||
|
||
var _ types.QueryServer = AccountKeeper{} | ||
|
||
// Account returns account details based on address | ||
func (k AccountKeeper) Account(c context.Context, req *types.QueryAccountRequest) (*types.QueryAccountResponse, error) { | ||
if req == nil { | ||
return nil, status.Errorf(codes.InvalidArgument, "empty request") | ||
} | ||
|
||
if req.Address.Empty() { | ||
return nil, status.Errorf(codes.InvalidArgument, "invalid request") | ||
} | ||
|
||
ctx := sdk.UnwrapSDKContext(c) | ||
account := k.GetAccount(ctx, req.Address) | ||
if account == nil { | ||
return nil, status.Errorf(codes.NotFound, "account %s not found", req.Address) | ||
} | ||
|
||
acc, err := ConvertAccount(account) | ||
if err != nil { | ||
return nil, status.Errorf(codes.Internal, err.Error()) | ||
} | ||
|
||
return &types.QueryAccountResponse{Account: acc}, nil | ||
} | ||
|
||
// Parameters returns parameters of auth module | ||
func (k AccountKeeper) Parameters(c context.Context, req *types.QueryParametersRequest) (*types.QueryParametersResponse, error) { | ||
if req == nil { | ||
return nil, status.Errorf(codes.InvalidArgument, "empty request") | ||
} | ||
ctx := sdk.UnwrapSDKContext(c) | ||
params := k.GetParams(ctx) | ||
|
||
return &types.QueryParametersResponse{Params: params}, nil | ||
} | ||
|
||
// ConvertAccount converts AccountI to Any type | ||
func ConvertAccount(account types.AccountI) (*codectypes.Any, error) { | ||
msg, ok := account.(proto.Message) | ||
if !ok { | ||
return nil, fmt.Errorf("can't protomarshal %T", msg) | ||
} | ||
|
||
any, err := codectypes.NewAnyWithValue(msg) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return any, 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,134 @@ | ||
package keeper_test | ||
|
||
import ( | ||
"fmt" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/cosmos/cosmos-sdk/x/auth/types" | ||
) | ||
|
||
func (suite *KeeperTestSuite) TestGRPCQueryAccount() { | ||
var ( | ||
req *types.QueryAccountRequest | ||
) | ||
_, _, addr := types.KeyTestPubAddr() | ||
|
||
testCases := []struct { | ||
msg string | ||
malleate func() | ||
expPass bool | ||
posttests func(res *types.QueryAccountResponse) | ||
}{ | ||
{ | ||
"empty request", | ||
func() { | ||
req = &types.QueryAccountRequest{} | ||
}, | ||
false, | ||
func(res *types.QueryAccountResponse) {}, | ||
}, | ||
{ | ||
"invalid request", | ||
func() { | ||
req = &types.QueryAccountRequest{Address: []byte("")} | ||
}, | ||
false, | ||
func(res *types.QueryAccountResponse) {}, | ||
}, | ||
{ | ||
"invalid request with empty byte array", | ||
func() { | ||
req = &types.QueryAccountRequest{Address: []byte{}} | ||
}, | ||
false, | ||
func(res *types.QueryAccountResponse) {}, | ||
}, | ||
{ | ||
"account not found", | ||
func() { | ||
req = &types.QueryAccountRequest{Address: addr} | ||
}, | ||
false, | ||
func(res *types.QueryAccountResponse) {}, | ||
}, | ||
{ | ||
"success", | ||
func() { | ||
suite.app.AccountKeeper.SetAccount(suite.ctx, | ||
suite.app.AccountKeeper.NewAccountWithAddress(suite.ctx, addr)) | ||
req = &types.QueryAccountRequest{Address: addr} | ||
}, | ||
true, | ||
func(res *types.QueryAccountResponse) { | ||
var newAccount types.AccountI | ||
err := suite.app.InterfaceRegistry().UnpackAny(res.Account, &newAccount) | ||
suite.Require().NoError(err) | ||
suite.Require().NotNil(newAccount) | ||
suite.Require().True(addr.Equals(newAccount.GetAddress())) | ||
}, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
suite.Run(fmt.Sprintf("Case %s", tc.msg), func() { | ||
suite.SetupTest() // reset | ||
|
||
tc.malleate() | ||
ctx := sdk.WrapSDKContext(suite.ctx) | ||
|
||
res, err := suite.queryClient.Account(ctx, req) | ||
|
||
if tc.expPass { | ||
suite.Require().NoError(err) | ||
suite.Require().NotNil(res) | ||
} else { | ||
suite.Require().Error(err) | ||
suite.Require().Nil(res) | ||
} | ||
|
||
tc.posttests(res) | ||
}) | ||
} | ||
} | ||
|
||
func (suite *KeeperTestSuite) TestGRPCQueryParameters() { | ||
var ( | ||
req *types.QueryParametersRequest | ||
expParams types.Params | ||
) | ||
|
||
testCases := []struct { | ||
msg string | ||
malleate func() | ||
expPass bool | ||
}{ | ||
{ | ||
"success", | ||
func() { | ||
req = &types.QueryParametersRequest{} | ||
expParams = suite.app.AccountKeeper.GetParams(suite.ctx) | ||
}, | ||
true, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
suite.Run(fmt.Sprintf("Case %s", tc.msg), func() { | ||
suite.SetupTest() // reset | ||
|
||
tc.malleate() | ||
ctx := sdk.WrapSDKContext(suite.ctx) | ||
|
||
res, err := suite.queryClient.Parameters(ctx, req) | ||
|
||
if tc.expPass { | ||
suite.Require().NoError(err) | ||
suite.Require().NotNil(res) | ||
suite.Require().Equal(expParams, res.Params) | ||
} 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
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
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.