Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

auth: query all accounts stored via gRPC #8522

Merged
merged 18 commits into from
Feb 22, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
* (x/bank) [\#8479](https://github.com/cosmos/cosmos-sdk/pull/8479) Adittional client denom metadata validation for `base` and `display` denoms.
* (x/ibc) [\#8404](https://github.com/cosmos/cosmos-sdk/pull/8404) Reorder IBC `ChanOpenAck` and `ChanOpenConfirm` handler execution to perform core handler first, followed by application callbacks.
* [\#8396](https://github.com/cosmos/cosmos-sdk/pull/8396) Add support for ARM platform
* [\#8522](https://github.com/cosmos/cosmos-sdk/pull/8522) Allow to query all stored accounts
RiccardoM marked this conversation as resolved.
Show resolved Hide resolved

### Bug Fixes

Expand Down
21 changes: 21 additions & 0 deletions proto/cosmos/auth/v1beta1/query.proto
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
syntax = "proto3";
package cosmos.auth.v1beta1;

import "cosmos/base/query/v1beta1/pagination.proto";
import "gogoproto/gogo.proto";
import "google/protobuf/any.proto";
import "google/api/annotations.proto";
Expand All @@ -11,6 +12,11 @@ option go_package = "github.com/cosmos/cosmos-sdk/x/auth/types";

// Query defines the gRPC querier service.
service Query {
// Accounts returns all the existing accounts
rpc Accounts(QueryAccountsRequest) returns (QueryAccountsResponse) {
option (google.api.http).get = "/cosmos/auth/v1beta1/accounts";
}

// Account returns account details based on address.
rpc Account(QueryAccountRequest) returns (QueryAccountResponse) {
option (google.api.http).get = "/cosmos/auth/v1beta1/accounts/{address}";
Expand All @@ -22,6 +28,21 @@ service Query {
}
}

// QueryAccountsRequest is the request type for the Query/Accounts RPC method.
message QueryAccountsRequest {
// pagination defines an optional pagination for the request.
cosmos.base.query.v1beta1.PageRequest pagination = 2;
RiccardoM marked this conversation as resolved.
Show resolved Hide resolved
}

// QueryAccountsResponse is the response type for the Query/Accounts RPC method.
message QueryAccountsResponse {
// accounts are the existing accounts
repeated google.protobuf.Any accounts = 1 [(cosmos_proto.accepts_interface) = "AccountI"];

// pagination defines the pagination in the response.
cosmos.base.query.v1beta1.PageResponse pagination = 2;
}

// QueryAccountRequest is the request type for the Query/Account RPC method.
message QueryAccountRequest {
option (gogoproto.equal) = false;
Expand Down
31 changes: 31 additions & 0 deletions x/auth/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package keeper
import (
"context"

"github.com/cosmos/cosmos-sdk/store/prefix"
"github.com/cosmos/cosmos-sdk/types/query"
amaury1093 marked this conversation as resolved.
Show resolved Hide resolved

"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

Expand All @@ -13,6 +16,34 @@ import (

var _ types.QueryServer = AccountKeeper{}

func (ak AccountKeeper) Accounts(c context.Context, req *types.QueryAccountsRequest) (*types.QueryAccountsResponse, error) {
if req == nil {
return nil, status.Errorf(codes.InvalidArgument, "empty request")
RiccardoM marked this conversation as resolved.
Show resolved Hide resolved
}

ctx := sdk.UnwrapSDKContext(c)
store := ctx.KVStore(ak.key)
accountsStore := prefix.NewStore(store, types.AddressStoreKeyPrefix)

var accounts []*codectypes.Any
pageRes, err := query.Paginate(accountsStore, req.Pagination, func(key []byte, value []byte) error {
RiccardoM marked this conversation as resolved.
Show resolved Hide resolved
account := ak.decodeAccount(value)
any, err := codectypes.NewAnyWithValue(account)
if err != nil {
return err
}

accounts = append(accounts, any)
return nil
})

if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "paginate: %v", err)
RiccardoM marked this conversation as resolved.
Show resolved Hide resolved
}

return &types.QueryAccountsResponse{Accounts: accounts, Pagination: pageRes}, err
}

// Account returns account details based on address
func (ak AccountKeeper) Account(c context.Context, req *types.QueryAccountRequest) (*types.QueryAccountResponse, error) {
if req == nil {
Expand Down
58 changes: 58 additions & 0 deletions x/auth/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,64 @@ import (
"github.com/cosmos/cosmos-sdk/x/auth/types"
)

func (suite *KeeperTestSuite) TestGRPCQueryAccounts() {
var (
req *types.QueryAccountsRequest
)
_, _, first := testdata.KeyTestPubAddr()
_, _, second := testdata.KeyTestPubAddr()

testCases := []struct {
msg string
malleate func()
expPass bool
posttests func(res *types.QueryAccountsResponse)
}{
{
"success",
func() {
suite.app.AccountKeeper.SetAccount(suite.ctx,
suite.app.AccountKeeper.NewAccountWithAddress(suite.ctx, first))
suite.app.AccountKeeper.SetAccount(suite.ctx,
suite.app.AccountKeeper.NewAccountWithAddress(suite.ctx, second))
req = &types.QueryAccountsRequest{}
},
true,
func(res *types.QueryAccountsResponse) {
for _, acc := range res.Accounts {
var account types.AccountI
err := suite.app.InterfaceRegistry().UnpackAny(acc, &account)
suite.Require().NoError(err)

suite.Require().True(
first.Equals(account.GetAddress()) || second.Equals(account.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.Accounts(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) TestGRPCQueryAccount() {
var (
req *types.QueryAccountRequest
Expand Down
Loading