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 <1293565+amaurym@users.noreply.github.com>

Co-authored-by: Amaury <1293565+amaurym@users.noreply.github.com>
  • Loading branch information
2 people authored and JeancarloBarrios committed Sep 28, 2024
1 parent a7aeae2 commit f8778c3
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 54 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,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
89 changes: 35 additions & 54 deletions baseapp/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ func (app *BaseApp) Query(_ context.Context, req *abci.QueryRequest) (resp *abci
// Ref: https://github.com/cosmos/cosmos-sdk/pull/8039
defer func() {
if r := recover(); r != nil {
resp = queryResult(errorsmod.Wrapf(sdkerrors.ErrPanic, "%v", r), app.trace)
res = sdkerrors.QueryResultWithDebug(sdkerrors.Wrapf(sdkerrors.ErrPanic, "%v", r), app.trace)
}
}()

Expand Down Expand Up @@ -492,7 +492,7 @@ func (app *BaseApp) Query(_ context.Context, req *abci.QueryRequest) (resp *abci

path := SplitABCIQueryPath(req.Path)
if len(path) == 0 {
return 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 @@ -510,7 +510,7 @@ func (app *BaseApp) Query(_ context.Context, req *abci.QueryRequest) (resp *abci
resp = queryResult(errorsmod.Wrap(sdkerrors.ErrUnknownRequest, "unknown query path"), app.trace)
}

return resp, nil
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 @@ -666,7 +666,7 @@ func (app *BaseApp) CheckTx(req *abci.CheckTxRequest) (*abci.CheckTxResponse, er

gInfo, result, anteEvents, err := app.runTx(mode, req.Tx)
if err != nil {
return responseCheckTxWithEvents(err, gInfo.GasWanted, gInfo.GasUsed, anteEvents, app.trace), nil
return sdkerrors.QueryResultWithDebug(err, app.trace)
}

return &abci.CheckTxResponse{
Expand Down Expand Up @@ -751,8 +751,9 @@ func (app *BaseApp) PrepareProposal(req *abci.PrepareProposalRequest) (resp *abc

resp, err = app.prepareProposal(app.prepareProposalState.Context(), req)
if err != nil {
app.logger.Error("failed to prepare proposal", "height", req.Height, "time", req.Time, "err", err)
return &abci.PrepareProposalResponse{Txs: req.Txs}, nil
res = sdkerrors.QueryResultWithDebug(gRPCErrorToSDKError(err), app.trace)
res.Height = req.Height
return res
}

return resp, nil
Expand Down Expand Up @@ -1311,7 +1312,7 @@ func handleQueryApp(app *BaseApp, path []string, req *abci.QueryRequest) *abci.Q

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

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

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

return &abci.QueryResponse{
Expand All @@ -1338,12 +1339,12 @@ func handleQueryApp(app *BaseApp, path []string, req *abci.QueryRequest) *abci.Q
}

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

return queryResult(
errorsmod.Wrap(
return sdkerrors.QueryResultWithDebug(
sdkerrors.Wrap(
sdkerrors.ErrUnknownRequest,
"expected second parameter to be either 'simulate' or 'version', neither was present",
), app.trace)
Expand All @@ -1353,14 +1354,14 @@ func handleQueryStore(app *BaseApp, path []string, req abci.QueryRequest) *abci.
// "/store" prefix for store queries
queryable, ok := app.cms.(storetypes.Queryable)
if !ok {
return queryResult(errorsmod.Wrap(sdkerrors.ErrUnknownRequest, "multi-store does not support queries"), app.trace)
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 queryResult(
errorsmod.Wrap(
return sdkerrors.QueryResultWithDebug(
sdkerrors.Wrap(
sdkerrors.ErrInvalidRequest,
"cannot query with proof when height <= 1; please provide a valid height",
), app.trace)
Expand All @@ -1381,7 +1382,10 @@ func handleQueryStore(app *BaseApp, path []string, req abci.QueryRequest) *abci.
func handleQueryP2P(app *BaseApp, path []string) *abci.QueryResponse {
// "/p2p" prefix for p2p queries
if len(path) < 4 {
return queryResult(errorsmod.Wrap(sdkerrors.ErrUnknownRequest, "path should be p2p filter <addr|id> <parameter>"), app.trace)
return sdkerrors.QueryResultWithDebug(
sdkerrors.Wrap(
sdkerrors.ErrUnknownRequest, "path should be p2p filter <addr|id> <parameter>",
), app.trace)
}

var resp *abci.QueryResponse
Expand All @@ -1398,39 +1402,25 @@ func handleQueryP2P(app *BaseApp, path []string) *abci.QueryResponse {
}

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

return resp
}

// SplitABCIQueryPath splits a string path using the delimiter '/'.
//
// e.g. "this/is/funny" becomes []string{"this", "is", "funny"}
func SplitABCIQueryPath(requestPath string) (path []string) {
path = strings.Split(requestPath, "/")

// first element is empty string
if len(path) > 0 && path[0] == "" {
path = path[1:]
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.QueryResultWithDebug(sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, "no route for custom query specified"), app.trace)
}

return path
}

// FilterPeerByAddrPort filters peers by address/port.
func (app *BaseApp) FilterPeerByAddrPort(info string) *abci.QueryResponse {
if app.addrPeerFilter != nil {
return app.addrPeerFilter(info)
}

return &abci.QueryResponse{}
}

// FilterPeerByID filters peers by node ID.
func (app *BaseApp) FilterPeerByID(info string) *abci.QueryResponse {
if app.idPeerFilter != nil {
return app.idPeerFilter(info)
querier := app.queryRouter.Route(path[1])
if querier == nil {
return sdkerrors.QueryResultWithDebug(sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "no custom querier found for route %s", path[1]), app.trace)
}

return &abci.QueryResponse{}
Expand All @@ -1454,23 +1444,14 @@ func (app *BaseApp) getContextForProposal(ctx sdk.Context, height int64) sdk.Con
func (app *BaseApp) handleQueryGRPC(handler GRPCQueryHandler, req *abci.QueryRequest) *abci.QueryResponse {
ctx, err := app.CreateQueryContext(req.Height, req.Prove)
if err != nil {
return queryResult(err, app.trace)
return sdkerrors.QueryResultWithDebug(err, app.trace)
}

resp, err := handler(ctx, req)
if err != nil {
resp = queryResult(gRPCErrorToSDKError(err), app.trace)
resp.Height = req.Height
return resp
}

return resp
}

func gRPCErrorToSDKError(err error) error {
status, ok := grpcstatus.FromError(err)
if !ok {
return errorsmod.Wrap(sdkerrors.ErrInvalidRequest, err.Error())
res := sdkerrors.QueryResultWithDebug(err, app.trace)
res.Height = req.Height
return res
}

switch status.Code() {
Expand Down

0 comments on commit f8778c3

Please sign in to comment.