Skip to content

Commit

Permalink
fix getTransactionReceipt response (#4590)
Browse files Browse the repository at this point in the history
  • Loading branch information
diego1q2w authored Dec 18, 2023
1 parent 3e7ff38 commit dd65484
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 21 deletions.
4 changes: 2 additions & 2 deletions rpc/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
44 changes: 44 additions & 0 deletions rpc/eth/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
14 changes: 11 additions & 3 deletions rpc/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 10 additions & 16 deletions rpc/v2/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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()
Expand All @@ -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
}
}

Expand Down

0 comments on commit dd65484

Please sign in to comment.