Skip to content

Commit 736fab2

Browse files
committed
receipt MarshalBinary and UnmarshalBinary unit tests
1 parent df799eb commit 736fab2

File tree

2 files changed

+118
-14
lines changed

2 files changed

+118
-14
lines changed

core/types/receipt.go

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,6 @@ var (
4141
// This error is returned when a typed receipt is decoded, but the string is empty.
4242
var errEmptyTypedReceipt = errors.New("empty typed receipt bytes")
4343

44-
// This error is returned when a typed receipt has an unsupported type
45-
var errRctTypeNotSupported = errors.New("receipt type not supported")
46-
4744
const (
4845
// ReceiptStatusFailed is the status code of a transaction if execution failed.
4946
ReceiptStatusFailed = uint64(0)
@@ -126,16 +123,19 @@ func (r *Receipt) EncodeRLP(w io.Writer) error {
126123
buf := encodeBufferPool.Get().(*bytes.Buffer)
127124
defer encodeBufferPool.Put(buf)
128125
buf.Reset()
129-
buf.WriteByte(r.Type)
130-
if err := rlp.Encode(buf, data); err != nil {
126+
if err := r.encodeTyped(data, buf); err != nil {
131127
return err
132128
}
133129
return rlp.Encode(w, buf.Bytes())
134130
}
135131

136-
// MarshalBinary returns the canonical encoding of the receipt.
137-
// For legacy receipts, it returns the RLP encoding. For EIP-2718 typed
138-
// receipts, it returns the `type || RLP encoding`.
132+
// encodeTyped writes the canonical encoding of a typed receipt to w.
133+
func (r *Receipt) encodeTyped(data *receiptRLP, w *bytes.Buffer) error {
134+
w.WriteByte(r.Type)
135+
return rlp.Encode(w, data)
136+
}
137+
138+
// MarshalBinary returns the consensus encoding of the receipt.
139139
func (r *Receipt) MarshalBinary() ([]byte, error) {
140140
if r.Type == LegacyTxType {
141141
return rlp.EncodeToBytes(r)
@@ -144,11 +144,8 @@ func (r *Receipt) MarshalBinary() ([]byte, error) {
144144
buf := encodeBufferPool.Get().(*bytes.Buffer)
145145
defer encodeBufferPool.Put(buf)
146146
buf.Reset()
147-
buf.WriteByte(r.Type)
148-
if err := rlp.Encode(buf, data); err != nil {
149-
return nil, err
150-
}
151-
return buf.Bytes(), nil
147+
err := r.encodeTyped(data, buf)
148+
return buf.Bytes(), err
152149
}
153150

154151
// DecodeRLP implements rlp.Decoder, and loads the consensus fields of a receipt
@@ -189,7 +186,7 @@ func (r *Receipt) DecodeRLP(s *rlp.Stream) error {
189186
}
190187
}
191188

192-
// UnmarshalBinary decodes the canonical encoding of receipts.
189+
// UnmarshalBinary decodes the consensus encoding of receipts.
193190
// It supports legacy RLP receipts and EIP-2718 typed receipts.
194191
func (r *Receipt) UnmarshalBinary(b []byte) error {
195192
if len(b) > 0 && b[0] > 0x7f {

core/types/receipt_test.go

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
3168
func 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+
195302
func clearComputedFieldsOnReceipts(t *testing.T, receipts Receipts) {
196303
t.Helper()
197304

0 commit comments

Comments
 (0)