From ff3c370fa67e42039cc71f0c98d5b04eaec62a7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20Negovanovi=C4=87?= Date: Mon, 7 Aug 2023 15:18:28 +0200 Subject: [PATCH] Prevent marshaling gas price in case of dynamic fee transactions --- structs_marshal.go | 20 +++++++++++--------- structs_unmarshal.go | 22 +++++++++++----------- testsuite/transaction-eip1159.json | 3 +-- 3 files changed, 23 insertions(+), 22 deletions(-) diff --git a/structs_marshal.go b/structs_marshal.go index c058c682..b88e079e 100644 --- a/structs_marshal.go +++ b/structs_marshal.go @@ -117,18 +117,20 @@ func (t *Transaction) marshalJSON(a *fastjson.Arena) *fastjson.Value { if t.Value != nil { o.Set("value", a.NewString(fmt.Sprintf("0x%x", t.Value))) } - o.Set("gasPrice", a.NewString(fmt.Sprintf("0x%x", t.GasPrice))) - + if t.Type == TransactionDynamicFee { + if t.MaxPriorityFeePerGas != nil { + o.Set("maxPriorityFeePerGas", a.NewString(fmt.Sprintf("0x%x", t.MaxPriorityFeePerGas))) + } + if t.MaxFeePerGas != nil { + o.Set("maxFeePerGas", a.NewString(fmt.Sprintf("0x%x", t.MaxFeePerGas))) + } + } else { + o.Set("gasPrice", a.NewString(fmt.Sprintf("0x%x", t.GasPrice))) + } // gas limit fields if t.Gas != 0 { o.Set("gas", a.NewString(fmt.Sprintf("0x%x", t.Gas))) } - if t.MaxPriorityFeePerGas != nil { - o.Set("maxPriorityFeePerGas", a.NewString(fmt.Sprintf("0x%x", t.MaxPriorityFeePerGas))) - } - if t.MaxFeePerGas != nil { - o.Set("maxFeePerGas", a.NewString(fmt.Sprintf("0x%x", t.MaxFeePerGas))) - } if t.Nonce != 0 { // we can remove this once we include support for custom nonces @@ -298,4 +300,4 @@ func (s StateOverride) MarshalJSON() ([]byte, error) { defaultArena.Put(a) return res, nil -} +} diff --git a/structs_unmarshal.go b/structs_unmarshal.go index 4b7ef4eb..a3f28c0b 100644 --- a/structs_unmarshal.go +++ b/structs_unmarshal.go @@ -156,8 +156,17 @@ func (t *Transaction) unmarshalJSON(v *fastjson.Value) error { if err = decodeAddr(&t.From, v, "from"); err != nil { return err } - if t.GasPrice, err = decodeUint(v, "gasPrice"); err != nil { - return err + if typ == 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 { + if t.GasPrice, err = decodeUint(v, "gasPrice"); err != nil { + return err + } } if t.Input, err = decodeBytes(t.Input[:0], v, "input"); err != nil { return err @@ -207,15 +216,6 @@ func (t *Transaction) unmarshalJSON(v *fastjson.Value) error { return err } - if typ == 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 - } - } - // Check if the block hash field is set // If it's not -> the transaction is a pending txn, so these fields should be omitted // If it is -> the transaction is a sealed txn, so these fields should be included diff --git a/testsuite/transaction-eip1159.json b/testsuite/transaction-eip1159.json index 534ebe36..bad0eb94 100644 --- a/testsuite/transaction-eip1159.json +++ b/testsuite/transaction-eip1159.json @@ -3,10 +3,9 @@ "from": "0x0000000000000000000000000000000000000001", "input": "0x00", "value": "0x0", - "gasPrice": "0x0", - "gas": "0x10", "maxPriorityFeePerGas": "0x10", "maxFeePerGas": "0x10", + "gas": "0x10", "nonce": "0x10", "to": null, "v":"0x25",