From 81fc39a9bc1dda28c959edc1303fea20049a68d1 Mon Sep 17 00:00:00 2001 From: Austin Abell Date: Sun, 29 Sep 2019 16:43:26 -0400 Subject: [PATCH] Implement eth_getBlockTransactionCountByHash (#116) --- rpc/eth_api.go | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/rpc/eth_api.go b/rpc/eth_api.go index b83b9b62..de15e99a 100644 --- a/rpc/eth_api.go +++ b/rpc/eth_api.go @@ -172,24 +172,33 @@ func (e *PublicEthAPI) GetTransactionCount(address common.Address, blockNum Bloc } // GetBlockTransactionCountByHash returns the number of transactions in the block identified by hash. -func (e *PublicEthAPI) GetBlockTransactionCountByHash(hash common.Hash) hexutil.Uint { - return 0 +func (e *PublicEthAPI) GetBlockTransactionCountByHash(hash common.Hash) *hexutil.Uint { + res, _, err := e.cliCtx.Query(fmt.Sprintf("custom/%s/%s/%s", types.ModuleName, evm.QueryHashToHeight, hash.Hex())) + if err != nil { + // Return nil if block does not exist + return nil + } + + var out types.QueryResBlockNumber + e.cliCtx.Codec.MustUnmarshalJSON(res, &out) + return e.getBlockTransactionCountByNumber(out.Number) } // GetBlockTransactionCountByNumber returns the number of transactions in the block identified by number. -func (e *PublicEthAPI) GetBlockTransactionCountByNumber(blockNum BlockNumber) (hexutil.Uint, error) { - node, err := e.cliCtx.GetNode() - if err != nil { - return 0, err - } - +func (e *PublicEthAPI) GetBlockTransactionCountByNumber(blockNum BlockNumber) *hexutil.Uint { height := blockNum.Int64() - block, err := node.Block(&height) + return e.getBlockTransactionCountByNumber(height) +} + +func (e *PublicEthAPI) getBlockTransactionCountByNumber(number int64) *hexutil.Uint { + block, err := e.cliCtx.Client.Block(&number) if err != nil { - return 0, err + // Return nil if block doesn't exist + return nil } - return hexutil.Uint(block.Block.NumTxs), nil + n := hexutil.Uint(block.Block.NumTxs) + return &n } // GetUncleCountByBlockHash returns the number of uncles in the block idenfied by hash. Always zero.