fix getTransactionReceipt response (#4590)

pull/4591/head v2023.4.1
Diego Nava 11 months ago committed by GitHub
parent 3e7ff3839f
commit dd65484d18
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 4
      rpc/blockchain.go
  2. 44
      rpc/eth/types.go
  3. 14
      rpc/transaction.go
  4. 26
      rpc/v2/types.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

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

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

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

Loading…
Cancel
Save