Skip to content

Commit

Permalink
address Chris's comments
Browse files Browse the repository at this point in the history
  • Loading branch information
sunnya97 committed Aug 22, 2018
1 parent 50dd53f commit 5ae20d2
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 99 deletions.
3 changes: 0 additions & 3 deletions baseapp/setters.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
8 changes: 4 additions & 4 deletions types/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}

//__________________________________________________________
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
116 changes: 50 additions & 66 deletions x/gov/client/rest/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,26 +186,22 @@ 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.StatusInternalServerError)
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))

return
}

var proposal gov.Proposal
cdc.MustUnmarshalBinary(res, &proposal)

output, err := wire.MarshalJSONIndent(cdc, proposal)
res, err := cliCtx.QueryWithData("custom/gov/proposal", bz)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))

return
}

w.Write(output)
w.Write(res)
}
}

Expand Down Expand Up @@ -252,24 +248,23 @@ 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.StatusInternalServerError)
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))

return
}

var deposit gov.Deposit
cdc.MustUnmarshalBinary(res, &deposit)

output, err := wire.MarshalJSONIndent(cdc, deposit)
res, err := cliCtx.QueryWithData("custom/gov/deposit", bz)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))

return
}

var deposit gov.Deposit
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 {
Expand All @@ -283,7 +278,8 @@ func queryDepositHandlerFn(cdc *wire.Codec) http.HandlerFunc {
w.Write([]byte(err.Error()))
return
}
w.Write(output)

w.Write(res)
}
}

Expand Down Expand Up @@ -327,27 +323,31 @@ func queryVoteHandlerFn(cdc *wire.Codec) http.HandlerFunc {
Voter: voterAddr,
ProposalID: proposalID,
}

res, err := cliCtx.QueryWithData("custom/gov/vote", cdc.MustMarshalBinary(params))
bz, err := cdc.MarshalJSON(params)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))

return
}

var vote gov.Vote
cdc.MustUnmarshalBinary(res, &vote)

output, err := wire.MarshalJSONIndent(cdc, vote)
res, err := cliCtx.QueryWithData("custom/gov/vote", bz)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))

return
}

var vote gov.Vote
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)
Expand All @@ -359,7 +359,7 @@ func queryVoteHandlerFn(cdc *wire.Codec) http.HandlerFunc {
w.Write([]byte(err.Error()))
return
}
w.Write(output)
w.Write(res)
}
}

Expand Down Expand Up @@ -388,26 +388,21 @@ func queryVotesOnProposalHandlerFn(cdc *wire.Codec) http.HandlerFunc {
params := gov.QueryVotesParams{
ProposalID: proposalID,
}

res, err := cliCtx.QueryWithData("custom/gov/votes", cdc.MustMarshalBinary(params))
bz, err := cdc.MarshalJSON(params)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
return
}

var votes []gov.Vote
cdc.MustUnmarshalBinary(res, &votes)

output, err := wire.MarshalJSONIndent(cdc, votes)
res, err := cliCtx.QueryWithData("custom/gov/votes", bz)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte(err.Error()))

return
}

w.Write(output)
w.Write(res)
}
}

Expand All @@ -420,78 +415,67 @@ 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)
w.Write([]byte(err.Error()))

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)
w.Write([]byte(err.Error()))

return
}
params.ProposalStatus = proposalStatus
}
if len(strNumLatest) != 0 {
numLatest, ok = parseInt64OrReturnBadRequest(strNumLatest, w)
numLatest, ok := parseInt64OrReturnBadRequest(strNumLatest, w)
if !ok {
return
}
params.NumLatestProposals = numLatest
}

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))
bz, err := cdc.MarshalJSON(params)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))

return
}

var matchingProposals []gov.Proposal
cdc.MustUnmarshalBinary(res, &matchingProposals)
output, err := wire.MarshalJSONIndent(cdc, matchingProposals)
cliCtx := context.NewCLIContext().WithCodec(cdc)

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

return
}

w.Write(output)
w.Write(res)
}
}
4 changes: 4 additions & 0 deletions x/gov/proposals.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ type ProposalKind byte

//nolint
const (
ProposalTypeNil ProposalKind = 0x00
ProposalTypeText ProposalKind = 0x01
ProposalTypeParameterChange ProposalKind = 0x02
ProposalTypeSoftwareUpgrade ProposalKind = 0x03
Expand Down Expand Up @@ -203,6 +204,7 @@ type ProposalStatus byte

//nolint
const (
StatusNil ProposalStatus = 0x00
StatusDepositPeriod ProposalStatus = 0x01
StatusVotingPeriod ProposalStatus = 0x02
StatusPassed ProposalStatus = 0x03
Expand All @@ -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)
}
Expand Down
Loading

0 comments on commit 5ae20d2

Please sign in to comment.