Address Anton's comments

This commit is contained in:
Sunny Aggarwal 2018-08-15 17:59:15 -07:00
parent 0eed9d1b60
commit 0134c3b7f1
6 changed files with 42 additions and 59 deletions

View File

@ -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
@ -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,
}
@ -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
@ -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{

View File

@ -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 }

View File

@ -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"
@ -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
}
@ -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
}
@ -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
}
@ -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
}
@ -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
}
@ -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
}
@ -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
@ -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
}
}

View File

@ -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"
@ -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
}

View File

@ -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
@ -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

View File

@ -198,8 +198,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
}