Skip to content

Commit b9631fe

Browse files
authored
Fix Receipt decode (#221)
* Fix old post-byzantium forks * Add to field to Receipt
1 parent be4553d commit b9631fe

File tree

4 files changed

+66
-2
lines changed

4 files changed

+66
-2
lines changed

structs.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,7 @@ type Receipt struct {
292292
LogsBloom []byte
293293
Logs []*Log
294294
Status uint64
295+
To *Address
295296
}
296297

297298
func (r *Receipt) Copy() *Receipt {

structs_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package ethgo
22

33
import (
4+
_ "embed"
5+
"encoding/json"
46
"math/big"
57
"reflect"
68
"testing"
@@ -103,3 +105,16 @@ func TestLog_Copy(t *testing.T) {
103105
t.Fatal("incorrect receipt")
104106
}
105107
}
108+
109+
//go:embed testsuite/receipts.json
110+
var receiptsFixtures []byte
111+
112+
func TestReceipt_Unmarshal(t *testing.T) {
113+
var cases []json.RawMessage
114+
assert.NoError(t, json.Unmarshal(receiptsFixtures, &cases))
115+
116+
for _, c := range cases {
117+
receipt := &Receipt{}
118+
assert.NoError(t, receipt.UnmarshalJSON(c))
119+
}
120+
}

structs_unmarshal.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -304,8 +304,22 @@ func (r *Receipt) UnmarshalJSON(buf []byte) error {
304304
if r.LogsBloom, err = decodeBytes(r.LogsBloom[:0], v, "logsBloom", 256); err != nil {
305305
return err
306306
}
307-
if r.Status, err = decodeUint(v, "status"); err != nil {
308-
return err
307+
if v.Exists("status") {
308+
// post-byzantium fork
309+
if r.Status, err = decodeUint(v, "status"); err != nil {
310+
return err
311+
}
312+
}
313+
314+
if v.Exists("to") {
315+
// Do not decode 'to' if it doesn't exist.
316+
if v.Get("to").String() != "null" {
317+
var to Address
318+
if err = decodeAddr(&to, v, "to"); err != nil {
319+
return err
320+
}
321+
r.To = &to
322+
}
309323
}
310324

311325
// logs

testsuite/receipts.json

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
[
2+
{
3+
"blockHash": "0x1ee40a01cdb865600eef166d175eade70ab51f215c5663a7fcc5ec1f960f4e40",
4+
"blockNumber": "0x10482a",
5+
"contractAddress": "0xd63d7145f125b16bb36fe73f2128bb77dcc8ccf1",
6+
"cumulativeGasUsed": "0x3765f",
7+
"effectiveGasPrice": "0xba43b7400",
8+
"from": "0x20e021a751cb6bcab2eaf7b9db570ec10c8ad0a2",
9+
"gasUsed": "0x32457",
10+
"logs": [],
11+
"logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
12+
"root": "0xe41f2551706d287917eb795bb81348d68ca6f9300e1394e4c8d3288ae16865dc",
13+
"to": null,
14+
"transactionHash": "0xcbb8db2cec3fa38e2ff221462708252bbbe32abb57e5a43f402155880d2883e0",
15+
"transactionIndex": "0x1",
16+
"type": "0x0"
17+
},
18+
{
19+
"blockHash": "0x7ba787b8371397a05412121bf915373ebbcdfa6e4117f6076911a34ee9bca4a8",
20+
"blockNumber": "0xee76d0",
21+
"contractAddress": null,
22+
"cumulativeGasUsed": "0x114db42",
23+
"effectiveGasPrice": "0x222359b14",
24+
"from": "0xdafea492d9c6733ae3d56b7ed1adb60692c98bc5",
25+
"gasUsed": "0x523f",
26+
"logs": [],
27+
"logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
28+
"status": "0x1",
29+
"to": "0xebec795c9c8bbd61ffc14a6662944748f299cacf",
30+
"transactionHash": "0xdd002538bd3165c8aed8a7435f965420bca4406951e490190f4271302a4c5254",
31+
"transactionIndex": "0x90",
32+
"type": "0x0"
33+
}
34+
]

0 commit comments

Comments
 (0)