rpc: fix GetBlockByHash crash on block not found (#256)

* Fix GetBlockByHash crashed on block not found

* Add and update log message based on review

* Apply suggestions from code review

* Apply suggestions from code review

Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com>
This commit is contained in:
Calvin Lau
2021-07-12 12:45:13 +00:00
committed by GitHub
co-authored by Federico Kunze Küllmer
parent 8bfb6b0a67
commit 8d51a70d6d
2 changed files with 39 additions and 2 deletions
+12 -2
View File
@@ -112,11 +112,16 @@ func (e *EVMBackend) GetBlockByNumber(blockNum types.BlockNumber, fullTx bool) (
if err != nil {
// e.logger.Debugf("GetBlockByNumber safely bumping down from %d to latest", height)
if resBlock, err = e.clientCtx.Client.Block(e.ctx, nil); err != nil {
e.logger.Debugln("GetBlockByNumber failed to get latest block")
e.logger.WithError(err).Debugln("GetBlockByNumber failed to get latest block")
return nil, nil
}
}
if resBlock.Block == nil {
e.logger.Debugln("GetBlockByNumber block not found", "height", height)
return nil, nil
}
res, err := e.EthBlockFromTendermint(e.clientCtx, e.queryClient, resBlock.Block, fullTx)
if err != nil {
e.logger.WithError(err).Debugf("EthBlockFromTendermint failed with block %s", resBlock.Block.String())
@@ -129,10 +134,15 @@ func (e *EVMBackend) GetBlockByNumber(blockNum types.BlockNumber, fullTx bool) (
func (e *EVMBackend) GetBlockByHash(hash common.Hash, fullTx bool) (map[string]interface{}, error) {
resBlock, err := e.clientCtx.Client.BlockByHash(e.ctx, hash.Bytes())
if err != nil {
e.logger.Warningf("BlockByHash failed for %s", hash.Hex())
e.logger.WithError(err).Debugln("BlockByHash block not found", "hash", hash.Hex())
return nil, err
}
if resBlock.Block == nil {
e.logger.Debugln("BlockByHash block not found", "hash", hash.Hex())
return nil, nil
}
return e.EthBlockFromTendermint(e.clientCtx, e.queryClient, resBlock.Block, fullTx)
}