imp: clean up the block fetch logic in json-rpc (#1081)

* clean up the block fetch logic in json-rpc

deduplicate some codes

fix EthBlockFromTm

fix latest block height

* add bug fix changelog
This commit is contained in:
yihuang
2022-05-23 18:36:31 +02:00
committed by GitHub
parent 2cfa8730c9
commit f06df8c265
6 changed files with 76 additions and 214 deletions
+2 -2
View File
@@ -342,7 +342,7 @@ 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", "height", blockNum.Int64())
block, err := e.clientCtx.Client.Block(e.ctx, blockNum.TmHeight())
block, err := e.backend.GetTendermintBlockByNumber(blockNum)
if err != nil {
e.logger.Debug("block not found", "height", blockNum.Int64(), "error", err.Error())
return nil
@@ -800,7 +800,7 @@ func (e *PublicAPI) GetTransactionByBlockHashAndIndex(hash common.Hash, idx hexu
func (e *PublicAPI) GetTransactionByBlockNumberAndIndex(blockNum rpctypes.BlockNumber, idx hexutil.Uint) (*rpctypes.RPCTransaction, error) {
e.logger.Debug("eth_getTransactionByBlockNumberAndIndex", "number", blockNum, "index", idx)
block, err := e.clientCtx.Client.Block(e.ctx, blockNum.TmHeight())
block, err := e.backend.GetTendermintBlockByNumber(blockNum)
if err != nil {
e.logger.Debug("block not found", "height", blockNum.Int64(), "error", err.Error())
return nil, nil
+1 -1
View File
@@ -29,7 +29,7 @@ type Backend interface {
HeaderByNumber(blockNum types.BlockNumber) (*ethtypes.Header, error)
HeaderByHash(blockHash common.Hash) (*ethtypes.Header, error)
GetLogs(blockHash common.Hash) ([][]*ethtypes.Log, error)
GetLogsByNumber(blockNum types.BlockNumber) ([][]*ethtypes.Log, error)
GetLogsByHeight(*int64) ([][]*ethtypes.Log, error)
BlockBloom(height *int64) (ethtypes.Bloom, error)
BloomStatus() (uint64, uint64)
@@ -172,7 +172,7 @@ func (f *Filter) blockLogs(height int64, bloom ethtypes.Bloom) ([]*ethtypes.Log,
// DANGER: do not call GetLogs(header.Hash())
// eth header's hash doesn't match tm block hash
logsList, err := f.backend.GetLogsByNumber(types.BlockNumber(height))
logsList, err := f.backend.GetLogsByHeight(&height)
if err != nil {
return []*ethtypes.Log{}, errors.Wrapf(err, "failed to fetch logs block number %d", height)
}