Skip to content

Commit

Permalink
Fix decoding of json transactions with type field (#259)
Browse files Browse the repository at this point in the history
* Fix decoding of json transactions with type field

* Add fallback
  • Loading branch information
ferranbt committed Jan 2, 2024
1 parent 6b06836 commit 68e48cf
Show file tree
Hide file tree
Showing 11 changed files with 176 additions and 27 deletions.
16 changes: 16 additions & 0 deletions structs_encoding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func compactJSON(s string) string {
Expand All @@ -18,6 +19,17 @@ func compactJSON(s string) string {
return buffer.String()
}

func TestDecodeL2Block(t *testing.T) {
c := readTestsuite(t, "./testsuite/arbitrum-block-full.json")

block := new(Block)
require.NoError(t, block.UnmarshalJSON(c[0].content))

for _, txn := range block.Transactions {
require.NotEqual(t, txn.Type, 0)
}
}

func TestEncodingJSON_Block(t *testing.T) {
for _, c := range readTestsuite(t, "./testsuite/block-*.json") {
content := []byte(compactJSON(string(c.content)))
Expand All @@ -44,6 +56,10 @@ func TestEncodingJSON_Transaction(t *testing.T) {
err := txn.UnmarshalJSON(content)
assert.NoError(t, err)

if c.name == "testsuite/transaction-eip1559-notype.json" {
continue
}

// marshal back
res2, err := txn.MarshalJSON()
assert.NoError(t, err)
Expand Down
1 change: 1 addition & 0 deletions structs_marshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ func (t *Transaction) MarshalJSON() ([]byte, error) {

func (t *Transaction) marshalJSON(a *fastjson.Arena) *fastjson.Value {
o := a.NewObject()
o.Set("type", a.NewString(fmt.Sprintf("0x%x", t.Type)))
o.Set("hash", a.NewString(t.Hash.String()))
o.Set("from", a.NewString(t.From.String()))
if len(t.Input) != 0 {
Expand Down
29 changes: 17 additions & 12 deletions structs_unmarshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,18 +141,23 @@ func (t *Transaction) unmarshalJSON(v *fastjson.Value) error {
return nil
}

// detect transaction type
var typ TransactionType
if isKeySet(v, "chainId") {
if isKeySet(v, "maxFeePerGas") {
typ = TransactionDynamicFee
} else {
typ = TransactionAccessList
if isKeySet(v, "type") {
txnType, err := decodeUint(v, "type")
if err != nil {
return err
}
t.Type = TransactionType(txnType)
} else {
typ = TransactionLegacy
if isKeySet(v, "chainId") {
if isKeySet(v, "maxFeePerGas") {
t.Type = TransactionDynamicFee
} else {
t.Type = TransactionAccessList
}
} else {
t.Type = TransactionLegacy
}
}
t.Type = typ

var err error
if err := decodeHash(&t.Hash, v, "hash"); err != nil {
Expand All @@ -161,14 +166,14 @@ func (t *Transaction) unmarshalJSON(v *fastjson.Value) error {
if err = decodeAddr(&t.From, v, "from"); err != nil {
return err
}
if typ == TransactionDynamicFee {
if t.Type == TransactionDynamicFee {
if t.MaxPriorityFeePerGas, err = decodeBigInt(t.MaxPriorityFeePerGas, v, "maxPriorityFeePerGas"); err != nil {
return err
}
if t.MaxFeePerGas, err = decodeBigInt(t.MaxFeePerGas, v, "maxFeePerGas"); err != nil {
return err
}
} else {
} else if t.Type == TransactionLegacy || t.Type == TransactionAccessList {
if t.GasPrice, err = decodeUint(v, "gasPrice"); err != nil {
return err
}
Expand Down Expand Up @@ -206,7 +211,7 @@ func (t *Transaction) unmarshalJSON(v *fastjson.Value) error {
return err
}

if typ != TransactionLegacy {
if t.Type == TransactionDynamicFee || t.Type == TransactionAccessList {
if t.ChainID, err = decodeBigInt(t.ChainID, v, "chainId"); err != nil {
return err
}
Expand Down
95 changes: 95 additions & 0 deletions testsuite/arbitrum-block-full.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions testsuite/block-full.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
],
"transactions": [
{
"type": "0x0",
"hash": "0x0000000000000000000000000000000000000000000000000000000000000001",
"from": "0x0000000000000000000000000000000000000001",
"input": "0x00",
Expand Down
7 changes: 4 additions & 3 deletions testsuite/transaction-call.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"type": "0x0",
"hash": "0x0000000000000000000000000000000000000000000000000000000000000001",
"from": "0x0000000000000000000000000000000000000001",
"input": "0x00",
Expand All @@ -7,9 +8,9 @@
"gas": "0x10",
"nonce": "0x10",
"to": "0x0000000000000000000000000000000000000001",
"v":"0x25",
"r":"0x0000000000000000000000000000000000000000000000000000000000000001",
"s":"0x0000000000000000000000000000000000000000000000000000000000000001",
"v": "0x25",
"r": "0x0000000000000000000000000000000000000000000000000000000000000001",
"s": "0x0000000000000000000000000000000000000000000000000000000000000001",
"blockHash": "0x0000000000000000000000000000000000000000000000000000000000000001",
"blockNumber": "0x0",
"transactionIndex": "0x0"
Expand Down
7 changes: 4 additions & 3 deletions testsuite/transaction-contract-creation.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"type": "0x0",
"hash": "0x0000000000000000000000000000000000000000000000000000000000000001",
"from": "0x0000000000000000000000000000000000000001",
"input": "0x00",
Expand All @@ -7,9 +8,9 @@
"gas": "0x10",
"nonce": "0x10",
"to": null,
"v":"0x25",
"r":"0x0000000000000000000000000000000000000000000000000000000000000001",
"s":"0x0000000000000000000000000000000000000000000000000000000000000001",
"v": "0x25",
"r": "0x0000000000000000000000000000000000000000000000000000000000000001",
"s": "0x0000000000000000000000000000000000000000000000000000000000000001",
"blockHash": "0x0000000000000000000000000000000000000000000000000000000000000001",
"blockNumber": "0x0",
"transactionIndex": "0x0"
Expand Down
7 changes: 4 additions & 3 deletions testsuite/transaction-eip1159.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"type": "0x2",
"hash": "0x0000000000000000000000000000000000000000000000000000000000000001",
"from": "0x0000000000000000000000000000000000000001",
"input": "0x00",
Expand All @@ -8,9 +9,9 @@
"gas": "0x10",
"nonce": "0x10",
"to": null,
"v":"0x25",
"r":"0x0000000000000000000000000000000000000000000000000000000000000001",
"s":"0x0000000000000000000000000000000000000000000000000000000000000001",
"v": "0x25",
"r": "0x0000000000000000000000000000000000000000000000000000000000000001",
"s": "0x0000000000000000000000000000000000000000000000000000000000000001",
"blockHash": "0x0000000000000000000000000000000000000000000000000000000000000001",
"blockNumber": "0x0",
"transactionIndex": "0x0",
Expand Down
26 changes: 26 additions & 0 deletions testsuite/transaction-eip1559-notype.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"hash": "0x0000000000000000000000000000000000000000000000000000000000000001",
"from": "0x0000000000000000000000000000000000000001",
"input": "0x00",
"value": "0x0",
"maxPriorityFeePerGas": "0x10",
"maxFeePerGas": "0x10",
"gas": "0x10",
"nonce": "0x10",
"to": null,
"v": "0x25",
"r": "0x0000000000000000000000000000000000000000000000000000000000000001",
"s": "0x0000000000000000000000000000000000000000000000000000000000000001",
"blockHash": "0x0000000000000000000000000000000000000000000000000000000000000001",
"blockNumber": "0x0",
"transactionIndex": "0x0",
"chainId": "0x1",
"accessList": [
{
"address": "0x0000000000000000000000000000000000000001",
"storageKeys": [
"0x0000000000000000000000000000000000000000000000000000000000000001"
]
}
]
}
7 changes: 4 additions & 3 deletions testsuite/transaction-eip2930.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"type": "0x1",
"hash": "0x0000000000000000000000000000000000000000000000000000000000000001",
"from": "0x0000000000000000000000000000000000000001",
"input": "0x00",
Expand All @@ -7,9 +8,9 @@
"gas": "0x10",
"nonce": "0x10",
"to": null,
"v":"0x25",
"r":"0x0000000000000000000000000000000000000000000000000000000000000001",
"s":"0x0000000000000000000000000000000000000000000000000000000000000001",
"v": "0x25",
"r": "0x0000000000000000000000000000000000000000000000000000000000000001",
"s": "0x0000000000000000000000000000000000000000000000000000000000000001",
"blockHash": "0x0000000000000000000000000000000000000000000000000000000000000001",
"blockNumber": "0x0",
"transactionIndex": "0x0",
Expand Down
7 changes: 4 additions & 3 deletions testsuite/transaction-pending.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"type": "0x0",
"hash": "0x0000000000000000000000000000000000000000000000000000000000000001",
"from": "0x0000000000000000000000000000000000000001",
"input": "0x00",
Expand All @@ -7,9 +8,9 @@
"gas": "0x10",
"nonce": "0x10",
"to": "0x0000000000000000000000000000000000000001",
"v":"0x25",
"r":"0x0000000000000000000000000000000000000000000000000000000000000001",
"s":"0x0000000000000000000000000000000000000000000000000000000000000001",
"v": "0x25",
"r": "0x0000000000000000000000000000000000000000000000000000000000000001",
"s": "0x0000000000000000000000000000000000000000000000000000000000000001",
"blockHash": null,
"blockNumber": null,
"transactionIndex": null
Expand Down

0 comments on commit 68e48cf

Please sign in to comment.