Skip to content

Commit 862d7b6

Browse files
authored
Move ChainID from CommonTx to DynamicFeeTransaction (#7732)
For legacy transactions ChainID is optional (missing in pre-[EIP155](https://eips.ethereum.org/EIPS/eip-155) transactions) and is derived from `V` anyway. Also, cherry pick ethereum/go-ethereum#27452.
1 parent 1b14785 commit 862d7b6

File tree

11 files changed

+54
-46
lines changed

11 files changed

+54
-46
lines changed

cmd/evm/internal/t8ntool/transition.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -447,13 +447,13 @@ func getTransaction(txJson commands.RPCTransaction) (types.Transaction, error) {
447447

448448
dynamicFeeTx := types.DynamicFeeTransaction{
449449
CommonTx: types.CommonTx{
450-
ChainID: chainId,
451-
Nonce: uint64(txJson.Nonce),
452-
To: txJson.To,
453-
Value: value,
454-
Gas: uint64(txJson.Gas),
455-
Data: txJson.Input,
450+
Nonce: uint64(txJson.Nonce),
451+
To: txJson.To,
452+
Value: value,
453+
Gas: uint64(txJson.Gas),
454+
Data: txJson.Input,
456455
},
456+
ChainID: chainId,
457457
Tip: tip,
458458
FeeCap: feeCap,
459459
AccessList: *txJson.Accesses,

core/types/block_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,12 +115,12 @@ func TestEIP1559BlockEncoding(t *testing.T) {
115115
feeCap, _ := uint256.FromBig(block.BaseFee())
116116
var tx2 Transaction = &DynamicFeeTransaction{
117117
CommonTx: CommonTx{
118-
ChainID: u256.Num1,
119-
Nonce: 0,
120-
To: &to,
121-
Gas: 123457,
122-
Data: []byte{},
118+
Nonce: 0,
119+
To: &to,
120+
Gas: 123457,
121+
Data: []byte{},
123122
},
123+
ChainID: u256.Num1,
124124
FeeCap: feeCap,
125125
Tip: u256.Num0,
126126
AccessList: accesses,

core/types/dynamic_fee_tx.go

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"math/bits"
2525

2626
"github.com/holiman/uint256"
27+
2728
"github.com/ledgerwatch/erigon-lib/chain"
2829
libcommon "github.com/ledgerwatch/erigon-lib/common"
2930
types2 "github.com/ledgerwatch/erigon-lib/types"
@@ -35,6 +36,7 @@ import (
3536

3637
type DynamicFeeTransaction struct {
3738
CommonTx
39+
ChainID *uint256.Int
3840
Tip *uint256.Int
3941
FeeCap *uint256.Int
4042
AccessList types2.AccessList
@@ -78,14 +80,14 @@ func (tx DynamicFeeTransaction) copy() *DynamicFeeTransaction {
7880
TransactionMisc: TransactionMisc{
7981
time: tx.time,
8082
},
81-
ChainID: new(uint256.Int),
82-
Nonce: tx.Nonce,
83-
To: tx.To, // TODO: copy pointed-to address
84-
Data: common.CopyBytes(tx.Data),
85-
Gas: tx.Gas,
83+
Nonce: tx.Nonce,
84+
To: tx.To, // TODO: copy pointed-to address
85+
Data: common.CopyBytes(tx.Data),
86+
Gas: tx.Gas,
8687
// These are copied below.
8788
Value: new(uint256.Int),
8889
},
90+
ChainID: new(uint256.Int),
8991
AccessList: make(types2.AccessList, len(tx.AccessList)),
9092
Tip: new(uint256.Int),
9193
FeeCap: new(uint256.Int),
@@ -471,14 +473,14 @@ func (tx *DynamicFeeTransaction) Sender(signer Signer) (libcommon.Address, error
471473
func NewEIP1559Transaction(chainID uint256.Int, nonce uint64, to libcommon.Address, amount *uint256.Int, gasLimit uint64, gasPrice *uint256.Int, gasTip *uint256.Int, gasFeeCap *uint256.Int, data []byte) *DynamicFeeTransaction {
472474
return &DynamicFeeTransaction{
473475
CommonTx: CommonTx{
474-
ChainID: &chainID,
475-
Nonce: nonce,
476-
To: &to,
477-
Value: amount,
478-
Gas: gasLimit,
479-
Data: data,
476+
Nonce: nonce,
477+
To: &to,
478+
Value: amount,
479+
Gas: gasLimit,
480+
Data: data,
480481
},
481-
Tip: gasTip,
482-
FeeCap: gasFeeCap,
482+
ChainID: &chainID,
483+
Tip: gasTip,
484+
FeeCap: gasFeeCap,
483485
}
484486
}

core/types/legacy_tx.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ import (
3636
type CommonTx struct {
3737
TransactionMisc
3838

39-
ChainID *uint256.Int
4039
Nonce uint64 // nonce of sender account
4140
Gas uint64 // gas limit
4241
To *libcommon.Address `rlp:"nil"` // nil means contract creation
@@ -46,7 +45,7 @@ type CommonTx struct {
4645
}
4746

4847
func (ct CommonTx) GetChainID() *uint256.Int {
49-
return ct.ChainID
48+
return nil
5049
}
5150

5251
func (ct CommonTx) GetNonce() uint64 {

core/types/transaction_marshalling.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ func (tx LegacyTx) MarshalJSON() ([]byte, error) {
6262
enc.V = (*hexutil.Big)(tx.V.ToBig())
6363
enc.R = (*hexutil.Big)(tx.R.ToBig())
6464
enc.S = (*hexutil.Big)(tx.S.ToBig())
65+
if tx.Protected() {
66+
enc.ChainID = (*hexutil.Big)(tx.GetChainID().ToBig())
67+
}
6568
return json.Marshal(&enc)
6669
}
6770

core/types/transaction_signing.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,9 @@ func DeriveChainId(v *uint256.Int) *uint256.Int {
336336
if v == 27 || v == 28 {
337337
return new(uint256.Int)
338338
}
339+
if v < 35 {
340+
return nil
341+
}
339342
return new(uint256.Int).SetUint64((v - 35) / 2)
340343
}
341344
r := new(uint256.Int).Sub(v, u256.Num35)

core/types/transaction_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -85,15 +85,15 @@ var (
8585

8686
dynFeeTx = &DynamicFeeTransaction{
8787
CommonTx: CommonTx{
88-
ChainID: u256.Num1,
89-
Nonce: 3,
90-
To: &testAddr,
91-
Value: uint256.NewInt(10),
92-
Gas: 25000,
93-
Data: common.FromHex("5544"),
88+
Nonce: 3,
89+
To: &testAddr,
90+
Value: uint256.NewInt(10),
91+
Gas: 25000,
92+
Data: common.FromHex("5544"),
9493
},
95-
Tip: uint256.NewInt(1),
96-
FeeCap: uint256.NewInt(1),
94+
ChainID: u256.Num1,
95+
Tip: uint256.NewInt(1),
96+
FeeCap: uint256.NewInt(1),
9797
}
9898

9999
signedDynFeeTx, _ = dynFeeTx.WithSignature(

eth/stagedsync/stage_bodies_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func TestBodiesUnwind(t *testing.T) {
2626
defer tx.Rollback()
2727
_, bw := m.NewBlocksIO()
2828

29-
txn := &types.DynamicFeeTransaction{Tip: u256.N1, FeeCap: u256.N1, CommonTx: types.CommonTx{ChainID: u256.N1, Value: u256.N1, Gas: 1, Nonce: 1}}
29+
txn := &types.DynamicFeeTransaction{Tip: u256.N1, FeeCap: u256.N1, ChainID: u256.N1, CommonTx: types.CommonTx{Value: u256.N1, Gas: 1, Nonce: 1}}
3030
buf := bytes.NewBuffer(nil)
3131
err = txn.MarshalBinary(buf)
3232
require.NoError(err)

migrations/txs_begin_end_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import (
2424

2525
func TestTxsBeginEnd(t *testing.T) {
2626
require, tmpDir, db := require.New(t), t.TempDir(), memdb.NewTestDB(t)
27-
txn := &types.DynamicFeeTransaction{Tip: u256.N1, FeeCap: u256.N1, CommonTx: types.CommonTx{ChainID: u256.N1, Value: u256.N1, Gas: 1, Nonce: 1}}
27+
txn := &types.DynamicFeeTransaction{Tip: u256.N1, FeeCap: u256.N1, ChainID: u256.N1, CommonTx: types.CommonTx{Value: u256.N1, Gas: 1, Nonce: 1}}
2828
buf := bytes.NewBuffer(nil)
2929
err := txn.MarshalBinary(buf)
3030
require.NoError(err)

migrations/txs_v3_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import (
2626

2727
func TestTxsV3(t *testing.T) {
2828
require, tmpDir, db := require.New(t), t.TempDir(), memdb.NewTestDB(t)
29-
txn := &types.DynamicFeeTransaction{Tip: u256.N1, FeeCap: u256.N1, CommonTx: types.CommonTx{ChainID: u256.N1, Value: u256.N1, Gas: 1, Nonce: 1}}
29+
txn := &types.DynamicFeeTransaction{Tip: u256.N1, FeeCap: u256.N1, ChainID: u256.N1, CommonTx: types.CommonTx{Value: u256.N1, Gas: 1, Nonce: 1}}
3030
buf := bytes.NewBuffer(nil)
3131
err := txn.MarshalBinary(buf)
3232
require.NoError(err)

0 commit comments

Comments
 (0)