Skip to content

Commit

Permalink
Merge PR #2141: Governance CLI uses Querier
Browse files Browse the repository at this point in the history
  • Loading branch information
cwgoes authored Aug 28, 2018
2 parents 73f90e8 + bfdeb3f commit d1ecc8f
Show file tree
Hide file tree
Showing 2 changed files with 189 additions and 84 deletions.
230 changes: 146 additions & 84 deletions x/gov/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ import (
"github.com/cosmos/cosmos-sdk/x/gov"

"encoding/json"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"io/ioutil"
"strings"

"github.com/spf13/cobra"
"github.com/spf13/viper"
)

const (
Expand Down Expand Up @@ -244,28 +244,29 @@ func GetCmdVote(cdc *wire.Codec) *cobra.Command {
}

// GetCmdQueryProposal implements the query proposal command.
func GetCmdQueryProposal(storeName string, cdc *wire.Codec) *cobra.Command {
func GetCmdQueryProposal(queryRoute string, cdc *wire.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "query-proposal",
Short: "query proposal details",
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)
proposalID := viper.GetInt64(flagProposalID)

res, err := cliCtx.QueryStore(gov.KeyProposal(proposalID), storeName)
if len(res) == 0 || err != nil {
return errors.Errorf("proposalID [%d] is not existed", proposalID)
params := gov.QueryProposalParams{
ProposalID: proposalID,
}

var proposal gov.Proposal
cdc.MustUnmarshalBinary(res, &proposal)
bz, err := cdc.MarshalJSON(params)
if err != nil {
return err
}

output, err := wire.MarshalJSONIndent(cdc, proposal)
res, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/proposal", queryRoute), bz)
if err != nil {
return err
}

fmt.Println(string(output))
fmt.Println(string(res))
return nil
},
}
Expand All @@ -277,7 +278,7 @@ func GetCmdQueryProposal(storeName string, cdc *wire.Codec) *cobra.Command {

// nolint: gocyclo
// GetCmdQueryProposals implements a query proposals command.
func GetCmdQueryProposals(storeName string, cdc *wire.Codec) *cobra.Command {
func GetCmdQueryProposals(queryRoute string, cdc *wire.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "query-proposals",
Short: "query proposals with optional filters",
Expand All @@ -287,77 +288,50 @@ func GetCmdQueryProposals(storeName string, cdc *wire.Codec) *cobra.Command {
strProposalStatus := viper.GetString(flagStatus)
latestProposalsIDs := viper.GetInt64(flagLatestProposalIDs)

var err error
var voterAddr sdk.AccAddress
var depositerAddr sdk.AccAddress
var proposalStatus gov.ProposalStatus
params := gov.QueryProposalsParams{
NumLatestProposals: latestProposalsIDs,
}

if len(bechDepositerAddr) != 0 {
depositerAddr, err = sdk.AccAddressFromBech32(bechDepositerAddr)
depositerAddr, err := sdk.AccAddressFromBech32(bechDepositerAddr)
if err != nil {
return err
}
params.Depositer = depositerAddr
}

if len(bechVoterAddr) != 0 {
voterAddr, err = sdk.AccAddressFromBech32(bechVoterAddr)
voterAddr, err := sdk.AccAddressFromBech32(bechVoterAddr)
if err != nil {
return err
}
params.Voter = voterAddr
}

if len(strProposalStatus) != 0 {
proposalStatus, err = gov.ProposalStatusFromString(strProposalStatus)
proposalStatus, err := gov.ProposalStatusFromString(strProposalStatus)
if err != nil {
return err
}
params.ProposalStatus = proposalStatus
}

cliCtx := context.NewCLIContext().WithCodec(cdc)

res, err := cliCtx.QueryStore(gov.KeyNextProposalID, storeName)
bz, err := cdc.MarshalJSON(params)
if err != nil {
return err
}
var maxProposalID int64
cdc.MustUnmarshalBinary(res, &maxProposalID)

matchingProposals := []gov.Proposal{}
cliCtx := context.NewCLIContext().WithCodec(cdc)

if latestProposalsIDs == 0 {
latestProposalsIDs = maxProposalID
res, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/proposals", queryRoute), bz)
if err != nil {
return err
}

for proposalID := maxProposalID - latestProposalsIDs; proposalID < maxProposalID; proposalID++ {
if voterAddr != nil {
res, err = cliCtx.QueryStore(gov.KeyVote(proposalID, voterAddr), storeName)
if err != nil || len(res) == 0 {
continue
}
}

if depositerAddr != nil {
res, err = cliCtx.QueryStore(gov.KeyDeposit(proposalID, depositerAddr), storeName)
if err != nil || len(res) == 0 {
continue
}
}

res, err = cliCtx.QueryStore(gov.KeyProposal(proposalID), storeName)
if err != nil || len(res) == 0 {
continue
}

var proposal gov.Proposal
cdc.MustUnmarshalBinary(res, &proposal)

if len(strProposalStatus) != 0 {
if proposal.GetStatus() != proposalStatus {
continue
}
}

matchingProposals = append(matchingProposals, proposal)
var matchingProposals []gov.Proposal
err = cdc.UnmarshalJSON(res, &matchingProposals)
if err != nil {
return err
}

if len(matchingProposals) == 0 {
Expand All @@ -383,7 +357,7 @@ func GetCmdQueryProposals(storeName string, cdc *wire.Codec) *cobra.Command {

// Command to Get a Proposal Information
// GetCmdQueryVote implements the query proposal vote command.
func GetCmdQueryVote(storeName string, cdc *wire.Codec) *cobra.Command {
func GetCmdQueryVote(queryRoute string, cdc *wire.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "query-vote",
Short: "query vote",
Expand All @@ -396,20 +370,21 @@ func GetCmdQueryVote(storeName string, cdc *wire.Codec) *cobra.Command {
return err
}

res, err := cliCtx.QueryStore(gov.KeyVote(proposalID, voterAddr), storeName)
if len(res) == 0 || err != nil {
return errors.Errorf("proposalID [%d] does not exist", proposalID)
params := gov.QueryVoteParams{
Voter: voterAddr,
ProposalID: proposalID,
}
bz, err := cdc.MarshalJSON(params)
if err != nil {
return err
}

var vote gov.Vote
cdc.MustUnmarshalBinary(res, &vote)

output, err := wire.MarshalJSONIndent(cdc, vote)
res, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/vote", queryRoute), bz)
if err != nil {
return err
}

fmt.Println(string(output))
fmt.Println(string(res))
return nil
},
}
Expand All @@ -421,50 +396,137 @@ func GetCmdQueryVote(storeName string, cdc *wire.Codec) *cobra.Command {
}

// GetCmdQueryVotes implements the command to query for proposal votes.
func GetCmdQueryVotes(storeName string, cdc *wire.Codec) *cobra.Command {
func GetCmdQueryVotes(queryRoute string, cdc *wire.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "query-votes",
Short: "query votes on a proposal",
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)
proposalID := viper.GetInt64(flagProposalID)

res, err := cliCtx.QueryStore(gov.KeyProposal(proposalID), storeName)
if len(res) == 0 || err != nil {
return errors.Errorf("proposalID [%d] does not exist", proposalID)
params := gov.QueryVotesParams{
ProposalID: proposalID,
}
bz, err := cdc.MarshalJSON(params)
if err != nil {
return err
}

var proposal gov.Proposal
cdc.MustUnmarshalBinary(res, &proposal)
res, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/votes", queryRoute), bz)
if err != nil {
return err
}

if proposal.GetStatus() != gov.StatusVotingPeriod {
fmt.Println("Proposal not in voting period.")
return nil
fmt.Println(string(res))
return nil
},
}

cmd.Flags().String(flagProposalID, "", "proposalID of which proposal's votes are being queried")

return cmd
}

// Command to Get a specific Deposit Information
// GetCmdQueryDeposit implements the query proposal deposit command.
func GetCmdQueryDeposit(queryRoute string, cdc *wire.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "query-deposit",
Short: "query deposit",
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)
proposalID := viper.GetInt64(flagProposalID)

depositerAddr, err := sdk.AccAddressFromBech32(viper.GetString(flagDepositer))
if err != nil {
return err
}

params := gov.QueryDepositParams{
Depositer: depositerAddr,
ProposalID: proposalID,
}
bz, err := cdc.MarshalJSON(params)
if err != nil {
return err
}

res, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/deposit", queryRoute), bz)
if err != nil {
return err
}

fmt.Println(string(res))
return nil
},
}

cmd.Flags().String(flagProposalID, "", "proposalID of proposal deposited on")
cmd.Flags().String(flagDepositer, "", "bech32 depositer address")

return cmd
}

// GetCmdQueryDeposits implements the command to query for proposal deposits.
func GetCmdQueryDeposits(queryRoute string, cdc *wire.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "query-deposits",
Short: "query deposits on a proposal",
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)
proposalID := viper.GetInt64(flagProposalID)

params := gov.QueryDepositsParams{
ProposalID: proposalID,
}
bz, err := cdc.MarshalJSON(params)
if err != nil {
return err
}

res2, err := cliCtx.QuerySubspace(gov.KeyVotesSubspace(proposalID), storeName)
res, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/deposits", queryRoute), bz)
if err != nil {
return err
}

var votes []gov.Vote
for i := 0; i < len(res2); i++ {
var vote gov.Vote
cdc.MustUnmarshalBinary(res2[i].Value, &vote)
votes = append(votes, vote)
fmt.Println(string(res))
return nil
},
}

cmd.Flags().String(flagProposalID, "", "proposalID of which proposal's deposits are being queried")

return cmd
}

// GetCmdQueryDeposits implements the command to query for proposal deposits.
func GetCmdQueryTally(queryRoute string, cdc *wire.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "query-tally",
Short: "get the tally of a proposal vote",
RunE: func(cmd *cobra.Command, args []string) error {
cliCtx := context.NewCLIContext().WithCodec(cdc)
proposalID := viper.GetInt64(flagProposalID)

params := gov.QueryTallyParams{
ProposalID: proposalID,
}
bz, err := cdc.MarshalJSON(params)
if err != nil {
return err
}

output, err := wire.MarshalJSONIndent(cdc, votes)
res, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/tally", queryRoute), bz)
if err != nil {
return err
}

fmt.Println(string(output))
fmt.Println(string(res))
return nil
},
}

cmd.Flags().String(flagProposalID, "", "proposalID of which proposal's votes are being queried")
cmd.Flags().String(flagProposalID, "", "proposalID of which proposal is being tallied")

return cmd
}
43 changes: 43 additions & 0 deletions x/gov/client/rest/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -438,3 +438,46 @@ func queryProposalsWithParameterFn(cdc *wire.Codec) http.HandlerFunc {
w.Write(res)
}
}

// nolint: gocyclo
// todo: Split this functionality into helper functions to remove the above
func queryTallyOnProposalHandlerFn(cdc *wire.Codec) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
strProposalID := vars[RestProposalID]

if len(strProposalID) == 0 {
w.WriteHeader(http.StatusBadRequest)
err := errors.New("proposalId required but not specified")
w.Write([]byte(err.Error()))

return
}

proposalID, ok := parseInt64OrReturnBadRequest(strProposalID, w)
if !ok {
return
}

cliCtx := context.NewCLIContext().WithCodec(cdc)

params := gov.QueryTallyParams{
ProposalID: proposalID,
}
bz, err := cdc.MarshalJSON(params)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
return
}

res, err := cliCtx.QueryWithData("custom/gov/tally", bz)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}

w.Write(res)
}
}

0 comments on commit d1ecc8f

Please sign in to comment.