chore: remove query router (#13142)

This commit is contained in:
Marko 2022-09-02 21:52:35 +02:00 committed by GitHub
parent 6a4d2a16a5
commit ccdc355eea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 0 additions and 119 deletions

View File

@ -420,9 +420,6 @@ func (app *BaseApp) Query(req abci.RequestQuery) (res abci.ResponseQuery) {
case QueryPathP2P:
return handleQueryP2P(app, path)
case QueryPathCustom:
return handleQueryCustom(app, path, req)
}
return sdkerrors.QueryResult(sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, "unknown query path"), app.trace)
@ -816,43 +813,6 @@ func handleQueryP2P(app *BaseApp, path []string) abci.ResponseQuery {
return resp
}
func handleQueryCustom(app *BaseApp, path []string, req abci.RequestQuery) 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 len(path) < 2 || path[1] == "" {
return sdkerrors.QueryResult(sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, "no route for custom query specified"), app.trace)
}
querier := app.queryRouter.Route(path[1])
if querier == nil {
return sdkerrors.QueryResult(sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "no custom querier found for route %s", path[1]), app.trace)
}
ctx, err := app.createQueryContext(req.Height, req.Prove)
if err != nil {
return sdkerrors.QueryResult(err, app.trace)
}
// 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 {
res := sdkerrors.QueryResult(err, app.trace)
res.Height = req.Height
return res
}
return abci.ResponseQuery{
Height: req.Height,
Value: resBytes,
}
}
// SplitABCIQueryPath splits a string path using the delimiter '/'.
//
// e.g. "this/is/funny" becomes []string{"this", "is", "funny"}

View File

@ -48,7 +48,6 @@ type BaseApp struct { //nolint: maligned
db dbm.DB // common DB backend
cms sdk.CommitMultiStore // Main (uncached) state
storeLoader StoreLoader // function to handle store loading, may be overridden with SetStoreLoader()
queryRouter sdk.QueryRouter // router for redirecting query calls
grpcQueryRouter *GRPCQueryRouter // router for redirecting gRPC query calls
msgServiceRouter *MsgServiceRouter // router for redirecting Msg service messages
interfaceRegistry codectypes.InterfaceRegistry
@ -147,7 +146,6 @@ func NewBaseApp(
db: db,
cms: store.NewCommitMultiStore(db),
storeLoader: DefaultStoreLoader,
queryRouter: NewQueryRouter(),
grpcQueryRouter: NewGRPCQueryRouter(),
msgServiceRouter: NewMsgServiceRouter(),
txDecoder: txDecoder,
@ -364,9 +362,6 @@ func (app *BaseApp) setIndexEvents(ie []string) {
}
}
// QueryRouter returns the QueryRouter of a BaseApp.
func (app *BaseApp) QueryRouter() sdk.QueryRouter { return app.queryRouter }
// Seal seals a BaseApp. It prohibits any further modifications to a BaseApp.
func (app *BaseApp) Seal() { app.sealed = true }

View File

@ -1,41 +0,0 @@
package baseapp
import (
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
)
type QueryRouter struct {
routes map[string]sdk.Querier
}
var _ sdk.QueryRouter = NewQueryRouter()
// NewQueryRouter returns a reference to a new QueryRouter.
func NewQueryRouter() *QueryRouter {
return &QueryRouter{
routes: map[string]sdk.Querier{},
}
}
// AddRoute adds a query path to the router with a given Querier. It will panic
// if a duplicate route is given. The route must be alphanumeric.
func (qrt *QueryRouter) AddRoute(path string, q sdk.Querier) sdk.QueryRouter {
if !sdk.IsAlphaNumeric(path) {
panic("route expressions can only contain alphanumeric characters")
}
if qrt.routes[path] != nil {
panic(fmt.Sprintf("route %s has already been initialized", path))
}
qrt.routes[path] = q
return qrt
}
// Route returns the Querier for a given query route path.
func (qrt *QueryRouter) Route(path string) sdk.Querier {
return qrt.routes[path]
}

View File

@ -1,33 +0,0 @@
package baseapp
import (
"testing"
"github.com/stretchr/testify/require"
abci "github.com/tendermint/tendermint/abci/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)
var testQuerier = func(_ sdk.Context, _ []string, _ abci.RequestQuery) ([]byte, error) {
return nil, nil
}
func TestQueryRouter(t *testing.T) {
qr := NewQueryRouter()
// require panic on invalid route
require.Panics(t, func() {
qr.AddRoute("*", testQuerier)
})
qr.AddRoute("testRoute", testQuerier)
q := qr.Route("testRoute")
require.NotNil(t, q)
// require panic on duplicate route
require.Panics(t, func() {
qr.AddRoute("testRoute", testQuerier)
})
}