fix review comments

This commit is contained in:
Arpit Temani 2021-08-11 18:51:10 +05:30
parent 42a6c5e6ff
commit 01c967146c
4 changed files with 16 additions and 99 deletions

View file

@ -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.
//

View file

@ -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),

View file

@ -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
];
};

View file

@ -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',