Skip to content

Commit

Permalink
asdf
Browse files Browse the repository at this point in the history
  • Loading branch information
sunnya97 committed Aug 24, 2018
1 parent 5128a7c commit 6391611
Show file tree
Hide file tree
Showing 2 changed files with 182 additions and 91 deletions.
230 changes: 139 additions & 91 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 @@ -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)
bz, err := cdc.MarshalJSON(params)
if err != nil {
return err
}

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

fmt.Println(string(output))
fmt.Println(string(res))
return nil
},
}
Expand All @@ -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
}

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{}

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
}
cliCtx := context.NewCLIContext().WithCodec(cdc)

for _, proposal := range matchingProposals {
fmt.Printf(" %d - %s\n", proposal.GetProposalID(), proposal.GetTitle())
res, err := cliCtx.QueryWithData("custom/gov/proposals", bz)
if err != nil {
return err
}

fmt.Println(string(res))
return nil
},
}
Expand Down Expand Up @@ -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,
}
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("custom/gov/vote", bz)
if err != nil {
return err
}

fmt.Println(string(output))
fmt.Println(string(res))
return nil
},
}
Expand All @@ -429,42 +390,129 @@ 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,
}
bz, err := cdc.MarshalJSON(params)
if err != nil {
return err
}

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

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

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

return cmd
}

if proposal.GetStatus() != gov.StatusVotingPeriod {
fmt.Println("Proposal not in voting period.")
return nil
// 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
}

res2, err := cliCtx.QuerySubspace(gov.KeyVotesSubspace(proposalID), storeName)
params := gov.QueryDepositParams{
Depositer: depositerAddr,
ProposalID: proposalID,
}
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)
res, err := cliCtx.QueryWithData("custom/gov/deposit", bz)
if err != nil {
return err
}

output, err := wire.MarshalJSONIndent(cdc, votes)
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
}

fmt.Println(string(output))
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 votes are being queried")
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
}
43 changes: 43 additions & 0 deletions x/gov/client/rest/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit 6391611

Please sign in to comment.