From 01c967146c3a2de06cb38fec7ea68a3c393e8795 Mon Sep 17 00:00:00 2001 From: Arpit Temani Date: Wed, 11 Aug 2021 18:51:10 +0530 Subject: [PATCH] fix review comments --- ethclient/ethclient.go | 21 +--------- internal/ethapi/api.go | 79 +++++-------------------------------- internal/jsre/deps/web3.js | 13 ++---- internal/web3ext/web3ext.go | 2 +- 4 files changed, 16 insertions(+), 99 deletions(-) diff --git a/ethclient/ethclient.go b/ethclient/ethclient.go index 73c00de86e..daba37b666 100644 --- a/ethclient/ethclient.go +++ b/ethclient/ethclient.go @@ -70,32 +70,15 @@ func (ec *Client) ChainID(ctx context.Context) (*big.Int, error) { return (*big.Int)(&result), err } -func (ec *Client) TransactionReceiptsInBlockByBlockNumber(ctx context.Context, number *big.Int) ([]*types.Receipt, error) { +func (ec *Client) TransactionReceiptsInBlockByBlockNumber(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) ([]*types.Receipt, error) { var rs []*types.Receipt - err := ec.c.CallContext(ctx, &rs, "eth_getTransactionReceiptsByBlockNumber", toBlockNumArg(number)) + err := ec.c.CallContext(ctx, &rs, "eth_getTransactionReceiptsByBlock", blockNrOrHash) if err != nil { return nil, err } return rs, err } -func (ec *Client) TransactionReceiptsInBlockByBlockHash(ctx context.Context, hash common.Hash) ([]*types.Receipt, error) { - var rs []*types.Receipt - err := ec.c.CallContext(ctx, &rs, "eth_getTransactionReceiptsByBlockHash", hash) - if err != nil { - return nil, err - } - return rs, err -} - -// BlockByHash returns the given full block. -// -// Note that loading full blocks requires two requests. Use HeaderByHash -// if you don't need all transactions or uncle headers. -func (ec *Client) BlockByHash(ctx context.Context, hash common.Hash) (*types.Block, error) { - return ec.getBlock(ctx, "eth_getBlockByHash", hash, true) -} - // BlockByNumber returns a block from the current canonical chain. If number is nil, the // latest known block is returned. // diff --git a/internal/ethapi/api.go b/internal/ethapi/api.go index fd64652b4f..c759b307dc 100644 --- a/internal/ethapi/api.go +++ b/internal/ethapi/api.go @@ -531,19 +531,18 @@ func NewPublicBlockChainAPI(b Backend) *PublicBlockChainAPI { return &PublicBlockChainAPI{b} } -// GetTransactionReceipt returns the transaction receipt for the given transaction hash. -func (s *PublicBlockChainAPI) GetTransactionReceiptsByBlockNumber(ctx context.Context, blockNr rpc.BlockNumber) ([]map[string]interface{}, error) { - blockNumber := uint64(blockNr.Int64()) - blockHash := rawdb.ReadCanonicalHash(s.b.ChainDb(), blockNumber) +// GetTransactionReceiptsByBlock returns the transaction receipts for the given block number or hash. +func (s *PublicBlockChainAPI) GetTransactionReceiptsByBlock(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) ([]map[string]interface{}, error) { + block, err := s.b.BlockByNumberOrHash(ctx, blockNrOrHash) + if err != nil { + return nil, err + } - receipts, err := s.b.GetReceipts(ctx, blockHash) - if err != nil { - return nil, err - } - block, err := s.b.BlockByHash(ctx, blockHash) + receipts, err := s.b.GetReceipts(ctx, block.Hash()) if err != nil { return nil, err } + txs := block.Transactions() if len(txs) != len(receipts) { return nil, fmt.Errorf("txs length doesn't equal to receipts' length") @@ -559,67 +558,7 @@ func (s *PublicBlockChainAPI) GetTransactionReceiptsByBlockNumber(ctx context.Co from, _ := types.Sender(signer, tx) fields := map[string]interface{}{ - "blockHash": blockHash, - "blockNumber": hexutil.Uint64(blockNumber), - "transactionHash": tx.Hash(), - "transactionIndex": hexutil.Uint64(idx), - "from": from, - "to": tx.To(), - "gasUsed": hexutil.Uint64(receipt.GasUsed), - "cumulativeGasUsed": hexutil.Uint64(receipt.CumulativeGasUsed), - "contractAddress": nil, - "logs": receipt.Logs, - "logsBloom": receipt.Bloom, - "transactions": []interface{}{tx}, - } - - // Assign receipt status or post state. - if len(receipt.PostState) > 0 { - fields["root"] = hexutil.Bytes(receipt.PostState) - } else { - fields["status"] = hexutil.Uint(receipt.Status) - } - if receipt.Logs == nil { - fields["logs"] = [][]*types.Log{} - } - // If the ContractAddress is 20 0x0 bytes, assume it is not a contract creation - if receipt.ContractAddress != (common.Address{}) { - fields["contractAddress"] = receipt.ContractAddress - } - fields = s.appendRPCMarshalBorTransaction(ctx, block, fields, true) - - txReceipts = append(txReceipts, fields) - } - - return txReceipts, nil -} - -// GetTransactionReceipt returns the transaction receipt for the given transaction hash. -func (s *PublicBlockChainAPI) GetTransactionReceiptsByBlockHash(ctx context.Context, hash common.Hash) ([]map[string]interface{}, error) { - receipts, err := s.b.GetReceipts(ctx, hash) - if err != nil { - return nil, err - } - block, err := s.b.BlockByHash(ctx, hash) - if err != nil { - return nil, err - } - txs := block.Transactions() - if len(txs) != len(receipts) { - return nil, fmt.Errorf("txs length doesn't equal to receipts' length") - } - - txReceipts := make([]map[string]interface{}, 0, len(txs)) - for idx, receipt := range receipts { - tx := txs[idx] - var signer types.Signer = types.FrontierSigner{} - if tx.Protected() { - signer = types.NewEIP155Signer(tx.ChainId()) - } - from, _ := types.Sender(signer, tx) - - fields := map[string]interface{}{ - "blockHash": hash, + "blockHash": block.Hash(), "blockNumber": hexutil.Uint64(block.NumberU64()), "transactionHash": tx.Hash(), "transactionIndex": hexutil.Uint64(idx), diff --git a/internal/jsre/deps/web3.js b/internal/jsre/deps/web3.js index ed1d16f608..9b52740a59 100644 --- a/internal/jsre/deps/web3.js +++ b/internal/jsre/deps/web3.js @@ -5214,10 +5214,6 @@ var transactionFromBlockCall = function (args) { return (utils.isString(args[0]) && args[0].indexOf('0x') === 0) ? 'eth_getTransactionByBlockHashAndIndex' : 'eth_getTransactionByBlockNumberAndIndex'; }; -var transactionReceiptsFromBlockCall = function (args) { - return (utils.isString(args[0]) && args[0].indexOf('0x') === 0) ? 'eth_getTransactionReceiptsByBlockHash' : 'eth_getTransactionReceiptsByBlockNumber'; -}; - var uncleCall = function (args) { return (utils.isString(args[0]) && args[0].indexOf('0x') === 0) ? 'eth_getUncleByBlockHashAndIndex' : 'eth_getUncleByBlockNumberAndIndex'; }; @@ -5397,11 +5393,10 @@ var methods = function () { inputFormatter: [formatters.inputCallFormatter, formatters.inputDefaultBlockNumberFormatter] }); - var transactionReceiptsByBlock = new Method({ + var getTransactionReceiptsByBlock = new Method({ name: 'getTransactionReceiptsByBlock', - call: transactionReceiptsFromBlockCall, - params: 1, - outputFormatter: formatters.outputTransactionReceiptFormatter + call: 'eth_getTransactionReceiptsByBlock', + params: 1 }); var estimateGas = new Method({ @@ -5466,7 +5461,7 @@ var methods = function () { compileSerpent, submitWork, getWork, - transactionReceiptsByBlock + getTransactionReceiptsByBlock ]; }; diff --git a/internal/web3ext/web3ext.go b/internal/web3ext/web3ext.go index 454134e1b8..fa74f7a19d 100644 --- a/internal/web3ext/web3ext.go +++ b/internal/web3ext/web3ext.go @@ -500,7 +500,7 @@ web3._extend({ return (web3._extend.utils.isString(args[0]) && args[0].indexOf('0x') === 0) ? 'eth_getTransactionReceiptsByBlockHash' : 'eth_getTransactionReceiptsByBlockNumber'; }, params: 1, - inputFormatter: [web3._extend.formatters.inputBlockNumberFormatter, web3._extend.utils.toHex] + inputFormatter: [web3._extend.formatters.inputBlockNumberFormatter] }), new web3._extend.Method({ name: 'chainId',