Skip to content

Commit

Permalink
gov query procedures
Browse files Browse the repository at this point in the history
  • Loading branch information
sunnya97 committed Oct 24, 2018
1 parent d666658 commit 7301a4b
Show file tree
Hide file tree
Showing 7 changed files with 147 additions and 5 deletions.
3 changes: 3 additions & 0 deletions PENDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ FEATURES
* [gaia-lite] [\#2113](https://github.com/cosmos/cosmos-sdk/issues/2113) Rename `/accounts/{address}/send` to `/bank/accounts/{address}/transfers`, rename `/accounts/{address}` to `/auth/accounts/{address}`
* [gaia-lite] [\#2478](https://github.com/cosmos/cosmos-sdk/issues/2478) Add query gov proposal's deposits endpoint
* [gaia-lite] [\#2477](https://github.com/cosmos/cosmos-sdk/issues/2477) Add query validator's outgoing redelegations and unbonding delegations endpoints
* [gaia-lite] [\#2479](https://github.com/cosmos/cosmos-sdk/issues/2479) Add query governance procedures endpoints

* Gaia CLI (`gaiacli`)
* [cli] Cmds to query staking pool and params
Expand All @@ -149,6 +150,7 @@ FEATURES
* [stake][cli] [\#1672](https://github.com/cosmos/cosmos-sdk/issues/1672) Introduced
new commission flags for validator commands `create-validator` and `edit-validator`.
* [stake][cli] [\#1890](https://github.com/cosmos/cosmos-sdk/issues/1890) Add `--genesis-format` flag to `gaiacli tx create-validator` to produce transactions in genesis-friendly format.
* [stake][cli] [\#2479](https://github.com/cosmos/cosmos-sdk/issues/2479) Add query governance procedures commands

* Gaia
* [cli] #2170 added ability to show the node's address via `gaiad tendermint show-address`
Expand Down Expand Up @@ -217,6 +219,7 @@ IMPROVEMENTS
* [types/decimal] \#2378 - Added truncate functionality to decimal
* [client] [\#1184](https://github.com/cosmos/cosmos-sdk/issues/1184) Remove unused `client/tx/sign.go`.
* [tools] \#2464 Lock binary dependencies to a specific version
* [x/gov] [\#2479](https://github.com/cosmos/cosmos-sdk/issues/2479) Added querier for getting Procedures

* Tendermint

Expand Down
45 changes: 40 additions & 5 deletions client/lcd/lcd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -730,26 +730,31 @@ func TestProposalsQuery(t *testing.T) {
cleanup, _, _, port := InitializeTestLCD(t, 1, []sdk.AccAddress{addrs[0], addrs[1]})
defer cleanup()

depositProcedure := getDepositProcedure(t, port)
halfMinDeposit := depositProcedure.MinDeposit.AmountOf("steak").Int64() / 2
getVotingProcedure(t, port)
getTallyingProcedure(t, port)

// Addr1 proposes (and deposits) proposals #1 and #2
resultTx := doSubmitProposal(t, port, seeds[0], names[0], passwords[0], addrs[0], 5)
resultTx := doSubmitProposal(t, port, seeds[0], names[0], passwords[0], addrs[0], halfMinDeposit)
var proposalID1 int64
cdc.UnmarshalBinaryBare(resultTx.DeliverTx.GetData(), &proposalID1)
tests.WaitForHeight(resultTx.Height+1, port)
resultTx = doSubmitProposal(t, port, seeds[0], names[0], passwords[0], addrs[0], 5)
resultTx = doSubmitProposal(t, port, seeds[0], names[0], passwords[0], addrs[0], halfMinDeposit)
var proposalID2 int64
cdc.UnmarshalBinaryBare(resultTx.DeliverTx.GetData(), &proposalID2)
tests.WaitForHeight(resultTx.Height+1, port)

// Addr2 proposes (and deposits) proposals #3
resultTx = doSubmitProposal(t, port, seeds[1], names[1], passwords[1], addrs[1], 5)
resultTx = doSubmitProposal(t, port, seeds[1], names[1], passwords[1], addrs[1], halfMinDeposit)
var proposalID3 int64
cdc.UnmarshalBinaryBare(resultTx.DeliverTx.GetData(), &proposalID3)
tests.WaitForHeight(resultTx.Height+1, port)

// Addr2 deposits on proposals #2 & #3
resultTx = doDeposit(t, port, seeds[1], names[1], passwords[1], addrs[1], proposalID2, 5)
resultTx = doDeposit(t, port, seeds[1], names[1], passwords[1], addrs[1], proposalID2, halfMinDeposit)
tests.WaitForHeight(resultTx.Height+1, port)
resultTx = doDeposit(t, port, seeds[1], names[1], passwords[1], addrs[1], proposalID3, 5)
resultTx = doDeposit(t, port, seeds[1], names[1], passwords[1], addrs[1], proposalID3, halfMinDeposit)
tests.WaitForHeight(resultTx.Height+1, port)

// check deposits match proposal and individual deposits
Expand Down Expand Up @@ -1230,6 +1235,36 @@ func getValidatorRedelegations(t *testing.T, port string, validatorAddr sdk.ValA

// ============= Governance Module ================

func getDepositProcedure(t *testing.T, port string) gov.DepositProcedure {
res, body := Request(t, port, "GET", "/gov/procedure/deposit", nil)
require.Equal(t, http.StatusOK, res.StatusCode, body)

var depositProcedure gov.DepositProcedure
err := cdc.UnmarshalJSON([]byte(body), &depositProcedure)
require.Nil(t, err)
return depositProcedure
}

func getVotingProcedure(t *testing.T, port string) gov.VotingProcedure {
res, body := Request(t, port, "GET", "/gov/procedure/voting", nil)
require.Equal(t, http.StatusOK, res.StatusCode, body)

var votingProcedure gov.VotingProcedure
err := cdc.UnmarshalJSON([]byte(body), &votingProcedure)
require.Nil(t, err)
return votingProcedure
}

func getTallyingProcedure(t *testing.T, port string) gov.TallyingProcedure {
res, body := Request(t, port, "GET", "/gov/procedure/tallying", nil)
require.Equal(t, http.StatusOK, res.StatusCode, body)

var tallyingProcedure gov.TallyingProcedure
err := cdc.UnmarshalJSON([]byte(body), &tallyingProcedure)
require.Nil(t, err)
return tallyingProcedure
}

func getProposal(t *testing.T, port string, proposalID int64) gov.Proposal {
res, body := Request(t, port, "GET", fmt.Sprintf("/gov/proposals/%d", proposalID), nil)
require.Equal(t, http.StatusOK, res.StatusCode, body)
Expand Down
31 changes: 31 additions & 0 deletions cmd/gaia/cli_test/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,10 @@ func TestGaiaCLISubmitProposal(t *testing.T) {
tests.WaitForTMStart(port)
tests.WaitForNextNBlocksTM(2, port)

executeGetDepositProcedure(t, fmt.Sprintf("gaiacli query procedure deposit %v", flags))
executeGetVotingProcedure(t, fmt.Sprintf("gaiacli query procedure voting %v", flags))
executeGetTallyingProcedure(t, fmt.Sprintf("gaiacli query procedure tallying %v", flags))

fooAddr, _ := executeGetAddrPK(t, fmt.Sprintf("gaiacli keys show foo --output=json --home=%s", gaiacliHome))

fooAcc := executeGetAccount(t, fmt.Sprintf("gaiacli query account %s %v", fooAddr, flags))
Expand Down Expand Up @@ -714,6 +718,33 @@ func executeGetParams(t *testing.T, cmdStr string) stake.Params {
//___________________________________________________________________________________
// gov

func executeGetDepositProcedure(t *testing.T, cmdStr string) gov.DepositProcedure {
out, _ := tests.ExecuteT(t, cmdStr, "")
var depositProcedure gov.DepositProcedure
cdc := app.MakeCodec()
err := cdc.UnmarshalJSON([]byte(out), &depositProcedure)
require.NoError(t, err, "out %v\n, err %v", out, err)
return depositProcedure
}

func executeGetVotingProcedure(t *testing.T, cmdStr string) gov.VotingProcedure {
out, _ := tests.ExecuteT(t, cmdStr, "")
var votingProcedure gov.VotingProcedure
cdc := app.MakeCodec()
err := cdc.UnmarshalJSON([]byte(out), &votingProcedure)
require.NoError(t, err, "out %v\n, err %v", out, err)
return votingProcedure
}

func executeGetTallyingProcedure(t *testing.T, cmdStr string) gov.TallyingProcedure {
out, _ := tests.ExecuteT(t, cmdStr, "")
var tallyingProcedure gov.TallyingProcedure
cdc := app.MakeCodec()
err := cdc.UnmarshalJSON([]byte(out), &tallyingProcedure)
require.NoError(t, err, "out %v\n, err %v", out, err)
return tallyingProcedure
}

func executeGetProposal(t *testing.T, cmdStr string) gov.Proposal {
out, _ := tests.ExecuteT(t, cmdStr, "")
var proposal gov.Proposal
Expand Down
1 change: 1 addition & 0 deletions cmd/gaia/cmd/gaiacli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ func main() {
stakecmd.GetCmdQueryDelegations(storeStake, cdc),
stakecmd.GetCmdQueryParams(storeStake, cdc),
stakecmd.GetCmdQueryPool(storeStake, cdc),
govcmd.GetCmdQueryProcedure(storeGov, cdc),
govcmd.GetCmdQueryProposal(storeGov, cdc),
govcmd.GetCmdQueryProposals(storeGov, cdc),
govcmd.GetCmdQueryDeposit(storeGov, cdc),
Expand Down
24 changes: 24 additions & 0 deletions x/gov/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,30 @@ func GetCmdVote(cdc *codec.Codec) *cobra.Command {
return cmd
}

// GetCmdQueryProposal implements the query proposal command.
func GetCmdQueryProcedure(queryRoute string, cdc *codec.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "procedure [procedure-type]",
Short: "Query the procedure parameters of a certain part of the governance process",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
procedureType := args[0]

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

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

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

return cmd
}

// GetCmdQueryProposal implements the query proposal command.
func GetCmdQueryProposal(queryRoute string, cdc *codec.Codec) *cobra.Command {
cmd := &cobra.Command{
Expand Down
18 changes: 18 additions & 0 deletions x/gov/client/rest/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
// REST Variable names
// nolint
const (
RestProcedureType = "procedure-type"
RestProposalID = "proposal-id"
RestDepositer = "depositer"
RestVoter = "voter"
Expand All @@ -31,6 +32,8 @@ func RegisterRoutes(cliCtx context.CLIContext, r *mux.Router, cdc *codec.Codec)
r.HandleFunc(fmt.Sprintf("/gov/proposals/{%s}/deposits", RestProposalID), depositHandlerFn(cdc, cliCtx)).Methods("POST")
r.HandleFunc(fmt.Sprintf("/gov/proposals/{%s}/votes", RestProposalID), voteHandlerFn(cdc, cliCtx)).Methods("POST")

r.HandleFunc(fmt.Sprintf("/gov/procedure/{%s}", RestProcedureType), queryProcedureHandlerFn(cdc, cliCtx)).Methods("GET")

r.HandleFunc("/gov/proposals", queryProposalsWithParameterFn(cdc, cliCtx)).Methods("GET")
r.HandleFunc(fmt.Sprintf("/gov/proposals/{%s}", RestProposalID), queryProposalHandlerFn(cdc, cliCtx)).Methods("GET")
r.HandleFunc(fmt.Sprintf("/gov/proposals/{%s}/deposits", RestProposalID), queryDepositsHandlerFn(cdc, cliCtx)).Methods("GET")
Expand Down Expand Up @@ -163,6 +166,21 @@ func voteHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.HandlerFunc
}
}

func queryProcedureHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
procedureType := vars[RestProcedureType]

res, err := cliCtx.QueryWithData(fmt.Sprintf("custom/gov/procedure/%s", procedureType), nil)
if err != nil {
utils.WriteErrorResponse(w, http.StatusNotFound, err.Error())
return
}

utils.PostProcessResponse(w, cdc, res, cliCtx.Indent)
}
}

func queryProposalHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
Expand Down
30 changes: 30 additions & 0 deletions x/gov/queryable.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package gov

import (
"fmt"

"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
abci "github.com/tendermint/tendermint/abci/types"
)

// query endpoints supported by the governance Querier
const (
QueryProcedure = "procedure"
QueryProposals = "proposals"
QueryProposal = "proposal"
QueryDeposits = "deposits"
Expand All @@ -20,6 +23,8 @@ const (
func NewQuerier(keeper Keeper) sdk.Querier {
return func(ctx sdk.Context, path []string, req abci.RequestQuery) (res []byte, err sdk.Error) {
switch path[0] {
case QueryProcedure:
return queryProcedure(ctx, path[1:], req, keeper)
case QueryProposals:
return queryProposals(ctx, path[1:], req, keeper)
case QueryProposal:
Expand All @@ -40,6 +45,31 @@ func NewQuerier(keeper Keeper) sdk.Querier {
}
}

func queryProcedure(ctx sdk.Context, path []string, req abci.RequestQuery, keeper Keeper) (res []byte, err sdk.Error) {
switch path[0] {
case "deposit":
bz, err2 := codec.MarshalJSONIndent(keeper.cdc, keeper.GetDepositProcedure(ctx))
if err2 != nil {
return nil, sdk.ErrInternal(sdk.AppendMsgToErr("could not marshal result to JSON", err2.Error()))
}
return bz, nil
case "voting":
bz, err2 := codec.MarshalJSONIndent(keeper.cdc, keeper.GetVotingProcedure(ctx))
if err2 != nil {
return nil, sdk.ErrInternal(sdk.AppendMsgToErr("could not marshal result to JSON", err2.Error()))
}
return bz, nil
case "tallying":
bz, err2 := codec.MarshalJSONIndent(keeper.cdc, keeper.GetTallyingProcedure(ctx))
if err2 != nil {
return nil, sdk.ErrInternal(sdk.AppendMsgToErr("could not marshal result to JSON", err2.Error()))
}
return bz, nil
}

return res, sdk.ErrUnknownRequest(fmt.Sprintf("%s is not a valid query request path", req.Path))
}

// Params for query 'custom/gov/proposal'
type QueryProposalParams struct {
ProposalID int64
Expand Down

0 comments on commit 7301a4b

Please sign in to comment.