address Chris's comments

This commit is contained in:
Sunny Aggarwal 2018-08-22 00:10:11 -07:00
parent 50dd53fec3
commit 5ae20d2d51
5 changed files with 129 additions and 111 deletions

View File

@ -75,9 +75,6 @@ func (app *BaseApp) Router() Router {
return app.router
}
func (app *BaseApp) QueryRouter() QueryRouter {
if app.sealed {
panic("QueryRouter() on sealed BaseApp")
}
return app.queryRouter
}
func (app *BaseApp) Seal() { app.sealed = true }

View File

@ -114,7 +114,7 @@ func (bz AccAddress) Equals(bz2 AccAddress) bool {
if bz.Empty() && bz2.Empty() {
return true
}
return (bytes.Compare(bz.Bytes(), bz2.Bytes()) == 0)
return bytes.Compare(bz.Bytes(), bz2.Bytes()) == 0
}
// Returns boolean for whether an AccAddress is empty
@ -123,7 +123,7 @@ func (bz AccAddress) Empty() bool {
return true
}
bz2 := AccAddress{}
return (bytes.Compare(bz.Bytes(), bz2.Bytes()) == 0)
return bytes.Compare(bz.Bytes(), bz2.Bytes()) == 0
}
//__________________________________________________________
@ -215,7 +215,7 @@ func (bz ValAddress) Equals(bz2 ValAddress) bool {
if bz.Empty() && bz2.Empty() {
return true
}
return (bytes.Compare(bz.Bytes(), bz2.Bytes()) == 0)
return bytes.Compare(bz.Bytes(), bz2.Bytes()) == 0
}
// Returns boolean for whether an AccAddress is empty
@ -224,7 +224,7 @@ func (bz ValAddress) Empty() bool {
return true
}
bz2 := ValAddress{}
return (bytes.Compare(bz.Bytes(), bz2.Bytes()) == 0)
return bytes.Compare(bz.Bytes(), bz2.Bytes()) == 0
}
// Bech32ifyAccPub takes AccountPubKey and returns the bech32 encoded string

View File

@ -186,7 +186,14 @@ func queryProposalHandlerFn(cdc *wire.Codec) http.HandlerFunc {
ProposalID: proposalID,
}
res, err := cliCtx.QueryWithData("custom/gov/proposal", cdc.MustMarshalBinary(params))
bz, err := cdc.MarshalJSON(params)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
return
}
res, err := cliCtx.QueryWithData("custom/gov/proposal", bz)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
@ -194,18 +201,7 @@ func queryProposalHandlerFn(cdc *wire.Codec) http.HandlerFunc {
return
}
var proposal gov.Proposal
cdc.MustUnmarshalBinary(res, &proposal)
output, err := wire.MarshalJSONIndent(cdc, proposal)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
return
}
w.Write(output)
w.Write(res)
}
}
@ -252,7 +248,14 @@ func queryDepositHandlerFn(cdc *wire.Codec) http.HandlerFunc {
Depositer: depositerAddr,
}
res, err := cliCtx.QueryWithData("custom/gov/deposit", cdc.MustMarshalBinary(params))
bz, err := cdc.MarshalJSON(params)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
return
}
res, err := cliCtx.QueryWithData("custom/gov/deposit", bz)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
@ -261,15 +264,7 @@ func queryDepositHandlerFn(cdc *wire.Codec) http.HandlerFunc {
}
var deposit gov.Deposit
cdc.MustUnmarshalBinary(res, &deposit)
output, err := wire.MarshalJSONIndent(cdc, deposit)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
return
}
cdc.UnmarshalJSON(res, &deposit)
if deposit.Empty() {
res, err := cliCtx.QueryWithData("custom/gov/proposal", cdc.MustMarshalBinary(gov.QueryProposalParams{params.ProposalID}))
if err != nil || len(res) == 0 {
@ -283,7 +278,8 @@ func queryDepositHandlerFn(cdc *wire.Codec) http.HandlerFunc {
w.Write([]byte(err.Error()))
return
}
w.Write(output)
w.Write(res)
}
}
@ -327,8 +323,14 @@ func queryVoteHandlerFn(cdc *wire.Codec) http.HandlerFunc {
Voter: voterAddr,
ProposalID: proposalID,
}
bz, err := cdc.MarshalJSON(params)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
return
}
res, err := cliCtx.QueryWithData("custom/gov/vote", cdc.MustMarshalBinary(params))
res, err := cliCtx.QueryWithData("custom/gov/vote", bz)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
@ -337,17 +339,15 @@ func queryVoteHandlerFn(cdc *wire.Codec) http.HandlerFunc {
}
var vote gov.Vote
cdc.MustUnmarshalBinary(res, &vote)
output, err := wire.MarshalJSONIndent(cdc, vote)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
return
}
cdc.UnmarshalJSON(res, &vote)
if vote.Empty() {
res, err := cliCtx.QueryWithData("custom/gov/proposal", cdc.MustMarshalBinary(gov.QueryProposalParams{params.ProposalID}))
bz, err := cdc.MarshalJSON(gov.QueryProposalParams{params.ProposalID})
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
return
}
res, err := cliCtx.QueryWithData("custom/gov/proposal", bz)
if err != nil || len(res) == 0 {
w.WriteHeader(http.StatusNotFound)
err := errors.Errorf("proposalID [%d] does not exist", proposalID)
@ -359,7 +359,7 @@ func queryVoteHandlerFn(cdc *wire.Codec) http.HandlerFunc {
w.Write([]byte(err.Error()))
return
}
w.Write(output)
w.Write(res)
}
}
@ -388,26 +388,21 @@ func queryVotesOnProposalHandlerFn(cdc *wire.Codec) http.HandlerFunc {
params := gov.QueryVotesParams{
ProposalID: proposalID,
}
bz, err := cdc.MarshalJSON(params)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
return
}
res, err := cliCtx.QueryWithData("custom/gov/votes", cdc.MustMarshalBinary(params))
res, err := cliCtx.QueryWithData("custom/gov/votes", bz)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
return
}
var votes []gov.Vote
cdc.MustUnmarshalBinary(res, &votes)
output, err := wire.MarshalJSONIndent(cdc, votes)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
return
}
w.Write(output)
w.Write(res)
}
}
@ -420,25 +415,21 @@ func queryProposalsWithParameterFn(cdc *wire.Codec) http.HandlerFunc {
strProposalStatus := r.URL.Query().Get(RestProposalStatus)
strNumLatest := r.URL.Query().Get(RestNumLatest)
var err error
var ok bool
var voterAddr sdk.AccAddress
var depositerAddr sdk.AccAddress
var proposalStatus gov.ProposalStatus
var numLatest int64
params := gov.QueryProposalsParams{}
if len(bechVoterAddr) != 0 {
voterAddr, err = sdk.AccAddressFromBech32(bechVoterAddr)
voterAddr, err := sdk.AccAddressFromBech32(bechVoterAddr)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
err := errors.Errorf("'%s' needs to be bech32 encoded", RestVoter)
w.Write([]byte(err.Error()))
return
}
params.Voter = voterAddr
}
if len(bechDepositerAddr) != 0 {
depositerAddr, err = sdk.AccAddressFromBech32(bechDepositerAddr)
depositerAddr, err := sdk.AccAddressFromBech32(bechDepositerAddr)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
err := errors.Errorf("'%s' needs to be bech32 encoded", RestDepositer)
@ -446,10 +437,11 @@ func queryProposalsWithParameterFn(cdc *wire.Codec) http.HandlerFunc {
return
}
params.Depositer = depositerAddr
}
if len(strProposalStatus) != 0 {
proposalStatus, err = gov.ProposalStatusFromString(strProposalStatus)
proposalStatus, err := gov.ProposalStatusFromString(strProposalStatus)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
err := errors.Errorf("'%s' is not a valid Proposal Status", strProposalStatus)
@ -457,24 +449,26 @@ func queryProposalsWithParameterFn(cdc *wire.Codec) http.HandlerFunc {
return
}
params.ProposalStatus = proposalStatus
}
if len(strNumLatest) != 0 {
numLatest, ok = parseInt64OrReturnBadRequest(strNumLatest, w)
numLatest, ok := parseInt64OrReturnBadRequest(strNumLatest, w)
if !ok {
return
}
params.NumLatestProposals = numLatest
}
bz, err := cdc.MarshalJSON(params)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
return
}
cliCtx := context.NewCLIContext().WithCodec(cdc)
params := gov.QueryProposalsParams{
Depositer: depositerAddr,
Voter: voterAddr,
ProposalStatus: proposalStatus,
NumLatestProposals: numLatest,
}
res, err := cliCtx.QueryWithData("custom/gov/proposals", cdc.MustMarshalBinary(params))
res, err := cliCtx.QueryWithData("custom/gov/proposals", bz)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))
@ -482,16 +476,6 @@ func queryProposalsWithParameterFn(cdc *wire.Codec) http.HandlerFunc {
return
}
var matchingProposals []gov.Proposal
cdc.MustUnmarshalBinary(res, &matchingProposals)
output, err := wire.MarshalJSONIndent(cdc, matchingProposals)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
return
}
w.Write(output)
w.Write(res)
}
}

View File

@ -110,6 +110,7 @@ type ProposalKind byte
//nolint
const (
ProposalTypeNil ProposalKind = 0x00
ProposalTypeText ProposalKind = 0x01
ProposalTypeParameterChange ProposalKind = 0x02
ProposalTypeSoftwareUpgrade ProposalKind = 0x03
@ -203,6 +204,7 @@ type ProposalStatus byte
//nolint
const (
StatusNil ProposalStatus = 0x00
StatusDepositPeriod ProposalStatus = 0x01
StatusVotingPeriod ProposalStatus = 0x02
StatusPassed ProposalStatus = 0x03
@ -220,6 +222,8 @@ func ProposalStatusFromString(str string) (ProposalStatus, error) {
return StatusPassed, nil
case "Rejected":
return StatusRejected, nil
case "":
return StatusNil, nil
default:
return ProposalStatus(0xff), errors.Errorf("'%s' is not a valid proposal status", str)
}

View File

@ -1,7 +1,10 @@
package gov
import (
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/wire"
abci "github.com/tendermint/tendermint/abci/types"
)
@ -35,16 +38,21 @@ type QueryProposalParams struct {
func queryProposal(ctx sdk.Context, path []string, req abci.RequestQuery, keeper Keeper) (res []byte, err sdk.Error) {
var params QueryProposalParams
err2 := keeper.cdc.UnmarshalBinary(req.Data, &params)
err2 := keeper.cdc.UnmarshalJSON(req.Data, &params)
if err2 != nil {
return []byte{}, sdk.ErrUnknownRequest("incorrectly formatted request data")
return []byte{}, sdk.ErrUnknownRequest(fmt.Sprintf("incorrectly formatted request data - %s", err2.Error()))
}
proposal := keeper.GetProposal(ctx, params.ProposalID)
if proposal == nil {
return []byte{}, ErrUnknownProposal(DefaultCodespace, params.ProposalID)
}
return keeper.cdc.MustMarshalBinary(proposal), nil
bz, err2 := wire.MarshalJSONIndent(keeper.cdc, proposal)
if err2 != nil {
panic("could not marshal result to JSON")
}
return bz, nil
}
// Params for query 'custom/gov/deposit'
@ -55,13 +63,17 @@ type QueryDepositParams struct {
func queryDeposit(ctx sdk.Context, path []string, req abci.RequestQuery, keeper Keeper) (res []byte, err sdk.Error) {
var params QueryDepositParams
err2 := keeper.cdc.UnmarshalBinary(req.Data, &params)
err2 := keeper.cdc.UnmarshalJSON(req.Data, &params)
if err2 != nil {
return []byte{}, sdk.ErrUnknownRequest("incorrectly formatted request data")
return []byte{}, sdk.ErrUnknownRequest(fmt.Sprintf("incorrectly formatted request data - %s", err2.Error()))
}
deposit, _ := keeper.GetDeposit(ctx, params.ProposalID, params.Depositer)
return keeper.cdc.MustMarshalBinary(deposit), nil
bz, err2 := wire.MarshalJSONIndent(keeper.cdc, deposit)
if err2 != nil {
panic("could not marshal result to JSON")
}
return bz, nil
}
// Params for query 'custom/gov/vote'
@ -72,13 +84,17 @@ type QueryVoteParams struct {
func queryVote(ctx sdk.Context, path []string, req abci.RequestQuery, keeper Keeper) (res []byte, err sdk.Error) {
var params QueryVoteParams
err2 := keeper.cdc.UnmarshalBinary(req.Data, &params)
err2 := keeper.cdc.UnmarshalJSON(req.Data, &params)
if err2 != nil {
return []byte{}, sdk.ErrUnknownRequest("incorrectly formatted request data")
return []byte{}, sdk.ErrUnknownRequest(fmt.Sprintf("incorrectly formatted request data - %s", err2.Error()))
}
vote, _ := keeper.GetVote(ctx, params.ProposalID, params.Voter)
return keeper.cdc.MustMarshalBinary(vote), nil
bz, err2 := wire.MarshalJSONIndent(keeper.cdc, vote)
if err2 != nil {
panic("could not marshal result to JSON")
}
return bz, nil
}
// Params for query 'custom/gov/deposits'
@ -88,9 +104,9 @@ type QueryDepositsParams struct {
func queryDeposits(ctx sdk.Context, path []string, req abci.RequestQuery, keeper Keeper) (res []byte, err sdk.Error) {
var params QueryDepositParams
err2 := keeper.cdc.UnmarshalBinary(req.Data, &params)
err2 := keeper.cdc.UnmarshalJSON(req.Data, &params)
if err2 != nil {
return []byte{}, sdk.ErrUnknownRequest("incorrectly formatted request data")
return []byte{}, sdk.ErrUnknownRequest(fmt.Sprintf("incorrectly formatted request data - %s", err2.Error()))
}
var deposits []Deposit
@ -101,7 +117,11 @@ func queryDeposits(ctx sdk.Context, path []string, req abci.RequestQuery, keeper
deposits = append(deposits, deposit)
}
return keeper.cdc.MustMarshalBinary(deposits), nil
bz, err2 := wire.MarshalJSONIndent(keeper.cdc, deposits)
if err2 != nil {
panic("could not marshal result to JSON")
}
return bz, nil
}
// Params for query 'custom/gov/votes'
@ -111,9 +131,10 @@ type QueryVotesParams struct {
func queryVotes(ctx sdk.Context, path []string, req abci.RequestQuery, keeper Keeper) (res []byte, err sdk.Error) {
var params QueryVotesParams
err2 := keeper.cdc.UnmarshalBinary(req.Data, &params)
err2 := keeper.cdc.UnmarshalJSON(req.Data, &params)
if err2 != nil {
return []byte{}, sdk.ErrUnknownRequest("incorrectly formatted request data")
return []byte{}, sdk.ErrUnknownRequest(fmt.Sprintf("incorrectly formatted request data - %s", err2.Error()))
}
var votes []Vote
@ -124,7 +145,11 @@ func queryVotes(ctx sdk.Context, path []string, req abci.RequestQuery, keeper Ke
votes = append(votes, vote)
}
return keeper.cdc.MustMarshalBinary(votes), nil
bz, err2 := wire.MarshalJSONIndent(keeper.cdc, votes)
if err2 != nil {
panic("could not marshal result to JSON")
}
return bz, nil
}
// Params for query 'custom/gov/proposals'
@ -137,14 +162,17 @@ type QueryProposalsParams struct {
func queryProposals(ctx sdk.Context, path []string, req abci.RequestQuery, keeper Keeper) (res []byte, err sdk.Error) {
var params QueryProposalsParams
err2 := keeper.cdc.UnmarshalBinary(req.Data, &params)
err2 := keeper.cdc.UnmarshalJSON(req.Data, &params)
if err2 != nil {
return []byte{}, sdk.ErrUnknownRequest("incorrectly formatted request data")
return []byte{}, sdk.ErrUnknownRequest(fmt.Sprintf("incorrectly formatted request data - %s", err2.Error()))
}
proposals := keeper.GetProposalsFiltered(ctx, params.Voter, params.Depositer, params.ProposalStatus, params.NumLatestProposals)
bz := keeper.cdc.MustMarshalBinary(proposals)
bz, err2 := wire.MarshalJSONIndent(keeper.cdc, proposals)
if err2 != nil {
panic("could not marshal result to JSON")
}
return bz, nil
}
@ -157,9 +185,9 @@ func queryTally(ctx sdk.Context, path []string, req abci.RequestQuery, keeper Ke
// TODO: Dependant on #1914
var proposalID int64
err2 := keeper.cdc.UnmarshalBinary(req.Data, proposalID)
err2 := keeper.cdc.UnmarshalJSON(req.Data, proposalID)
if err2 != nil {
return []byte{}, sdk.ErrUnknownRequest("incorrectly formatted request data")
return []byte{}, sdk.ErrUnknownRequest(fmt.Sprintf("incorrectly formatted request data - %s", err2.Error()))
}
proposal := keeper.GetProposal(ctx, proposalID)
@ -167,14 +195,19 @@ func queryTally(ctx sdk.Context, path []string, req abci.RequestQuery, keeper Ke
return []byte{}, ErrUnknownProposal(DefaultCodespace, proposalID)
}
var tallyResult TallyResult
if proposal.GetStatus() == StatusDepositPeriod {
return keeper.cdc.MustMarshalBinaryBare(EmptyTallyResult()), nil
tallyResult = EmptyTallyResult()
} else if proposal.GetStatus() == StatusPassed || proposal.GetStatus() == StatusRejected {
tallyResult = proposal.GetTallyResult()
} else {
_, tallyResult, _ = tally(ctx, keeper, proposal)
}
if proposal.GetStatus() == StatusPassed || proposal.GetStatus() == StatusRejected {
return keeper.cdc.MustMarshalBinaryBare(proposal.GetTallyResult()), nil
bz, err2 := wire.MarshalJSONIndent(keeper.cdc, tallyResult)
if err2 != nil {
panic("could not marshal result to JSON")
}
_, tallyResult, _ := tally(ctx, keeper, proposal)
return keeper.cdc.MustMarshalBinaryBare(tallyResult), nil
return bz, nil
}