Skip to content

Commit

Permalink
support debug trace QueryResult (cosmos#11148)
Browse files Browse the repository at this point in the history
Update types/errors/abci.go

Co-authored-by: Amaury <[email protected]>

Co-authored-by: Amaury <[email protected]>
  • Loading branch information
2 people authored and randy75828 committed Aug 10, 2022
1 parent a4cfb80 commit 126ee72
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 23 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ Ref: https://keepachangelog.com/en/1.0.0/

* (store) [\#11117](https://github.com/cosmos/cosmos-sdk/pull/11117) Fix data race in store trace component

### Improvements

* [\#9576](https://github.com/cosmos/cosmos-sdk/pull/9576) Add debug error message to query result when enabled

## [v0.45.1](https://github.com/cosmos/cosmos-sdk/releases/tag/v0.45.1) - 2022-02-03

### Bug Fixes
Expand Down
43 changes: 20 additions & 23 deletions baseapp/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ func (app *BaseApp) Query(req abci.RequestQuery) (res abci.ResponseQuery) {
// ref: https://github.com/cosmos/cosmos-sdk/pull/8039
defer func() {
if r := recover(); r != nil {
res = sdkerrors.QueryResult(sdkerrors.Wrapf(sdkerrors.ErrPanic, "%v", r))
res = sdkerrors.QueryResultWithDebug(sdkerrors.Wrapf(sdkerrors.ErrPanic, "%v", r), app.trace)
}
}()

Expand All @@ -436,7 +436,7 @@ func (app *BaseApp) Query(req abci.RequestQuery) (res abci.ResponseQuery) {

path := splitPath(req.Path)
if len(path) == 0 {
sdkerrors.QueryResult(sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, "no query path provided"))
sdkerrors.QueryResultWithDebug(sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, "no query path provided"), app.trace)
}

switch path[0] {
Expand All @@ -454,7 +454,7 @@ func (app *BaseApp) Query(req abci.RequestQuery) (res abci.ResponseQuery) {
return handleQueryCustom(app, path, req)
}

return sdkerrors.QueryResult(sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, "unknown query path"))
return sdkerrors.QueryResultWithDebug(sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, "unknown query path"), app.trace)
}

// ListSnapshots implements the ABCI interface. It delegates to app.snapshotManager if set.
Expand Down Expand Up @@ -584,12 +584,12 @@ func (app *BaseApp) ApplySnapshotChunk(req abci.RequestApplySnapshotChunk) abci.
func (app *BaseApp) handleQueryGRPC(handler GRPCQueryHandler, req abci.RequestQuery) abci.ResponseQuery {
ctx, err := app.createQueryContext(req.Height, req.Prove)
if err != nil {
return sdkerrors.QueryResult(err)
return sdkerrors.QueryResultWithDebug(err, app.trace)
}

res, err := handler(ctx, req)
if err != nil {
res = sdkerrors.QueryResult(gRPCErrorToSDKError(err))
res = sdkerrors.QueryResultWithDebug(gRPCErrorToSDKError(err), app.trace)
res.Height = req.Height
return res
}
Expand Down Expand Up @@ -760,7 +760,7 @@ func handleQueryApp(app *BaseApp, path []string, req abci.RequestQuery) abci.Res

gInfo, res, err := app.Simulate(txBytes)
if err != nil {
return sdkerrors.QueryResult(sdkerrors.Wrap(err, "failed to simulate tx"))
return sdkerrors.QueryResultWithDebug(sdkerrors.Wrap(err, "failed to simulate tx"), app.trace)
}

simRes := &sdk.SimulationResponse{
Expand All @@ -770,7 +770,7 @@ func handleQueryApp(app *BaseApp, path []string, req abci.RequestQuery) abci.Res

bz, err := codec.ProtoMarshalJSON(simRes, app.interfaceRegistry)
if err != nil {
return sdkerrors.QueryResult(sdkerrors.Wrap(err, "failed to JSON encode simulation response"))
return sdkerrors.QueryResultWithDebug(sdkerrors.Wrap(err, "failed to JSON encode simulation response"), app.trace)
}

return abci.ResponseQuery{
Expand All @@ -787,34 +787,32 @@ func handleQueryApp(app *BaseApp, path []string, req abci.RequestQuery) abci.Res
}

default:
return sdkerrors.QueryResult(sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "unknown query: %s", path))
return sdkerrors.QueryResultWithDebug(sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "unknown query: %s", path), app.trace)
}
}

return sdkerrors.QueryResult(
return sdkerrors.QueryResultWithDebug(
sdkerrors.Wrap(
sdkerrors.ErrUnknownRequest,
"expected second parameter to be either 'simulate' or 'version', neither was present",
),
)
), app.trace)
}

func handleQueryStore(app *BaseApp, path []string, req abci.RequestQuery) abci.ResponseQuery {
// "/store" prefix for store queries
queryable, ok := app.cms.(sdk.Queryable)
if !ok {
return sdkerrors.QueryResult(sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, "multistore doesn't support queries"))
return sdkerrors.QueryResultWithDebug(sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, "multistore doesn't support queries"), app.trace)
}

req.Path = "/" + strings.Join(path[1:], "/")

if req.Height <= 1 && req.Prove {
return sdkerrors.QueryResult(
return sdkerrors.QueryResultWithDebug(
sdkerrors.Wrap(
sdkerrors.ErrInvalidRequest,
"cannot query with proof when height <= 1; please provide a valid height",
),
)
), app.trace)
}

resp := queryable.Query(req)
Expand All @@ -826,11 +824,10 @@ func handleQueryStore(app *BaseApp, path []string, req abci.RequestQuery) abci.R
func handleQueryP2P(app *BaseApp, path []string) abci.ResponseQuery {
// "/p2p" prefix for p2p queries
if len(path) < 4 {
return sdkerrors.QueryResult(
return sdkerrors.QueryResultWithDebug(
sdkerrors.Wrap(
sdkerrors.ErrUnknownRequest, "path should be p2p filter <addr|id> <parameter>",
),
)
), app.trace)
}

var resp abci.ResponseQuery
Expand All @@ -847,7 +844,7 @@ func handleQueryP2P(app *BaseApp, path []string) abci.ResponseQuery {
}

default:
resp = sdkerrors.QueryResult(sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, "expected second parameter to be 'filter'"))
resp = sdkerrors.QueryResultWithDebug(sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, "expected second parameter to be 'filter'"), app.trace)
}

return resp
Expand All @@ -860,17 +857,17 @@ func handleQueryCustom(app *BaseApp, path []string, req abci.RequestQuery) abci.
// 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"))
return sdkerrors.QueryResultWithDebug(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]))
return sdkerrors.QueryResultWithDebug(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)
return sdkerrors.QueryResultWithDebug(err, app.trace)
}

// Passes the rest of the path as an argument to the querier.
Expand All @@ -879,7 +876,7 @@ func handleQueryCustom(app *BaseApp, path []string, req abci.RequestQuery) abci.
// []string{"proposal", "test"} as the path.
resBytes, err := querier(ctx, path[2:], req)
if err != nil {
res := sdkerrors.QueryResult(err)
res := sdkerrors.QueryResultWithDebug(err, app.trace)
res.Height = req.Height
return res
}
Expand Down
12 changes: 12 additions & 0 deletions types/errors/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,18 @@ func QueryResult(err error) abci.ResponseQuery {
}
}

// QueryResultWithDebug returns a ResponseQuery from an error. It will try to parse ABCI
// info from the error. It will use debugErrEncoder if debug parameter is true.
// Starting from v0.46, this function will be removed, and be replaced by `QueryResult`.
func QueryResultWithDebug(err error, debug bool) abci.ResponseQuery {
space, code, log := ABCIInfo(err, debug)
return abci.ResponseQuery{
Codespace: space,
Code: code,
Log: log,
}
}

// The debugErrEncoder encodes the error with a stacktrace.
func debugErrEncoder(err error) string {
return fmt.Sprintf("%+v", err)
Expand Down

0 comments on commit 126ee72

Please sign in to comment.