Skip to content

Commit

Permalink
Merge PR #2869: Gov tally endpoint
Browse files Browse the repository at this point in the history
* Added tally endpoint
* Update querier.go
* rename queryable
* Address @alexanderbez comments
  • Loading branch information
fedekunze authored and cwgoes committed Nov 21, 2018
1 parent d8bbf85 commit d227e2a
Show file tree
Hide file tree
Showing 9 changed files with 217 additions and 182 deletions.
1 change: 1 addition & 0 deletions PENDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ IMPROVEMENTS
BUG FIXES

* Gaia REST API (`gaiacli advanced rest-server`)
- [gaia-lite] #2868 Added handler for governance tally endpoit

* Gaia CLI (`gaiacli`)

Expand Down
29 changes: 26 additions & 3 deletions client/lcd/lcd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,7 @@ func TestDeposit(t *testing.T) {
func TestVote(t *testing.T) {
name, password := "test", "1234567890"
addr, seed := CreateAddr(t, "test", password, GetKeyBase(t))
cleanup, _, _, port := InitializeTestLCD(t, 1, []sdk.AccAddress{addr})
cleanup, _, operAddrs, port := InitializeTestLCD(t, 1, []sdk.AccAddress{addr})
defer cleanup()

// create SubmitProposal TX
Expand All @@ -696,21 +696,35 @@ func TestVote(t *testing.T) {
proposal := getProposal(t, port, proposalID)
require.Equal(t, "Test", proposal.GetTitle())

// create SubmitProposal TX
// deposit
resultTx = doDeposit(t, port, seed, name, password, addr, proposalID, 5)
tests.WaitForHeight(resultTx.Height+1, port)

// query proposal
proposal = getProposal(t, port, proposalID)
require.Equal(t, gov.StatusVotingPeriod, proposal.GetStatus())

// create SubmitProposal TX
// vote
resultTx = doVote(t, port, seed, name, password, addr, proposalID)
tests.WaitForHeight(resultTx.Height+1, port)

vote := getVote(t, port, proposalID, addr)
require.Equal(t, proposalID, vote.ProposalID)
require.Equal(t, gov.OptionYes, vote.Option)

tally := getTally(t, port, proposalID)
require.Equal(t, sdk.ZeroDec(), tally.Yes, "tally should be 0 as the address is not bonded")

// create bond TX
resultTx = doDelegate(t, port, seed, name, password, addr, operAddrs[0], 60)
tests.WaitForHeight(resultTx.Height+1, port)

// vote
resultTx = doVote(t, port, seed, name, password, addr, proposalID)
tests.WaitForHeight(resultTx.Height+1, port)

tally = getTally(t, port, proposalID)
require.Equal(t, sdk.NewDec(60), tally.Yes, "tally should be equal to the amount delegated")
}

func TestUnjail(t *testing.T) {
Expand Down Expand Up @@ -1328,6 +1342,15 @@ func getVotes(t *testing.T, port string, proposalID uint64) []gov.Vote {
return votes
}

func getTally(t *testing.T, port string, proposalID uint64) gov.TallyResult {
res, body := Request(t, port, "GET", fmt.Sprintf("/gov/proposals/%d/tally", proposalID), nil)
require.Equal(t, http.StatusOK, res.StatusCode, body)
var tally gov.TallyResult
err := cdc.UnmarshalJSON([]byte(body), &tally)
require.Nil(t, err)
return tally
}

func getProposalsAll(t *testing.T, port string) []gov.Proposal {
res, body := Request(t, port, "GET", "/gov/proposals", nil)
require.Equal(t, http.StatusOK, res.StatusCode, body)
Expand Down
49 changes: 39 additions & 10 deletions client/lcd/swagger-ui/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1321,6 +1321,29 @@ paths:
description: Invalid proposal id
500:
description: Internal Server Error
/gov/proposals/{proposalId}/tally:
get:
summary: Get a proposal's tally result at the current time
description: Gets a proposal's tally result at the current time. If the proposal is pending deposits (i.e status 'DepositPeriod') it returns an empty tally result.
produces:
- application/json
tags:
- ICS22
parameters:
- type: string
description: proposal id
name: proposalId
required: true
in: path
responses:
200:
description: OK
schema:
$ref: "#/definitions/TallyResult"
400:
description: Invalid proposal id
500:
description: Internal Server Error
/gov/proposals/{proposalId}/votes:
post:
summary: Vote a proposal
Expand Down Expand Up @@ -1893,16 +1916,7 @@ definitions:
proposal_status:
type: string
tally_result:
type: object
properties:
yes:
type: string
abstain:
type: string
no:
type: string
no_with_veto:
type: string
$ref: "#/definitions/TallyResult"
submit_time:
type: string
total_deposit:
Expand All @@ -1922,6 +1936,21 @@ definitions:
type: integer
depositer:
"$ref": "#/definitions/Address"
TallyResult:
type: object
properties:
yes:
type: string
example: "0.0000000000"
abstain:
type: string
example: "0.0000000000"
no:
type: string
example: "0.0000000000"
no_with_veto:
type: string
example: "0.0000000000"
Vote:
type: object
properties:
Expand Down
35 changes: 11 additions & 24 deletions x/gov/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@ func GetCmdQueryProposal(queryRoute string, cdc *codec.Codec) *cobra.Command {
cliCtx := context.NewCLIContext().WithCodec(cdc)
proposalID := uint64(viper.GetInt64(flagProposalID))

params := gov.QueryProposalParams{
ProposalID: proposalID,
}

params := gov.NewQueryProposalParams(proposalID)
bz, err := cdc.MarshalJSON(params)
if err != nil {
return err
Expand Down Expand Up @@ -56,9 +53,11 @@ func GetCmdQueryProposals(queryRoute string, cdc *codec.Codec) *cobra.Command {
strProposalStatus := viper.GetString(flagStatus)
numLimit := uint64(viper.GetInt64(flagNumLimit))

params := gov.QueryProposalsParams{
Limit: numLimit,
}
var depositerAddr sdk.AccAddress
var voterAddr sdk.AccAddress
var proposalStatus gov.ProposalStatus

params := gov.NewQueryProposalsParams(proposalStatus, numLimit, voterAddr, depositerAddr)

if len(bechDepositerAddr) != 0 {
depositerAddr, err := sdk.AccAddressFromBech32(bechDepositerAddr)
Expand Down Expand Up @@ -138,10 +137,7 @@ func GetCmdQueryVote(queryRoute string, cdc *codec.Codec) *cobra.Command {
return err
}

params := gov.QueryVoteParams{
Voter: voterAddr,
ProposalID: proposalID,
}
params := gov.NewQueryVoteParams(proposalID, voterAddr)
bz, err := cdc.MarshalJSON(params)
if err != nil {
return err
Expand Down Expand Up @@ -172,9 +168,7 @@ func GetCmdQueryVotes(queryRoute string, cdc *codec.Codec) *cobra.Command {
cliCtx := context.NewCLIContext().WithCodec(cdc)
proposalID := uint64(viper.GetInt64(flagProposalID))

params := gov.QueryVotesParams{
ProposalID: proposalID,
}
params := gov.NewQueryProposalParams(proposalID)
bz, err := cdc.MarshalJSON(params)
if err != nil {
return err
Expand Down Expand Up @@ -210,10 +204,7 @@ func GetCmdQueryDeposit(queryRoute string, cdc *codec.Codec) *cobra.Command {
return err
}

params := gov.QueryDepositParams{
Depositer: depositerAddr,
ProposalID: proposalID,
}
params := gov.NewQueryDepositParams(proposalID, depositerAddr)
bz, err := cdc.MarshalJSON(params)
if err != nil {
return err
Expand Down Expand Up @@ -244,9 +235,7 @@ func GetCmdQueryDeposits(queryRoute string, cdc *codec.Codec) *cobra.Command {
cliCtx := context.NewCLIContext().WithCodec(cdc)
proposalID := uint64(viper.GetInt64(flagProposalID))

params := gov.QueryDepositsParams{
ProposalID: proposalID,
}
params := gov.NewQueryProposalParams(proposalID)
bz, err := cdc.MarshalJSON(params)
if err != nil {
return err
Expand Down Expand Up @@ -276,9 +265,7 @@ func GetCmdQueryTally(queryRoute string, cdc *codec.Codec) *cobra.Command {
cliCtx := context.NewCLIContext().WithCodec(cdc)
proposalID := uint64(viper.GetInt64(flagProposalID))

params := gov.QueryTallyParams{
ProposalID: proposalID,
}
params := gov.NewQueryProposalParams(proposalID)
bz, err := cdc.MarshalJSON(params)
if err != nil {
return err
Expand Down
25 changes: 9 additions & 16 deletions x/gov/client/rest/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func RegisterRoutes(cliCtx context.CLIContext, r *mux.Router, cdc *codec.Codec)
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")
r.HandleFunc(fmt.Sprintf("/gov/proposals/{%s}/deposits/{%s}", RestProposalID, RestDepositer), queryDepositHandlerFn(cdc, cliCtx)).Methods("GET")
r.HandleFunc(fmt.Sprintf("/gov/proposals/{%s}/tally", RestProposalID), queryTallyOnProposalHandlerFn(cdc, cliCtx)).Methods("GET")
r.HandleFunc(fmt.Sprintf("/gov/proposals/{%s}/votes", RestProposalID), queryVotesOnProposalHandlerFn(cdc, cliCtx)).Methods("GET")
r.HandleFunc(fmt.Sprintf("/gov/proposals/{%s}/votes/{%s}", RestProposalID, RestVoter), queryVoteHandlerFn(cdc, cliCtx)).Methods("GET")
}
Expand Down Expand Up @@ -244,9 +245,7 @@ func queryDepositsHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.Ha
return
}

params := gov.QueryDepositsParams{
ProposalID: proposalID,
}
params := gov.NewQueryProposalParams(proposalID)

bz, err := cdc.MarshalJSON(params)
if err != nil {
Expand Down Expand Up @@ -412,9 +411,8 @@ func queryVotesOnProposalHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext)
return
}

params := gov.QueryVotesParams{
ProposalID: proposalID,
}
params := gov.NewQueryProposalParams(proposalID)

bz, err := cdc.MarshalJSON(params)
if err != nil {
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
Expand Down Expand Up @@ -498,10 +496,8 @@ func queryTallyOnProposalHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext)
strProposalID := vars[RestProposalID]

if len(strProposalID) == 0 {
w.WriteHeader(http.StatusBadRequest)
err := errors.New("proposalId required but not specified")
w.Write([]byte(err.Error()))

utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}

Expand All @@ -510,20 +506,17 @@ func queryTallyOnProposalHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext)
return
}

params := gov.QueryTallyParams{
ProposalID: proposalID,
}
params := gov.NewQueryProposalParams(proposalID)

bz, err := cdc.MarshalJSON(params)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}

res, err := cliCtx.QueryWithData("custom/gov/tally", bz)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}

Expand Down
Loading

0 comments on commit d227e2a

Please sign in to comment.