Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

R4R: x/gov Get Vote(s)/Deposit(s) Updates #3091

Merged
merged 26 commits into from
Dec 14, 2018
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions PENDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,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 @@ -686,6 +686,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()
alexanderbez marked this conversation as resolved.
Show resolved Hide resolved
if !(propStatus == gov.StatusVotingPeriod || propStatus == gov.StatusDepositPeriod) {
fedekunze marked this conversation as resolved.
Show resolved Hide resolved
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.
alexanderbez marked this conversation as resolved.
Show resolved Hide resolved

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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't we get the proposal and query the deposit tx if it's not on deposit or voting period ? I mean, shouldn't we have an if...else around res, err := cliCtx.QueryWithData(fmt.Sprintf("custom/%s/deposit", queryRoute)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm I dont think so. I think its implicit. Deposit will be empty in the case where its not voting or deposit period, no?

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