Skip to content

Commit dd65484

Browse files
authored
fix getTransactionReceipt response (#4590)
1 parent 3e7ff38 commit dd65484

File tree

4 files changed

+67
-21
lines changed

4 files changed

+67
-21
lines changed

Diff for: rpc/blockchain.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -461,10 +461,10 @@ func (s *PublicBlockchainService) GetBlockReceipts(
461461
case V1:
462462
r, err = v1.NewReceipt(tx, blockHash, block.NumberU64(), index, rmap[tx.Hash()])
463463
case V2:
464-
r, err = v2.NewReceipt(tx, blockHash, block.NumberU64(), index, rmap[tx.Hash()], false)
464+
r, err = v2.NewReceipt(tx, blockHash, block.NumberU64(), index, rmap[tx.Hash()])
465465
case Eth:
466466
if tx, ok := tx.(*types.Transaction); ok {
467-
r, err = v2.NewReceipt(tx, blockHash, block.NumberU64(), index, rmap[tx.Hash()], true)
467+
r, err = v2.NewReceipt(tx, blockHash, block.NumberU64(), index, rmap[tx.Hash()])
468468
}
469469
default:
470470
return nil, ErrUnknownRPCVersion

Diff for: rpc/eth/types.go

+44
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,50 @@ func NewReceipt(tx *types.EthTransaction, blockHash common.Hash, blockNumber, bl
155155
return fields, nil
156156
}
157157

158+
// NewReceiptFromTransaction returns the RPC data for a new receipt. It is unused at the moment.
159+
func NewReceiptFromTransaction(tx *types.Transaction, blockHash common.Hash, blockNumber, blockIndex uint64, receipt *types.Receipt) (map[string]interface{}, error) {
160+
senderAddr, err := tx.SenderAddress()
161+
if err != nil {
162+
return nil, err
163+
}
164+
165+
ethTxHash := tx.Hash()
166+
for i, _ := range receipt.Logs {
167+
// Override log txHash with receipt's
168+
receipt.Logs[i].TxHash = ethTxHash
169+
}
170+
171+
fields := map[string]interface{}{
172+
"blockHash": blockHash,
173+
"blockNumber": hexutil.Uint64(blockNumber),
174+
"transactionHash": ethTxHash,
175+
"transactionIndex": hexutil.Uint64(blockIndex),
176+
"from": senderAddr,
177+
"to": tx.To(),
178+
"gasUsed": hexutil.Uint64(receipt.GasUsed),
179+
"cumulativeGasUsed": hexutil.Uint64(receipt.CumulativeGasUsed),
180+
"contractAddress": nil,
181+
"logs": receipt.Logs,
182+
"logsBloom": receipt.Bloom,
183+
}
184+
185+
// Assign receipt status or post state.
186+
if len(receipt.PostState) > 0 {
187+
fields["root"] = hexutil.Bytes(receipt.PostState)
188+
} else {
189+
fields["status"] = hexutil.Uint(receipt.Status)
190+
}
191+
if receipt.Logs == nil {
192+
fields["logs"] = [][]*types.Log{}
193+
}
194+
// If the ContractAddress is 20 0x0 bytes, assume it is not a contract creation
195+
if receipt.ContractAddress != (common.Address{}) {
196+
fields["contractAddress"] = receipt.ContractAddress
197+
}
198+
199+
return fields, nil
200+
}
201+
158202
func newBlock(b *types.Block) *Block {
159203
head := b.Header()
160204

Diff for: rpc/transaction.go

+11-3
Original file line numberDiff line numberDiff line change
@@ -751,11 +751,19 @@ func (s *PublicTransactionService) GetTransactionReceipt(
751751
return nil, err
752752
}
753753
return NewStructuredResponse(RPCReceipt)
754-
case V2, Eth:
754+
case V2:
755755
if tx == nil {
756-
RPCReceipt, err = v2.NewReceipt(stx, blockHash, blockNumber, index, receipt, false)
756+
RPCReceipt, err = v2.NewReceipt(stx, blockHash, blockNumber, index, receipt)
757757
} else {
758-
RPCReceipt, err = v2.NewReceipt(tx, blockHash, blockNumber, index, receipt, s.version == Eth)
758+
RPCReceipt, err = v2.NewReceipt(tx, blockHash, blockNumber, index, receipt)
759+
}
760+
if err != nil {
761+
return nil, err
762+
}
763+
return NewStructuredResponse(RPCReceipt)
764+
case Eth:
765+
if tx != nil {
766+
RPCReceipt, err = eth.NewReceiptFromTransaction(tx, blockHash, blockNumber, index, receipt)
759767
}
760768
if err != nil {
761769
return nil, err

Diff for: rpc/v2/types.go

+10-16
Original file line numberDiff line numberDiff line change
@@ -334,11 +334,11 @@ func NewTransaction(
334334

335335
// NewReceipt returns a transaction OR staking transaction that will serialize to the RPC representation
336336
func NewReceipt(
337-
tx interface{}, blockHash common.Hash, blockNumber, blockIndex uint64, receipt *types.Receipt, eth bool,
337+
tx interface{}, blockHash common.Hash, blockNumber, blockIndex uint64, receipt *types.Receipt,
338338
) (interface{}, error) {
339339
plainTx, ok := tx.(*types.Transaction)
340340
if ok {
341-
return NewTxReceipt(plainTx, blockHash, blockNumber, blockIndex, receipt, eth)
341+
return NewTxReceipt(plainTx, blockHash, blockNumber, blockIndex, receipt)
342342
}
343343
stakingTx, ok := tx.(*staking.StakingTransaction)
344344
if ok {
@@ -349,7 +349,7 @@ func NewReceipt(
349349

350350
// NewTxReceipt returns a plain transaction receipt that will serialize to the RPC representation
351351
func NewTxReceipt(
352-
tx *types.Transaction, blockHash common.Hash, blockNumber, blockIndex uint64, receipt *types.Receipt, eth bool,
352+
tx *types.Transaction, blockHash common.Hash, blockNumber, blockIndex uint64, receipt *types.Receipt,
353353
) (*TxReceipt, error) {
354354
// Set correct to & from address
355355
senderAddr, err := tx.SenderAddress()
@@ -362,19 +362,13 @@ func NewTxReceipt(
362362
sender = senderAddr.String()
363363
receiver = ""
364364
} else {
365-
// Handle response type for regular transaction
366-
if eth {
367-
sender = senderAddr.String()
368-
receiver = tx.To().String()
369-
} else {
370-
sender, err = internal_common.AddressToBech32(senderAddr)
371-
if err != nil {
372-
return nil, err
373-
}
374-
receiver, err = internal_common.AddressToBech32(*tx.To())
375-
if err != nil {
376-
return nil, err
377-
}
365+
sender, err = internal_common.AddressToBech32(senderAddr)
366+
if err != nil {
367+
return nil, err
368+
}
369+
receiver, err = internal_common.AddressToBech32(*tx.To())
370+
if err != nil {
371+
return nil, err
378372
}
379373
}
380374

0 commit comments

Comments
 (0)