From 8d77ab3551e965ab6af6dd306d48318a08f12669 Mon Sep 17 00:00:00 2001 From: Arpit Temani Date: Wed, 11 Aug 2021 14:07:32 +0530 Subject: [PATCH] add by hash --- internal/ethapi/api.go | 64 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 63 insertions(+), 1 deletion(-) diff --git a/internal/ethapi/api.go b/internal/ethapi/api.go index 5e4913f7f6..fd64652b4f 100644 --- a/internal/ethapi/api.go +++ b/internal/ethapi/api.go @@ -532,7 +532,7 @@ func NewPublicBlockChainAPI(b Backend) *PublicBlockChainAPI { } // GetTransactionReceipt returns the transaction receipt for the given transaction hash. -func (s *PublicTransactionPoolAPI) GetTransactionReceiptsByBlockNumber(ctx context.Context, blockNr rpc.BlockNumber) ([]map[string]interface{}, error) { +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) @@ -570,6 +570,7 @@ func (s *PublicTransactionPoolAPI) GetTransactionReceiptsByBlockNumber(ctx conte "contractAddress": nil, "logs": receipt.Logs, "logsBloom": receipt.Bloom, + "transactions": []interface{}{tx}, } // Assign receipt status or post state. @@ -585,6 +586,67 @@ func (s *PublicTransactionPoolAPI) GetTransactionReceiptsByBlockNumber(ctx conte 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, + "blockNumber": hexutil.Uint64(block.NumberU64()), + "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) }