cosmos-sdk/x/gov/keeper/querier.go
Amaury 1715693ca2
chore: Bump gov and group to v1 (#11334)
## Description

Closes: #11331



---

### 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...

- [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] added `!` to the type prefix if API or client breaking change
- [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting))
- [ ] provided a link to the relevant issue or specification
- [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules)
- [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/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
- [ ] reviewed "Files changed" and left comments if necessary
- [ ] 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)
2022-03-11 15:58:01 +00:00

236 lines
7.1 KiB
Go

package keeper
import (
abci "github.com/tendermint/tendermint/abci/types"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/x/gov/types"
v1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
)
// NewQuerier creates a new gov Querier instance
func NewQuerier(keeper Keeper, legacyQuerierCdc *codec.LegacyAmino) sdk.Querier {
return func(ctx sdk.Context, path []string, req abci.RequestQuery) ([]byte, error) {
switch path[0] {
case v1.QueryParams:
return queryParams(ctx, path[1:], req, keeper, legacyQuerierCdc)
case v1.QueryProposals:
return queryProposals(ctx, path[1:], req, keeper, legacyQuerierCdc)
case v1.QueryProposal:
return queryProposal(ctx, path[1:], req, keeper, legacyQuerierCdc)
case v1.QueryDeposits:
return queryDeposits(ctx, path[1:], req, keeper, legacyQuerierCdc)
case v1.QueryDeposit:
return queryDeposit(ctx, path[1:], req, keeper, legacyQuerierCdc)
case v1.QueryVotes:
return queryVotes(ctx, path[1:], req, keeper, legacyQuerierCdc)
case v1.QueryVote:
return queryVote(ctx, path[1:], req, keeper, legacyQuerierCdc)
case v1.QueryTally:
return queryTally(ctx, path[1:], req, keeper, legacyQuerierCdc)
default:
return nil, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "unknown query path: %s", path[0])
}
}
}
func queryParams(ctx sdk.Context, path []string, req abci.RequestQuery, keeper Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) {
switch path[0] {
case v1.ParamDeposit:
bz, err := codec.MarshalJSONIndent(legacyQuerierCdc, keeper.GetDepositParams(ctx))
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return bz, nil
case v1.ParamVoting:
bz, err := codec.MarshalJSONIndent(legacyQuerierCdc, keeper.GetVotingParams(ctx))
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return bz, nil
case v1.ParamTallying:
bz, err := codec.MarshalJSONIndent(legacyQuerierCdc, keeper.GetTallyParams(ctx))
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return bz, nil
default:
return nil, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "%s is not a valid query request path", req.Path)
}
}
// nolint: unparam
func queryProposal(ctx sdk.Context, path []string, req abci.RequestQuery, keeper Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) {
var params v1.QueryProposalParams
err := legacyQuerierCdc.UnmarshalJSON(req.Data, &params)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
}
proposal, ok := keeper.GetProposal(ctx, params.ProposalID)
if !ok {
return nil, sdkerrors.Wrapf(types.ErrUnknownProposal, "%d", params.ProposalID)
}
bz, err := codec.MarshalJSONIndent(legacyQuerierCdc, proposal)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return bz, nil
}
// nolint: unparam
func queryDeposit(ctx sdk.Context, path []string, req abci.RequestQuery, keeper Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) {
var params v1.QueryDepositParams
err := legacyQuerierCdc.UnmarshalJSON(req.Data, &params)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
}
deposit, _ := keeper.GetDeposit(ctx, params.ProposalID, params.Depositor)
bz, err := codec.MarshalJSONIndent(legacyQuerierCdc, deposit)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return bz, nil
}
// nolint: unparam
func queryVote(ctx sdk.Context, path []string, req abci.RequestQuery, keeper Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) {
var params v1.QueryVoteParams
err := legacyQuerierCdc.UnmarshalJSON(req.Data, &params)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
}
vote, _ := keeper.GetVote(ctx, params.ProposalID, params.Voter)
bz, err := codec.MarshalJSONIndent(legacyQuerierCdc, vote)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return bz, nil
}
// nolint: unparam
func queryDeposits(ctx sdk.Context, path []string, req abci.RequestQuery, keeper Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) {
var params v1.QueryProposalParams
err := legacyQuerierCdc.UnmarshalJSON(req.Data, &params)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
}
deposits := keeper.GetDeposits(ctx, params.ProposalID)
if deposits == nil {
deposits = v1.Deposits{}
}
bz, err := codec.MarshalJSONIndent(legacyQuerierCdc, deposits)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return bz, nil
}
// nolint: unparam
func queryTally(ctx sdk.Context, path []string, req abci.RequestQuery, keeper Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) {
var params v1.QueryProposalParams
err := legacyQuerierCdc.UnmarshalJSON(req.Data, &params)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
}
proposalID := params.ProposalID
proposal, ok := keeper.GetProposal(ctx, proposalID)
if !ok {
return nil, sdkerrors.Wrapf(types.ErrUnknownProposal, "%d", proposalID)
}
var tallyResult v1.TallyResult
switch {
case proposal.Status == v1.StatusDepositPeriod:
tallyResult = v1.EmptyTallyResult()
case proposal.Status == v1.StatusPassed || proposal.Status == v1.StatusRejected:
tallyResult = *proposal.FinalTallyResult
default:
// proposal is in voting period
_, _, tallyResult = keeper.Tally(ctx, proposal)
}
bz, err := codec.MarshalJSONIndent(legacyQuerierCdc, tallyResult)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return bz, nil
}
// nolint: unparam
func queryVotes(ctx sdk.Context, path []string, req abci.RequestQuery, keeper Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) {
var params v1.QueryProposalVotesParams
err := legacyQuerierCdc.UnmarshalJSON(req.Data, &params)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
}
votes := keeper.GetVotes(ctx, params.ProposalID)
if votes == nil {
votes = v1.Votes{}
} else {
start, end := client.Paginate(len(votes), params.Page, params.Limit, 100)
if start < 0 || end < 0 {
votes = v1.Votes{}
} else {
votes = votes[start:end]
}
}
bz, err := codec.MarshalJSONIndent(legacyQuerierCdc, votes)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return bz, nil
}
func queryProposals(ctx sdk.Context, _ []string, req abci.RequestQuery, keeper Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) {
var params v1.QueryProposalsParams
err := legacyQuerierCdc.UnmarshalJSON(req.Data, &params)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
}
proposals := keeper.GetProposalsFiltered(ctx, params)
if proposals == nil {
proposals = v1.Proposals{}
}
bz, err := codec.MarshalJSONIndent(legacyQuerierCdc, proposals)
if err != nil {
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return bz, nil
}