From 4f76df8b3ca8f25056409d661c669f6b04458494 Mon Sep 17 00:00:00 2001 From: Maciej Kulawik Date: Wed, 6 Mar 2024 17:49:48 +0000 Subject: [PATCH 1/2] eth/tracers: add canonical hash check in API.blockByHash --- eth/tracers/api.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/eth/tracers/api.go b/eth/tracers/api.go index cfbd683d89..afeed9f7d2 100644 --- a/eth/tracers/api.go +++ b/eth/tracers/api.go @@ -128,6 +128,15 @@ func (api *API) blockByHash(ctx context.Context, hash common.Hash) (*types.Block if block == nil { return nil, fmt.Errorf("block %s not found", hash.Hex()) } + + number := block.NumberU64() + canonical := rawdb.ReadCanonicalHash(api.backend.ChainDb(), number) + if canonical == (common.Hash{}) { + return nil, fmt.Errorf("canonical hash for block %s not found", hash.Hex()) + } + if hash.Cmp(canonical) != 0 { + return nil, fmt.Errorf("canonical hash doesn't match requested hash, hash %s, canonical %s, block #%d", hash.Hex(), canonical.Hex(), number) + } return block, nil } From 01b68594f694364d095f9f3d50251f8479df287f Mon Sep 17 00:00:00 2001 From: Maciej Kulawik Date: Tue, 12 Mar 2024 22:29:41 +0000 Subject: [PATCH 2/2] simpify recent block check --- eth/tracers/api.go | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/eth/tracers/api.go b/eth/tracers/api.go index afeed9f7d2..e8ba938d59 100644 --- a/eth/tracers/api.go +++ b/eth/tracers/api.go @@ -128,14 +128,9 @@ func (api *API) blockByHash(ctx context.Context, hash common.Hash) (*types.Block if block == nil { return nil, fmt.Errorf("block %s not found", hash.Hex()) } - - number := block.NumberU64() - canonical := rawdb.ReadCanonicalHash(api.backend.ChainDb(), number) - if canonical == (common.Hash{}) { - return nil, fmt.Errorf("canonical hash for block %s not found", hash.Hex()) - } - if hash.Cmp(canonical) != 0 { - return nil, fmt.Errorf("canonical hash doesn't match requested hash, hash %s, canonical %s, block #%d", hash.Hex(), canonical.Hex(), number) + canonical := rawdb.ReadCanonicalHash(api.backend.ChainDb(), block.NumberU64()) + if hash != canonical { + return nil, fmt.Errorf("hash %s is not currently canonical", hash.Hex()) } return block, nil }