@@ -82,27 +82,15 @@ var (
8282 },
8383 Type : DynamicFeeTxType ,
8484 }
85- )
86-
87- func TestDecodeEmptyTypedReceipt (t * testing.T ) {
88- input := []byte {0x80 }
89- var r Receipt
90- err := rlp .DecodeBytes (input , & r )
91- if err != errShortTypedReceipt {
92- t .Fatal ("wrong error:" , err )
93- }
94- }
9585
96- // Tests that receipt data can be correctly derived from the contextual infos
97- func TestDeriveFields (t * testing.T ) {
9886 // Create a few transactions to have receipts for
99- to2 : = common .HexToAddress ("0x2" )
100- to3 : = common .HexToAddress ("0x3" )
101- to4 : = common .HexToAddress ("0x4" )
102- to5 : = common .HexToAddress ("0x5" )
103- to6 : = common .HexToAddress ("0x6" )
104- to7 : = common .HexToAddress ("0x7" )
105- txs : = Transactions {
87+ to2 = common .HexToAddress ("0x2" )
88+ to3 = common .HexToAddress ("0x3" )
89+ to4 = common .HexToAddress ("0x4" )
90+ to5 = common .HexToAddress ("0x5" )
91+ to6 = common .HexToAddress ("0x6" )
92+ to7 = common .HexToAddress ("0x7" )
93+ txs = Transactions {
10694 NewTx (& LegacyTx {
10795 Nonce : 1 ,
10896 Value : big .NewInt (1 ),
@@ -161,18 +149,19 @@ func TestDeriveFields(t *testing.T) {
161149 }),
162150 }
163151
164- blockNumber : = big .NewInt (1 )
165- blockTime : = uint64 (2 )
166- blockHash : = common .BytesToHash ([]byte {0x03 , 0x14 })
152+ blockNumber = big .NewInt (1 )
153+ blockTime = uint64 (2 )
154+ blockHash = common .BytesToHash ([]byte {0x03 , 0x14 })
167155
168156 // Create the corresponding receipts
169- receipts : = Receipts {
157+ receipts = Receipts {
170158 & Receipt {
171159 Status : ReceiptStatusFailed ,
172160 CumulativeGasUsed : 1 ,
173161 Logs : []* Log {
174162 {
175163 Address : common .BytesToAddress ([]byte {0x11 }),
164+ Topics : []common.Hash {common .HexToHash ("dead" ), common .HexToHash ("beef" )},
176165 // derived fields:
177166 BlockNumber : blockNumber .Uint64 (),
178167 TxHash : txs [0 ].Hash (),
@@ -182,6 +171,7 @@ func TestDeriveFields(t *testing.T) {
182171 },
183172 {
184173 Address : common .BytesToAddress ([]byte {0x01 , 0x11 }),
174+ Topics : []common.Hash {common .HexToHash ("dead" ), common .HexToHash ("beef" )},
185175 // derived fields:
186176 BlockNumber : blockNumber .Uint64 (),
187177 TxHash : txs [0 ].Hash (),
@@ -205,6 +195,7 @@ func TestDeriveFields(t *testing.T) {
205195 Logs : []* Log {
206196 {
207197 Address : common .BytesToAddress ([]byte {0x22 }),
198+ Topics : []common.Hash {common .HexToHash ("dead" ), common .HexToHash ("beef" )},
208199 // derived fields:
209200 BlockNumber : blockNumber .Uint64 (),
210201 TxHash : txs [1 ].Hash (),
@@ -214,6 +205,7 @@ func TestDeriveFields(t *testing.T) {
214205 },
215206 {
216207 Address : common .BytesToAddress ([]byte {0x02 , 0x22 }),
208+ Topics : []common.Hash {common .HexToHash ("dead" ), common .HexToHash ("beef" )},
217209 // derived fields:
218210 BlockNumber : blockNumber .Uint64 (),
219211 TxHash : txs [1 ].Hash (),
@@ -296,7 +288,19 @@ func TestDeriveFields(t *testing.T) {
296288 TransactionIndex : 6 ,
297289 },
298290 }
291+ )
292+
293+ func TestDecodeEmptyTypedReceipt (t * testing.T ) {
294+ input := []byte {0x80 }
295+ var r Receipt
296+ err := rlp .DecodeBytes (input , & r )
297+ if err != errShortTypedReceipt {
298+ t .Fatal ("wrong error:" , err )
299+ }
300+ }
299301
302+ // Tests that receipt data can be correctly derived from the contextual infos
303+ func TestDeriveFields (t * testing.T ) {
300304 // Re-derive receipts.
301305 basefee := big .NewInt (1000 )
302306 derivedReceipts := clearComputedFieldsOnReceipts (receipts )
@@ -310,6 +314,7 @@ func TestDeriveFields(t *testing.T) {
310314 if err != nil {
311315 t .Fatal ("error marshaling input receipts:" , err )
312316 }
317+
313318 r2 , err := json .MarshalIndent (derivedReceipts , "" , " " )
314319 if err != nil {
315320 t .Fatal ("error marshaling derived receipts:" , err )
@@ -320,6 +325,38 @@ func TestDeriveFields(t *testing.T) {
320325 }
321326}
322327
328+ // Test that we can marshal/unmarshal receipts to/from json without errors.
329+ // This also confirms that our test receipts contain all the required fields.
330+ func TestReceiptJSON (t * testing.T ) {
331+ for i := range receipts {
332+ b , err := receipts [i ].MarshalJSON ()
333+ if err != nil {
334+ t .Fatal ("error marshaling receipt to json:" , err )
335+ }
336+ r := Receipt {}
337+ err = r .UnmarshalJSON (b )
338+ if err != nil {
339+ t .Fatal ("error unmarshaling receipt from json:" , err )
340+ }
341+ }
342+ }
343+
344+ // Test we can still parse receipt without EffectiveGasPrice for backwards compatibility, even
345+ // though it is required per the spec.
346+ func TestEffectiveGasPriceNotRequired (t * testing.T ) {
347+ r := * receipts [0 ]
348+ r .EffectiveGasPrice = nil
349+ b , err := r .MarshalJSON ()
350+ if err != nil {
351+ t .Fatal ("error marshaling receipt to json:" , err )
352+ }
353+ r2 := Receipt {}
354+ err = r2 .UnmarshalJSON (b )
355+ if err != nil {
356+ t .Fatal ("error unmarshaling receipt from json:" , err )
357+ }
358+ }
359+
323360// TestTypedReceiptEncodingDecoding reproduces a flaw that existed in the receipt
324361// rlp decoder, which failed due to a shadowing error.
325362func TestTypedReceiptEncodingDecoding (t * testing.T ) {
0 commit comments