Skip to content

Commit 59cc387

Browse files
add test cases for receipt json marshal/unmarshal
1 parent a0a0793 commit 59cc387

File tree

1 file changed

+75
-39
lines changed

1 file changed

+75
-39
lines changed

core/types/receipt_test.go

Lines changed: 75 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
testTxs = Transactions{
10694
NewTx(&LegacyTx{
10795
Nonce: 1,
10896
Value: big.NewInt(1),
@@ -161,37 +149,38 @@ func TestDeriveFields(t *testing.T) {
161149
}),
162150
}
163151

164-
blockNumber := big.NewInt(1)
165-
blockTime := uint64(2)
166-
blockHash := common.BytesToHash([]byte{0x03, 0x14})
167-
168-
// Create the corresponding receipts
169-
receipts := Receipts{
152+
// Create corresponding receipts for the testTxs
153+
blockNumber = big.NewInt(1)
154+
blockTime = uint64(2)
155+
blockHash = common.BytesToHash([]byte{0x03, 0x14})
156+
testReceipts = Receipts{
170157
&Receipt{
171158
Status: ReceiptStatusFailed,
172159
CumulativeGasUsed: 1,
173160
Logs: []*Log{
174161
{
175162
Address: common.BytesToAddress([]byte{0x11}),
163+
Topics: []common.Hash{common.HexToHash("dead"), common.HexToHash("beef")},
176164
// derived fields:
177165
BlockNumber: blockNumber.Uint64(),
178-
TxHash: txs[0].Hash(),
166+
TxHash: testTxs[0].Hash(),
179167
TxIndex: 0,
180168
BlockHash: blockHash,
181169
Index: 0,
182170
},
183171
{
184172
Address: common.BytesToAddress([]byte{0x01, 0x11}),
173+
Topics: []common.Hash{common.HexToHash("dead"), common.HexToHash("beef")},
185174
// derived fields:
186175
BlockNumber: blockNumber.Uint64(),
187-
TxHash: txs[0].Hash(),
176+
TxHash: testTxs[0].Hash(),
188177
TxIndex: 0,
189178
BlockHash: blockHash,
190179
Index: 1,
191180
},
192181
},
193182
// derived fields:
194-
TxHash: txs[0].Hash(),
183+
TxHash: testTxs[0].Hash(),
195184
ContractAddress: common.HexToAddress("0x5a443704dd4b594b382c22a083e2bd3090a6fef3"),
196185
GasUsed: 1,
197186
EffectiveGasPrice: big.NewInt(11),
@@ -205,25 +194,27 @@ func TestDeriveFields(t *testing.T) {
205194
Logs: []*Log{
206195
{
207196
Address: common.BytesToAddress([]byte{0x22}),
197+
Topics: []common.Hash{common.HexToHash("dead"), common.HexToHash("beef")},
208198
// derived fields:
209199
BlockNumber: blockNumber.Uint64(),
210-
TxHash: txs[1].Hash(),
200+
TxHash: testTxs[1].Hash(),
211201
TxIndex: 1,
212202
BlockHash: blockHash,
213203
Index: 2,
214204
},
215205
{
216206
Address: common.BytesToAddress([]byte{0x02, 0x22}),
207+
Topics: []common.Hash{common.HexToHash("dead"), common.HexToHash("beef")},
217208
// derived fields:
218209
BlockNumber: blockNumber.Uint64(),
219-
TxHash: txs[1].Hash(),
210+
TxHash: testTxs[1].Hash(),
220211
TxIndex: 1,
221212
BlockHash: blockHash,
222213
Index: 3,
223214
},
224215
},
225216
// derived fields:
226-
TxHash: txs[1].Hash(),
217+
TxHash: testTxs[1].Hash(),
227218
GasUsed: 2,
228219
EffectiveGasPrice: big.NewInt(22),
229220
BlockHash: blockHash,
@@ -236,7 +227,7 @@ func TestDeriveFields(t *testing.T) {
236227
CumulativeGasUsed: 6,
237228
Logs: []*Log{},
238229
// derived fields:
239-
TxHash: txs[2].Hash(),
230+
TxHash: testTxs[2].Hash(),
240231
GasUsed: 3,
241232
EffectiveGasPrice: big.NewInt(33),
242233
BlockHash: blockHash,
@@ -249,7 +240,7 @@ func TestDeriveFields(t *testing.T) {
249240
CumulativeGasUsed: 10,
250241
Logs: []*Log{},
251242
// derived fields:
252-
TxHash: txs[3].Hash(),
243+
TxHash: testTxs[3].Hash(),
253244
GasUsed: 4,
254245
EffectiveGasPrice: big.NewInt(1044),
255246
BlockHash: blockHash,
@@ -262,7 +253,7 @@ func TestDeriveFields(t *testing.T) {
262253
CumulativeGasUsed: 15,
263254
Logs: []*Log{},
264255
// derived fields:
265-
TxHash: txs[4].Hash(),
256+
TxHash: testTxs[4].Hash(),
266257
GasUsed: 5,
267258
EffectiveGasPrice: big.NewInt(1055),
268259
BlockHash: blockHash,
@@ -275,7 +266,7 @@ func TestDeriveFields(t *testing.T) {
275266
CumulativeGasUsed: 21,
276267
Logs: []*Log{},
277268
// derived fields:
278-
TxHash: txs[5].Hash(),
269+
TxHash: testTxs[5].Hash(),
279270
GasUsed: 6,
280271
EffectiveGasPrice: big.NewInt(1066),
281272
BlockHash: blockHash,
@@ -288,28 +279,41 @@ func TestDeriveFields(t *testing.T) {
288279
CumulativeGasUsed: 28,
289280
Logs: []*Log{},
290281
// derived fields:
291-
TxHash: txs[6].Hash(),
282+
TxHash: testTxs[6].Hash(),
292283
GasUsed: 7,
293284
EffectiveGasPrice: big.NewInt(1077),
294285
BlockHash: blockHash,
295286
BlockNumber: blockNumber,
296287
TransactionIndex: 6,
297288
},
298289
}
290+
)
299291

292+
func TestDecodeEmptyTypedReceipt(t *testing.T) {
293+
input := []byte{0x80}
294+
var r Receipt
295+
err := rlp.DecodeBytes(input, &r)
296+
if err != errShortTypedReceipt {
297+
t.Fatal("wrong error:", err)
298+
}
299+
}
300+
301+
// Tests that receipt data can be correctly derived from the contextual infos
302+
func TestDeriveFields(t *testing.T) {
300303
// Re-derive receipts.
301304
basefee := big.NewInt(1000)
302-
derivedReceipts := clearComputedFieldsOnReceipts(receipts)
303-
err := Receipts(derivedReceipts).DeriveFields(params.TestChainConfig, blockHash, blockNumber.Uint64(), blockTime, basefee, txs)
305+
derivedReceipts := clearComputedFieldsOnReceipts(testReceipts)
306+
err := Receipts(derivedReceipts).DeriveFields(params.TestChainConfig, blockHash, blockNumber.Uint64(), blockTime, basefee, testTxs)
304307
if err != nil {
305308
t.Fatalf("DeriveFields(...) = %v, want <nil>", err)
306309
}
307310

308311
// Check diff of receipts against derivedReceipts.
309-
r1, err := json.MarshalIndent(receipts, "", " ")
312+
r1, err := json.MarshalIndent(testReceipts, "", " ")
310313
if err != nil {
311314
t.Fatal("error marshaling input receipts:", err)
312315
}
316+
313317
r2, err := json.MarshalIndent(derivedReceipts, "", " ")
314318
if err != nil {
315319
t.Fatal("error marshaling derived receipts:", err)
@@ -320,6 +324,38 @@ func TestDeriveFields(t *testing.T) {
320324
}
321325
}
322326

327+
// Test that we can marshal/unmarshal receipts to/from json without errors.
328+
// This also confirms that our test receipts contain all the required fields.
329+
func TestReceiptJSON(t *testing.T) {
330+
for i := range testReceipts {
331+
b, err := testReceipts[i].MarshalJSON()
332+
if err != nil {
333+
t.Fatal("error marshaling receipt to json:", err)
334+
}
335+
r := Receipt{}
336+
err = r.UnmarshalJSON(b)
337+
if err != nil {
338+
t.Fatal("error unmarshaling receipt from json:", err)
339+
}
340+
}
341+
}
342+
343+
// Test we can still parse receipt without EffectiveGasPrice for backwards compatibility, even
344+
// though it is required per the spec.
345+
func TestEffectiveGasPriceNotRequired(t *testing.T) {
346+
r := *testReceipts[0]
347+
r.EffectiveGasPrice = nil
348+
b, err := r.MarshalJSON()
349+
if err != nil {
350+
t.Fatal("error marshaling receipt to json:", err)
351+
}
352+
r2 := Receipt{}
353+
err = r2.UnmarshalJSON(b)
354+
if err != nil {
355+
t.Fatal("error unmarshaling receipt from json:", err)
356+
}
357+
}
358+
323359
// TestTypedReceiptEncodingDecoding reproduces a flaw that existed in the receipt
324360
// rlp decoder, which failed due to a shadowing error.
325361
func TestTypedReceiptEncodingDecoding(t *testing.T) {

0 commit comments

Comments
 (0)