From 50f6c95c61869faed8eb21d1b5ec6eb3c4ebf708 Mon Sep 17 00:00:00 2001 From: Freeman Jiang Date: Sun, 11 Jun 2023 00:38:15 -0400 Subject: [PATCH 1/5] core/types: add derived chain ID to LegacyTx JSON encoding --- core/types/transaction_marshalling.go | 5 +++++ core/types/transaction_test.go | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/core/types/transaction_marshalling.go b/core/types/transaction_marshalling.go index 070927484cc7c..65a0d316a5fd0 100644 --- a/core/types/transaction_marshalling.go +++ b/core/types/transaction_marshalling.go @@ -60,6 +60,11 @@ func (tx *Transaction) MarshalJSON() ([]byte, error) { // Other fields are set conditionally depending on tx type. switch itx := tx.inner.(type) { case *LegacyTx: + if itx.V.Sign() != 0 || itx.R.Sign() != 0 || itx.S.Sign() != 0 { + enc.ChainID = (*hexutil.Big)(tx.ChainId()) + } else { + enc.ChainID = (*hexutil.Big)(new(big.Int)) + } enc.Nonce = (*hexutil.Uint64)(&itx.Nonce) enc.To = tx.To() enc.Gas = (*hexutil.Uint64)(&itx.Gas) diff --git a/core/types/transaction_test.go b/core/types/transaction_test.go index f6442c5a35746..9e7604f91a606 100644 --- a/core/types/transaction_test.go +++ b/core/types/transaction_test.go @@ -25,6 +25,7 @@ import ( "math/big" "math/rand" "reflect" + "strings" "testing" "time" @@ -501,6 +502,10 @@ func encodeDecodeJSON(tx *Transaction) (*Transaction, error) { if err := json.Unmarshal(data, &parsedTx); err != nil { return nil, fmt.Errorf("json decoding failed: %v", err) } + + if !strings.Contains(string(data), "chainId") { + return nil, fmt.Errorf("json encoding does not contain chainId: %v", string(data)) + } return parsedTx, nil } From 301218c69f932f79ec4f3cbee54537a491beed38 Mon Sep 17 00:00:00 2001 From: Freeman Jiang Date: Sun, 11 Jun 2023 00:47:58 -0400 Subject: [PATCH 2/5] core/types: only derive the chain ID if tx is signed and post EIP-155 --- core/types/transaction_marshalling.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/core/types/transaction_marshalling.go b/core/types/transaction_marshalling.go index 65a0d316a5fd0..e2dbe5796cb66 100644 --- a/core/types/transaction_marshalling.go +++ b/core/types/transaction_marshalling.go @@ -60,10 +60,9 @@ func (tx *Transaction) MarshalJSON() ([]byte, error) { // Other fields are set conditionally depending on tx type. switch itx := tx.inner.(type) { case *LegacyTx: - if itx.V.Sign() != 0 || itx.R.Sign() != 0 || itx.S.Sign() != 0 { + // Only derive the chain ID if tx is signed and post EIP-155 + if (itx.V.Sign() != 0 || itx.R.Sign() != 0 || itx.S.Sign() != 0) && tx.ChainId().Sign() != 0 { enc.ChainID = (*hexutil.Big)(tx.ChainId()) - } else { - enc.ChainID = (*hexutil.Big)(new(big.Int)) } enc.Nonce = (*hexutil.Uint64)(&itx.Nonce) enc.To = tx.To() From cd6461f7bb17ea1d9dc42a455f1146cd1479d257 Mon Sep 17 00:00:00 2001 From: Freeman Jiang Date: Sun, 11 Jun 2023 14:45:11 -0400 Subject: [PATCH 3/5] core/types: simplify protected check --- core/types/transaction_marshalling.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/core/types/transaction_marshalling.go b/core/types/transaction_marshalling.go index e2dbe5796cb66..ecd58683dc783 100644 --- a/core/types/transaction_marshalling.go +++ b/core/types/transaction_marshalling.go @@ -60,10 +60,6 @@ func (tx *Transaction) MarshalJSON() ([]byte, error) { // Other fields are set conditionally depending on tx type. switch itx := tx.inner.(type) { case *LegacyTx: - // Only derive the chain ID if tx is signed and post EIP-155 - if (itx.V.Sign() != 0 || itx.R.Sign() != 0 || itx.S.Sign() != 0) && tx.ChainId().Sign() != 0 { - enc.ChainID = (*hexutil.Big)(tx.ChainId()) - } enc.Nonce = (*hexutil.Uint64)(&itx.Nonce) enc.To = tx.To() enc.Gas = (*hexutil.Uint64)(&itx.Gas) @@ -73,6 +69,10 @@ func (tx *Transaction) MarshalJSON() ([]byte, error) { enc.V = (*hexutil.Big)(itx.V) enc.R = (*hexutil.Big)(itx.R) enc.S = (*hexutil.Big)(itx.S) + // Only derive the chain ID if tx is signed and post EIP-155 + if tx.Protected() { + enc.ChainID = (*hexutil.Big)(tx.ChainId()) + } case *AccessListTx: enc.ChainID = (*hexutil.Big)(itx.ChainID) From c27c1297dc7d638ecfc381a4790a263a11c74988 Mon Sep 17 00:00:00 2001 From: Freeman Jiang Date: Sun, 11 Jun 2023 14:51:25 -0400 Subject: [PATCH 4/5] core/types: revert test --- core/types/transaction_test.go | 5 ----- 1 file changed, 5 deletions(-) diff --git a/core/types/transaction_test.go b/core/types/transaction_test.go index 9e7604f91a606..f6442c5a35746 100644 --- a/core/types/transaction_test.go +++ b/core/types/transaction_test.go @@ -25,7 +25,6 @@ import ( "math/big" "math/rand" "reflect" - "strings" "testing" "time" @@ -502,10 +501,6 @@ func encodeDecodeJSON(tx *Transaction) (*Transaction, error) { if err := json.Unmarshal(data, &parsedTx); err != nil { return nil, fmt.Errorf("json decoding failed: %v", err) } - - if !strings.Contains(string(data), "chainId") { - return nil, fmt.Errorf("json encoding does not contain chainId: %v", string(data)) - } return parsedTx, nil } From b6d968acde5b8ba5b7a58bec9ac969ad7b2ba95b Mon Sep 17 00:00:00 2001 From: lightclient <14004106+lightclient@users.noreply.github.com> Date: Mon, 12 Jun 2023 08:32:24 +0200 Subject: [PATCH 5/5] core/types/transaction_marshalling: remove comment --- core/types/transaction_marshalling.go | 1 - 1 file changed, 1 deletion(-) diff --git a/core/types/transaction_marshalling.go b/core/types/transaction_marshalling.go index ecd58683dc783..8e3db617703a5 100644 --- a/core/types/transaction_marshalling.go +++ b/core/types/transaction_marshalling.go @@ -69,7 +69,6 @@ func (tx *Transaction) MarshalJSON() ([]byte, error) { enc.V = (*hexutil.Big)(itx.V) enc.R = (*hexutil.Big)(itx.R) enc.S = (*hexutil.Big)(itx.S) - // Only derive the chain ID if tx is signed and post EIP-155 if tx.Protected() { enc.ChainID = (*hexutil.Big)(tx.ChainId()) }