@@ -29,6 +29,59 @@ import (
29
29
"github.com/ethereum/go-ethereum/rlp"
30
30
)
31
31
32
+ var (
33
+ legacyReceipt = & Receipt {
34
+ Status : ReceiptStatusFailed ,
35
+ CumulativeGasUsed : 1 ,
36
+ Logs : []* Log {
37
+ {
38
+ Address : common .BytesToAddress ([]byte {0x11 }),
39
+ Topics : []common.Hash {common .HexToHash ("dead" ), common .HexToHash ("beef" )},
40
+ Data : []byte {0x01 , 0x00 , 0xff },
41
+ },
42
+ {
43
+ Address : common .BytesToAddress ([]byte {0x01 , 0x11 }),
44
+ Topics : []common.Hash {common .HexToHash ("dead" ), common .HexToHash ("beef" )},
45
+ Data : []byte {0x01 , 0x00 , 0xff },
46
+ },
47
+ },
48
+ }
49
+ accessListReceipt = & Receipt {
50
+ Status : ReceiptStatusFailed ,
51
+ CumulativeGasUsed : 1 ,
52
+ Logs : []* Log {
53
+ {
54
+ Address : common .BytesToAddress ([]byte {0x11 }),
55
+ Topics : []common.Hash {common .HexToHash ("dead" ), common .HexToHash ("beef" )},
56
+ Data : []byte {0x01 , 0x00 , 0xff },
57
+ },
58
+ {
59
+ Address : common .BytesToAddress ([]byte {0x01 , 0x11 }),
60
+ Topics : []common.Hash {common .HexToHash ("dead" ), common .HexToHash ("beef" )},
61
+ Data : []byte {0x01 , 0x00 , 0xff },
62
+ },
63
+ },
64
+ Type : AccessListTxType ,
65
+ }
66
+ eip1559Receipt = & Receipt {
67
+ Status : ReceiptStatusFailed ,
68
+ CumulativeGasUsed : 1 ,
69
+ Logs : []* Log {
70
+ {
71
+ Address : common .BytesToAddress ([]byte {0x11 }),
72
+ Topics : []common.Hash {common .HexToHash ("dead" ), common .HexToHash ("beef" )},
73
+ Data : []byte {0x01 , 0x00 , 0xff },
74
+ },
75
+ {
76
+ Address : common .BytesToAddress ([]byte {0x01 , 0x11 }),
77
+ Topics : []common.Hash {common .HexToHash ("dead" ), common .HexToHash ("beef" )},
78
+ Data : []byte {0x01 , 0x00 , 0xff },
79
+ },
80
+ },
81
+ Type : DynamicFeeTxType ,
82
+ }
83
+ )
84
+
32
85
func TestDecodeEmptyTypedReceipt (t * testing.T ) {
33
86
input := []byte {0x80 }
34
87
var r Receipt
@@ -312,6 +365,105 @@ func TestTypedReceiptEncodingDecoding(t *testing.T) {
312
365
}
313
366
}
314
367
368
+ func TestReceiptMarshalBinary (t * testing.T ) {
369
+ // Legacy Receipt
370
+ legacyReceipt .Bloom = CreateBloom (Receipts {legacyReceipt })
371
+ have , err := legacyReceipt .MarshalBinary ()
372
+ if err != nil {
373
+ t .Fatalf ("marshal binary error: %v" , err )
374
+ }
375
+ legacyReceipts := Receipts {legacyReceipt }
376
+ buf := new (bytes.Buffer )
377
+ legacyReceipts .EncodeIndex (0 , buf )
378
+ haveEncodeIndex := buf .Bytes ()
379
+ if ! bytes .Equal (have , haveEncodeIndex ) {
380
+ t .Errorf ("BinaryMarshal and EncodeIndex mismatch, got %x want %x" , have , haveEncodeIndex )
381
+ }
382
+ buf .Reset ()
383
+ if err := legacyReceipt .EncodeRLP (buf ); err != nil {
384
+ t .Fatalf ("encode rlp error: %v" , err )
385
+ }
386
+ haveRLPEncode := buf .Bytes ()
387
+ if ! bytes .Equal (have , haveRLPEncode ) {
388
+ t .Errorf ("BinaryMarshal and EncodeRLP mismatch for legacy tx, got %x want %x" , have , haveRLPEncode )
389
+ }
390
+ legacyWant := common .FromHex ("f901c58001b9010000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000500000000000000000000000000000000000014000000000000000000000000000000000000000000000000000000000000000000000000000010000080000000000000000000004000000000000000000000000000040000000000000000000000000000800000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000f8bef85d940000000000000000000000000000000000000011f842a0000000000000000000000000000000000000000000000000000000000000deada0000000000000000000000000000000000000000000000000000000000000beef830100fff85d940000000000000000000000000000000000000111f842a0000000000000000000000000000000000000000000000000000000000000deada0000000000000000000000000000000000000000000000000000000000000beef830100ff" )
391
+ if ! bytes .Equal (have , legacyWant ) {
392
+ t .Errorf ("encoded RLP mismatch, got %x want %x" , have , legacyWant )
393
+ }
394
+
395
+ // 2930 Receipt
396
+ buf .Reset ()
397
+ accessListReceipt .Bloom = CreateBloom (Receipts {accessListReceipt })
398
+ have , err = accessListReceipt .MarshalBinary ()
399
+ if err != nil {
400
+ t .Fatalf ("marshal binary error: %v" , err )
401
+ }
402
+ accessListReceipts := Receipts {accessListReceipt }
403
+ accessListReceipts .EncodeIndex (0 , buf )
404
+ haveEncodeIndex = buf .Bytes ()
405
+ if ! bytes .Equal (have , haveEncodeIndex ) {
406
+ t .Errorf ("BinaryMarshal and EncodeIndex mismatch, got %x want %x" , have , haveEncodeIndex )
407
+ }
408
+ accessListWant := common .FromHex ("01f901c58001b9010000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000500000000000000000000000000000000000014000000000000000000000000000000000000000000000000000000000000000000000000000010000080000000000000000000004000000000000000000000000000040000000000000000000000000000800000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000f8bef85d940000000000000000000000000000000000000011f842a0000000000000000000000000000000000000000000000000000000000000deada0000000000000000000000000000000000000000000000000000000000000beef830100fff85d940000000000000000000000000000000000000111f842a0000000000000000000000000000000000000000000000000000000000000deada0000000000000000000000000000000000000000000000000000000000000beef830100ff" )
409
+ if ! bytes .Equal (have , accessListWant ) {
410
+ t .Errorf ("encoded RLP mismatch, got %x want %x" , have , accessListWant )
411
+ }
412
+
413
+ // 1559 Receipt
414
+ buf .Reset ()
415
+ eip1559Receipt .Bloom = CreateBloom (Receipts {eip1559Receipt })
416
+ have , err = eip1559Receipt .MarshalBinary ()
417
+ if err != nil {
418
+ t .Fatalf ("marshal binary error: %v" , err )
419
+ }
420
+ eip1559Receipts := Receipts {eip1559Receipt }
421
+ eip1559Receipts .EncodeIndex (0 , buf )
422
+ haveEncodeIndex = buf .Bytes ()
423
+ if ! bytes .Equal (have , haveEncodeIndex ) {
424
+ t .Errorf ("BinaryMarshal and EncodeIndex mismatch, got %x want %x" , have , haveEncodeIndex )
425
+ }
426
+ eip1559Want := common .FromHex ("02f901c58001b9010000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000500000000000000000000000000000000000014000000000000000000000000000000000000000000000000000000000000000000000000000010000080000000000000000000004000000000000000000000000000040000000000000000000000000000800000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000f8bef85d940000000000000000000000000000000000000011f842a0000000000000000000000000000000000000000000000000000000000000deada0000000000000000000000000000000000000000000000000000000000000beef830100fff85d940000000000000000000000000000000000000111f842a0000000000000000000000000000000000000000000000000000000000000deada0000000000000000000000000000000000000000000000000000000000000beef830100ff" )
427
+ if ! bytes .Equal (have , eip1559Want ) {
428
+ t .Errorf ("encoded RLP mismatch, got %x want %x" , have , eip1559Want )
429
+ }
430
+ }
431
+
432
+ func TestReceiptUnmarshalBinary (t * testing.T ) {
433
+ // Legacy Receipt
434
+ legacyBinary := common .FromHex ("f901c58001b9010000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000500000000000000000000000000000000000014000000000000000000000000000000000000000000000000000000000000000000000000000010000080000000000000000000004000000000000000000000000000040000000000000000000000000000800000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000f8bef85d940000000000000000000000000000000000000011f842a0000000000000000000000000000000000000000000000000000000000000deada0000000000000000000000000000000000000000000000000000000000000beef830100fff85d940000000000000000000000000000000000000111f842a0000000000000000000000000000000000000000000000000000000000000deada0000000000000000000000000000000000000000000000000000000000000beef830100ff" )
435
+ gotLegacyReceipt := new (Receipt )
436
+ if err := gotLegacyReceipt .UnmarshalBinary (legacyBinary ); err != nil {
437
+ t .Fatalf ("unmarshal binary error: %v" , err )
438
+ }
439
+ legacyReceipt .Bloom = CreateBloom (Receipts {legacyReceipt })
440
+ if ! reflect .DeepEqual (gotLegacyReceipt , legacyReceipt ) {
441
+ t .Errorf ("receipt unmarshalled from binary mismatch, got %v want %v" , gotLegacyReceipt , legacyReceipt )
442
+ }
443
+
444
+ // 2930 Receipt
445
+ accessListBinary := common .FromHex ("01f901c58001b9010000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000500000000000000000000000000000000000014000000000000000000000000000000000000000000000000000000000000000000000000000010000080000000000000000000004000000000000000000000000000040000000000000000000000000000800000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000f8bef85d940000000000000000000000000000000000000011f842a0000000000000000000000000000000000000000000000000000000000000deada0000000000000000000000000000000000000000000000000000000000000beef830100fff85d940000000000000000000000000000000000000111f842a0000000000000000000000000000000000000000000000000000000000000deada0000000000000000000000000000000000000000000000000000000000000beef830100ff" )
446
+ gotAccessListReceipt := new (Receipt )
447
+ if err := gotAccessListReceipt .UnmarshalBinary (accessListBinary ); err != nil {
448
+ t .Fatalf ("unmarshal binary error: %v" , err )
449
+ }
450
+ accessListReceipt .Bloom = CreateBloom (Receipts {accessListReceipt })
451
+ if ! reflect .DeepEqual (gotAccessListReceipt , accessListReceipt ) {
452
+ t .Errorf ("receipt unmarshalled from binary mismatch, got %v want %v" , gotAccessListReceipt , accessListReceipt )
453
+ }
454
+
455
+ // 1559 Receipt
456
+ eip1559RctBinary := common .FromHex ("02f901c58001b9010000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000500000000000000000000000000000000000014000000000000000000000000000000000000000000000000000000000000000000000000000010000080000000000000000000004000000000000000000000000000040000000000000000000000000000800000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000f8bef85d940000000000000000000000000000000000000011f842a0000000000000000000000000000000000000000000000000000000000000deada0000000000000000000000000000000000000000000000000000000000000beef830100fff85d940000000000000000000000000000000000000111f842a0000000000000000000000000000000000000000000000000000000000000deada0000000000000000000000000000000000000000000000000000000000000beef830100ff" )
457
+ got1559Receipt := new (Receipt )
458
+ if err := got1559Receipt .UnmarshalBinary (eip1559RctBinary ); err != nil {
459
+ t .Fatalf ("unmarshal binary error: %v" , err )
460
+ }
461
+ eip1559Receipt .Bloom = CreateBloom (Receipts {eip1559Receipt })
462
+ if ! reflect .DeepEqual (got1559Receipt , eip1559Receipt ) {
463
+ t .Errorf ("receipt unmarshalled from binary mismatch, got %v want %v" , got1559Receipt , eip1559Receipt )
464
+ }
465
+ }
466
+
315
467
func clearComputedFieldsOnReceipts (t * testing.T , receipts Receipts ) {
316
468
t .Helper ()
317
469
0 commit comments