-
Notifications
You must be signed in to change notification settings - Fork 189
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
When fetching non-existing block using eth namespace APIs, it must return null #1261
Conversation
eth namespace APIs fetching non-existing block must returns null based on its spec
This is for preventing panic just in case.
klaytnBlock, err := api.publicBlockChainAPI.b.BlockByNumber(ctx, number) | ||
if klaytnBlock != nil && err == nil { | ||
if err != nil { |
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.
@aeharvlee What if other error occurs? There is no way to 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.
@kjhman21
Currently only one error which is fmt.Errorf("the block does not exist (block number: %d)", blockNr)
can be returned from CNAPIBackend.BlockByNumber.
Ethereum treats that case(= The block does not exist) as nil
.
By the way, there is the case when error can occur during EthereumAPI.GetBlockByNumber
and EthereumAPI.GetBlockByHash
.
Both APIs use rpcMarshalBlock to marshal Block data structure to map[string]interface{}
.
rpcMarshalBlock calls rpcMarshalHeader internally and rpcMarshalHeader try to fetch block proposer using backend.Author at istanbul/backend/engine.go
.
If error occurs during backend.Author, it returns error so EthereumAPI.GetBlockByNumber
and EthereumAPI.GetBlockByHash
returns error in that case. This is the case when other error can occur during those APIs and this it the different part from Ethereum.
cc. @aidan-kwon
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.
Either one is okay for now. But, I think error string comparison increases the complexity of these APIs.
Remove un-necessary if branch and err. Ethereum does not treat error(non exist block) in this API.
api/api_ethereum.go
Outdated
// Klaytn backend returns error when there is no matched block but | ||
// Ethereum returns it as nil without error, so we should return is as nil when there is no matched block. | ||
klaytnBlock, _ := api.publicBlockChainAPI.b.BlockByNumber(ctx, number) | ||
if klaytnBlock != nil { |
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.
What about this?
klaytnBlock, err := api.publicBlockChainAPI.b.BlockByNumber(ctx, number)
if err != nil {
if err == errBlockNotFound {
return nil, nil
}
return nil, err
}
response, err := api.rpcMarshalBlock(klaytnBlock, true, fullTx)
if err == nil && number == rpc.PendingBlockNumber {
// Pending blocks need to nil out a few fields
for _, field := range []string{"hash", "nonce", "miner"} {
response[field] = nil
}
}
return response, 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.
@kjhman21
Looks great to me.
I'll update this PR.
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.
PTAL this commit.
I considered defining not existing err as separate error variables in cn package and try to pack with that error like below, but there is a import cycle problem between cn package and api package.
// node/cn/api_backend.go
var ErrBlockNotFound = errors.New("the block does not exist")
func (b *CNAPIBackend) BlockByHash(ctx context.Context, hash common.Hash) (*types.Block, error) {
block := b.cn.blockchain.GetBlockByHash(hash)
if block == nil {
return nil, fmt.Errorf("%w (block hash: %s)", ErrBlockNotFound, hash.String())
}
return block, nil
}
// api/api_ethereum.go
func (api *EthereumAPI) GetBlockByNumber(ctx context.Context, number rpc.BlockNumber) (map[string]interface{}, error) {
klaytnHeader, err := api.publicBlockChainAPI.b.BlockByNumber(ctx, number)
if err != nil {
if errors.Is(err, cn.ErrBlockNotFound) {
return nil, nil
}
return nil, err
}
// ...
}
So I checked the error whether it is containing "does not exist" text or not in its error message.
And I also updated other apis using similar logics too. (e.g. GetHeaderByNumber)
Look if err is not a known error (does not exist). And if so, returns error.
b04e32c
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.
For now, it is okay to me, but it would be better to more specify the exact error string in the if statement like Ethereum.
Proposed changes
eth namespace APIs fetching non-existing block must returns null based on its spec.
As-Is
Is must returns
null
, but currently it returns error like below.To-be (If this PR is merged.)
Geth
Klaytn
null
for non-existing block.Types of changes
Please put an x in the boxes related to your change.
Checklist
Put an x in the boxes that apply. You can also fill these out after creating the PR. If you're unsure about any of them, don't hesitate to ask. We're here to help! This is simply a reminder of what we are going to look for before merging your code.
$ make test
)Related issues
Further comments
If this is a relatively large or complex change, kick off the discussion by explaining why you chose the solution you did and what alternatives you considered, etc...