From dd65484d186f8c86aa7560f7bd22ea6ccd39b7f7 Mon Sep 17 00:00:00 2001 From: Diego Nava <8563843+diego1q2w@users.noreply.github.com> Date: Mon, 18 Dec 2023 20:42:19 +0100 Subject: [PATCH] fix getTransactionReceipt response (#4590) --- rpc/blockchain.go | 4 ++-- rpc/eth/types.go | 44 ++++++++++++++++++++++++++++++++++++++++++++ rpc/transaction.go | 14 +++++++++++--- rpc/v2/types.go | 26 ++++++++++---------------- 4 files changed, 67 insertions(+), 21 deletions(-) diff --git a/rpc/blockchain.go b/rpc/blockchain.go index e830b30f0..284bff5af 100644 --- a/rpc/blockchain.go +++ b/rpc/blockchain.go @@ -461,10 +461,10 @@ func (s *PublicBlockchainService) GetBlockReceipts( case V1: r, err = v1.NewReceipt(tx, blockHash, block.NumberU64(), index, rmap[tx.Hash()]) case V2: - r, err = v2.NewReceipt(tx, blockHash, block.NumberU64(), index, rmap[tx.Hash()], false) + r, err = v2.NewReceipt(tx, blockHash, block.NumberU64(), index, rmap[tx.Hash()]) case Eth: if tx, ok := tx.(*types.Transaction); ok { - r, err = v2.NewReceipt(tx, blockHash, block.NumberU64(), index, rmap[tx.Hash()], true) + r, err = v2.NewReceipt(tx, blockHash, block.NumberU64(), index, rmap[tx.Hash()]) } default: return nil, ErrUnknownRPCVersion diff --git a/rpc/eth/types.go b/rpc/eth/types.go index e3a64eb7f..2767984cd 100644 --- a/rpc/eth/types.go +++ b/rpc/eth/types.go @@ -155,6 +155,50 @@ func NewReceipt(tx *types.EthTransaction, blockHash common.Hash, blockNumber, bl return fields, nil } +// NewReceiptFromTransaction returns the RPC data for a new receipt. It is unused at the moment. +func NewReceiptFromTransaction(tx *types.Transaction, blockHash common.Hash, blockNumber, blockIndex uint64, receipt *types.Receipt) (map[string]interface{}, error) { + senderAddr, err := tx.SenderAddress() + if err != nil { + return nil, err + } + + ethTxHash := tx.Hash() + for i, _ := range receipt.Logs { + // Override log txHash with receipt's + receipt.Logs[i].TxHash = ethTxHash + } + + fields := map[string]interface{}{ + "blockHash": blockHash, + "blockNumber": hexutil.Uint64(blockNumber), + "transactionHash": ethTxHash, + "transactionIndex": hexutil.Uint64(blockIndex), + "from": senderAddr, + "to": tx.To(), + "gasUsed": hexutil.Uint64(receipt.GasUsed), + "cumulativeGasUsed": hexutil.Uint64(receipt.CumulativeGasUsed), + "contractAddress": nil, + "logs": receipt.Logs, + "logsBloom": receipt.Bloom, + } + + // 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 + } + + return fields, nil +} + func newBlock(b *types.Block) *Block { head := b.Header() diff --git a/rpc/transaction.go b/rpc/transaction.go index ef30092b2..53f8f4767 100644 --- a/rpc/transaction.go +++ b/rpc/transaction.go @@ -751,11 +751,19 @@ func (s *PublicTransactionService) GetTransactionReceipt( return nil, err } return NewStructuredResponse(RPCReceipt) - case V2, Eth: + case V2: if tx == nil { - RPCReceipt, err = v2.NewReceipt(stx, blockHash, blockNumber, index, receipt, false) + RPCReceipt, err = v2.NewReceipt(stx, blockHash, blockNumber, index, receipt) } else { - RPCReceipt, err = v2.NewReceipt(tx, blockHash, blockNumber, index, receipt, s.version == Eth) + RPCReceipt, err = v2.NewReceipt(tx, blockHash, blockNumber, index, receipt) + } + if err != nil { + return nil, err + } + return NewStructuredResponse(RPCReceipt) + case Eth: + if tx != nil { + RPCReceipt, err = eth.NewReceiptFromTransaction(tx, blockHash, blockNumber, index, receipt) } if err != nil { return nil, err diff --git a/rpc/v2/types.go b/rpc/v2/types.go index d2b21627c..76f36ad40 100644 --- a/rpc/v2/types.go +++ b/rpc/v2/types.go @@ -334,11 +334,11 @@ func NewTransaction( // NewReceipt returns a transaction OR staking transaction that will serialize to the RPC representation func NewReceipt( - tx interface{}, blockHash common.Hash, blockNumber, blockIndex uint64, receipt *types.Receipt, eth bool, + tx interface{}, blockHash common.Hash, blockNumber, blockIndex uint64, receipt *types.Receipt, ) (interface{}, error) { plainTx, ok := tx.(*types.Transaction) if ok { - return NewTxReceipt(plainTx, blockHash, blockNumber, blockIndex, receipt, eth) + return NewTxReceipt(plainTx, blockHash, blockNumber, blockIndex, receipt) } stakingTx, ok := tx.(*staking.StakingTransaction) if ok { @@ -349,7 +349,7 @@ func NewReceipt( // NewTxReceipt returns a plain transaction receipt that will serialize to the RPC representation func NewTxReceipt( - tx *types.Transaction, blockHash common.Hash, blockNumber, blockIndex uint64, receipt *types.Receipt, eth bool, + tx *types.Transaction, blockHash common.Hash, blockNumber, blockIndex uint64, receipt *types.Receipt, ) (*TxReceipt, error) { // Set correct to & from address senderAddr, err := tx.SenderAddress() @@ -362,19 +362,13 @@ func NewTxReceipt( sender = senderAddr.String() receiver = "" } else { - // Handle response type for regular transaction - if eth { - sender = senderAddr.String() - receiver = tx.To().String() - } else { - sender, err = internal_common.AddressToBech32(senderAddr) - if err != nil { - return nil, err - } - receiver, err = internal_common.AddressToBech32(*tx.To()) - if err != nil { - return nil, err - } + sender, err = internal_common.AddressToBech32(senderAddr) + if err != nil { + return nil, err + } + receiver, err = internal_common.AddressToBech32(*tx.To()) + if err != nil { + return nil, err } }