Skip to content

Commit

Permalink
eth/tracers: return proper error from debug_TraceTransaction when tx …
Browse files Browse the repository at this point in the history
…not found (#26211)

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 <martin@swende.se>
Co-authored-by: Sina Mahmoodi <itz.s1na@gmail.com>
  • Loading branch information
3 people committed Dec 10, 2022
1 parent 3775e19 commit 262bd38
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
8 changes: 7 additions & 1 deletion eth/tracers/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -801,10 +803,14 @@ func containsTx(block *types.Block, hash common.Hash) bool {
// 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")
Expand Down
14 changes: 8 additions & 6 deletions eth/tracers/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,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 {
Expand Down Expand Up @@ -117,9 +116,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
}

Expand Down Expand Up @@ -365,6 +361,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) {
Expand Down

0 comments on commit 262bd38

Please sign in to comment.