-
Notifications
You must be signed in to change notification settings - Fork 20.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix nil pointer when block does not exist #31104
Conversation
If the block body doesn't exist, an error will be returned: https://github.com/ethereum/go-ethereum/blob/master/eth/api_backend.go#L178 |
I guess you are using ethclient for this and don't handle when a block is nil. But in most of the JSON-RPC API if an item does not exist the server returns nil. It is the same e.g. in eth_getBlockByNumber. |
While I think that this PR is on the wrong track, the issue you're reporting is correct. I would fix it in the following way: diff --git a/internal/ethapi/api.go b/internal/ethapi/api.go
index d9cec560ea..bdc1fea381 100644
--- a/internal/ethapi/api.go
+++ b/internal/ethapi/api.go
@@ -1711,6 +1711,9 @@ func (api *DebugAPI) GetRawReceipts(ctx context.Context, blockNrOrHash rpc.Block
if err != nil {
return nil, err
}
+ if block == nil {
+ return nil, fmt.Errorf("block #%v not found", blockNrOrHash)
+ }
hash = block.Hash()
}
receipts, err := api.b.GetReceipts(ctx, hash) Please fix all four occurrences in |
@@ -526,7 +526,12 @@ func (b testBackend) BlockByHash(ctx context.Context, hash common.Hash) (*types. | |||
} | |||
func (b testBackend) BlockByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (*types.Block, error) { | |||
if blockNr, ok := blockNrOrHash.Number(); ok { | |||
return b.BlockByNumber(ctx, blockNr) | |||
block, err := b.BlockByNumber(ctx, blockNr) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should fix the b.BlockByNumber
, by returning the error if the required block is not existent
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rjl493456442
Thank you for reviewing.
I was thinking the same thing, so I'll fix it to what you said.
The following https://github.com/ethereum/go-ethereum/blob/master/eth/api_backend.go#L146 So I think we also should fix the func (bc *core.BlockChain) GetBlockByNumber(number uint64) *types.Block |
This PR fixes the following thing:
When
block
does not exist,hash = block.Hash()
will cause a panic.