Skip to content

Commit f8aa623

Browse files
roberto-bayardolightclientholiman
authored
core/types: fix discrepancy in receipt.EffectiveGasPrice json encoding tags (ethereum#27114)
Regenerate receipt json code to remove omit empty. Previously, there was a discrepancy between the generated code and the source. --------- Co-authored-by: [email protected] <[email protected]> Co-authored-by: Martin Holst Swende <[email protected]>
1 parent 5d3f580 commit f8aa623

File tree

10 files changed

+76
-29
lines changed

10 files changed

+76
-29
lines changed

cmd/evm/t8n_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -275,17 +275,18 @@ func TestT8n(t *testing.T) {
275275
tt.Run("evm-test", args...)
276276
// Compare the expected output, if provided
277277
if tc.expOut != "" {
278-
want, err := os.ReadFile(fmt.Sprintf("%v/%v", tc.base, tc.expOut))
278+
file := fmt.Sprintf("%v/%v", tc.base, tc.expOut)
279+
want, err := os.ReadFile(file)
279280
if err != nil {
280281
t.Fatalf("test %d: could not read expected output: %v", i, err)
281282
}
282283
have := tt.Output()
283284
ok, err := cmpJson(have, want)
284285
switch {
285286
case err != nil:
286-
t.Fatalf("test %d, json parsing failed: %v", i, err)
287+
t.Fatalf("test %d, file %v: json parsing failed: %v", i, file, err)
287288
case !ok:
288-
t.Fatalf("test %d: output wrong, have \n%v\nwant\n%v\n", i, string(have), string(want))
289+
t.Fatalf("test %d, file %v: output wrong, have \n%v\nwant\n%v\n", i, file, string(have), string(want))
289290
}
290291
}
291292
tt.WaitExit()

cmd/evm/testdata/1/exp.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"transactionHash": "0x0557bacce3375c98d806609b8d5043072f0b6a8bae45ae5a67a00d3a1a18d673",
2929
"contractAddress": "0x0000000000000000000000000000000000000000",
3030
"gasUsed": "0x5208",
31+
"effectiveGasPrice": null,
3132
"blockHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
3233
"transactionIndex": "0x0"
3334
}

cmd/evm/testdata/13/exp2.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"transactionHash": "0xa98a24882ea90916c6a86da650fbc6b14238e46f0af04a131ce92be897507476",
1717
"contractAddress": "0x0000000000000000000000000000000000000000",
1818
"gasUsed": "0x84d0",
19+
"effectiveGasPrice": null,
1920
"blockHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
2021
"transactionIndex": "0x0"
2122
},
@@ -29,6 +30,7 @@
2930
"transactionHash": "0x36bad80acce7040c45fd32764b5c2b2d2e6f778669fb41791f73f546d56e739a",
3031
"contractAddress": "0x0000000000000000000000000000000000000000",
3132
"gasUsed": "0x84d0",
33+
"effectiveGasPrice": null,
3234
"blockHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
3335
"transactionIndex": "0x1"
3436
}

cmd/evm/testdata/23/exp.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"transactionHash": "0x72fadbef39cd251a437eea619cfeda752271a5faaaa2147df012e112159ffb81",
1616
"contractAddress": "0x0000000000000000000000000000000000000000",
1717
"gasUsed": "0x520b",
18+
"effectiveGasPrice": null,
1819
"blockHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
1920
"transactionIndex": "0x0"
2021
}

cmd/evm/testdata/24/exp.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"transactionHash": "0x92ea4a28224d033afb20e0cc2b290d4c7c2d61f6a4800a680e4e19ac962ee941",
3232
"contractAddress": "0x0000000000000000000000000000000000000000",
3333
"gasUsed": "0xa861",
34+
"effectiveGasPrice": null,
3435
"blockHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
3536
"transactionIndex": "0x0"
3637
},
@@ -43,6 +44,7 @@
4344
"transactionHash": "0x16b1d912f1d664f3f60f4e1b5f296f3c82a64a1a253117b4851d18bc03c4f1da",
4445
"contractAddress": "0x0000000000000000000000000000000000000000",
4546
"gasUsed": "0x5aa5",
47+
"effectiveGasPrice": null,
4648
"blockHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
4749
"transactionIndex": "0x1"
4850
}

cmd/evm/testdata/25/exp.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"transactionHash": "0x92ea4a28224d033afb20e0cc2b290d4c7c2d61f6a4800a680e4e19ac962ee941",
2828
"contractAddress": "0x0000000000000000000000000000000000000000",
2929
"gasUsed": "0x5208",
30+
"effectiveGasPrice": null,
3031
"blockHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
3132
"transactionIndex": "0x0"
3233
}

cmd/evm/testdata/3/exp.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"transactionHash": "0x72fadbef39cd251a437eea619cfeda752271a5faaaa2147df012e112159ffb81",
2929
"contractAddress": "0x0000000000000000000000000000000000000000",
3030
"gasUsed": "0x521f",
31+
"effectiveGasPrice": null,
3132
"blockHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
3233
"transactionIndex": "0x0"
3334
}

core/types/gen_receipt_json.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

core/types/receipt.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ type Receipt struct {
6262
TxHash common.Hash `json:"transactionHash" gencodec:"required"`
6363
ContractAddress common.Address `json:"contractAddress"`
6464
GasUsed uint64 `json:"gasUsed" gencodec:"required"`
65-
EffectiveGasPrice *big.Int `json:"effectiveGasPrice"`
65+
EffectiveGasPrice *big.Int `json:"effectiveGasPrice"` // required, but tag omitted for backwards compatibility
6666

6767
// Inclusion information: These fields provide information about the inclusion of the
6868
// transaction corresponding to this receipt.
@@ -77,6 +77,7 @@ type receiptMarshaling struct {
7777
Status hexutil.Uint64
7878
CumulativeGasUsed hexutil.Uint64
7979
GasUsed hexutil.Uint64
80+
EffectiveGasPrice *hexutil.Big
8081
BlockNumber *hexutil.Big
8182
TransactionIndex hexutil.Uint
8283
}

core/types/receipt_test.go

Lines changed: 60 additions & 23 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+
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.
325362
func TestTypedReceiptEncodingDecoding(t *testing.T) {

0 commit comments

Comments
 (0)