From 97f7b88a9fd9f4cd72e2d2c7db5593cdf0c5fde8 Mon Sep 17 00:00:00 2001 From: Sunny Aggarwal Date: Thu, 16 Aug 2018 18:35:17 -0700 Subject: [PATCH] addressed Jae's comments --- baseapp/baseapp.go | 4 ++++ baseapp/queryrouter.go | 30 ++++++++++-------------------- types/account.go | 16 ++++++++++++++-- 3 files changed, 28 insertions(+), 22 deletions(-) diff --git a/baseapp/baseapp.go b/baseapp/baseapp.go index aadea1561..828414f1e 100644 --- a/baseapp/baseapp.go +++ b/baseapp/baseapp.go @@ -370,6 +370,9 @@ func handleQueryP2P(app *BaseApp, path []string, req abci.RequestQuery) (res abc func handleQueryCustom(app *BaseApp, path []string, req abci.RequestQuery) (res abci.ResponseQuery) { // 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" + if path[1] == "" { + sdk.ErrUnknownRequest("No route for custom query specified").QueryResult() + } querier := app.queryRouter.Route(path[1]) ctx := app.checkState.ctx // Passes the rest of the path as an argument to the querier. @@ -378,6 +381,7 @@ func handleQueryCustom(app *BaseApp, path []string, req abci.RequestQuery) (res if err != nil { return abci.ResponseQuery{ Code: uint32(err.ABCICode()), + Log: err.ABCILog(), } } return abci.ResponseQuery{ diff --git a/baseapp/queryrouter.go b/baseapp/queryrouter.go index ade071f1f..23cfad072 100644 --- a/baseapp/queryrouter.go +++ b/baseapp/queryrouter.go @@ -10,14 +10,8 @@ type QueryRouter interface { Route(path string) (h sdk.Querier) } -// map a transaction type to a handler and an initgenesis function -type queryroute struct { - r string - h sdk.Querier -} - type queryrouter struct { - routes []queryroute + routes map[string]sdk.Querier } // nolint @@ -25,27 +19,23 @@ type queryrouter struct { // TODO either make Function unexported or make return type (router) Exported func NewQueryRouter() *queryrouter { return &queryrouter{ - routes: make([]queryroute, 0), + routes: map[string]sdk.Querier{}, } } -// AddRoute - TODO add description -func (rtr *queryrouter) AddRoute(r string, h sdk.Querier) QueryRouter { +// AddRoute - Adds an sdk.Querier to the route provided. Panics on duplicate +func (rtr *queryrouter) AddRoute(r string, q sdk.Querier) QueryRouter { if !isAlphaNumeric(r) { panic("route expressions can only contain alphanumeric characters") } - rtr.routes = append(rtr.routes, queryroute{r, h}) - + if rtr.routes[r] != nil { + panic("route has already been initialized") + } + rtr.routes[r] = q return rtr } -// Route - TODO add description -// TODO handle expressive matches. +// Returns the sdk.Querier for a certain route path func (rtr *queryrouter) Route(path string) (h sdk.Querier) { - for _, route := range rtr.routes { - if route.r == path { - return route.h - } - } - return nil + return rtr.routes[path] } diff --git a/types/account.go b/types/account.go index 258a095e8..9f19c2628 100644 --- a/types/account.go +++ b/types/account.go @@ -111,13 +111,19 @@ func (bz AccAddress) Format(s fmt.State, verb rune) { // Returns boolean for whether two AccAddresses are Equal func (bz AccAddress) Equals(bz2 AccAddress) bool { + if bz.Empty() && bz2.Empty() { + return true + } return (bytes.Compare(bz.Bytes(), bz2.Bytes()) == 0) } // Returns boolean for whether an AccAddress is empty func (bz AccAddress) Empty() bool { + if bz == nil { + return true + } bz2 := AccAddress{} - return bz.Equals(bz2) + return (bytes.Compare(bz.Bytes(), bz2.Bytes()) == 0) } //__________________________________________________________ @@ -206,13 +212,19 @@ func (bz ValAddress) Format(s fmt.State, verb rune) { // Returns boolean for whether two ValAddresses are Equal func (bz ValAddress) Equals(bz2 ValAddress) bool { + if bz.Empty() && bz2.Empty() { + return true + } return (bytes.Compare(bz.Bytes(), bz2.Bytes()) == 0) } // Returns boolean for whether an AccAddress is empty func (bz ValAddress) Empty() bool { + if bz == nil { + return true + } bz2 := ValAddress{} - return bz.Equals(bz2) + return (bytes.Compare(bz.Bytes(), bz2.Bytes()) == 0) } // Bech32ifyAccPub takes AccountPubKey and returns the bech32 encoded string