rpc: fix eth_getBlockTransactionCountByNumber crash on block not found (#254)

* Fix GetBlockTransactionCountByNumber crashed on block not found

* Fix getTransactionByBlock*

* Update log based on review

* Apply suggestions from code review

* Update ethereum/rpc/namespaces/eth/api.go

* 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-16 10:13:15 +00:00
committed by GitHub
co-authored by Federico Kunze Küllmer
parent 333a76bdf8
commit 071ae3f618
2 changed files with 98 additions and 1 deletions
+23 -1
View File
@@ -273,6 +273,12 @@ func (e *PublicAPI) GetBlockTransactionCountByHash(hash common.Hash) *hexutil.Ui
resBlock, err := e.clientCtx.Client.BlockByHash(e.ctx, hash.Bytes())
if err != nil {
e.logger.Debug("block not found", "hash", hash.Hex(), "error", err.Error())
return nil
}
if resBlock.Block == nil {
e.logger.Debug("block not found", "hash", hash.Hex())
return nil
}
@@ -282,9 +288,15 @@ func (e *PublicAPI) GetBlockTransactionCountByHash(hash common.Hash) *hexutil.Ui
// GetBlockTransactionCountByNumber returns the number of transactions in the block identified by number.
func (e *PublicAPI) GetBlockTransactionCountByNumber(blockNum rpctypes.BlockNumber) *hexutil.Uint {
e.logger.Debug("eth_getBlockTransactionCountByNumber", "block number", blockNum)
e.logger.Debug("eth_getBlockTransactionCountByNumber", "height", blockNum.Int64())
resBlock, err := e.clientCtx.Client.Block(e.ctx, blockNum.TmHeight())
if err != nil {
e.logger.Debug("block not found", "height", blockNum.Int64(), "error", err.Error())
return nil
}
if resBlock.Block == nil {
e.logger.Debug("block not found", "height", blockNum.Int64())
return nil
}
@@ -668,6 +680,11 @@ func (e *PublicAPI) GetTransactionByBlockHashAndIndex(hash common.Hash, idx hexu
return nil, nil
}
if resBlock.Block == nil {
e.logger.Debug("block not found", "hash", hash.Hex())
return nil, nil
}
i := int(idx)
if i >= len(resBlock.Block.Txs) {
e.logger.Debug("block txs index out of bound", "index", i)
@@ -706,6 +723,11 @@ func (e *PublicAPI) GetTransactionByBlockNumberAndIndex(blockNum rpctypes.BlockN
return nil, nil
}
if resBlock.Block == nil {
e.logger.Debug("block not found", "height", blockNum.Int64())
return nil, nil
}
i := int(idx)
if i >= len(resBlock.Block.Txs) {
e.logger.Debug("block txs index out of bound", "index", i)