## Description This PR works towards #12133 and does a fumpt for consistency once sdk.Int is merged. The suggested merge order is to begin with sdk.Int, merge in the various linter PR's one by one, and then finally merge the changes to CI from #12134 --- ### Author Checklist *All items are required. Please add a note to the item if the item is not applicable and please add links to any relevant follow up issues.* I have... - [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [x] added `!` to the type prefix if API or client breaking change - [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#pr-targeting)) - [x] provided a link to the relevant issue or specification - [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/main/docs/building-modules) - [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#testing) - [ ] added a changelog entry to `CHANGELOG.md` - [ ] included comments for [documenting Go code](https://blog.golang.org/godoc) - [ ] updated the relevant documentation or specification - [x] reviewed "Files changed" and left comments if necessary - [x] confirmed all CI checks have passed ### Reviewers Checklist *All items are required. Please add a note if the item is not applicable and please add your handle next to the items reviewed if you only reviewed selected items.* I have... - [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] confirmed `!` in the type prefix if API or client breaking change - [ ] confirmed all author checklist items have been addressed - [ ] reviewed state machine logic - [ ] reviewed API design and naming - [ ] reviewed documentation is accurate - [ ] reviewed tests and test coverage - [ ] manually tested (if applicable)
349 lines
11 KiB
Go
349 lines
11 KiB
Go
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/types/query"
|
|
"github.com/cosmos/cosmos-sdk/x/group"
|
|
"github.com/cosmos/cosmos-sdk/x/group/errors"
|
|
"github.com/cosmos/cosmos-sdk/x/group/internal/orm"
|
|
)
|
|
|
|
var _ group.QueryServer = Keeper{}
|
|
|
|
// GroupInfo queries info about a group.
|
|
func (k Keeper) GroupInfo(goCtx context.Context, request *group.QueryGroupInfoRequest) (*group.QueryGroupInfoResponse, error) {
|
|
ctx := sdk.UnwrapSDKContext(goCtx)
|
|
groupID := request.GroupId
|
|
groupInfo, err := k.getGroupInfo(ctx, groupID)
|
|
if err != nil {
|
|
return nil, sdkerrors.Wrap(err, "group")
|
|
}
|
|
|
|
return &group.QueryGroupInfoResponse{Info: &groupInfo}, nil
|
|
}
|
|
|
|
func (k Keeper) getGroupInfo(ctx sdk.Context, id uint64) (group.GroupInfo, error) {
|
|
var obj group.GroupInfo
|
|
_, err := k.groupTable.GetOne(ctx.KVStore(k.key), id, &obj)
|
|
return obj, err
|
|
}
|
|
|
|
// GroupPolicyInfo queries info about a group policy.
|
|
func (k Keeper) GroupPolicyInfo(goCtx context.Context, request *group.QueryGroupPolicyInfoRequest) (*group.QueryGroupPolicyInfoResponse, error) {
|
|
ctx := sdk.UnwrapSDKContext(goCtx)
|
|
groupPolicyInfo, err := k.getGroupPolicyInfo(ctx, request.Address)
|
|
if err != nil {
|
|
return nil, sdkerrors.Wrap(err, "group policy")
|
|
}
|
|
|
|
return &group.QueryGroupPolicyInfoResponse{Info: &groupPolicyInfo}, nil
|
|
}
|
|
|
|
func (k Keeper) getGroupPolicyInfo(ctx sdk.Context, accountAddress string) (group.GroupPolicyInfo, error) {
|
|
var obj group.GroupPolicyInfo
|
|
return obj, k.groupPolicyTable.GetOne(ctx.KVStore(k.key), orm.PrimaryKey(&group.GroupPolicyInfo{Address: accountAddress}), &obj)
|
|
}
|
|
|
|
// GroupMembers queries all members of a group.
|
|
func (k Keeper) GroupMembers(goCtx context.Context, request *group.QueryGroupMembersRequest) (*group.QueryGroupMembersResponse, error) {
|
|
ctx := sdk.UnwrapSDKContext(goCtx)
|
|
groupID := request.GroupId
|
|
it, err := k.getGroupMembers(ctx, groupID, request.Pagination)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var members []*group.GroupMember
|
|
pageRes, err := orm.Paginate(it, request.Pagination, &members)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &group.QueryGroupMembersResponse{
|
|
Members: members,
|
|
Pagination: pageRes,
|
|
}, nil
|
|
}
|
|
|
|
func (k Keeper) getGroupMembers(ctx sdk.Context, id uint64, pageRequest *query.PageRequest) (orm.Iterator, error) {
|
|
return k.groupMemberByGroupIndex.GetPaginated(ctx.KVStore(k.key), id, pageRequest)
|
|
}
|
|
|
|
// GroupsByAdmin queries all groups where a given address is admin.
|
|
func (k Keeper) GroupsByAdmin(goCtx context.Context, request *group.QueryGroupsByAdminRequest) (*group.QueryGroupsByAdminResponse, error) {
|
|
ctx := sdk.UnwrapSDKContext(goCtx)
|
|
addr, err := sdk.AccAddressFromBech32(request.Admin)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
it, err := k.getGroupsByAdmin(ctx, addr, request.Pagination)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var groups []*group.GroupInfo
|
|
pageRes, err := orm.Paginate(it, request.Pagination, &groups)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &group.QueryGroupsByAdminResponse{
|
|
Groups: groups,
|
|
Pagination: pageRes,
|
|
}, nil
|
|
}
|
|
|
|
func (k Keeper) getGroupsByAdmin(ctx sdk.Context, admin sdk.AccAddress, pageRequest *query.PageRequest) (orm.Iterator, error) {
|
|
return k.groupByAdminIndex.GetPaginated(ctx.KVStore(k.key), admin.Bytes(), pageRequest)
|
|
}
|
|
|
|
// GroupPoliciesByGroup queries all groups policies of a given group.
|
|
func (k Keeper) GroupPoliciesByGroup(goCtx context.Context, request *group.QueryGroupPoliciesByGroupRequest) (*group.QueryGroupPoliciesByGroupResponse, error) {
|
|
ctx := sdk.UnwrapSDKContext(goCtx)
|
|
groupID := request.GroupId
|
|
it, err := k.getGroupPoliciesByGroup(ctx, groupID, request.Pagination)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var policies []*group.GroupPolicyInfo
|
|
pageRes, err := orm.Paginate(it, request.Pagination, &policies)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &group.QueryGroupPoliciesByGroupResponse{
|
|
GroupPolicies: policies,
|
|
Pagination: pageRes,
|
|
}, nil
|
|
}
|
|
|
|
func (k Keeper) getGroupPoliciesByGroup(ctx sdk.Context, id uint64, pageRequest *query.PageRequest) (orm.Iterator, error) {
|
|
return k.groupPolicyByGroupIndex.GetPaginated(ctx.KVStore(k.key), id, pageRequest)
|
|
}
|
|
|
|
// GroupPoliciesByAdmin queries all groups policies where a given address is
|
|
// admin.
|
|
func (k Keeper) GroupPoliciesByAdmin(goCtx context.Context, request *group.QueryGroupPoliciesByAdminRequest) (*group.QueryGroupPoliciesByAdminResponse, error) {
|
|
ctx := sdk.UnwrapSDKContext(goCtx)
|
|
addr, err := sdk.AccAddressFromBech32(request.Admin)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
it, err := k.getGroupPoliciesByAdmin(ctx, addr, request.Pagination)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var policies []*group.GroupPolicyInfo
|
|
pageRes, err := orm.Paginate(it, request.Pagination, &policies)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &group.QueryGroupPoliciesByAdminResponse{
|
|
GroupPolicies: policies,
|
|
Pagination: pageRes,
|
|
}, nil
|
|
}
|
|
|
|
func (k Keeper) getGroupPoliciesByAdmin(ctx sdk.Context, admin sdk.AccAddress, pageRequest *query.PageRequest) (orm.Iterator, error) {
|
|
return k.groupPolicyByAdminIndex.GetPaginated(ctx.KVStore(k.key), admin.Bytes(), pageRequest)
|
|
}
|
|
|
|
// Proposal queries a proposal.
|
|
func (k Keeper) Proposal(goCtx context.Context, request *group.QueryProposalRequest) (*group.QueryProposalResponse, error) {
|
|
ctx := sdk.UnwrapSDKContext(goCtx)
|
|
proposalID := request.ProposalId
|
|
proposal, err := k.getProposal(ctx, proposalID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &group.QueryProposalResponse{Proposal: &proposal}, nil
|
|
}
|
|
|
|
// Proposal queries all proposals of a group policy.
|
|
func (k Keeper) ProposalsByGroupPolicy(goCtx context.Context, request *group.QueryProposalsByGroupPolicyRequest) (*group.QueryProposalsByGroupPolicyResponse, error) {
|
|
ctx := sdk.UnwrapSDKContext(goCtx)
|
|
addr, err := sdk.AccAddressFromBech32(request.Address)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
it, err := k.getProposalsByGroupPolicy(ctx, addr, request.Pagination)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var proposals []*group.Proposal
|
|
pageRes, err := orm.Paginate(it, request.Pagination, &proposals)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &group.QueryProposalsByGroupPolicyResponse{
|
|
Proposals: proposals,
|
|
Pagination: pageRes,
|
|
}, nil
|
|
}
|
|
|
|
func (k Keeper) getProposalsByGroupPolicy(ctx sdk.Context, account sdk.AccAddress, pageRequest *query.PageRequest) (orm.Iterator, error) {
|
|
return k.proposalByGroupPolicyIndex.GetPaginated(ctx.KVStore(k.key), account.Bytes(), pageRequest)
|
|
}
|
|
|
|
func (k Keeper) getProposal(ctx sdk.Context, proposalID uint64) (group.Proposal, error) {
|
|
var p group.Proposal
|
|
if _, err := k.proposalTable.GetOne(ctx.KVStore(k.key), proposalID, &p); err != nil {
|
|
return group.Proposal{}, sdkerrors.Wrap(err, "load proposal")
|
|
}
|
|
return p, nil
|
|
}
|
|
|
|
// VoteByProposalVoter queries a vote given a voter and a proposal ID.
|
|
func (k Keeper) VoteByProposalVoter(goCtx context.Context, request *group.QueryVoteByProposalVoterRequest) (*group.QueryVoteByProposalVoterResponse, error) {
|
|
ctx := sdk.UnwrapSDKContext(goCtx)
|
|
addr, err := sdk.AccAddressFromBech32(request.Voter)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
proposalID := request.ProposalId
|
|
vote, err := k.getVote(ctx, proposalID, addr)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &group.QueryVoteByProposalVoterResponse{
|
|
Vote: &vote,
|
|
}, nil
|
|
}
|
|
|
|
// VotesByProposal queries all votes on a proposal.
|
|
func (k Keeper) VotesByProposal(goCtx context.Context, request *group.QueryVotesByProposalRequest) (*group.QueryVotesByProposalResponse, error) {
|
|
ctx := sdk.UnwrapSDKContext(goCtx)
|
|
proposalID := request.ProposalId
|
|
it, err := k.getVotesByProposal(ctx, proposalID, request.Pagination)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var votes []*group.Vote
|
|
pageRes, err := orm.Paginate(it, request.Pagination, &votes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &group.QueryVotesByProposalResponse{
|
|
Votes: votes,
|
|
Pagination: pageRes,
|
|
}, nil
|
|
}
|
|
|
|
// VotesByProposal queries all votes of a voter.
|
|
func (k Keeper) VotesByVoter(goCtx context.Context, request *group.QueryVotesByVoterRequest) (*group.QueryVotesByVoterResponse, error) {
|
|
ctx := sdk.UnwrapSDKContext(goCtx)
|
|
addr, err := sdk.AccAddressFromBech32(request.Voter)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
it, err := k.getVotesByVoter(ctx, addr, request.Pagination)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var votes []*group.Vote
|
|
pageRes, err := orm.Paginate(it, request.Pagination, &votes)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &group.QueryVotesByVoterResponse{
|
|
Votes: votes,
|
|
Pagination: pageRes,
|
|
}, nil
|
|
}
|
|
|
|
// GroupsByMember queries all groups where the given address is a member of.
|
|
func (k Keeper) GroupsByMember(goCtx context.Context, request *group.QueryGroupsByMemberRequest) (*group.QueryGroupsByMemberResponse, error) {
|
|
if request == nil {
|
|
return nil, status.Errorf(codes.InvalidArgument, "empty request")
|
|
}
|
|
|
|
ctx := sdk.UnwrapSDKContext(goCtx)
|
|
member, err := sdk.AccAddressFromBech32(request.Address)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
iter, err := k.groupMemberByMemberIndex.GetPaginated(ctx.KVStore(k.key), member.Bytes(), request.Pagination)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var members []*group.GroupMember
|
|
pageRes, err := orm.Paginate(iter, request.Pagination, &members)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
var groups []*group.GroupInfo
|
|
for _, gm := range members {
|
|
groupInfo, err := k.getGroupInfo(ctx, gm.GroupId)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
groups = append(groups, &groupInfo)
|
|
}
|
|
|
|
return &group.QueryGroupsByMemberResponse{
|
|
Groups: groups,
|
|
Pagination: pageRes,
|
|
}, nil
|
|
}
|
|
|
|
func (k Keeper) getVote(ctx sdk.Context, proposalID uint64, voter sdk.AccAddress) (group.Vote, error) {
|
|
var v group.Vote
|
|
return v, k.voteTable.GetOne(ctx.KVStore(k.key), orm.PrimaryKey(&group.Vote{ProposalId: proposalID, Voter: voter.String()}), &v)
|
|
}
|
|
|
|
func (k Keeper) getVotesByProposal(ctx sdk.Context, proposalID uint64, pageRequest *query.PageRequest) (orm.Iterator, error) {
|
|
return k.voteByProposalIndex.GetPaginated(ctx.KVStore(k.key), proposalID, pageRequest)
|
|
}
|
|
|
|
func (k Keeper) getVotesByVoter(ctx sdk.Context, voter sdk.AccAddress, pageRequest *query.PageRequest) (orm.Iterator, error) {
|
|
return k.voteByVoterIndex.GetPaginated(ctx.KVStore(k.key), voter.Bytes(), pageRequest)
|
|
}
|
|
|
|
// TallyResult computes the live tally result of a proposal.
|
|
func (k Keeper) TallyResult(goCtx context.Context, request *group.QueryTallyResultRequest) (*group.QueryTallyResultResponse, error) {
|
|
ctx := sdk.UnwrapSDKContext(goCtx)
|
|
proposalID := request.ProposalId
|
|
|
|
proposal, err := k.getProposal(ctx, proposalID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if proposal.Status == group.PROPOSAL_STATUS_WITHDRAWN || proposal.Status == group.PROPOSAL_STATUS_ABORTED {
|
|
return nil, sdkerrors.Wrapf(errors.ErrInvalid, "can't get the tally of a proposal with status %s", proposal.Status)
|
|
}
|
|
|
|
var policyInfo group.GroupPolicyInfo
|
|
if policyInfo, err = k.getGroupPolicyInfo(ctx, proposal.GroupPolicyAddress); err != nil {
|
|
return nil, sdkerrors.Wrap(err, "load group policy")
|
|
}
|
|
|
|
tallyResult, err := k.Tally(ctx, proposal, policyInfo.GroupId)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &group.QueryTallyResultResponse{
|
|
Tally: tallyResult,
|
|
}, nil
|
|
}
|