diff --git a/CHANGELOG.md b/CHANGELOG.md index 1ad253413b..8494b06a79 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -36,6 +36,12 @@ Ref: https://keepachangelog.com/en/1.0.0/ # Changelog +## [v0.14.1] - 2022-07-13 + +### Improvements + +* (rpc) [\#1169](https://github.com/evmos/ethermint/pull/1169) Remove unnecessary queries from `getBlockNumber` function + ## [v0.14.0] - 2022-04-19 ### API Breaking diff --git a/rpc/ethereum/backend/backend.go b/rpc/ethereum/backend/backend.go index 51565b28f0..818a3c416e 100644 --- a/rpc/ethereum/backend/backend.go +++ b/rpc/ethereum/backend/backend.go @@ -62,6 +62,7 @@ type Backend interface { BlockByHash(blockHash common.Hash) (*ethtypes.Block, error) CurrentHeader() *ethtypes.Header HeaderByNumber(blockNum types.BlockNumber) (*ethtypes.Header, error) + GetBlockNumberByHash(blockHash common.Hash) (*big.Int, error) HeaderByHash(blockHash common.Hash) (*ethtypes.Header, error) PendingTransactions() ([]*sdk.Tx, error) GetTransactionCount(address common.Address, blockNum types.BlockNumber) (*hexutil.Uint64, error) @@ -508,6 +509,18 @@ func (e *EVMBackend) HeaderByNumber(blockNum types.BlockNumber) (*ethtypes.Heade return ethHeader, nil } +// GetBlockNumberByHash returns the block height of given block hash +func (e *EVMBackend) GetBlockNumberByHash(blockHash common.Hash) (*big.Int, error) { + resBlock, err := e.GetTendermintBlockByHash(blockHash) + if err != nil { + return nil, err + } + if resBlock == nil { + return nil, errors.Errorf("block not found for hash %s", blockHash.Hex()) + } + return big.NewInt(resBlock.Block.Height), nil +} + // HeaderByHash returns the block header identified by hash. func (e *EVMBackend) HeaderByHash(blockHash common.Hash) (*ethtypes.Header, error) { resBlock, err := e.clientCtx.Client.BlockByHash(e.ctx, blockHash.Bytes()) diff --git a/rpc/ethereum/namespaces/eth/api.go b/rpc/ethereum/namespaces/eth/api.go index 2cbbb24732..7fb2d91bcf 100644 --- a/rpc/ethereum/namespaces/eth/api.go +++ b/rpc/ethereum/namespaces/eth/api.go @@ -1102,11 +1102,11 @@ func (e *PublicAPI) getBlockNumber(blockNrOrHash rpctypes.BlockNumberOrHash) (rp case blockNrOrHash.BlockHash == nil && blockNrOrHash.BlockNumber == nil: return rpctypes.EthEarliestBlockNumber, fmt.Errorf("types BlockHash and BlockNumber cannot be both nil") case blockNrOrHash.BlockHash != nil: - blockHeader, err := e.backend.HeaderByHash(*blockNrOrHash.BlockHash) + blockNumber, err := e.backend.GetBlockNumberByHash(*blockNrOrHash.BlockHash) if err != nil { return rpctypes.EthEarliestBlockNumber, err } - return rpctypes.NewBlockNumber(blockHeader.Number), nil + return rpctypes.NewBlockNumber(blockNumber), nil case blockNrOrHash.BlockNumber != nil: return *blockNrOrHash.BlockNumber, nil default: