## Description Ref: #9438 This PR performs the major work of swapping out the v1beta1 msg server and query server for the new one which can process a proposal as an array of messages. This PR still retains the legacy servers which simply wrap around the new ones, providing the same interface as before. In order to keep backwards compatibility, a new msg, `MsgExecLegacyContent` has been created which allows `Content` to become a `Msg` type and still be used as part of the new implementation. --- ### 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)
236 lines
7.3 KiB
Go
236 lines
7.3 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"
|
|
"github.com/cosmos/cosmos-sdk/x/gov/types/v1beta2"
|
|
)
|
|
|
|
// 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 v1beta2.QueryParams:
|
|
return queryParams(ctx, path[1:], req, keeper, legacyQuerierCdc)
|
|
|
|
case v1beta2.QueryProposals:
|
|
return queryProposals(ctx, path[1:], req, keeper, legacyQuerierCdc)
|
|
|
|
case v1beta2.QueryProposal:
|
|
return queryProposal(ctx, path[1:], req, keeper, legacyQuerierCdc)
|
|
|
|
case v1beta2.QueryDeposits:
|
|
return queryDeposits(ctx, path[1:], req, keeper, legacyQuerierCdc)
|
|
|
|
case v1beta2.QueryDeposit:
|
|
return queryDeposit(ctx, path[1:], req, keeper, legacyQuerierCdc)
|
|
|
|
case v1beta2.QueryVotes:
|
|
return queryVotes(ctx, path[1:], req, keeper, legacyQuerierCdc)
|
|
|
|
case v1beta2.QueryVote:
|
|
return queryVote(ctx, path[1:], req, keeper, legacyQuerierCdc)
|
|
|
|
case v1beta2.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 v1beta2.ParamDeposit:
|
|
bz, err := codec.MarshalJSONIndent(legacyQuerierCdc, keeper.GetDepositParams(ctx))
|
|
if err != nil {
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
|
|
}
|
|
return bz, nil
|
|
|
|
case v1beta2.ParamVoting:
|
|
bz, err := codec.MarshalJSONIndent(legacyQuerierCdc, keeper.GetVotingParams(ctx))
|
|
if err != nil {
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
|
|
}
|
|
return bz, nil
|
|
|
|
case v1beta2.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 v1beta2.QueryProposalParams
|
|
err := legacyQuerierCdc.UnmarshalJSON(req.Data, ¶ms)
|
|
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 v1beta2.QueryDepositParams
|
|
err := legacyQuerierCdc.UnmarshalJSON(req.Data, ¶ms)
|
|
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 v1beta2.QueryVoteParams
|
|
err := legacyQuerierCdc.UnmarshalJSON(req.Data, ¶ms)
|
|
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 v1beta2.QueryProposalParams
|
|
err := legacyQuerierCdc.UnmarshalJSON(req.Data, ¶ms)
|
|
if err != nil {
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
|
|
}
|
|
|
|
deposits := keeper.GetDeposits(ctx, params.ProposalID)
|
|
if deposits == nil {
|
|
deposits = v1beta2.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 v1beta2.QueryProposalParams
|
|
err := legacyQuerierCdc.UnmarshalJSON(req.Data, ¶ms)
|
|
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 v1beta2.TallyResult
|
|
|
|
switch {
|
|
case proposal.Status == v1beta2.StatusDepositPeriod:
|
|
tallyResult = v1beta2.EmptyTallyResult()
|
|
|
|
case proposal.Status == v1beta2.StatusPassed || proposal.Status == v1beta2.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 v1beta2.QueryProposalVotesParams
|
|
err := legacyQuerierCdc.UnmarshalJSON(req.Data, ¶ms)
|
|
if err != nil {
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
|
|
}
|
|
|
|
votes := keeper.GetVotes(ctx, params.ProposalID)
|
|
if votes == nil {
|
|
votes = v1beta2.Votes{}
|
|
} else {
|
|
start, end := client.Paginate(len(votes), params.Page, params.Limit, 100)
|
|
if start < 0 || end < 0 {
|
|
votes = v1beta2.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 v1beta2.QueryProposalsParams
|
|
err := legacyQuerierCdc.UnmarshalJSON(req.Data, ¶ms)
|
|
if err != nil {
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
|
|
}
|
|
|
|
proposals := keeper.GetProposalsFiltered(ctx, params)
|
|
if proposals == nil {
|
|
proposals = v1beta2.Proposals{}
|
|
}
|
|
|
|
bz, err := codec.MarshalJSONIndent(legacyQuerierCdc, proposals)
|
|
if err != nil {
|
|
return nil, sdkerrors.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
|
|
}
|
|
|
|
return bz, nil
|
|
}
|