0g-chain/x/committee/keeper/querier.go

209 lines
7.0 KiB
Go
Raw Permalink Normal View History

2020-03-13 15:11:31 +00:00
package keeper
import (
errorsmod "cosmossdk.io/errors"
2020-03-13 15:11:31 +00:00
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
2020-04-27 18:19:05 +00:00
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
2020-03-13 15:11:31 +00:00
abci "github.com/tendermint/tendermint/abci/types"
2024-05-01 03:17:24 +00:00
"github.com/0glabs/0g-chain/x/committee/types"
2020-03-13 15:11:31 +00:00
)
// NewQuerier creates a new gov Querier instance
func NewQuerier(k Keeper, legacyQuerierCdc *codec.LegacyAmino) sdk.Querier {
return func(ctx sdk.Context, path []string, req abci.RequestQuery) (res []byte, err error) {
2020-03-13 15:11:31 +00:00
switch path[0] {
case types.QueryCommittees:
return queryCommittees(ctx, req, k, legacyQuerierCdc)
2020-03-13 15:11:31 +00:00
case types.QueryCommittee:
return queryCommittee(ctx, req, k, legacyQuerierCdc)
2020-03-13 15:11:31 +00:00
case types.QueryProposals:
return queryProposals(ctx, req, k, legacyQuerierCdc)
2020-03-13 15:11:31 +00:00
case types.QueryProposal:
return queryProposal(ctx, req, k, legacyQuerierCdc)
2020-03-13 15:11:31 +00:00
case types.QueryVotes:
return queryVotes(ctx, req, k, legacyQuerierCdc)
2020-03-13 15:11:31 +00:00
case types.QueryVote:
return queryVote(ctx, req, k, legacyQuerierCdc)
2020-03-13 15:11:31 +00:00
case types.QueryTally:
return queryTally(ctx, req, k, legacyQuerierCdc)
case types.QueryNextProposalID:
return queryNextProposalID(ctx, req, k, legacyQuerierCdc)
case types.QueryRawParams:
return queryRawParams(ctx, req, k, legacyQuerierCdc)
2020-03-13 15:11:31 +00:00
default:
return nil, errorsmod.Wrapf(sdkerrors.ErrUnknownRequest, "unknown %s query endpoint", types.ModuleName)
2020-03-13 15:11:31 +00:00
}
}
}
2020-03-30 13:06:31 +00:00
// ------------------------------------------
// Committees
// ------------------------------------------
2020-03-13 15:11:31 +00:00
func queryCommittees(ctx sdk.Context, req abci.RequestQuery, keeper Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) {
2020-04-27 14:04:47 +00:00
committees := keeper.GetCommittees(ctx)
2020-03-13 15:11:31 +00:00
bz, err := codec.MarshalJSONIndent(legacyQuerierCdc, committees)
2020-03-13 15:11:31 +00:00
if err != nil {
return nil, errorsmod.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
2020-03-13 15:11:31 +00:00
}
return bz, nil
}
func queryCommittee(ctx sdk.Context, req abci.RequestQuery, keeper Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) {
2020-03-13 15:11:31 +00:00
var params types.QueryCommitteeParams
err := legacyQuerierCdc.UnmarshalJSON(req.Data, &params)
2020-03-13 15:11:31 +00:00
if err != nil {
return nil, errorsmod.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
2020-03-13 15:11:31 +00:00
}
committee, found := keeper.GetCommittee(ctx, params.CommitteeID)
if !found {
return nil, errorsmod.Wrapf(types.ErrUnknownCommittee, "%d", params.CommitteeID)
2020-03-13 15:11:31 +00:00
}
bz, err := codec.MarshalJSONIndent(legacyQuerierCdc, committee)
2020-03-13 15:11:31 +00:00
if err != nil {
return nil, errorsmod.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
2020-03-13 15:11:31 +00:00
}
return bz, nil
}
2020-03-30 13:06:31 +00:00
// ------------------------------------------
// Proposals
// ------------------------------------------
2020-03-13 15:11:31 +00:00
func queryProposals(ctx sdk.Context, req abci.RequestQuery, keeper Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) {
2020-03-13 15:11:31 +00:00
var params types.QueryCommitteeParams
err := legacyQuerierCdc.UnmarshalJSON(req.Data, &params)
2020-03-13 15:11:31 +00:00
if err != nil {
return nil, errorsmod.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
2020-03-13 15:11:31 +00:00
}
2020-04-27 14:04:47 +00:00
proposals := keeper.GetProposalsByCommittee(ctx, params.CommitteeID)
2020-03-13 15:11:31 +00:00
bz, err := codec.MarshalJSONIndent(legacyQuerierCdc, proposals)
2020-03-13 15:11:31 +00:00
if err != nil {
return nil, errorsmod.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
2020-03-13 15:11:31 +00:00
}
return bz, nil
}
func queryProposal(ctx sdk.Context, req abci.RequestQuery, keeper Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) {
2020-03-13 15:11:31 +00:00
var params types.QueryProposalParams
err := legacyQuerierCdc.UnmarshalJSON(req.Data, &params)
2020-03-13 15:11:31 +00:00
if err != nil {
return nil, errorsmod.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
2020-03-13 15:11:31 +00:00
}
proposal, found := keeper.GetProposal(ctx, params.ProposalID)
if !found {
return nil, errorsmod.Wrapf(types.ErrUnknownProposal, "%d", params.ProposalID)
2020-03-13 15:11:31 +00:00
}
bz, err := codec.MarshalJSONIndent(legacyQuerierCdc, proposal)
2020-03-13 15:11:31 +00:00
if err != nil {
return nil, errorsmod.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
2020-03-13 15:11:31 +00:00
}
return bz, nil
}
func queryNextProposalID(ctx sdk.Context, req abci.RequestQuery, k Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) {
nextProposalID, _ := k.GetNextProposalID(ctx)
bz, err := types.ModuleCdc.LegacyAmino.MarshalJSON(nextProposalID)
if err != nil {
return nil, errorsmod.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
}
return bz, nil
}
2020-03-30 13:06:31 +00:00
// ------------------------------------------
// Votes
// ------------------------------------------
2020-03-13 15:11:31 +00:00
func queryVotes(ctx sdk.Context, req abci.RequestQuery, keeper Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) {
2020-03-13 15:11:31 +00:00
var params types.QueryProposalParams
err := legacyQuerierCdc.UnmarshalJSON(req.Data, &params)
2020-03-13 15:11:31 +00:00
if err != nil {
return nil, errorsmod.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
2020-03-13 15:11:31 +00:00
}
2020-04-27 14:04:47 +00:00
votes := keeper.GetVotesByProposal(ctx, params.ProposalID)
2020-03-13 15:11:31 +00:00
bz, err := codec.MarshalJSONIndent(legacyQuerierCdc, votes)
2020-03-13 15:11:31 +00:00
if err != nil {
return nil, errorsmod.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
2020-03-13 15:11:31 +00:00
}
return bz, nil
}
func queryVote(ctx sdk.Context, req abci.RequestQuery, keeper Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) {
2020-03-13 15:11:31 +00:00
var params types.QueryVoteParams
err := legacyQuerierCdc.UnmarshalJSON(req.Data, &params)
2020-03-13 15:11:31 +00:00
if err != nil {
return nil, errorsmod.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
2020-03-13 15:11:31 +00:00
}
vote, found := keeper.GetVote(ctx, params.ProposalID, params.Voter)
if !found {
return nil, errorsmod.Wrapf(types.ErrUnknownVote, "proposal id: %d, voter: %s", params.ProposalID, params.Voter)
2020-03-13 15:11:31 +00:00
}
bz, err := codec.MarshalJSONIndent(legacyQuerierCdc, vote)
2020-03-13 15:11:31 +00:00
if err != nil {
return nil, errorsmod.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
2020-03-13 15:11:31 +00:00
}
return bz, nil
}
2020-03-30 13:06:31 +00:00
// ------------------------------------------
// Tally
// ------------------------------------------
2020-03-13 15:11:31 +00:00
func queryTally(ctx sdk.Context, req abci.RequestQuery, keeper Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) {
2020-03-13 15:11:31 +00:00
var params types.QueryProposalParams
err := legacyQuerierCdc.UnmarshalJSON(req.Data, &params)
2020-03-13 15:11:31 +00:00
if err != nil {
return nil, errorsmod.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
2020-03-13 15:11:31 +00:00
}
tally, found := keeper.GetProposalTallyResponse(ctx, params.ProposalID)
2020-03-13 15:11:31 +00:00
if !found {
return nil, errorsmod.Wrapf(types.ErrNotFoundProposalTally, "proposal id: %d", params.ProposalID)
Token holder governance (#917) * Committee types (#899) * committee types * refactor to committee interface * include tokencommitee stringer method * add members to BaseCommittee * address revisions * update querier * update querier * fix compilation errors, tests, etc. * Update MsgVote with vote type (#900) * add vote to msg * update querier/rest * update example cli vote msg * remove incorrect comments * address revisions * update handler, stub keeper method * add vote type to vote struct * Committee module keeper logic for token holder governance (#902) * fix keeper/test compilation errors * fix keeper/test compilation errors pt 2 * add setters to committee interface * fix sims compilation errors * fix incentive tests compilation errors * update types, expected keepers * core keeper logic * don't allow bond denom * implement vote tallying * query proposal polling status * update module keepers in app.go * register committee interface * fix failing incentive test * commitee types tests * refactor GetProposalResult by committee types * update invariants * implement most proposal keeper tests * add nulls to custom enums * remove abstain vote type * add test for close proposal * remove outdated TODOs * update ProcessProposals * switch on committee type directly * reintroduce Abstain votes and update vote tallying * don't allow divide by 0 panics * delete unused setters on committee interface * clean up tally methods return values for querier * update enum validation to catch negative ints * reintroduce setters for sims compilation * address revisions * remove commented out test * implement ProcessProposals test * additional revisions * Committee migrations (#909) * add committee v14 legacy types * update migration imports for compile * addRegisterCodec() to committee v14 legacy types * migrate committee genesis state from v14 to v15 * set stability committee permissions properly * fix committee allowed params * migration test, kava-7 sample data * add concrete types to committees (#911) * revisions: migrate + tests * register msgs on legacy codec * Prepare Committee module for migrations (#906) * remove invariants * edits * fix abci test * fix keeper querier tests * add committee interface registration * use codec.Codec * don't allow null vote types * don't allow null tally option * minor spelling fixes * update example cli proposal * fix cli tally query * enable vote abstain from cli * include vote options in cli help text * call CloseProposal from handler * custom enum marshaling * committee: fix failing tests (#921) * fix failing tests * fix: spelling Co-authored-by: rhuairahrighairigh <ruaridh.odonnell@gmail.com> Co-authored-by: Ruaridh <rhuairahrighairidh@users.noreply.github.com> Co-authored-by: Kevin Davis <karzak@users.noreply.github.com>
2021-06-07 16:08:03 +00:00
}
bz, err := codec.MarshalJSONIndent(legacyQuerierCdc, tally)
2020-03-13 15:11:31 +00:00
if err != nil {
return nil, errorsmod.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
2020-03-13 15:11:31 +00:00
}
return bz, nil
}
// ------------------------------------------
// Raw Params
// ------------------------------------------
func queryRawParams(ctx sdk.Context, req abci.RequestQuery, keeper Keeper, legacyQuerierCdc *codec.LegacyAmino) ([]byte, error) {
var params types.QueryRawParamsParams
err := legacyQuerierCdc.UnmarshalJSON(req.Data, &params)
if err != nil {
return nil, errorsmod.Wrap(sdkerrors.ErrJSONUnmarshal, err.Error())
}
subspace, found := keeper.paramKeeper.GetSubspace(params.Subspace)
if !found {
return nil, errorsmod.Wrapf(types.ErrUnknownSubspace, "subspace: %s", params.Subspace)
}
rawParams := subspace.GetRaw(ctx, []byte(params.Key))
// encode the raw params as json, which converts them to a base64 string
bz, err := codec.MarshalJSONIndent(legacyQuerierCdc, rawParams)
if err != nil {
return nil, errorsmod.Wrap(sdkerrors.ErrJSONMarshal, err.Error())
}
return bz, nil
}