1717package types
1818
1919import (
20- "bytes"
2120 "fmt"
2221 "io"
2322 "math/big"
@@ -27,89 +26,116 @@ import (
2726 "github.com/ethereum/go-ethereum/rlp"
2827)
2928
29+ // Receipt represents the results of a transaction.
3030type Receipt struct {
31+ // Consensus fields
3132 PostState []byte
3233 CumulativeGasUsed * big.Int
3334 Bloom Bloom
34- TxHash common.Hash
35- ContractAddress common.Address
36- logs vm.Logs
37- GasUsed * big.Int
35+ Logs vm.Logs
36+
37+ // Implementation fields
38+ TxHash common.Hash
39+ ContractAddress common.Address
40+ GasUsed * big.Int
3841}
3942
43+ // NewReceipt creates a barebone transaction receipt, copying the init fields.
4044func NewReceipt (root []byte , cumalativeGasUsed * big.Int ) * Receipt {
4145 return & Receipt {PostState : common .CopyBytes (root ), CumulativeGasUsed : new (big.Int ).Set (cumalativeGasUsed )}
4246}
4347
44- func (self * Receipt ) SetLogs (logs vm.Logs ) {
45- self .logs = logs
46- }
47-
48- func (self * Receipt ) Logs () vm.Logs {
49- return self .logs
50- }
51-
52- func (self * Receipt ) EncodeRLP (w io.Writer ) error {
53- return rlp .Encode (w , []interface {}{self .PostState , self .CumulativeGasUsed , self .Bloom , self .logs })
48+ // EncodeRLP implements rlp.Encoder, and flattens the consensus fields of a receipt
49+ // into an RLP stream.
50+ func (r * Receipt ) EncodeRLP (w io.Writer ) error {
51+ return rlp .Encode (w , []interface {}{r .PostState , r .CumulativeGasUsed , r .Bloom , r .Logs })
5452}
5553
56- func (self * Receipt ) DecodeRLP (s * rlp.Stream ) error {
57- var r struct {
54+ // DecodeRLP implements rlp.Decoder, and loads the consensus fields of a receipt
55+ // from an RLP stream.
56+ func (r * Receipt ) DecodeRLP (s * rlp.Stream ) error {
57+ var receipt struct {
5858 PostState []byte
5959 CumulativeGasUsed * big.Int
6060 Bloom Bloom
61- TxHash common.Hash
62- ContractAddress common.Address
6361 Logs vm.Logs
64- GasUsed * big.Int
6562 }
66- if err := s .Decode (& r ); err != nil {
63+ if err := s .Decode (& receipt ); err != nil {
6764 return err
6865 }
69- self .PostState , self .CumulativeGasUsed , self .Bloom , self .TxHash , self .ContractAddress , self .logs , self .GasUsed = r .PostState , r .CumulativeGasUsed , r .Bloom , r .TxHash , r .ContractAddress , r .Logs , r .GasUsed
70-
66+ r .PostState , r .CumulativeGasUsed , r .Bloom , r .Logs = receipt .PostState , receipt .CumulativeGasUsed , receipt .Bloom , receipt .Logs
7167 return nil
7268}
7369
74- type ReceiptForStorage Receipt
75-
76- func (self * ReceiptForStorage ) EncodeRLP (w io.Writer ) error {
77- storageLogs := make ([]* vm.LogForStorage , len (self .logs ))
78- for i , log := range self .logs {
79- storageLogs [i ] = (* vm .LogForStorage )(log )
80- }
81- return rlp .Encode (w , []interface {}{self .PostState , self .CumulativeGasUsed , self .Bloom , self .TxHash , self .ContractAddress , storageLogs , self .GasUsed })
82- }
83-
84- func (self * Receipt ) RlpEncode () []byte {
85- bytes , err := rlp .EncodeToBytes (self )
70+ // RlpEncode implements common.RlpEncode required for SHA derivation.
71+ func (r * Receipt ) RlpEncode () []byte {
72+ bytes , err := rlp .EncodeToBytes (r )
8673 if err != nil {
87- fmt . Println ( "TMP -- RECEIPT ENCODE ERROR" , err )
74+ panic ( err )
8875 }
8976 return bytes
9077}
9178
92- func ( self * Receipt ) Cmp ( other * Receipt ) bool {
93- if bytes . Compare ( self . PostState , other . PostState ) != 0 {
94- return false
95- }
79+ // String implements the Stringer interface.
80+ func ( r * Receipt ) String () string {
81+ return fmt . Sprintf ( "receipt{med=%x cgas=%v bloom=%x logs=%v}" , r . PostState , r . CumulativeGasUsed , r . Bloom , r . Logs )
82+ }
9683
97- return true
84+ // ReceiptForStorage is a wrapper around a Receipt that flattens and parses the
85+ // entire content of a receipt, opposed to only the consensus fields originally.
86+ type ReceiptForStorage Receipt
87+
88+ // EncodeRLP implements rlp.Encoder, and flattens all content fields of a receipt
89+ // into an RLP stream.
90+ func (r * ReceiptForStorage ) EncodeRLP (w io.Writer ) error {
91+ logs := make ([]* vm.LogForStorage , len (r .Logs ))
92+ for i , log := range r .Logs {
93+ logs [i ] = (* vm .LogForStorage )(log )
94+ }
95+ return rlp .Encode (w , []interface {}{r .PostState , r .CumulativeGasUsed , r .Bloom , r .TxHash , r .ContractAddress , logs , r .GasUsed })
9896}
9997
100- func (self * Receipt ) String () string {
101- return fmt .Sprintf ("receipt{med=%x cgas=%v bloom=%x logs=%v}" , self .PostState , self .CumulativeGasUsed , self .Bloom , self .logs )
98+ // DecodeRLP implements rlp.Decoder, and loads the consensus fields of a receipt
99+ // from an RLP stream.
100+ func (r * ReceiptForStorage ) DecodeRLP (s * rlp.Stream ) error {
101+ var receipt struct {
102+ PostState []byte
103+ CumulativeGasUsed * big.Int
104+ Bloom Bloom
105+ TxHash common.Hash
106+ ContractAddress common.Address
107+ Logs []* vm.LogForStorage
108+ GasUsed * big.Int
109+ }
110+ if err := s .Decode (& receipt ); err != nil {
111+ return err
112+ }
113+ // Assign the consensus fields
114+ r .PostState , r .CumulativeGasUsed , r .Bloom = receipt .PostState , receipt .CumulativeGasUsed , receipt .Bloom
115+ r .Logs = make (vm.Logs , len (receipt .Logs ))
116+ for i , log := range receipt .Logs {
117+ r .Logs [i ] = (* vm .Log )(log )
118+ }
119+ // Assign the implementation fields
120+ r .TxHash , r .ContractAddress , r .GasUsed = receipt .TxHash , receipt .ContractAddress , receipt .GasUsed
121+
122+ return nil
102123}
103124
125+ // Receipts is a wrapper around a Receipt array to implement types.DerivableList.
104126type Receipts []* Receipt
105127
106- func (self Receipts ) RlpEncode () []byte {
107- bytes , err := rlp .EncodeToBytes (self )
128+ // RlpEncode implements common.RlpEncode required for SHA derivation.
129+ func (r Receipts ) RlpEncode () []byte {
130+ bytes , err := rlp .EncodeToBytes (r )
108131 if err != nil {
109- fmt . Println ( "TMP -- RECEIPTS ENCODE ERROR" , err )
132+ panic ( err )
110133 }
111134 return bytes
112135}
113136
114- func (self Receipts ) Len () int { return len (self ) }
115- func (self Receipts ) GetRlp (i int ) []byte { return common .Rlp (self [i ]) }
137+ // Len returns the number of receipts in this list.
138+ func (r Receipts ) Len () int { return len (r ) }
139+
140+ // GetRlp returns the RLP encoding of one receipt from the list.
141+ func (r Receipts ) GetRlp (i int ) []byte { return common .Rlp (r [i ]) }
0 commit comments