Skip to content
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 #31116

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

ysk49
Copy link

@ysk49 ysk49 commented Feb 3, 2025

This PR fixes "nil pointer error occurs when block is nil".

The following GetBlockByNumber function is used in several places, but only in func (b *EthAPIBackend) BlockByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Block, error) at eth/api_backend.go did not implement error handling when block is nil.

https://github.com/ethereum/go-ethereum/blob/master/eth/api_backend.go#L146

So I think we also should fix the b.BlockByNumber, by returning the error if the required block is not existent.

func (bc *core.BlockChain) GetBlockByNumber(number uint64) *types.Block

quote: #31104 (comment)

@@ -114,9 +114,6 @@ func (api *API) blockByNumber(ctx context.Context, number rpc.BlockNumber) (*typ
if err != nil {
return nil, err
}
if block == nil {
return nil, fmt.Errorf("block #%d not found", number)
}
Copy link
Author

@ysk49 ysk49 Feb 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

block, err := api.backend.BlockByNumber(ctx, number), returning err if block is nil.
Therefore, this handling is unnecessary and has been removed.
https://github.com/ethereum/go-ethereum/pull/31116/files#diff-bd288cd51c1874c7e65ce0d35047c0a98366479c8924606c9efc577f6a98f75cR113

return "", fmt.Errorf("block #%d not found", number)
block, err := api.b.BlockByNumber(ctx, rpc.BlockNumber(number))
if err != nil {
return "", err
Copy link
Author

@ysk49 ysk49 Feb 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

api.b.BlockByNumber(ctx, rpc.BlockNumber(number)) returns an error if block is nil,
so if block == nil {...} handling becomes unnecessary.

@s1na
Copy link
Contributor

s1na commented Feb 3, 2025

As expressed in the last PR, I disagree with this change as of now. The spec requires us to return nil for not found objects: https://ethereum.github.io/execution-apis/api-documentation/

So if there is a panic, please check the block == nil and return nil, nil in the API.

@MariusVanDerWijden
Copy link
Member

Seems like @rjl493456442 @s1na and I have different opinions on how to do this change, but we all agree that it should be fixed. We should discuss on triage

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants