From 2834be6131ff286381fbed36267873ef39f8550a Mon Sep 17 00:00:00 2001 From: Daniel Liu <139250065@qq.com> Date: Tue, 9 Sep 2025 09:36:14 +0800 Subject: [PATCH] eth/tracers: return proper error from debug_TraceTransaction when tx not found #26211 (#1312) Currently calling `debug_TraceTransaction` with a transaction hash that doesn't exist returns a confusing error: `genesis is not traceable`. This PR changes the behaviour to instead return an error message saying `transaction not found` Co-authored-by: Martin Holst Swende Co-authored-by: Sina Mahmoodi --- eth/tracers/api.go | 8 +++++++- eth/tracers/api_test.go | 14 ++++++++------ 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/eth/tracers/api.go b/eth/tracers/api.go index bbb8ebc30e..19438e3eb5 100644 --- a/eth/tracers/api.go +++ b/eth/tracers/api.go @@ -68,6 +68,8 @@ const ( maximumPendingTraceStates = 128 ) +var errTxNotFound = errors.New("transaction not found") + // StateReleaseFunc is used to deallocate resources held by constructing a // historical state for tracing purposes. type StateReleaseFunc func() @@ -671,10 +673,14 @@ func (api *API) traceBlock(ctx context.Context, block *types.Block, config *Trac // TraceTransaction returns the structured logs created during the execution of EVM // and returns them as a JSON object. func (api *API) TraceTransaction(ctx context.Context, hash common.Hash, config *TraceConfig) (interface{}, error) { - _, blockHash, blockNumber, index, err := api.backend.GetTransaction(ctx, hash) + tx, blockHash, blockNumber, index, err := api.backend.GetTransaction(ctx, hash) if err != nil { return nil, err } + // Only mined txes are supported + if tx == nil { + return nil, errTxNotFound + } // It shouldn't happen in practice. if blockNumber == 0 { return nil, errors.New("genesis is not traceable") diff --git a/eth/tracers/api_test.go b/eth/tracers/api_test.go index 2187b4abd1..7182008553 100644 --- a/eth/tracers/api_test.go +++ b/eth/tracers/api_test.go @@ -49,9 +49,8 @@ import ( ) var ( - errStateNotFound = errors.New("state not found") - errBlockNotFound = errors.New("block not found") - errTransactionNotFound = errors.New("transaction not found") + errStateNotFound = errors.New("state not found") + errBlockNotFound = errors.New("block not found") ) type testBackend struct { @@ -121,9 +120,6 @@ func (b *testBackend) BlockByNumber(ctx context.Context, number rpc.BlockNumber) func (b *testBackend) GetTransaction(ctx context.Context, txHash common.Hash) (*types.Transaction, common.Hash, uint64, uint64, error) { tx, hash, blockNumber, index := rawdb.ReadTransaction(b.chaindb, txHash) - if tx == nil { - return nil, common.Hash{}, 0, 0, errTransactionNotFound - } return tx, hash, blockNumber, index, nil } @@ -372,6 +368,12 @@ func TestTraceTransaction(t *testing.T) { }) { t.Error("Transaction tracing result is different") } + + // Test non-existent transaction + _, err = api.TraceTransaction(context.Background(), common.Hash{42}, nil) + if !errors.Is(err, errTxNotFound) { + t.Fatalf("want %v, have %v", errTxNotFound, err) + } } func TestTraceBlock(t *testing.T) {