Skip to content

Commit

Permalink
feat: add query groups in x/group (backport #14879) (#15476)
Browse files Browse the repository at this point in the history
Co-authored-by: atheeshp <[email protected]>
Co-authored-by: marbar3778 <[email protected]>
  • Loading branch information
3 people authored Mar 20, 2023
1 parent 8d053c2 commit cc39456
Show file tree
Hide file tree
Showing 7 changed files with 774 additions and 80 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ Ref: https://keepachangelog.com/en/1.0.0/

## [Unreleased]

### Features

* (x/groups) [#14879](https://github.com/cosmos/cosmos-sdk/pull/14879) Add `Query/Groups` query to get all the groups.

### Improvements

* (simapp) [#15305](https://github.com/cosmos/cosmos-sdk/pull/15305) Add `AppStateFnWithExtendedCb` with callback function to extend rawState and `AppStateRandomizedFnWithState` with extra genesisState argument which is the genesis state of the app.
Expand Down
27 changes: 27 additions & 0 deletions proto/cosmos/group/v1/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,13 @@ service Query {
rpc TallyResult(QueryTallyResultRequest) returns (QueryTallyResultResponse) {
option (google.api.http).get = "/cosmos/group/v1/proposals/{proposal_id}/tally";
};

// Groups queries all groups in state.
//
// Since: cosmos-sdk 0.47.1
rpc Groups(QueryGroupsRequest) returns (QueryGroupsResponse) {
option (google.api.http).get = "/cosmos/group/v1/groups";
};
}

// QueryGroupInfoRequest is the Query/GroupInfo request type.
Expand Down Expand Up @@ -311,3 +318,23 @@ message QueryTallyResultResponse {
// tally defines the requested tally.
TallyResult tally = 1 [(gogoproto.nullable) = false];
}

// QueryGroupsRequest is the Query/Groups request type.
//
// Since: cosmos-sdk 0.47.1
message QueryGroupsRequest {

// pagination defines an optional pagination for the request.
cosmos.base.query.v1beta1.PageRequest pagination = 2;
}

// QueryGroupsResponse is the Query/Groups response type.
//
// Since: cosmos-sdk 0.47.1
message QueryGroupsResponse {
// `groups` is all the groups present in state.
repeated GroupInfo groups = 1;

// pagination defines the pagination in the response.
cosmos.base.query.v1beta1.PageResponse pagination = 2;
}
35 changes: 35 additions & 0 deletions x/group/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func QueryCmd(name string) *cobra.Command {
QueryVotesByVoterCmd(),
QueryGroupsByMemberCmd(),
QueryTallyResultCmd(),
QueryGroupsCmd(),
)

return queryCmd
Expand Down Expand Up @@ -504,3 +505,37 @@ func QueryVotesByVoterCmd() *cobra.Command {

return cmd
}

func QueryGroupsCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "groups",
Short: "Query for groups present in the state",
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}

pageReq, err := client.ReadPageRequest(cmd.Flags())
if err != nil {
return err
}

queryClient := group.NewQueryClient(clientCtx)

res, err := queryClient.Groups(cmd.Context(), &group.QueryGroupsRequest{
Pagination: pageReq,
})
if err != nil {
return err
}

return clientCtx.PrintProto(res)
},
}

flags.AddQueryFlagsToCmd(cmd)
flags.AddPaginationFlagsToCmd(cmd, "groups")

return cmd
}
49 changes: 49 additions & 0 deletions x/group/client/testutil/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,55 @@ func (s *IntegrationTestSuite) TestQueryGroupsByMembers() {
}
}

func (s *IntegrationTestSuite) TestQueryGroups() {
val := s.network.Validators[0]
clientCtx := val.ClientCtx
require := s.Require()

testCases := []struct {
name string
args []string
expectErr bool
expectErrMsg string
numItems int
expectGroups []*group.GroupInfo
}{
{
name: "valid req",
args: []string{fmt.Sprintf("--%s=json", tmcli.OutputFlag)},
expectErr: false,
numItems: 5,
},
{
name: "valid req with pagination",
args: []string{
"--limit=2",
fmt.Sprintf("--%s=json", tmcli.OutputFlag),
},
expectErr: false,
numItems: 2,
},
}

for _, tc := range testCases {
tc := tc
s.Run(tc.name, func() {
cmd := client.QueryGroupsCmd()
out, err := cli.ExecTestCLICmd(clientCtx, cmd, tc.args)
if tc.expectErr {
require.Contains(out.String(), tc.expectErrMsg)
} else {
require.NoError(err, out.String())

var resp group.QueryGroupsResponse
val.ClientCtx.Codec.MustUnmarshalJSON(out.Bytes(), &resp)

require.Len(resp.Groups, tc.numItems)
}
})
}
}

func (s *IntegrationTestSuite) TestQueryGroupMembers() {
val := s.network.Validators[0]
clientCtx := val.ClientCtx
Expand Down
23 changes: 23 additions & 0 deletions x/group/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package keeper

import (
"context"
"math"

"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
Expand Down Expand Up @@ -346,3 +347,25 @@ func (k Keeper) TallyResult(goCtx context.Context, request *group.QueryTallyResu
Tally: tallyResult,
}, nil
}

// Groups returns all the groups present in the state.
func (k Keeper) Groups(goCtx context.Context, request *group.QueryGroupsRequest) (*group.QueryGroupsResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

it, err := k.groupTable.PrefixScan(ctx.KVStore(k.key), 1, math.MaxUint64)
if err != nil {
return nil, err
}
defer it.Close()

var groups []*group.GroupInfo
pageRes, err := orm.Paginate(it, request.Pagination, &groups)
if err != nil {
return nil, err
}

return &group.QueryGroupsResponse{
Groups: groups,
Pagination: pageRes,
}, nil
}
Loading

0 comments on commit cc39456

Please sign in to comment.