1717package types
1818
1919import (
20+ "bytes"
2021 "fmt"
2122 "io"
2223 "math/big"
@@ -28,9 +29,9 @@ import (
2829
2930//go:generate gencodec -type Receipt -field-override receiptMarshaling -out gen_receipt_json.go
3031
31- const (
32- receiptStatusSuccessful = byte ( 0x01 )
33- receiptStatusFailed = byte ( 0x00 )
32+ var (
33+ receiptStatusFailed = [] byte {}
34+ receiptStatusSuccessful = [] byte { 0x01 }
3435)
3536
3637// Receipt represents the results of a transaction.
@@ -54,22 +55,22 @@ type receiptMarshaling struct {
5455 GasUsed * hexutil.Big
5556}
5657
57- // homesteadReceiptRLP contains the receipt's Homestead consensus fields, used
58- // during RLP serialization.
59- type homesteadReceiptRLP struct {
60- PostState []byte
58+ // receiptRLP is the consensus encoding of a receipt.
59+ type receiptRLP struct {
60+ PostStateOrStatus []byte
6161 CumulativeGasUsed * big.Int
6262 Bloom Bloom
6363 Logs []* Log
6464}
6565
66- // metropolisReceiptRLP contains the receipt's Metropolis consensus fields, used
67- // during RLP serialization.
68- type metropolisReceiptRLP struct {
69- Status byte
66+ type receiptStorageRLP struct {
67+ PostStateOrStatus []byte
7068 CumulativeGasUsed * big.Int
7169 Bloom Bloom
72- Logs []* Log
70+ TxHash common.Hash
71+ ContractAddress common.Address
72+ Logs []* LogForStorage
73+ GasUsed * big.Int
7374}
7475
7576// NewReceipt creates a barebone transaction receipt, copying the init fields.
@@ -80,69 +81,46 @@ func NewReceipt(root []byte, failed bool, cumulativeGasUsed *big.Int) *Receipt {
8081// EncodeRLP implements rlp.Encoder, and flattens the consensus fields of a receipt
8182// into an RLP stream. If no post state is present, metropolis fork is assumed.
8283func (r * Receipt ) EncodeRLP (w io.Writer ) error {
83- if r .PostState == nil {
84- status := receiptStatusSuccessful
85- if r .Failed {
86- status = receiptStatusFailed
87- }
88- return rlp .Encode (w , & metropolisReceiptRLP {status , r .CumulativeGasUsed , r .Bloom , r .Logs })
89- }
90- return rlp .Encode (w , & homesteadReceiptRLP {r .PostState , r .CumulativeGasUsed , r .Bloom , r .Logs })
84+ return rlp .Encode (w , & receiptRLP {r .statusEncoding (), r .CumulativeGasUsed , r .Bloom , r .Logs })
9185}
9286
9387// DecodeRLP implements rlp.Decoder, and loads the consensus fields of a receipt
9488// from an RLP stream.
9589func (r * Receipt ) DecodeRLP (s * rlp.Stream ) error {
96- // Load the raw bytes since we have multiple possible formats
97- raw , err := s .Raw ()
98- if err != nil {
90+ var dec receiptRLP
91+ if err := s .Decode (& dec ); err != nil {
9992 return err
10093 }
101- content , _ , err := rlp .SplitList (raw )
102- if err != nil {
94+ if err := r .setStatus (dec .PostStateOrStatus ); err != nil {
10395 return err
10496 }
105- kind , cnt , _ , err := rlp .Split (content )
106- if err != nil {
107- return err
108- }
109- // Deserialize based on the first component type.
110- switch {
111- case kind == rlp .Byte || (kind == rlp .String && len (cnt ) == 0 ):
112- // The first component of metropolis receipts is Byte (0x01), or the empty
113- // string (0x80, decoded as a byte with 0x00 value).
114- var metro metropolisReceiptRLP
115- if err := rlp .DecodeBytes (raw , & metro ); err != nil {
116- return err
117- }
118- switch metro .Status {
119- case receiptStatusSuccessful :
120- r .Failed = false
121- case receiptStatusFailed :
122- r .Failed = true
123- default :
124- return fmt .Errorf ("invalid status byte: 0x%x" , metro .Status )
125- }
126- r .CumulativeGasUsed = metro .CumulativeGasUsed
127- r .Bloom = metro .Bloom
128- r .Logs = metro .Logs
129- return nil
130-
131- case kind == rlp .String :
132- // The first component of homestead receipts is non-empty String.
133- var home homesteadReceiptRLP
134- if err := rlp .DecodeBytes (raw , & home ); err != nil {
135- return err
136- }
137- r .PostState = home .PostState [:]
138- r .CumulativeGasUsed = home .CumulativeGasUsed
139- r .Bloom = home .Bloom
140- r .Logs = home .Logs
141- return nil
97+ r .CumulativeGasUsed , r .Bloom , r .Logs = dec .CumulativeGasUsed , dec .Bloom , dec .Logs
98+ return nil
99+ }
142100
101+ func (r * Receipt ) setStatus (postStateOrStatus []byte ) error {
102+ switch {
103+ case bytes .Equal (postStateOrStatus , receiptStatusSuccessful ):
104+ r .Failed = false
105+ case bytes .Equal (postStateOrStatus , receiptStatusFailed ):
106+ r .Failed = true
107+ case len (postStateOrStatus ) == len (common.Hash {}):
108+ r .PostState = postStateOrStatus
143109 default :
144- return fmt .Errorf ("invalid first receipt component: %v " , kind )
110+ return fmt .Errorf ("invalid receipt status %x " , postStateOrStatus )
145111 }
112+ return nil
113+ }
114+
115+ func (r * Receipt ) statusEncoding () []byte {
116+ if len (r .PostState ) == 0 {
117+ if r .Failed {
118+ return receiptStatusFailed
119+ } else {
120+ return receiptStatusSuccessful
121+ }
122+ }
123+ return r .PostState
146124}
147125
148126// String implements the Stringer interface.
@@ -160,38 +138,39 @@ type ReceiptForStorage Receipt
160138// EncodeRLP implements rlp.Encoder, and flattens all content fields of a receipt
161139// into an RLP stream.
162140func (r * ReceiptForStorage ) EncodeRLP (w io.Writer ) error {
163- logs := make ([]* LogForStorage , len (r .Logs ))
141+ enc := & receiptStorageRLP {
142+ PostStateOrStatus : (* Receipt )(r ).statusEncoding (),
143+ CumulativeGasUsed : r .CumulativeGasUsed ,
144+ Bloom : r .Bloom ,
145+ TxHash : r .TxHash ,
146+ ContractAddress : r .ContractAddress ,
147+ Logs : make ([]* LogForStorage , len (r .Logs )),
148+ GasUsed : r .GasUsed ,
149+ }
164150 for i , log := range r .Logs {
165- logs [i ] = (* LogForStorage )(log )
151+ enc . Logs [i ] = (* LogForStorage )(log )
166152 }
167- return rlp .Encode (w , [] interface {}{ r . PostState , r . Failed , r . CumulativeGasUsed , r . Bloom , r . TxHash , r . ContractAddress , logs , r . GasUsed } )
153+ return rlp .Encode (w , enc )
168154}
169155
170156// DecodeRLP implements rlp.Decoder, and loads both consensus and implementation
171157// fields of a receipt from an RLP stream.
172158func (r * ReceiptForStorage ) DecodeRLP (s * rlp.Stream ) error {
173- var receipt struct {
174- PostState []byte
175- Failed bool
176- CumulativeGasUsed * big.Int
177- Bloom Bloom
178- TxHash common.Hash
179- ContractAddress common.Address
180- Logs []* LogForStorage
181- GasUsed * big.Int
159+ var dec receiptStorageRLP
160+ if err := s .Decode (& dec ); err != nil {
161+ return err
182162 }
183- if err := s . Decode ( & receipt ); err != nil {
163+ if err := ( * Receipt )( r ). setStatus ( dec . PostStateOrStatus ); err != nil {
184164 return err
185165 }
186166 // Assign the consensus fields
187- r .PostState , r . Failed , r . CumulativeGasUsed , r .Bloom = receipt . PostState , receipt . Failed , receipt . CumulativeGasUsed , receipt .Bloom
188- r .Logs = make ([]* Log , len (receipt .Logs ))
189- for i , log := range receipt .Logs {
167+ r .CumulativeGasUsed , r .Bloom = dec . CumulativeGasUsed , dec .Bloom
168+ r .Logs = make ([]* Log , len (dec .Logs ))
169+ for i , log := range dec .Logs {
190170 r .Logs [i ] = (* Log )(log )
191171 }
192172 // Assign the implementation fields
193- r .TxHash , r .ContractAddress , r .GasUsed = receipt .TxHash , receipt .ContractAddress , receipt .GasUsed
194-
173+ r .TxHash , r .ContractAddress , r .GasUsed = dec .TxHash , dec .ContractAddress , dec .GasUsed
195174 return nil
196175}
197176
0 commit comments