-
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 #31116
base: master
Are you sure you want to change the base?
Conversation
@@ -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) | |||
} |
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.
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 |
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.
api.b.BlockByNumber(ctx, rpc.BlockNumber(number))
returns an error if block is nil,
so if block == nil {...}
handling becomes unnecessary.
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 |
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 |
@s1na Even if you adopt my modification, I think it will return nil if the block does not exist. If if block == nil || err != nil {
// When the block doesn't exist, the RPC method should return JSON null
// as per specification.
return nil, nil
} |
Right but as the PR stands some methods like |
return b.eth.blockchain.GetBlockByNumber(uint64(number)), nil | ||
block := b.eth.blockchain.GetBlockByNumber(uint64(number)) | ||
if block == nil { | ||
return nil, fmt.Errorf("block #%d not found", number) |
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.
Please define this as an error, so it can be caught and handled
Not sure if I agree with returning error. I think my preference would be to fix any place where we don't check for nil and continue returning nil when the block isn't found. |
Very good thanks |
This PR fixes "nil pointer error occurs when block is nil".
The following
GetBlockByNumber
function is used in several places, but only infunc (b *EthAPIBackend) BlockByNumber(ctx context.Context, number rpc.BlockNumber) (*types.Block, error)
ateth/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.quote: #31104 (comment)