From 77dfc466062ed8da8fdc563eabe16e05b4649fcd Mon Sep 17 00:00:00 2001 From: Philip Su Date: Wed, 11 Dec 2024 19:15:18 -0800 Subject: [PATCH] Recover at traceTx --- eth/tracers/api.go | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/eth/tracers/api.go b/eth/tracers/api.go index e3fa5c8620..cb472aebc3 100644 --- a/eth/tracers/api.go +++ b/eth/tracers/api.go @@ -612,20 +612,12 @@ func (api *API) traceBlock(ctx context.Context, block *types.Block, config *Trac TxHash: tx.Hash(), } - func() { - defer func() { - if r := recover(); r != nil { - results[i] = &txTraceResult{TxHash: tx.Hash(), Error: fmt.Sprintf("panic: %v", r)} - } - }() - - res, err := api.traceTx(ctx, tx, msg, txctx, blockCtx, statedb, config) - if err != nil { - results[i] = &txTraceResult{TxHash: tx.Hash(), Error: err.Error()} - return - } + res, err := api.traceTx(ctx, tx, msg, txctx, blockCtx, statedb, config) + if err != nil { + results[i] = &txTraceResult{TxHash: tx.Hash(), Error: err.Error()} + } else { results[i] = &txTraceResult{TxHash: tx.Hash(), Result: res} - }() + } } return results, nil } @@ -953,13 +945,20 @@ func (api *API) TraceCall(ctx context.Context, args ethapi.TransactionArgs, bloc // traceTx configures a new tracer according to the provided configuration, and // executes the given message in the provided environment. The return value will // be tracer dependent. -func (api *API) traceTx(ctx context.Context, tx *types.Transaction, message *core.Message, txctx *Context, vmctx vm.BlockContext, statedb vm.StateDB, config *TraceConfig) (interface{}, error) { +func (api *API) traceTx(ctx context.Context, tx *types.Transaction, message *core.Message, txctx *Context, vmctx vm.BlockContext, statedb vm.StateDB, config *TraceConfig) (value interface{}, returnErr error) { + // Note that traceTx could panic, so we want to catch it and send a generic error message var ( tracer *Tracer err error timeout = defaultTraceTimeout usedGas uint64 ) + defer func() { + if r := recover(); r != nil { + value = nil + returnErr = fmt.Errorf("panic occured: %s, could not trace tx: %s", r, tx.Hash()) + } + }() if config == nil { config = &TraceConfig{} }