From 44c31e7493ea4f76f4b5813c5daedefef1817750 Mon Sep 17 00:00:00 2001 From: Daniel Liu Date: Sat, 13 Jul 2024 12:18:10 +0800 Subject: [PATCH] eth: check nil block for tracer api --- eth/api_tracer.go | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/eth/api_tracer.go b/eth/api_tracer.go index 01abc14b340f..fbd3bfabd8c6 100644 --- a/eth/api_tracer.go +++ b/eth/api_tracer.go @@ -106,6 +106,32 @@ type txTraceTask struct { index int // Transaction offset in the block } +// blockByNumber is the wrapper of the chain access function offered by the backend. +// It will return an error if the block is not found. +func (api *PrivateDebugAPI) blockByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Block, error) { + block, err := api.eth.ApiBackend.BlockByNumber(ctx, number) + if err != nil { + return nil, err + } + if block == nil { + return nil, fmt.Errorf("block #%d not found", number) + } + return block, nil +} + +// blockByHash is the wrapper of the chain access function offered by the backend. +// It will return an error if the block is not found. +func (api *PrivateDebugAPI) blockByHash(ctx context.Context, hash common.Hash) (*types.Block, error) { + block, err := api.eth.ApiBackend.BlockByHash(ctx, hash) + if err != nil { + return nil, err + } + if block == nil { + return nil, fmt.Errorf("block %s not found", hash.Hex()) + } + return block, nil +} + // TraceChain returns the structured logs created during the execution of EVM // between two blocks (excluding start) and returns them as a JSON object. func (api *PrivateDebugAPI) TraceChain(ctx context.Context, start, end rpc.BlockNumber, config *TraceConfig) (*rpc.Subscription, error) { @@ -640,7 +666,7 @@ func (api *PrivateDebugAPI) TraceCall(ctx context.Context, args ethapi.CallArgs, block *types.Block ) if hash, ok := blockNrOrHash.Hash(); ok { - block, err = api.eth.ApiBackend.BlockByHash(ctx, hash) + block, err = api.blockByHash(ctx, hash) } else if number, ok := blockNrOrHash.Number(); ok { if number == rpc.PendingBlockNumber { // We don't have access to the miner here. For tracing 'future' transactions, @@ -650,7 +676,7 @@ func (api *PrivateDebugAPI) TraceCall(ctx context.Context, args ethapi.CallArgs, // of what the next actual block is likely to contain. return nil, errors.New("tracing on top of pending is not supported") } - block, err = api.eth.ApiBackend.BlockByNumber(ctx, number) + block, err = api.blockByNumber(ctx, number) } else { return nil, errors.New("invalid arguments; neither block nor hash specified") }