fix(rpc): optimize eth_getBalance endpoint (#1169)

* optimize get balance endpoint

* add comment

* add changelog

Co-authored-by: Federico Kunze Küllmer <31522760+fedekunze@users.noreply.github.com>
This commit is contained in:
crypto-facs 2022-07-11 11:04:45 -04:00 committed by GitHub
parent 9ae73b670b
commit da8fcc3ae2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 16 additions and 2 deletions

View File

@ -46,6 +46,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
### Improvements ### Improvements
* (feemarket) [\#1165](https://github.com/evmos/ethermint/pull/1165) Add hint in specs about different gas terminology for gas in Cosmos and Ethereum. * (feemarket) [\#1165](https://github.com/evmos/ethermint/pull/1165) Add hint in specs about different gas terminology for gas in Cosmos and Ethereum.
* (rpc) [\#1169](https://github.com/evmos/ethermint/pull/1169) Remove unnecessary queries from `getBlockNumber` function
## [v0.17.0] - 2022-06-27 ## [v0.17.0] - 2022-06-27

View File

@ -61,6 +61,7 @@ type EVMBackend interface {
CurrentHeader() *ethtypes.Header CurrentHeader() *ethtypes.Header
HeaderByNumber(blockNum types.BlockNumber) (*ethtypes.Header, error) HeaderByNumber(blockNum types.BlockNumber) (*ethtypes.Header, error)
HeaderByHash(blockHash common.Hash) (*ethtypes.Header, error) HeaderByHash(blockHash common.Hash) (*ethtypes.Header, error)
GetBlockNumberByHash(blockHash common.Hash) (*big.Int, error)
PendingTransactions() ([]*sdk.Tx, error) PendingTransactions() ([]*sdk.Tx, error)
GetTransactionCount(address common.Address, blockNum types.BlockNumber) (*hexutil.Uint64, error) GetTransactionCount(address common.Address, blockNum types.BlockNumber) (*hexutil.Uint64, error)
SendTransaction(args evmtypes.TransactionArgs) (common.Hash, error) SendTransaction(args evmtypes.TransactionArgs) (common.Hash, error)

View File

@ -369,6 +369,18 @@ func (b *Backend) HeaderByNumber(blockNum types.BlockNumber) (*ethtypes.Header,
return ethHeader, nil 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. // HeaderByHash returns the block header identified by hash.
func (b *Backend) HeaderByHash(blockHash common.Hash) (*ethtypes.Header, error) { func (b *Backend) HeaderByHash(blockHash common.Hash) (*ethtypes.Header, error) {
resBlock, err := b.GetTendermintBlockByHash(blockHash) resBlock, err := b.GetTendermintBlockByHash(blockHash)

View File

@ -1138,11 +1138,11 @@ func (e *PublicAPI) getBlockNumber(blockNrOrHash rpctypes.BlockNumberOrHash) (rp
case blockNrOrHash.BlockHash == nil && blockNrOrHash.BlockNumber == nil: case blockNrOrHash.BlockHash == nil && blockNrOrHash.BlockNumber == nil:
return rpctypes.EthEarliestBlockNumber, fmt.Errorf("types BlockHash and BlockNumber cannot be both nil") return rpctypes.EthEarliestBlockNumber, fmt.Errorf("types BlockHash and BlockNumber cannot be both nil")
case blockNrOrHash.BlockHash != nil: case blockNrOrHash.BlockHash != nil:
blockHeader, err := e.backend.HeaderByHash(*blockNrOrHash.BlockHash) blockNumber, err := e.backend.GetBlockNumberByHash(*blockNrOrHash.BlockHash)
if err != nil { if err != nil {
return rpctypes.EthEarliestBlockNumber, err return rpctypes.EthEarliestBlockNumber, err
} }
return rpctypes.NewBlockNumber(blockHeader.Number), nil return rpctypes.NewBlockNumber(blockNumber), nil
case blockNrOrHash.BlockNumber != nil: case blockNrOrHash.BlockNumber != nil:
return *blockNrOrHash.BlockNumber, nil return *blockNrOrHash.BlockNumber, nil
default: default: