2018-07-31 18:48:32 -07:00
|
|
|
package gov
|
|
|
|
|
|
|
|
import (
|
2018-09-13 11:17:32 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/codec"
|
2018-07-31 18:48:32 -07:00
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
abci "github.com/tendermint/tendermint/abci/types"
|
|
|
|
)
|
|
|
|
|
2018-09-12 00:41:09 -07:00
|
|
|
// query endpoints supported by the governance Querier
|
|
|
|
const (
|
|
|
|
QueryProposals = "proposals"
|
|
|
|
QueryProposal = "proposal"
|
|
|
|
QueryDeposits = "deposits"
|
|
|
|
QueryDeposit = "deposit"
|
|
|
|
QueryVotes = "votes"
|
|
|
|
QueryVote = "vote"
|
|
|
|
QueryTally = "tally"
|
|
|
|
)
|
|
|
|
|
2018-08-03 12:55:00 -07:00
|
|
|
func NewQuerier(keeper Keeper) sdk.Querier {
|
|
|
|
return func(ctx sdk.Context, path []string, req abci.RequestQuery) (res []byte, err sdk.Error) {
|
|
|
|
switch path[0] {
|
2018-09-12 00:41:09 -07:00
|
|
|
case QueryProposals:
|
|
|
|
return queryProposals(ctx, path[1:], req, keeper)
|
|
|
|
case QueryProposal:
|
2018-08-03 12:55:00 -07:00
|
|
|
return queryProposal(ctx, path[1:], req, keeper)
|
2018-09-12 00:41:09 -07:00
|
|
|
case QueryDeposits:
|
2018-08-03 12:55:00 -07:00
|
|
|
return queryDeposits(ctx, path[1:], req, keeper)
|
2018-09-12 00:41:09 -07:00
|
|
|
case QueryDeposit:
|
|
|
|
return queryDeposit(ctx, path[1:], req, keeper)
|
|
|
|
case QueryVotes:
|
2018-08-03 12:55:00 -07:00
|
|
|
return queryVotes(ctx, path[1:], req, keeper)
|
2018-09-12 00:41:09 -07:00
|
|
|
case QueryVote:
|
|
|
|
return queryVote(ctx, path[1:], req, keeper)
|
|
|
|
case QueryTally:
|
2018-08-03 12:55:00 -07:00
|
|
|
return queryTally(ctx, path[1:], req, keeper)
|
|
|
|
default:
|
|
|
|
return nil, sdk.ErrUnknownRequest("unknown gov query endpoint")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Params for query 'custom/gov/proposal'
|
|
|
|
type QueryProposalParams struct {
|
|
|
|
ProposalID int64
|
2018-07-31 18:48:32 -07:00
|
|
|
}
|
|
|
|
|
2018-08-31 15:22:37 -07:00
|
|
|
// nolint: unparam
|
2018-08-03 12:55:00 -07:00
|
|
|
func queryProposal(ctx sdk.Context, path []string, req abci.RequestQuery, keeper Keeper) (res []byte, err sdk.Error) {
|
|
|
|
var params QueryProposalParams
|
2018-08-22 00:10:11 -07:00
|
|
|
err2 := keeper.cdc.UnmarshalJSON(req.Data, ¶ms)
|
2018-08-03 12:55:00 -07:00
|
|
|
if err2 != nil {
|
2018-10-19 09:55:20 -07:00
|
|
|
return nil, sdk.ErrUnknownRequest(sdk.AppendMsgToErr("incorrectly formatted request data", err2.Error()))
|
2018-08-03 12:55:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
proposal := keeper.GetProposal(ctx, params.ProposalID)
|
|
|
|
if proposal == nil {
|
2018-10-19 09:55:20 -07:00
|
|
|
return nil, ErrUnknownProposal(DefaultCodespace, params.ProposalID)
|
2018-07-31 18:48:32 -07:00
|
|
|
}
|
2018-08-22 00:10:11 -07:00
|
|
|
|
2018-09-13 11:17:32 -07:00
|
|
|
bz, err2 := codec.MarshalJSONIndent(keeper.cdc, proposal)
|
2018-08-22 00:10:11 -07:00
|
|
|
if err2 != nil {
|
2018-10-19 09:55:20 -07:00
|
|
|
return nil, sdk.ErrInternal(sdk.AppendMsgToErr("could not marshal result to JSON", err2.Error()))
|
2018-08-22 00:10:11 -07:00
|
|
|
}
|
|
|
|
return bz, nil
|
2018-07-31 18:48:32 -07:00
|
|
|
}
|
|
|
|
|
2018-08-03 12:55:00 -07:00
|
|
|
// Params for query 'custom/gov/deposit'
|
|
|
|
type QueryDepositParams struct {
|
|
|
|
ProposalID int64
|
|
|
|
Depositer sdk.AccAddress
|
|
|
|
}
|
|
|
|
|
2018-08-31 15:22:37 -07:00
|
|
|
// nolint: unparam
|
2018-08-03 12:55:00 -07:00
|
|
|
func queryDeposit(ctx sdk.Context, path []string, req abci.RequestQuery, keeper Keeper) (res []byte, err sdk.Error) {
|
|
|
|
var params QueryDepositParams
|
2018-08-22 00:10:11 -07:00
|
|
|
err2 := keeper.cdc.UnmarshalJSON(req.Data, ¶ms)
|
2018-08-03 12:55:00 -07:00
|
|
|
if err2 != nil {
|
2018-10-19 09:55:20 -07:00
|
|
|
return nil, sdk.ErrUnknownRequest(sdk.AppendMsgToErr("incorrectly formatted request data", err2.Error()))
|
2018-07-31 18:48:32 -07:00
|
|
|
}
|
2018-08-03 12:55:00 -07:00
|
|
|
|
|
|
|
deposit, _ := keeper.GetDeposit(ctx, params.ProposalID, params.Depositer)
|
2018-09-13 11:17:32 -07:00
|
|
|
bz, err2 := codec.MarshalJSONIndent(keeper.cdc, deposit)
|
2018-08-22 00:10:11 -07:00
|
|
|
if err2 != nil {
|
2018-10-19 09:55:20 -07:00
|
|
|
return nil, sdk.ErrInternal(sdk.AppendMsgToErr("could not marshal result to JSON", err2.Error()))
|
2018-08-22 00:10:11 -07:00
|
|
|
}
|
|
|
|
return bz, nil
|
2018-07-31 18:48:32 -07:00
|
|
|
}
|
|
|
|
|
2018-08-03 12:55:00 -07:00
|
|
|
// Params for query 'custom/gov/vote'
|
|
|
|
type QueryVoteParams struct {
|
|
|
|
ProposalID int64
|
|
|
|
Voter sdk.AccAddress
|
|
|
|
}
|
|
|
|
|
2018-08-31 15:22:37 -07:00
|
|
|
// nolint: unparam
|
2018-08-03 12:55:00 -07:00
|
|
|
func queryVote(ctx sdk.Context, path []string, req abci.RequestQuery, keeper Keeper) (res []byte, err sdk.Error) {
|
|
|
|
var params QueryVoteParams
|
2018-08-22 00:10:11 -07:00
|
|
|
err2 := keeper.cdc.UnmarshalJSON(req.Data, ¶ms)
|
2018-08-03 12:55:00 -07:00
|
|
|
if err2 != nil {
|
2018-10-19 09:55:20 -07:00
|
|
|
return nil, sdk.ErrUnknownRequest(sdk.AppendMsgToErr("incorrectly formatted request data", err2.Error()))
|
2018-07-31 18:48:32 -07:00
|
|
|
}
|
2018-08-03 12:55:00 -07:00
|
|
|
|
|
|
|
vote, _ := keeper.GetVote(ctx, params.ProposalID, params.Voter)
|
2018-09-13 11:17:32 -07:00
|
|
|
bz, err2 := codec.MarshalJSONIndent(keeper.cdc, vote)
|
2018-08-22 00:10:11 -07:00
|
|
|
if err2 != nil {
|
2018-10-19 09:55:20 -07:00
|
|
|
return nil, sdk.ErrInternal(sdk.AppendMsgToErr("could not marshal result to JSON", err2.Error()))
|
2018-08-22 00:10:11 -07:00
|
|
|
}
|
|
|
|
return bz, nil
|
2018-08-03 12:55:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Params for query 'custom/gov/deposits'
|
|
|
|
type QueryDepositsParams struct {
|
|
|
|
ProposalID int64
|
|
|
|
}
|
|
|
|
|
2018-08-31 15:22:37 -07:00
|
|
|
// nolint: unparam
|
2018-08-03 12:55:00 -07:00
|
|
|
func queryDeposits(ctx sdk.Context, path []string, req abci.RequestQuery, keeper Keeper) (res []byte, err sdk.Error) {
|
2018-10-12 19:05:22 -07:00
|
|
|
var params QueryDepositsParams
|
2018-08-22 00:10:11 -07:00
|
|
|
err2 := keeper.cdc.UnmarshalJSON(req.Data, ¶ms)
|
2018-08-03 12:55:00 -07:00
|
|
|
if err2 != nil {
|
2018-10-19 09:55:20 -07:00
|
|
|
return nil, sdk.ErrUnknownRequest(sdk.AppendMsgToErr("incorrectly formatted request data", err2.Error()))
|
2018-07-31 18:48:32 -07:00
|
|
|
}
|
2018-08-03 12:55:00 -07:00
|
|
|
|
|
|
|
var deposits []Deposit
|
|
|
|
depositsIterator := keeper.GetDeposits(ctx, params.ProposalID)
|
|
|
|
for ; depositsIterator.Valid(); depositsIterator.Next() {
|
|
|
|
deposit := Deposit{}
|
|
|
|
keeper.cdc.MustUnmarshalBinary(depositsIterator.Value(), &deposit)
|
|
|
|
deposits = append(deposits, deposit)
|
|
|
|
}
|
|
|
|
|
2018-09-13 11:17:32 -07:00
|
|
|
bz, err2 := codec.MarshalJSONIndent(keeper.cdc, deposits)
|
2018-08-22 00:10:11 -07:00
|
|
|
if err2 != nil {
|
2018-10-19 09:55:20 -07:00
|
|
|
return nil, sdk.ErrInternal(sdk.AppendMsgToErr("could not marshal result to JSON", err2.Error()))
|
2018-08-22 00:10:11 -07:00
|
|
|
}
|
|
|
|
return bz, nil
|
2018-07-31 18:48:32 -07:00
|
|
|
}
|
|
|
|
|
2018-08-03 12:55:00 -07:00
|
|
|
// Params for query 'custom/gov/votes'
|
|
|
|
type QueryVotesParams struct {
|
|
|
|
ProposalID int64
|
|
|
|
}
|
|
|
|
|
2018-08-31 15:22:37 -07:00
|
|
|
// nolint: unparam
|
2018-08-03 12:55:00 -07:00
|
|
|
func queryVotes(ctx sdk.Context, path []string, req abci.RequestQuery, keeper Keeper) (res []byte, err sdk.Error) {
|
|
|
|
var params QueryVotesParams
|
2018-08-22 00:10:11 -07:00
|
|
|
err2 := keeper.cdc.UnmarshalJSON(req.Data, ¶ms)
|
|
|
|
|
2018-08-03 12:55:00 -07:00
|
|
|
if err2 != nil {
|
2018-10-19 09:55:20 -07:00
|
|
|
return nil, sdk.ErrUnknownRequest(sdk.AppendMsgToErr("incorrectly formatted request data", err2.Error()))
|
2018-07-31 18:48:32 -07:00
|
|
|
}
|
2018-08-03 12:55:00 -07:00
|
|
|
|
|
|
|
var votes []Vote
|
|
|
|
votesIterator := keeper.GetVotes(ctx, params.ProposalID)
|
|
|
|
for ; votesIterator.Valid(); votesIterator.Next() {
|
|
|
|
vote := Vote{}
|
|
|
|
keeper.cdc.MustUnmarshalBinary(votesIterator.Value(), &vote)
|
|
|
|
votes = append(votes, vote)
|
|
|
|
}
|
|
|
|
|
2018-09-13 11:17:32 -07:00
|
|
|
bz, err2 := codec.MarshalJSONIndent(keeper.cdc, votes)
|
2018-08-22 00:10:11 -07:00
|
|
|
if err2 != nil {
|
2018-10-19 09:55:20 -07:00
|
|
|
return nil, sdk.ErrInternal(sdk.AppendMsgToErr("could not marshal result to JSON", err2.Error()))
|
2018-08-22 00:10:11 -07:00
|
|
|
}
|
|
|
|
return bz, nil
|
2018-08-03 12:55:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Params for query 'custom/gov/proposals'
|
|
|
|
type QueryProposalsParams struct {
|
|
|
|
Voter sdk.AccAddress
|
|
|
|
Depositer sdk.AccAddress
|
|
|
|
ProposalStatus ProposalStatus
|
|
|
|
NumLatestProposals int64
|
|
|
|
}
|
|
|
|
|
2018-08-31 15:22:37 -07:00
|
|
|
// nolint: unparam
|
2018-08-03 12:55:00 -07:00
|
|
|
func queryProposals(ctx sdk.Context, path []string, req abci.RequestQuery, keeper Keeper) (res []byte, err sdk.Error) {
|
|
|
|
var params QueryProposalsParams
|
2018-08-22 00:10:11 -07:00
|
|
|
err2 := keeper.cdc.UnmarshalJSON(req.Data, ¶ms)
|
2018-08-03 12:55:00 -07:00
|
|
|
if err2 != nil {
|
2018-10-19 09:55:20 -07:00
|
|
|
return nil, sdk.ErrUnknownRequest(sdk.AppendMsgToErr("incorrectly formatted request data", err2.Error()))
|
2018-07-31 18:48:32 -07:00
|
|
|
}
|
2018-08-03 12:55:00 -07:00
|
|
|
|
|
|
|
proposals := keeper.GetProposalsFiltered(ctx, params.Voter, params.Depositer, params.ProposalStatus, params.NumLatestProposals)
|
|
|
|
|
2018-09-13 11:17:32 -07:00
|
|
|
bz, err2 := codec.MarshalJSONIndent(keeper.cdc, proposals)
|
2018-08-22 00:10:11 -07:00
|
|
|
if err2 != nil {
|
2018-10-19 09:55:20 -07:00
|
|
|
return nil, sdk.ErrInternal(sdk.AppendMsgToErr("could not marshal result to JSON", err2.Error()))
|
2018-08-22 00:10:11 -07:00
|
|
|
}
|
2018-08-03 12:55:00 -07:00
|
|
|
return bz, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Params for query 'custom/gov/tally'
|
|
|
|
type QueryTallyParams struct {
|
|
|
|
ProposalID int64
|
|
|
|
}
|
|
|
|
|
2018-08-31 15:22:37 -07:00
|
|
|
// nolint: unparam
|
2018-08-03 12:55:00 -07:00
|
|
|
func queryTally(ctx sdk.Context, path []string, req abci.RequestQuery, keeper Keeper) (res []byte, err sdk.Error) {
|
|
|
|
// TODO: Dependant on #1914
|
|
|
|
|
2018-08-17 00:40:30 -07:00
|
|
|
var proposalID int64
|
2018-08-22 00:10:11 -07:00
|
|
|
err2 := keeper.cdc.UnmarshalJSON(req.Data, proposalID)
|
2018-08-17 00:40:30 -07:00
|
|
|
if err2 != nil {
|
2018-10-19 09:55:20 -07:00
|
|
|
return nil, sdk.ErrUnknownRequest(sdk.AppendMsgToErr("incorrectly formatted request data", err2.Error()))
|
2018-08-17 00:40:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
proposal := keeper.GetProposal(ctx, proposalID)
|
|
|
|
if proposal == nil {
|
2018-10-19 09:55:20 -07:00
|
|
|
return nil, ErrUnknownProposal(DefaultCodespace, proposalID)
|
2018-08-17 00:40:30 -07:00
|
|
|
}
|
|
|
|
|
2018-08-22 00:10:11 -07:00
|
|
|
var tallyResult TallyResult
|
|
|
|
|
2018-08-17 00:40:30 -07:00
|
|
|
if proposal.GetStatus() == StatusDepositPeriod {
|
2018-08-22 00:10:11 -07:00
|
|
|
tallyResult = EmptyTallyResult()
|
|
|
|
} else if proposal.GetStatus() == StatusPassed || proposal.GetStatus() == StatusRejected {
|
|
|
|
tallyResult = proposal.GetTallyResult()
|
|
|
|
} else {
|
2018-09-25 11:45:20 -07:00
|
|
|
_, tallyResult = tally(ctx, keeper, proposal)
|
2018-08-17 00:40:30 -07:00
|
|
|
}
|
|
|
|
|
2018-09-13 11:17:32 -07:00
|
|
|
bz, err2 := codec.MarshalJSONIndent(keeper.cdc, tallyResult)
|
2018-08-22 00:10:11 -07:00
|
|
|
if err2 != nil {
|
2018-10-19 09:55:20 -07:00
|
|
|
return nil, sdk.ErrInternal(sdk.AppendMsgToErr("could not marshal result to JSON", err2.Error()))
|
2018-08-17 00:40:30 -07:00
|
|
|
}
|
2018-08-22 00:10:11 -07:00
|
|
|
return bz, nil
|
2018-07-31 18:48:32 -07:00
|
|
|
}
|