From 639161100515156455c5349096268d4f61f8caa7 Mon Sep 17 00:00:00 2001 From: Sunny Aggarwal Date: Wed, 22 Aug 2018 18:44:35 -0700 Subject: [PATCH 1/2] asdf --- x/gov/client/cli/tx.go | 246 +++++++++++++++++++++++--------------- x/gov/client/rest/rest.go | 43 +++++++ 2 files changed, 190 insertions(+), 99 deletions(-) diff --git a/x/gov/client/cli/tx.go b/x/gov/client/cli/tx.go index 2a7e71aac8..26883b9c37 100644 --- a/x/gov/client/cli/tx.go +++ b/x/gov/client/cli/tx.go @@ -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 ( @@ -252,20 +252,21 @@ func GetCmdQueryProposal(storeName string, cdc *wire.Codec) *cobra.Command { 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) - - output, err := wire.MarshalJSONIndent(cdc, proposal) + bz, err := cdc.MarshalJSON(params) if err != nil { return err } - fmt.Println(string(output)) + res, err := cliCtx.QueryWithData("custom/gov/proposal", bz) + if err != nil { + return err + } + + fmt.Println(string(res)) return nil }, } @@ -287,88 +288,47 @@ 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 + } + + bz, err := cdc.MarshalJSON(params) + if err != nil { + return err } cliCtx := context.NewCLIContext().WithCodec(cdc) - res, err := cliCtx.QueryStore(gov.KeyNextProposalID, storeName) + res, err := cliCtx.QueryWithData("custom/gov/proposals", bz) if err != nil { return err } - var maxProposalID int64 - cdc.MustUnmarshalBinary(res, &maxProposalID) - - matchingProposals := []gov.Proposal{} - - if latestProposalsIDs == 0 { - latestProposalsIDs = maxProposalID - } - - 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) - } - - if len(matchingProposals) == 0 { - fmt.Println("No matching proposals found") - return nil - } - - for _, proposal := range matchingProposals { - fmt.Printf(" %d - %s\n", proposal.GetProposalID(), proposal.GetTitle()) - } + fmt.Println(string(res)) return nil }, } @@ -396,20 +356,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, } - - var vote gov.Vote - cdc.MustUnmarshalBinary(res, &vote) - - output, err := wire.MarshalJSONIndent(cdc, vote) + bz, err := cdc.MarshalJSON(params) if err != nil { return err } - fmt.Println(string(output)) + res, err := cliCtx.QueryWithData("custom/gov/vote", bz) + if err != nil { + return err + } + + fmt.Println(string(res)) return nil }, } @@ -429,37 +390,20 @@ func GetCmdQueryVotes(storeName string, cdc *wire.Codec) *cobra.Command { 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, } - - var proposal gov.Proposal - cdc.MustUnmarshalBinary(res, &proposal) - - if proposal.GetStatus() != gov.StatusVotingPeriod { - fmt.Println("Proposal not in voting period.") - return nil - } - - res2, err := cliCtx.QuerySubspace(gov.KeyVotesSubspace(proposalID), storeName) + bz, err := cdc.MarshalJSON(params) 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) - } - - output, err := wire.MarshalJSONIndent(cdc, votes) + res, err := cliCtx.QueryWithData("custom/gov/votes", bz) if err != nil { return err } - fmt.Println(string(output)) + fmt.Println(string(res)) return nil }, } @@ -468,3 +412,107 @@ func GetCmdQueryVotes(storeName string, cdc *wire.Codec) *cobra.Command { return cmd } + +// Command to Get a specific Deposit Information +// GetCmdQueryDeposit implements the query proposal deposit command. +func GetCmdQueryDeposit(storeName 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("custom/gov/deposit", 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(storeName 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 + } + + res, err := cliCtx.QueryWithData("custom/gov/deposits", bz) + if err != nil { + return err + } + + 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(storeName 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 + } + + res, err := cliCtx.QueryWithData("custom/gov/tally", bz) + if err != nil { + return err + } + + fmt.Println(string(res)) + return nil + }, + } + + cmd.Flags().String(flagProposalID, "", "proposalID of which proposal is being tallied") + + return cmd +} diff --git a/x/gov/client/rest/rest.go b/x/gov/client/rest/rest.go index 5cdc7bda2b..ebe6123141 100644 --- a/x/gov/client/rest/rest.go +++ b/x/gov/client/rest/rest.go @@ -479,3 +479,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) + } +} From bfdeb3f8ce33d08828dd2e0f0822c3006d6751cc Mon Sep 17 00:00:00 2001 From: Sunny Aggarwal Date: Mon, 27 Aug 2018 15:18:01 -0700 Subject: [PATCH 2/2] works --- x/gov/client/cli/tx.go | 44 ++++++++++++++++++++++++++++-------------- 1 file changed, 29 insertions(+), 15 deletions(-) diff --git a/x/gov/client/cli/tx.go b/x/gov/client/cli/tx.go index 26883b9c37..65c922c6c1 100644 --- a/x/gov/client/cli/tx.go +++ b/x/gov/client/cli/tx.go @@ -244,7 +244,7 @@ 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", @@ -261,7 +261,7 @@ func GetCmdQueryProposal(storeName string, cdc *wire.Codec) *cobra.Command { return err } - res, err := cliCtx.QueryWithData("custom/gov/proposal", bz) + res, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/proposal", queryRoute), bz) if err != nil { return err } @@ -278,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", @@ -323,12 +323,26 @@ func GetCmdQueryProposals(storeName string, cdc *wire.Codec) *cobra.Command { cliCtx := context.NewCLIContext().WithCodec(cdc) - res, err := cliCtx.QueryWithData("custom/gov/proposals", bz) + res, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/proposals", queryRoute), bz) if err != nil { return err } - fmt.Println(string(res)) + var matchingProposals []gov.Proposal + err = cdc.UnmarshalJSON(res, &matchingProposals) + if err != nil { + return err + } + + if len(matchingProposals) == 0 { + fmt.Println("No matching proposals found") + return nil + } + + for _, proposal := range matchingProposals { + fmt.Printf(" %d - %s\n", proposal.GetProposalID(), proposal.GetTitle()) + } + return nil }, } @@ -343,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", @@ -365,7 +379,7 @@ func GetCmdQueryVote(storeName string, cdc *wire.Codec) *cobra.Command { return err } - res, err := cliCtx.QueryWithData("custom/gov/vote", bz) + res, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/vote", queryRoute), bz) if err != nil { return err } @@ -382,7 +396,7 @@ 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", @@ -398,7 +412,7 @@ func GetCmdQueryVotes(storeName string, cdc *wire.Codec) *cobra.Command { return err } - res, err := cliCtx.QueryWithData("custom/gov/votes", bz) + res, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/votes", queryRoute), bz) if err != nil { return err } @@ -415,7 +429,7 @@ func GetCmdQueryVotes(storeName string, cdc *wire.Codec) *cobra.Command { // Command to Get a specific Deposit Information // GetCmdQueryDeposit implements the query proposal deposit command. -func GetCmdQueryDeposit(storeName string, cdc *wire.Codec) *cobra.Command { +func GetCmdQueryDeposit(queryRoute string, cdc *wire.Codec) *cobra.Command { cmd := &cobra.Command{ Use: "query-deposit", Short: "query deposit", @@ -437,7 +451,7 @@ func GetCmdQueryDeposit(storeName string, cdc *wire.Codec) *cobra.Command { return err } - res, err := cliCtx.QueryWithData("custom/gov/deposit", bz) + res, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/deposit", queryRoute), bz) if err != nil { return err } @@ -454,7 +468,7 @@ func GetCmdQueryDeposit(storeName string, cdc *wire.Codec) *cobra.Command { } // GetCmdQueryDeposits implements the command to query for proposal deposits. -func GetCmdQueryDeposits(storeName string, cdc *wire.Codec) *cobra.Command { +func GetCmdQueryDeposits(queryRoute string, cdc *wire.Codec) *cobra.Command { cmd := &cobra.Command{ Use: "query-deposits", Short: "query deposits on a proposal", @@ -470,7 +484,7 @@ func GetCmdQueryDeposits(storeName string, cdc *wire.Codec) *cobra.Command { return err } - res, err := cliCtx.QueryWithData("custom/gov/deposits", bz) + res, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/deposits", queryRoute), bz) if err != nil { return err } @@ -486,7 +500,7 @@ func GetCmdQueryDeposits(storeName string, cdc *wire.Codec) *cobra.Command { } // GetCmdQueryDeposits implements the command to query for proposal deposits. -func GetCmdQueryTally(storeName string, cdc *wire.Codec) *cobra.Command { +func GetCmdQueryTally(queryRoute string, cdc *wire.Codec) *cobra.Command { cmd := &cobra.Command{ Use: "query-tally", Short: "get the tally of a proposal vote", @@ -502,7 +516,7 @@ func GetCmdQueryTally(storeName string, cdc *wire.Codec) *cobra.Command { return err } - res, err := cliCtx.QueryWithData("custom/gov/tally", bz) + res, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/tally", queryRoute), bz) if err != nil { return err }