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 https://github.com/ethereum/go-ethereum/pull/27452.
This commit is contained in:
Andrew Ashikhmin 2023-06-14 11:44:05 +02:00 committed by GitHub
parent 1b14785d79
commit 862d7b6bc6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 54 additions and 46 deletions

View File

@ -447,13 +447,13 @@ func getTransaction(txJson commands.RPCTransaction) (types.Transaction, error) {
dynamicFeeTx := types.DynamicFeeTransaction{ dynamicFeeTx := types.DynamicFeeTransaction{
CommonTx: types.CommonTx{ CommonTx: types.CommonTx{
ChainID: chainId, Nonce: uint64(txJson.Nonce),
Nonce: uint64(txJson.Nonce), To: txJson.To,
To: txJson.To, Value: value,
Value: value, Gas: uint64(txJson.Gas),
Gas: uint64(txJson.Gas), Data: txJson.Input,
Data: txJson.Input,
}, },
ChainID: chainId,
Tip: tip, Tip: tip,
FeeCap: feeCap, FeeCap: feeCap,
AccessList: *txJson.Accesses, AccessList: *txJson.Accesses,

View File

@ -115,12 +115,12 @@ func TestEIP1559BlockEncoding(t *testing.T) {
feeCap, _ := uint256.FromBig(block.BaseFee()) feeCap, _ := uint256.FromBig(block.BaseFee())
var tx2 Transaction = &DynamicFeeTransaction{ var tx2 Transaction = &DynamicFeeTransaction{
CommonTx: CommonTx{ CommonTx: CommonTx{
ChainID: u256.Num1, Nonce: 0,
Nonce: 0, To: &to,
To: &to, Gas: 123457,
Gas: 123457, Data: []byte{},
Data: []byte{},
}, },
ChainID: u256.Num1,
FeeCap: feeCap, FeeCap: feeCap,
Tip: u256.Num0, Tip: u256.Num0,
AccessList: accesses, AccessList: accesses,

View File

@ -24,6 +24,7 @@ import (
"math/bits" "math/bits"
"github.com/holiman/uint256" "github.com/holiman/uint256"
"github.com/ledgerwatch/erigon-lib/chain" "github.com/ledgerwatch/erigon-lib/chain"
libcommon "github.com/ledgerwatch/erigon-lib/common" libcommon "github.com/ledgerwatch/erigon-lib/common"
types2 "github.com/ledgerwatch/erigon-lib/types" types2 "github.com/ledgerwatch/erigon-lib/types"
@ -35,6 +36,7 @@ import (
type DynamicFeeTransaction struct { type DynamicFeeTransaction struct {
CommonTx CommonTx
ChainID *uint256.Int
Tip *uint256.Int Tip *uint256.Int
FeeCap *uint256.Int FeeCap *uint256.Int
AccessList types2.AccessList AccessList types2.AccessList
@ -78,14 +80,14 @@ func (tx DynamicFeeTransaction) copy() *DynamicFeeTransaction {
TransactionMisc: TransactionMisc{ TransactionMisc: TransactionMisc{
time: tx.time, time: tx.time,
}, },
ChainID: new(uint256.Int), Nonce: tx.Nonce,
Nonce: tx.Nonce, To: tx.To, // TODO: copy pointed-to address
To: tx.To, // TODO: copy pointed-to address Data: common.CopyBytes(tx.Data),
Data: common.CopyBytes(tx.Data), Gas: tx.Gas,
Gas: tx.Gas,
// These are copied below. // These are copied below.
Value: new(uint256.Int), Value: new(uint256.Int),
}, },
ChainID: new(uint256.Int),
AccessList: make(types2.AccessList, len(tx.AccessList)), AccessList: make(types2.AccessList, len(tx.AccessList)),
Tip: new(uint256.Int), Tip: new(uint256.Int),
FeeCap: new(uint256.Int), FeeCap: new(uint256.Int),
@ -471,14 +473,14 @@ func (tx *DynamicFeeTransaction) Sender(signer Signer) (libcommon.Address, error
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 { 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 {
return &DynamicFeeTransaction{ return &DynamicFeeTransaction{
CommonTx: CommonTx{ CommonTx: CommonTx{
ChainID: &chainID, Nonce: nonce,
Nonce: nonce, To: &to,
To: &to, Value: amount,
Value: amount, Gas: gasLimit,
Gas: gasLimit, Data: data,
Data: data,
}, },
Tip: gasTip, ChainID: &chainID,
FeeCap: gasFeeCap, Tip: gasTip,
FeeCap: gasFeeCap,
} }
} }

View File

@ -36,7 +36,6 @@ import (
type CommonTx struct { type CommonTx struct {
TransactionMisc TransactionMisc
ChainID *uint256.Int
Nonce uint64 // nonce of sender account Nonce uint64 // nonce of sender account
Gas uint64 // gas limit Gas uint64 // gas limit
To *libcommon.Address `rlp:"nil"` // nil means contract creation To *libcommon.Address `rlp:"nil"` // nil means contract creation
@ -46,7 +45,7 @@ type CommonTx struct {
} }
func (ct CommonTx) GetChainID() *uint256.Int { func (ct CommonTx) GetChainID() *uint256.Int {
return ct.ChainID return nil
} }
func (ct CommonTx) GetNonce() uint64 { func (ct CommonTx) GetNonce() uint64 {

View File

@ -62,6 +62,9 @@ func (tx LegacyTx) MarshalJSON() ([]byte, error) {
enc.V = (*hexutil.Big)(tx.V.ToBig()) enc.V = (*hexutil.Big)(tx.V.ToBig())
enc.R = (*hexutil.Big)(tx.R.ToBig()) enc.R = (*hexutil.Big)(tx.R.ToBig())
enc.S = (*hexutil.Big)(tx.S.ToBig()) enc.S = (*hexutil.Big)(tx.S.ToBig())
if tx.Protected() {
enc.ChainID = (*hexutil.Big)(tx.GetChainID().ToBig())
}
return json.Marshal(&enc) return json.Marshal(&enc)
} }

View File

@ -336,6 +336,9 @@ func DeriveChainId(v *uint256.Int) *uint256.Int {
if v == 27 || v == 28 { if v == 27 || v == 28 {
return new(uint256.Int) return new(uint256.Int)
} }
if v < 35 {
return nil
}
return new(uint256.Int).SetUint64((v - 35) / 2) return new(uint256.Int).SetUint64((v - 35) / 2)
} }
r := new(uint256.Int).Sub(v, u256.Num35) r := new(uint256.Int).Sub(v, u256.Num35)

View File

@ -85,15 +85,15 @@ var (
dynFeeTx = &DynamicFeeTransaction{ dynFeeTx = &DynamicFeeTransaction{
CommonTx: CommonTx{ CommonTx: CommonTx{
ChainID: u256.Num1, Nonce: 3,
Nonce: 3, To: &testAddr,
To: &testAddr, Value: uint256.NewInt(10),
Value: uint256.NewInt(10), Gas: 25000,
Gas: 25000, Data: common.FromHex("5544"),
Data: common.FromHex("5544"),
}, },
Tip: uint256.NewInt(1), ChainID: u256.Num1,
FeeCap: uint256.NewInt(1), Tip: uint256.NewInt(1),
FeeCap: uint256.NewInt(1),
} }
signedDynFeeTx, _ = dynFeeTx.WithSignature( signedDynFeeTx, _ = dynFeeTx.WithSignature(

View File

@ -26,7 +26,7 @@ func TestBodiesUnwind(t *testing.T) {
defer tx.Rollback() defer tx.Rollback()
_, bw := m.NewBlocksIO() _, bw := m.NewBlocksIO()
txn := &types.DynamicFeeTransaction{Tip: u256.N1, FeeCap: u256.N1, CommonTx: types.CommonTx{ChainID: u256.N1, Value: u256.N1, Gas: 1, Nonce: 1}} txn := &types.DynamicFeeTransaction{Tip: u256.N1, FeeCap: u256.N1, ChainID: u256.N1, CommonTx: types.CommonTx{Value: u256.N1, Gas: 1, Nonce: 1}}
buf := bytes.NewBuffer(nil) buf := bytes.NewBuffer(nil)
err = txn.MarshalBinary(buf) err = txn.MarshalBinary(buf)
require.NoError(err) require.NoError(err)

View File

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

View File

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

View File

@ -26,6 +26,10 @@ import (
"testing" "testing"
"github.com/holiman/uint256" "github.com/holiman/uint256"
"github.com/ledgerwatch/log/v3"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/ledgerwatch/erigon-lib/chain" "github.com/ledgerwatch/erigon-lib/chain"
chain2 "github.com/ledgerwatch/erigon-lib/chain" chain2 "github.com/ledgerwatch/erigon-lib/chain"
libcommon "github.com/ledgerwatch/erigon-lib/common" libcommon "github.com/ledgerwatch/erigon-lib/common"
@ -33,10 +37,6 @@ import (
"github.com/ledgerwatch/erigon-lib/kv" "github.com/ledgerwatch/erigon-lib/kv"
"github.com/ledgerwatch/erigon-lib/kv/bitmapdb" "github.com/ledgerwatch/erigon-lib/kv/bitmapdb"
types2 "github.com/ledgerwatch/erigon-lib/types" types2 "github.com/ledgerwatch/erigon-lib/types"
"github.com/ledgerwatch/erigon/turbo/services"
"github.com/ledgerwatch/log/v3"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/ledgerwatch/erigon/common/hexutil" "github.com/ledgerwatch/erigon/common/hexutil"
"github.com/ledgerwatch/erigon/common/u256" "github.com/ledgerwatch/erigon/common/u256"
@ -49,6 +49,7 @@ import (
"github.com/ledgerwatch/erigon/crypto" "github.com/ledgerwatch/erigon/crypto"
"github.com/ledgerwatch/erigon/ethdb/prune" "github.com/ledgerwatch/erigon/ethdb/prune"
"github.com/ledgerwatch/erigon/params" "github.com/ledgerwatch/erigon/params"
"github.com/ledgerwatch/erigon/turbo/services"
"github.com/ledgerwatch/erigon/turbo/stages" "github.com/ledgerwatch/erigon/turbo/stages"
) )
@ -2128,12 +2129,12 @@ func TestEIP1559Transition(t *testing.T) {
chainID.SetFromBig(gspec.Config.ChainID) chainID.SetFromBig(gspec.Config.ChainID)
var tx types.Transaction = &types.DynamicFeeTransaction{ var tx types.Transaction = &types.DynamicFeeTransaction{
CommonTx: types.CommonTx{ CommonTx: types.CommonTx{
ChainID: &chainID, Nonce: 0,
Nonce: 0, To: &aa,
To: &aa, Gas: 30000,
Gas: 30000, Data: []byte{},
Data: []byte{},
}, },
ChainID: &chainID,
FeeCap: new(uint256.Int).Mul(new(uint256.Int).SetUint64(5), new(uint256.Int).SetUint64(params.GWei)), FeeCap: new(uint256.Int).Mul(new(uint256.Int).SetUint64(5), new(uint256.Int).SetUint64(params.GWei)),
Tip: u256.Num2, Tip: u256.Num2,
AccessList: accesses, AccessList: accesses,