Skip to content

Commit

Permalink
Gov Get Vote(s)/Deposit(s) Updates (#3091)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderbez authored and jackzampolin committed Dec 14, 2018
1 parent 110fd63 commit ff6c2f2
Show file tree
Hide file tree
Showing 9 changed files with 368 additions and 82 deletions.
6 changes: 6 additions & 0 deletions PENDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,14 @@ FEATURES
IMPROVEMENTS

* Gaia REST API (`gaiacli advanced rest-server`)
* \#2879, \#2880 Update deposit and vote endpoints to perform a direct txs query
when a given proposal is inactive and thus having votes and deposits removed
from state.

* Gaia CLI (`gaiacli`)
* \#2879, \#2880 Update deposit and vote CLI commands to perform a direct txs query
when a given proposal is inactive and thus having votes and deposits removed
from state.

* Gaia
* [\#3021](https://github.com/cosmos/cosmos-sdk/pull/3021) Add `--gentx-dir` to `gaiad collect-gentxs` to specify a directory from which collect and load gentxs.
Expand Down
1 change: 1 addition & 0 deletions client/lcd/lcd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,7 @@ func TestProposalsQuery(t *testing.T) {
proposals := getProposalsFilterStatus(t, port, gov.StatusDepositPeriod)
require.Len(t, proposals, 1)
require.Equal(t, proposalID1, proposals[0].GetProposalID())

// Only proposals #2 and #3 should be in Voting Period
proposals = getProposalsFilterStatus(t, port, gov.StatusVotingPeriod)
require.Len(t, proposals, 2)
Expand Down
9 changes: 6 additions & 3 deletions client/tx/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ $ gaiacli query txs --tags '<tag1>:<value1>&<tag2>:<value2>'
}

cliCtx := context.NewCLIContext().WithCodec(cdc)
txs, err := searchTxs(cliCtx, cdc, tmTags)
txs, err := SearchTxs(cliCtx, cdc, tmTags)
if err != nil {
return err
}
Expand Down Expand Up @@ -89,7 +89,10 @@ $ gaiacli query txs --tags '<tag1>:<value1>&<tag2>:<value2>'
return cmd
}

func searchTxs(cliCtx context.CLIContext, cdc *codec.Codec, tags []string) ([]Info, error) {
// SearchTxs performs a search for transactions for a given set of tags via
// Tendermint RPC. It returns a slice of Info object containing txs and metadata.
// An error is returned if the query fails.
func SearchTxs(cliCtx context.CLIContext, cdc *codec.Codec, tags []string) ([]Info, error) {
if len(tags) == 0 {
return nil, errors.New("must declare at least one tag to search")
}
Expand Down Expand Up @@ -172,7 +175,7 @@ func SearchTxRequestHandlerFn(cliCtx context.CLIContext, cdc *codec.Codec) http.
tags = append(tags, tag)
}

txs, err = searchTxs(cliCtx, cdc, tags)
txs, err = SearchTxs(cliCtx, cdc, tags)
if err != nil {
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
Expand Down
85 changes: 60 additions & 25 deletions x/gov/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/gov"
govClientUtils "github.com/cosmos/cosmos-sdk/x/gov/client/utils"
gcutils "github.com/cosmos/cosmos-sdk/x/gov/client/utils"
)

// GetCmdQueryProposal implements the query proposal command.
Expand Down Expand Up @@ -106,7 +106,7 @@ $ gaiacli query gov proposals --status (DepositPeriod|VotingPeriod|Passed|Reject
}

if len(strProposalStatus) != 0 {
proposalStatus, err := gov.ProposalStatusFromString(govClientUtils.NormalizeProposalStatus(strProposalStatus))
proposalStatus, err := gov.ProposalStatusFromString(gcutils.NormalizeProposalStatus(strProposalStatus))
if err != nil {
return err
}
Expand Down Expand Up @@ -160,8 +160,9 @@ func GetCmdQueryVote(queryRoute string, cdc *codec.Codec) *cobra.Command {
Args: cobra.ExactArgs(2),
Short: "Query details of a single vote",
Long: strings.TrimSpace(`
Query details for a single vote on a proposal. You can find the proposal-id by running gaiacli query gov proposals:
Query details for a single vote on a proposal given its identifier.
Example:
$ gaiacli query gov vote 1 cosmos1skjwj5whet0lpe65qaq4rpq03hjxlwd9nf39lk
`),
RunE: func(cmd *cobra.Command, args []string) error {
Expand All @@ -179,25 +180,32 @@ $ gaiacli query gov vote 1 cosmos1skjwj5whet0lpe65qaq4rpq03hjxlwd9nf39lk
return fmt.Errorf("Failed to fetch proposal-id %d: %s", proposalID, err)
}

// get voter address
voterAddr, err := sdk.AccAddressFromBech32(args[1])
if err != nil {
return err
}

// Construct query
params := gov.NewQueryVoteParams(proposalID, voterAddr)
bz, err := cdc.MarshalJSON(params)
if err != nil {
return err
}

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

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

if vote.Empty() {
res, err = gcutils.QueryVoteByTxQuery(cdc, cliCtx, params)
if err != nil {
return err
}
}

fmt.Println(string(res))
return nil
},
Expand All @@ -213,8 +221,9 @@ func GetCmdQueryVotes(queryRoute string, cdc *codec.Codec) *cobra.Command {
Args: cobra.ExactArgs(1),
Short: "Query votes on a proposal",
Long: strings.TrimSpace(`
Query vote details for a single proposal. You can find the proposal-id by running gaiacli query gov proposals:
Query vote details for a single proposal by its identifier.
Example:
$ gaiacli query gov votes 1
`),
RunE: func(cmd *cobra.Command, args []string) error {
Expand All @@ -226,21 +235,30 @@ $ gaiacli query gov votes 1
return fmt.Errorf("proposal-id %s not a valid int, please input a valid proposal-id", args[0])
}

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

// check to see if the proposal is in the store
_, err = queryProposal(proposalID, cliCtx, cdc, queryRoute)
res, err := queryProposal(proposalID, cliCtx, cdc, queryRoute)
if err != nil {
return fmt.Errorf("Failed to fetch proposal-id %d: %s", proposalID, err)
}

// Construct query
params := gov.NewQueryProposalParams(proposalID)
bz, err := cdc.MarshalJSON(params)
if err != nil {
var proposal gov.Proposal
if err := cdc.UnmarshalJSON(res, &proposal); err != nil {
return err
}

// Query store
res, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/votes", queryRoute), bz)
propStatus := proposal.GetStatus()
if !(propStatus == gov.StatusVotingPeriod || propStatus == gov.StatusDepositPeriod) {
res, err = gcutils.QueryVotesByTxQuery(cdc, cliCtx, params)
} else {
res, err = cliCtx.QueryWithData(fmt.Sprintf("custom/%s/votes", queryRoute), bz)
}

if err != nil {
return err
}
Expand All @@ -261,8 +279,9 @@ func GetCmdQueryDeposit(queryRoute string, cdc *codec.Codec) *cobra.Command {
Args: cobra.ExactArgs(2),
Short: "Query details of a deposit",
Long: strings.TrimSpace(`
Query details for a single proposal deposit on a proposal. You can find the proposal-id by running gaiacli query gov proposals:
Query details for a single proposal deposit on a proposal by its identifier.
Example:
$ gaiacli query gov deposit 1 cosmos1skjwj5whet0lpe65qaq4rpq03hjxlwd9nf39lk
`),
RunE: func(cmd *cobra.Command, args []string) error {
Expand All @@ -280,25 +299,32 @@ $ gaiacli query gov deposit 1 cosmos1skjwj5whet0lpe65qaq4rpq03hjxlwd9nf39lk
return fmt.Errorf("Failed to fetch proposal-id %d: %s", proposalID, err)
}

// Get the depositer address
depositorAddr, err := sdk.AccAddressFromBech32(args[1])
if err != nil {
return err
}

// Construct query
params := gov.NewQueryDepositParams(proposalID, depositorAddr)
bz, err := cdc.MarshalJSON(params)
if err != nil {
return err
}

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

var deposit gov.Deposit
cdc.UnmarshalJSON(res, &deposit)

if deposit.Empty() {
res, err = gcutils.QueryDepositByTxQuery(cdc, cliCtx, params)
if err != nil {
return err
}
}

fmt.Println(string(res))
return nil
},
Expand Down Expand Up @@ -327,21 +353,30 @@ $ gaiacli query gov deposits 1
return fmt.Errorf("proposal-id %s not a valid uint, please input a valid proposal-id", args[0])
}

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

// check to see if the proposal is in the store
_, err = queryProposal(proposalID, cliCtx, cdc, queryRoute)
res, err := queryProposal(proposalID, cliCtx, cdc, queryRoute)
if err != nil {
return fmt.Errorf("Failed to fetch proposal-id %d: %s", proposalID, err)
}

// Construct query
params := gov.NewQueryProposalParams(proposalID)
bz, err := cdc.MarshalJSON(params)
if err != nil {
var proposal gov.Proposal
if err := cdc.UnmarshalJSON(res, &proposal); err != nil {
return err
}

// Query store
res, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/deposits", queryRoute), bz)
propStatus := proposal.GetStatus()
if !(propStatus == gov.StatusVotingPeriod || propStatus == gov.StatusDepositPeriod) {
res, err = gcutils.QueryDepositsByTxQuery(cdc, cliCtx, params)
} else {
res, err = cliCtx.QueryWithData(fmt.Sprintf("custom/%s/deposits", queryRoute), bz)
}

if err != nil {
return err
}
Expand Down
Loading

0 comments on commit ff6c2f2

Please sign in to comment.