From aba4198dc17831264f8c875eae4a19dc432d05a2 Mon Sep 17 00:00:00 2001 From: Daniel Burckhardt Date: Wed, 13 Jul 2022 14:57:50 +0200 Subject: [PATCH] fix(rpc): optimize `eth_getBalance` endpoint (#1169) (#1182) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * optimize get balance endpoint * add comment * add changelog Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com> Co-authored-by: crypto-facs <84574577+crypto-facs@users.noreply.github.com> Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com> --- CHANGELOG.md | 6 ++++++ rpc/backend/backend.go | 1 + rpc/backend/evm_backend.go | 12 ++++++++++++ rpc/namespaces/ethereum/eth/api.go | 4 ++-- 4 files changed, 21 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index edd9353920..55f9be7319 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -36,6 +36,12 @@ Ref: https://keepachangelog.com/en/1.0.0/ # Changelog +## [v0.15.1] - 2022-07-13 + +### Improvements + +* (rpc) [\#1169](https://github.com/evmos/ethermint/pull/1169) Remove unnecessary queries from `getBlockNumber` function + ## [v0.15.0] - 2022-05-09 ### State Machine Breaking diff --git a/rpc/backend/backend.go b/rpc/backend/backend.go index e9692c8dbf..f268754a03 100644 --- a/rpc/backend/backend.go +++ b/rpc/backend/backend.go @@ -60,6 +60,7 @@ type EVMBackend interface { CurrentHeader() *ethtypes.Header HeaderByNumber(blockNum types.BlockNumber) (*ethtypes.Header, error) HeaderByHash(blockHash common.Hash) (*ethtypes.Header, error) + GetBlockNumberByHash(blockHash common.Hash) (*big.Int, error) PendingTransactions() ([]*sdk.Tx, error) GetTransactionCount(address common.Address, blockNum types.BlockNumber) (*hexutil.Uint64, error) SendTransaction(args evmtypes.TransactionArgs) (common.Hash, error) diff --git a/rpc/backend/evm_backend.go b/rpc/backend/evm_backend.go index 619afd1bc5..73f26bbb7c 100644 --- a/rpc/backend/evm_backend.go +++ b/rpc/backend/evm_backend.go @@ -428,6 +428,18 @@ func (b *Backend) HeaderByNumber(blockNum types.BlockNumber) (*ethtypes.Header, return ethHeader, nil } +// GetBlockNumberByHash returns the block height of given block hash +func (b *Backend) GetBlockNumberByHash(blockHash common.Hash) (*big.Int, error) { + resBlock, err := b.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 (b *Backend) HeaderByHash(blockHash common.Hash) (*ethtypes.Header, error) { resBlock, err := b.clientCtx.Client.BlockByHash(b.ctx, blockHash.Bytes()) diff --git a/rpc/namespaces/ethereum/eth/api.go b/rpc/namespaces/ethereum/eth/api.go index 625739e97c..21b98ddd59 100644 --- a/rpc/namespaces/ethereum/eth/api.go +++ b/rpc/namespaces/ethereum/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: