github.com/reapchain/go-reapchain@v0.2.15-0.20210609012950-9735c110c705/core/types/receipt.go (about)

     1  // Copyright 2014 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package types
    18  
    19  import (
    20  	"fmt"
    21  	"io"
    22  	"math/big"
    23  
    24  	"github.com/ethereum/go-ethereum/common"
    25  	"github.com/ethereum/go-ethereum/common/hexutil"
    26  	"github.com/ethereum/go-ethereum/rlp"
    27  )
    28  
    29  //go:generate gencodec -type Receipt -field-override receiptMarshaling -out gen_receipt_json.go
    30  
    31  // Receipt represents the results of a transaction.
    32  type Receipt struct {
    33  	// Consensus fields
    34  	PostState         []byte   `json:"root"              gencodec:"required"`
    35  	CumulativeGasUsed *big.Int `json:"cumulativeGasUsed" gencodec:"required"`
    36  	Bloom             Bloom    `json:"logsBloom"         gencodec:"required"`
    37  	Logs              []*Log   `json:"logs"              gencodec:"required"`
    38  
    39  	// Implementation fields (don't reorder!)
    40  	TxHash          common.Hash    `json:"transactionHash" gencodec:"required"`
    41  	ContractAddress common.Address `json:"contractAddress"`
    42  	GasUsed         *big.Int       `json:"gasUsed" gencodec:"required"`
    43  }
    44  
    45  type receiptMarshaling struct {
    46  	PostState         hexutil.Bytes
    47  	CumulativeGasUsed *hexutil.Big
    48  	GasUsed           *hexutil.Big
    49  }
    50  
    51  // NewReceipt creates a barebone transaction receipt, copying the init fields.
    52  func NewReceipt(root []byte, cumulativeGasUsed *big.Int) *Receipt {
    53  	return &Receipt{PostState: common.CopyBytes(root), CumulativeGasUsed: new(big.Int).Set(cumulativeGasUsed)}
    54  }
    55  
    56  // EncodeRLP implements rlp.Encoder, and flattens the consensus fields of a receipt
    57  // into an RLP stream.
    58  func (r *Receipt) EncodeRLP(w io.Writer) error {
    59  	return rlp.Encode(w, []interface{}{r.PostState, r.CumulativeGasUsed, r.Bloom, r.Logs})
    60  }
    61  
    62  // DecodeRLP implements rlp.Decoder, and loads the consensus fields of a receipt
    63  // from an RLP stream.
    64  func (r *Receipt) DecodeRLP(s *rlp.Stream) error {
    65  	var receipt struct {
    66  		PostState         []byte
    67  		CumulativeGasUsed *big.Int
    68  		Bloom             Bloom
    69  		Logs              []*Log
    70  	}
    71  	if err := s.Decode(&receipt); err != nil {
    72  		return err
    73  	}
    74  	r.PostState, r.CumulativeGasUsed, r.Bloom, r.Logs = receipt.PostState, receipt.CumulativeGasUsed, receipt.Bloom, receipt.Logs
    75  	return nil
    76  }
    77  
    78  // String implements the Stringer interface.
    79  func (r *Receipt) String() string {
    80  	return fmt.Sprintf("receipt{med=%x cgas=%v bloom=%x logs=%v}", r.PostState, r.CumulativeGasUsed, r.Bloom, r.Logs)
    81  }
    82  
    83  // ReceiptForStorage is a wrapper around a Receipt that flattens and parses the
    84  // entire content of a receipt, as opposed to only the consensus fields originally.
    85  type ReceiptForStorage Receipt
    86  
    87  // EncodeRLP implements rlp.Encoder, and flattens all content fields of a receipt
    88  // into an RLP stream.
    89  func (r *ReceiptForStorage) EncodeRLP(w io.Writer) error {
    90  	logs := make([]*LogForStorage, len(r.Logs))
    91  	for i, log := range r.Logs {
    92  		logs[i] = (*LogForStorage)(log)
    93  	}
    94  	return rlp.Encode(w, []interface{}{r.PostState, r.CumulativeGasUsed, r.Bloom, r.TxHash, r.ContractAddress, logs, r.GasUsed})
    95  }
    96  
    97  // DecodeRLP implements rlp.Decoder, and loads both consensus and implementation
    98  // fields of a receipt from an RLP stream.
    99  func (r *ReceiptForStorage) DecodeRLP(s *rlp.Stream) error {
   100  	var receipt struct {
   101  		PostState         []byte
   102  		CumulativeGasUsed *big.Int
   103  		Bloom             Bloom
   104  		TxHash            common.Hash
   105  		ContractAddress   common.Address
   106  		Logs              []*LogForStorage
   107  		GasUsed           *big.Int
   108  	}
   109  	if err := s.Decode(&receipt); err != nil {
   110  		return err
   111  	}
   112  	// Assign the consensus fields
   113  	r.PostState, r.CumulativeGasUsed, r.Bloom = receipt.PostState, receipt.CumulativeGasUsed, receipt.Bloom
   114  	r.Logs = make([]*Log, len(receipt.Logs))
   115  	for i, log := range receipt.Logs {
   116  		r.Logs[i] = (*Log)(log)
   117  	}
   118  	// Assign the implementation fields
   119  	r.TxHash, r.ContractAddress, r.GasUsed = receipt.TxHash, receipt.ContractAddress, receipt.GasUsed
   120  
   121  	return nil
   122  }
   123  
   124  // Receipts is a wrapper around a Receipt array to implement DerivableList.
   125  type Receipts []*Receipt
   126  
   127  // Len returns the number of receipts in this list.
   128  func (r Receipts) Len() int { return len(r) }
   129  
   130  // GetRlp returns the RLP encoding of one receipt from the list.
   131  func (r Receipts) GetRlp(i int) []byte {
   132  	bytes, err := rlp.EncodeToBytes(r[i])
   133  	if err != nil {
   134  		panic(err)
   135  	}
   136  	return bytes
   137  }