Skip to content

Commit

Permalink
fix lcd
Browse files Browse the repository at this point in the history
  • Loading branch information
mossid committed Oct 26, 2018
1 parent bc55027 commit 6559017
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 20 deletions.
49 changes: 29 additions & 20 deletions client/lcd/lcd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -637,7 +637,7 @@ func TestSubmitProposal(t *testing.T) {

// query proposal
proposal := getProposal(t, port, proposalID)
require.Equal(t, "Test", proposal.GetTitle())
require.Equal(t, "Test", proposal.GetProposalAbstract().Title)
}

func TestDeposit(t *testing.T) {
Expand All @@ -659,15 +659,15 @@ func TestDeposit(t *testing.T) {

// query proposal
proposal := getProposal(t, port, proposalID)
require.Equal(t, "Test", proposal.GetTitle())
require.Equal(t, "Test", proposal.GetProposalAbstract().Title)

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

// query proposal
proposal = getProposal(t, port, proposalID)
require.True(t, proposal.GetTotalDeposit().IsEqual(sdk.Coins{sdk.NewInt64Coin("steak", 10)}))
info := getProposalInfo(t, port, proposalID)
require.True(t, info.TotalDeposit.IsEqual(sdk.Coins{sdk.NewInt64Coin("steak", 10)}))

// query deposit
deposit := getDeposit(t, port, proposalID, addr)
Expand All @@ -693,15 +693,15 @@ func TestVote(t *testing.T) {

// query proposal
proposal := getProposal(t, port, proposalID)
require.Equal(t, "Test", proposal.GetTitle())
require.Equal(t, "Test", proposal.GetProposalAbstract().Title)

// create SubmitProposal TX
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())
info := getProposalInfo(t, port, proposalID)
require.Equal(t, gov.StatusVotingPeriod, info.Status)

// create SubmitProposal TX
resultTx = doVote(t, port, seed, name, password, addr, proposalID)
Expand Down Expand Up @@ -784,12 +784,12 @@ func TestProposalsQuery(t *testing.T) {
// Only proposals #1 should be in Deposit Period
proposals := getProposalsFilterStatus(t, port, gov.StatusDepositPeriod)
require.Len(t, proposals, 1)
require.Equal(t, proposalID1, proposals[0].GetProposalID())
require.Equal(t, proposalID1, proposals[0].GetProposalAbstract().ProposalID)
// Only proposals #2 and #3 should be in Voting Period
proposals = getProposalsFilterStatus(t, port, gov.StatusVotingPeriod)
require.Len(t, proposals, 2)
require.Equal(t, proposalID2, proposals[0].GetProposalID())
require.Equal(t, proposalID3, proposals[1].GetProposalID())
require.Equal(t, proposalID2, proposals[0].GetProposalAbstract().ProposalID)
require.Equal(t, proposalID3, proposals[1].GetProposalAbstract().ProposalID)

// Addr1 votes on proposals #2 & #3
resultTx = doVote(t, port, seeds[0], names[0], passwords[0], addrs[0], proposalID2)
Expand All @@ -803,31 +803,31 @@ func TestProposalsQuery(t *testing.T) {

// Test query all proposals
proposals = getProposalsAll(t, port)
require.Equal(t, proposalID1, (proposals[0]).GetProposalID())
require.Equal(t, proposalID2, (proposals[1]).GetProposalID())
require.Equal(t, proposalID3, (proposals[2]).GetProposalID())
require.Equal(t, proposalID1, (proposals[0]).GetProposalAbstract().ProposalID)
require.Equal(t, proposalID2, (proposals[1]).GetProposalAbstract().ProposalID)
require.Equal(t, proposalID3, (proposals[2]).GetProposalAbstract().ProposalID)

// Test query deposited by addr1
proposals = getProposalsFilterDepositer(t, port, addrs[0])
require.Equal(t, proposalID1, (proposals[0]).GetProposalID())
require.Equal(t, proposalID1, (proposals[0]).GetProposalAbstract().ProposalID)

// Test query deposited by addr2
proposals = getProposalsFilterDepositer(t, port, addrs[1])
require.Equal(t, proposalID2, (proposals[0]).GetProposalID())
require.Equal(t, proposalID3, (proposals[1]).GetProposalID())
require.Equal(t, proposalID2, (proposals[0]).GetProposalAbstract().ProposalID)
require.Equal(t, proposalID3, (proposals[1]).GetProposalAbstract().ProposalID)

// Test query voted by addr1
proposals = getProposalsFilterVoter(t, port, addrs[0])
require.Equal(t, proposalID2, (proposals[0]).GetProposalID())
require.Equal(t, proposalID3, (proposals[1]).GetProposalID())
require.Equal(t, proposalID2, (proposals[0]).GetProposalAbstract().ProposalID)
require.Equal(t, proposalID3, (proposals[1]).GetProposalAbstract().ProposalID)

// Test query voted by addr2
proposals = getProposalsFilterVoter(t, port, addrs[1])
require.Equal(t, proposalID3, (proposals[0]).GetProposalID())
require.Equal(t, proposalID3, (proposals[0]).GetProposalAbstract().ProposalID)

// Test query voted and deposited by addr1
proposals = getProposalsFilterVoterDepositer(t, port, addrs[0], addrs[0])
require.Equal(t, proposalID2, (proposals[0]).GetProposalID())
require.Equal(t, proposalID2, (proposals[0]).GetProposalAbstract().ProposalID)

// Test query votes on Proposal 2
votes := getVotes(t, port, proposalID2)
Expand Down Expand Up @@ -1219,6 +1219,15 @@ func getProposal(t *testing.T, port string, proposalID int64) gov.Proposal {
return proposal
}

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

func getDeposits(t *testing.T, port string, proposalID int64) []gov.Deposit {
res, body := Request(t, port, "GET", fmt.Sprintf("/gov/proposals/%d/deposits", proposalID), nil)
require.Equal(t, http.StatusOK, res.StatusCode, body)
Expand Down
36 changes: 36 additions & 0 deletions x/gov/client/rest/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func RegisterRoutes(cliCtx context.CLIContext, r *mux.Router, cdc *codec.Codec)

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}/info", RestProposalID), queryInfoHandlerFn(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}/votes", RestProposalID), queryVotesOnProposalHandlerFn(cdc, cliCtx)).Methods("GET")
Expand Down Expand Up @@ -199,6 +200,41 @@ func queryProposalHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.Ha
}
}

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

if len(strProposalID) == 0 {
err := errors.New("proposalId required but not specified")
utils.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
return
}

proposalID, ok := utils.ParseInt64OrReturnBadRequest(w, strProposalID)
if !ok {
return
}

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

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

res, err := cliCtx.QueryWithData("custom/gov/info", bz)
if err != nil {
utils.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}

utils.PostProcessResponse(w, cdc, res, cliCtx.Indent)
}
}
func queryDepositsHandlerFn(cdc *codec.Codec, cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
Expand Down
29 changes: 29 additions & 0 deletions x/gov/queryable.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
const (
QueryProposals = "proposals"
QueryProposal = "proposal"
QueryInfo = "info"
QueryDeposits = "deposits"
QueryDeposit = "deposit"
QueryVotes = "votes"
Expand All @@ -24,6 +25,8 @@ func NewQuerier(keeper Keeper) sdk.Querier {
return queryProposals(ctx, path[1:], req, keeper)
case QueryProposal:
return queryProposal(ctx, path[1:], req, keeper)
case QueryInfo:
return queryInfo(ctx, path[1:], req, keeper)
case QueryDeposits:
return queryDeposits(ctx, path[1:], req, keeper)
case QueryDeposit:
Expand Down Expand Up @@ -65,6 +68,32 @@ func queryProposal(ctx sdk.Context, path []string, req abci.RequestQuery, keeper
return bz, nil
}

// Params for query 'custom/gov/info'
type QueryInfoParams struct {
ProposalID int64
}

// nolint: unparam
func queryInfo(ctx sdk.Context, path []string, req abci.RequestQuery, keeper Keeper) (res []byte, err sdk.Error) {
var params QueryInfoParams
err2 := keeper.cdc.UnmarshalJSON(req.Data, &params)
if err2 != nil {
return nil, sdk.ErrUnknownRequest(sdk.AppendMsgToErr("incorrectly formatted request data", err2.Error()))
}

info := keeper.GetProposalInfo(ctx, params.ProposalID)
if info.IsEmpty() {
return nil, ErrUnknownProposal(DefaultCodespace, params.ProposalID)
}

bz, err2 := codec.MarshalJSONIndent(keeper.cdc, info)
if err2 != nil {
return nil, sdk.ErrInternal(sdk.AppendMsgToErr("could not marshal result to JSON", err2.Error()))
}
return bz, nil

}

// Params for query 'custom/gov/deposit'
type QueryDepositParams struct {
ProposalID int64
Expand Down

0 comments on commit 6559017

Please sign in to comment.