Skip to content

Commit

Permalink
Address Anton's comments
Browse files Browse the repository at this point in the history
  • Loading branch information
sunnya97 committed Aug 16, 2018
1 parent 7eb9774 commit 31cd4af
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 60 deletions.
2 changes: 1 addition & 1 deletion Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 8 additions & 4 deletions baseapp/baseapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ type BaseApp struct {
db dbm.DB // common DB backend
cms sdk.CommitMultiStore // Main (uncached) state
router Router // handle any kind of message
queryrouter QueryRouter // router for redirecting query calls
queryRouter QueryRouter // router for redirecting query calls
codespacer *sdk.Codespacer // handle module codespacing
txDecoder sdk.TxDecoder // unmarshal []byte into sdk.Tx

Expand Down Expand Up @@ -90,7 +90,7 @@ func NewBaseApp(name string, logger log.Logger, db dbm.DB, txDecoder sdk.TxDecod
db: db,
cms: store.NewCommitMultiStore(db),
router: NewRouter(),
queryrouter: NewQueryRouter(),
queryRouter: NewQueryRouter(),
codespacer: sdk.NewCodespacer(),
txDecoder: txDecoder,
}
Expand Down Expand Up @@ -268,6 +268,7 @@ func (app *BaseApp) FilterPeerByPubKey(info string) abci.ResponseQuery {
return abci.ResponseQuery{}
}

// Splits a string path using the delimter '/'. i.e. "this/is/funny" becomes []string{"this", "is", "funny"}
func splitPath(requestPath string) (path []string) {
path = strings.Split(requestPath, "/")
// first element is empty string
Expand Down Expand Up @@ -367,9 +368,12 @@ func handleQueryP2P(app *BaseApp, path []string, req abci.RequestQuery) (res abc
}

func handleQueryCustom(app *BaseApp, path []string, req abci.RequestQuery) (res abci.ResponseQuery) {
// "/custom" prefix for keeper queries
querier := app.queryrouter.Route(path[1])
// path[0] should be "custom" because "/custom" prefix is required for keeper queries.
// the queryRouter routes using path[1]. For example, in the path "custom/gov/proposal", queryRouter routes using "gov"
querier := app.queryRouter.Route(path[1])
ctx := app.checkState.ctx
// Passes the rest of the path as an argument to the querier.
// For example, in the path "custom/gov/proposal/test", the gov querier gets []string{"proposal", "test"} as the path
resBytes, err := querier(ctx, path[2:], req)
if err != nil {
return abci.ResponseQuery{
Expand Down
2 changes: 1 addition & 1 deletion baseapp/setters.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (app *BaseApp) QueryRouter() QueryRouter {
if app.sealed {
panic("QueryRouter() on sealed BaseApp")
}
return app.queryrouter
return app.queryRouter
}
func (app *BaseApp) Seal() { app.sealed = true }
func (app *BaseApp) IsSealed() bool { return app.sealed }
Expand Down
57 changes: 17 additions & 40 deletions x/gov/client/rest/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package rest
import (
"fmt"
"net/http"
"strconv"

"github.com/cosmos/cosmos-sdk/client/context"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -98,16 +97,13 @@ func depositHandlerFn(cdc *wire.Codec, cliCtx context.CLIContext) http.HandlerFu
return
}

proposalID, err := strconv.ParseInt(strProposalID, 10, 64)
if err != nil {
err := errors.Errorf("proposalID [%d] is not positive", proposalID)
w.Write([]byte(err.Error()))

proposalID, ok := parseInt64OrReturnBadRequest(strProposalID, w)
if !ok {
return
}

var req depositReq
err = buildReq(w, r, cdc, &req)
err := buildReq(w, r, cdc, &req)
if err != nil {
return
}
Expand Down Expand Up @@ -140,15 +136,13 @@ func voteHandlerFn(cdc *wire.Codec, cliCtx context.CLIContext) http.HandlerFunc
return
}

proposalID, err := strconv.ParseInt(strProposalID, 10, 64)
if err != nil {
err := errors.Errorf("proposalID [%d] is not positive", proposalID)
w.Write([]byte(err.Error()))
proposalID, ok := parseInt64OrReturnBadRequest(strProposalID, w)
if !ok {
return
}

var req voteReq
err = buildReq(w, r, cdc, &req)
err := buildReq(w, r, cdc, &req)
if err != nil {
return
}
Expand Down Expand Up @@ -181,11 +175,8 @@ func queryProposalHandlerFn(cdc *wire.Codec) http.HandlerFunc {
return
}

proposalID, err := strconv.ParseInt(strProposalID, 10, 64)
if err != nil {
err := errors.Errorf("proposalID [%d] is not positive", proposalID)
w.Write([]byte(err.Error()))

proposalID, ok := parseInt64OrReturnBadRequest(strProposalID, w)
if !ok {
return
}

Expand Down Expand Up @@ -232,12 +223,8 @@ func queryDepositHandlerFn(cdc *wire.Codec) http.HandlerFunc {
return
}

proposalID, err := strconv.ParseInt(strProposalID, 10, 64)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
err := errors.Errorf("proposalID [%d] is not positive", proposalID)
w.Write([]byte(err.Error()))

proposalID, ok := parseInt64OrReturnBadRequest(strProposalID, w)
if !ok {
return
}

Expand Down Expand Up @@ -313,12 +300,8 @@ func queryVoteHandlerFn(cdc *wire.Codec) http.HandlerFunc {
return
}

proposalID, err := strconv.ParseInt(strProposalID, 10, 64)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
err := errors.Errorf("proposalID [%s] is not positive", proposalID)
w.Write([]byte(err.Error()))

proposalID, ok := parseInt64OrReturnBadRequest(strProposalID, w)
if !ok {
return
}

Expand Down Expand Up @@ -395,12 +378,8 @@ func queryVotesOnProposalHandlerFn(cdc *wire.Codec) http.HandlerFunc {
return
}

proposalID, err := strconv.ParseInt(strProposalID, 10, 64)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
err := errors.Errorf("proposalID [%s] is not positive", proposalID)
w.Write([]byte(err.Error()))

proposalID, ok := parseInt64OrReturnBadRequest(strProposalID, w)
if !ok {
return
}

Expand Down Expand Up @@ -442,6 +421,7 @@ func queryProposalsWithParameterFn(cdc *wire.Codec) http.HandlerFunc {
strNumLatest := r.URL.Query().Get(RestNumLatest)

var err error
var ok bool
var voterAddr sdk.AccAddress
var depositerAddr sdk.AccAddress
var proposalStatus gov.ProposalStatus
Expand Down Expand Up @@ -479,11 +459,8 @@ func queryProposalsWithParameterFn(cdc *wire.Codec) http.HandlerFunc {
}
}
if len(strNumLatest) != 0 {
numLatest, err = strconv.ParseInt(strNumLatest, 10, 64)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
err := errors.Errorf("'%s' is not a valid int64", strNumLatest)
w.Write([]byte(err.Error()))
numLatest, ok = parseInt64OrReturnBadRequest(strNumLatest, w)
if !ok {
return
}
}
Expand Down
14 changes: 14 additions & 0 deletions x/gov/client/rest/util.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package rest

import (
"fmt"
"io/ioutil"
"net/http"
"strconv"

"github.com/cosmos/cosmos-sdk/client/context"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -100,3 +102,15 @@ func signAndBuild(w http.ResponseWriter, cliCtx context.CLIContext, baseReq base

w.Write(output)
}

func parseInt64OrReturnBadRequest(s string, w http.ResponseWriter) (n int64, ok bool) {
var err error
n, err = strconv.ParseInt(s, 10, 64)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
err := fmt.Errorf("'%s' is not a valid int64", s)
w.Write([]byte(err.Error()))
return 0, false
}
return n, true
}
14 changes: 2 additions & 12 deletions x/gov/depositsvotes.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,7 @@ type Vote struct {

// Returns whether 2 votes are equal
func (voteA Vote) Equals(voteB Vote) bool {
if voteA.Voter.Equals(voteB.Voter) &&
voteA.ProposalID == voteB.ProposalID &&
voteA.Option == voteB.Option {
return true
}
return false
return voteA.Voter.Equals(voteB.Voter) && voteA.ProposalID == voteB.ProposalID && voteA.Option == voteB.Option
}

// Returns whether a vote is empty
Expand All @@ -40,12 +35,7 @@ type Deposit struct {

// Returns whether 2 deposits are equal
func (depositA Deposit) Equals(depositB Deposit) bool {
if depositA.Depositer.Equals(depositB.Depositer) &&
depositA.ProposalID == depositB.ProposalID &&
depositA.Amount.IsEqual(depositB.Amount) {
return true
}
return false
return depositA.Depositer.Equals(depositB.Depositer) && depositA.ProposalID == depositB.ProposalID && depositA.Amount.IsEqual(depositB.Amount)
}

// Returns whether a deposit is empty
Expand Down
2 changes: 0 additions & 2 deletions x/gov/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,6 @@ func (keeper Keeper) peekCurrentProposalID(ctx sdk.Context) (proposalID int64, e
return -1, ErrInvalidGenesis(keeper.codespace, "InitialProposalID never set")
}
keeper.cdc.MustUnmarshalBinary(bz, &proposalID)
bz = keeper.cdc.MustMarshalBinary(proposalID + 1)
store.Set(KeyNextProposalID, bz)
return proposalID, nil
}

Expand Down

0 comments on commit 31cd4af

Please sign in to comment.