@@ -20,6 +20,7 @@ import (
2020 "bytes"
2121 "math"
2222 "math/big"
23+ "reflect"
2324 "testing"
2425
2526 "github.com/ethereum/go-ethereum/common"
@@ -28,6 +29,42 @@ import (
2829 "github.com/ethereum/go-ethereum/rlp"
2930)
3031
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+ )
67+
3168func TestDecodeEmptyTypedReceipt (t * testing.T ) {
3269 input := []byte {0x80 }
3370 var r Receipt
@@ -192,6 +229,76 @@ func TestTypedReceiptEncodingDecoding(t *testing.T) {
192229 }
193230}
194231
232+ func TestReceiptMarshalBinary (t * testing.T ) {
233+ // Legacy Receipt
234+ legacyReceipt .Bloom = CreateBloom (Receipts {legacyReceipt })
235+ have , err := legacyReceipt .MarshalBinary ()
236+ if err != nil {
237+ t .Fatalf ("marshal binary error: %v" , err )
238+ }
239+ legacyReceipts := Receipts {legacyReceipt }
240+ buf := new (bytes.Buffer )
241+ legacyReceipts .EncodeIndex (0 , buf )
242+ haveEncodeIndex := buf .Bytes ()
243+ if ! bytes .Equal (have , haveEncodeIndex ) {
244+ t .Errorf ("BinaryMarshal and EncodeIndex mismatch, got %x want %x" , have , haveEncodeIndex )
245+ }
246+ buf .Reset ()
247+ if err := legacyReceipt .EncodeRLP (buf ); err != nil {
248+ t .Fatalf ("encode rlp error: %v" , err )
249+ }
250+ haveRLPEncode := buf .Bytes ()
251+ if ! bytes .Equal (have , haveRLPEncode ) {
252+ t .Errorf ("BinaryMarshal and EncodeRLP mismatch for legacy tx, got %x want %x" , have , haveRLPEncode )
253+ }
254+ legacyWant := common .FromHex ("f901c58001b9010000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000500000000000000000000000000000000000014000000000000000000000000000000000000000000000000000000000000000000000000000010000080000000000000000000004000000000000000000000000000040000000000000000000000000000800000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000f8bef85d940000000000000000000000000000000000000011f842a0000000000000000000000000000000000000000000000000000000000000deada0000000000000000000000000000000000000000000000000000000000000beef830100fff85d940000000000000000000000000000000000000111f842a0000000000000000000000000000000000000000000000000000000000000deada0000000000000000000000000000000000000000000000000000000000000beef830100ff" )
255+ if ! bytes .Equal (have , legacyWant ) {
256+ t .Errorf ("encoded RLP mismatch, got %x want %x" , have , legacyWant )
257+ }
258+
259+ // 2930 Receipt
260+ buf .Reset ()
261+ accessListReceipt .Bloom = CreateBloom (Receipts {accessListReceipt })
262+ have , err = accessListReceipt .MarshalBinary ()
263+ if err != nil {
264+ t .Fatalf ("marshal binary error: %v" , err )
265+ }
266+ accessListReceipts := Receipts {accessListReceipt }
267+ accessListReceipts .EncodeIndex (0 , buf )
268+ haveEncodeIndex = buf .Bytes ()
269+ if ! bytes .Equal (have , haveEncodeIndex ) {
270+ t .Errorf ("BinaryMarshal and EncodeIndex mismatch, got %x want %x" , have , haveEncodeIndex )
271+ }
272+ accessListWant := common .FromHex ("01f901c58001b9010000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000500000000000000000000000000000000000014000000000000000000000000000000000000000000000000000000000000000000000000000010000080000000000000000000004000000000000000000000000000040000000000000000000000000000800000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000f8bef85d940000000000000000000000000000000000000011f842a0000000000000000000000000000000000000000000000000000000000000deada0000000000000000000000000000000000000000000000000000000000000beef830100fff85d940000000000000000000000000000000000000111f842a0000000000000000000000000000000000000000000000000000000000000deada0000000000000000000000000000000000000000000000000000000000000beef830100ff" )
273+ if ! bytes .Equal (have , accessListWant ) {
274+ t .Errorf ("encoded RLP mismatch, got %x want %x" , have , accessListWant )
275+ }
276+ }
277+
278+ func TestReceiptUnmarshalBinary (t * testing.T ) {
279+ // Legacy Receipt
280+ legacyBinary := common .FromHex ("f901c58001b9010000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000500000000000000000000000000000000000014000000000000000000000000000000000000000000000000000000000000000000000000000010000080000000000000000000004000000000000000000000000000040000000000000000000000000000800000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000f8bef85d940000000000000000000000000000000000000011f842a0000000000000000000000000000000000000000000000000000000000000deada0000000000000000000000000000000000000000000000000000000000000beef830100fff85d940000000000000000000000000000000000000111f842a0000000000000000000000000000000000000000000000000000000000000deada0000000000000000000000000000000000000000000000000000000000000beef830100ff" )
281+ gotLegacyReceipt := new (Receipt )
282+ if err := gotLegacyReceipt .UnmarshalBinary (legacyBinary ); err != nil {
283+ t .Fatalf ("unmarshal binary error: %v" , err )
284+ }
285+ legacyReceipt .Bloom = CreateBloom (Receipts {legacyReceipt })
286+ if ! reflect .DeepEqual (gotLegacyReceipt , legacyReceipt ) {
287+ t .Errorf ("receipt unmarshalled from binary mismatch, got %v want %v" , gotLegacyReceipt , legacyReceipt )
288+ }
289+
290+ // 2930 Receipt
291+ accessListBinary := common .FromHex ("01f901c58001b9010000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000500000000000000000000000000000000000014000000000000000000000000000000000000000000000000000000000000000000000000000010000080000000000000000000004000000000000000000000000000040000000000000000000000000000800000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000f8bef85d940000000000000000000000000000000000000011f842a0000000000000000000000000000000000000000000000000000000000000deada0000000000000000000000000000000000000000000000000000000000000beef830100fff85d940000000000000000000000000000000000000111f842a0000000000000000000000000000000000000000000000000000000000000deada0000000000000000000000000000000000000000000000000000000000000beef830100ff" )
292+ gotAccessListReceipt := new (Receipt )
293+ if err := gotAccessListReceipt .UnmarshalBinary (accessListBinary ); err != nil {
294+ t .Fatalf ("unmarshal binary error: %v" , err )
295+ }
296+ accessListReceipt .Bloom = CreateBloom (Receipts {accessListReceipt })
297+ if ! reflect .DeepEqual (gotAccessListReceipt , accessListReceipt ) {
298+ t .Errorf ("receipt unmarshalled from binary mismatch, got %v want %v" , gotAccessListReceipt , accessListReceipt )
299+ }
300+ }
301+
195302func clearComputedFieldsOnReceipts (t * testing.T , receipts Receipts ) {
196303 t .Helper ()
197304
0 commit comments