Skip to content

Commit

Permalink
Move ChainID from CommonTx to DynamicFeeTransaction (#7732)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
yperbasis committed Jun 14, 2023
1 parent 1b14785 commit 862d7b6
Show file tree
Hide file tree
Showing 11 changed files with 54 additions and 46 deletions.
12 changes: 6 additions & 6 deletions cmd/evm/internal/t8ntool/transition.go
Expand Up @@ -447,13 +447,13 @@ func getTransaction(txJson commands.RPCTransaction) (types.Transaction, error) {

dynamicFeeTx := types.DynamicFeeTransaction{
CommonTx: types.CommonTx{
ChainID: chainId,
Nonce: uint64(txJson.Nonce),
To: txJson.To,
Value: value,
Gas: uint64(txJson.Gas),
Data: txJson.Input,
Nonce: uint64(txJson.Nonce),
To: txJson.To,
Value: value,
Gas: uint64(txJson.Gas),
Data: txJson.Input,
},
ChainID: chainId,
Tip: tip,
FeeCap: feeCap,
AccessList: *txJson.Accesses,
Expand Down
10 changes: 5 additions & 5 deletions core/types/block_test.go
Expand Up @@ -115,12 +115,12 @@ func TestEIP1559BlockEncoding(t *testing.T) {
feeCap, _ := uint256.FromBig(block.BaseFee())
var tx2 Transaction = &DynamicFeeTransaction{
CommonTx: CommonTx{
ChainID: u256.Num1,
Nonce: 0,
To: &to,
Gas: 123457,
Data: []byte{},
Nonce: 0,
To: &to,
Gas: 123457,
Data: []byte{},
},
ChainID: u256.Num1,
FeeCap: feeCap,
Tip: u256.Num0,
AccessList: accesses,
Expand Down
28 changes: 15 additions & 13 deletions core/types/dynamic_fee_tx.go
Expand Up @@ -24,6 +24,7 @@ import (
"math/bits"

"github.com/holiman/uint256"

"github.com/ledgerwatch/erigon-lib/chain"
libcommon "github.com/ledgerwatch/erigon-lib/common"
types2 "github.com/ledgerwatch/erigon-lib/types"
Expand All @@ -35,6 +36,7 @@ import (

type DynamicFeeTransaction struct {
CommonTx
ChainID *uint256.Int
Tip *uint256.Int
FeeCap *uint256.Int
AccessList types2.AccessList
Expand Down Expand Up @@ -78,14 +80,14 @@ func (tx DynamicFeeTransaction) copy() *DynamicFeeTransaction {
TransactionMisc: TransactionMisc{
time: tx.time,
},
ChainID: new(uint256.Int),
Nonce: tx.Nonce,
To: tx.To, // TODO: copy pointed-to address
Data: common.CopyBytes(tx.Data),
Gas: tx.Gas,
Nonce: tx.Nonce,
To: tx.To, // TODO: copy pointed-to address
Data: common.CopyBytes(tx.Data),
Gas: tx.Gas,
// These are copied below.
Value: new(uint256.Int),
},
ChainID: new(uint256.Int),
AccessList: make(types2.AccessList, len(tx.AccessList)),
Tip: new(uint256.Int),
FeeCap: new(uint256.Int),
Expand Down Expand Up @@ -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 {
return &DynamicFeeTransaction{
CommonTx: CommonTx{
ChainID: &chainID,
Nonce: nonce,
To: &to,
Value: amount,
Gas: gasLimit,
Data: data,
Nonce: nonce,
To: &to,
Value: amount,
Gas: gasLimit,
Data: data,
},
Tip: gasTip,
FeeCap: gasFeeCap,
ChainID: &chainID,
Tip: gasTip,
FeeCap: gasFeeCap,
}
}
3 changes: 1 addition & 2 deletions core/types/legacy_tx.go
Expand Up @@ -36,7 +36,6 @@ import (
type CommonTx struct {
TransactionMisc

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

func (ct CommonTx) GetChainID() *uint256.Int {
return ct.ChainID
return nil
}

func (ct CommonTx) GetNonce() uint64 {
Expand Down
3 changes: 3 additions & 0 deletions core/types/transaction_marshalling.go
Expand Up @@ -62,6 +62,9 @@ func (tx LegacyTx) MarshalJSON() ([]byte, error) {
enc.V = (*hexutil.Big)(tx.V.ToBig())
enc.R = (*hexutil.Big)(tx.R.ToBig())
enc.S = (*hexutil.Big)(tx.S.ToBig())
if tx.Protected() {
enc.ChainID = (*hexutil.Big)(tx.GetChainID().ToBig())
}
return json.Marshal(&enc)
}

Expand Down
3 changes: 3 additions & 0 deletions core/types/transaction_signing.go
Expand Up @@ -336,6 +336,9 @@ func DeriveChainId(v *uint256.Int) *uint256.Int {
if v == 27 || v == 28 {
return new(uint256.Int)
}
if v < 35 {
return nil
}
return new(uint256.Int).SetUint64((v - 35) / 2)
}
r := new(uint256.Int).Sub(v, u256.Num35)
Expand Down
16 changes: 8 additions & 8 deletions core/types/transaction_test.go
Expand Up @@ -85,15 +85,15 @@ var (

dynFeeTx = &DynamicFeeTransaction{
CommonTx: CommonTx{
ChainID: u256.Num1,
Nonce: 3,
To: &testAddr,
Value: uint256.NewInt(10),
Gas: 25000,
Data: common.FromHex("5544"),
Nonce: 3,
To: &testAddr,
Value: uint256.NewInt(10),
Gas: 25000,
Data: common.FromHex("5544"),
},
Tip: uint256.NewInt(1),
FeeCap: uint256.NewInt(1),
ChainID: u256.Num1,
Tip: uint256.NewInt(1),
FeeCap: uint256.NewInt(1),
}

signedDynFeeTx, _ = dynFeeTx.WithSignature(
Expand Down
2 changes: 1 addition & 1 deletion eth/stagedsync/stage_bodies_test.go
Expand Up @@ -26,7 +26,7 @@ func TestBodiesUnwind(t *testing.T) {
defer tx.Rollback()
_, 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)
err = txn.MarshalBinary(buf)
require.NoError(err)
Expand Down
2 changes: 1 addition & 1 deletion migrations/txs_begin_end_test.go
Expand Up @@ -24,7 +24,7 @@ import (

func TestTxsBeginEnd(t *testing.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)
err := txn.MarshalBinary(buf)
require.NoError(err)
Expand Down
2 changes: 1 addition & 1 deletion migrations/txs_v3_test.go
Expand Up @@ -26,7 +26,7 @@ import (

func TestTxsV3(t *testing.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)
err := txn.MarshalBinary(buf)
require.NoError(err)
Expand Down
19 changes: 10 additions & 9 deletions turbo/stages/blockchain_test.go
Expand Up @@ -26,17 +26,17 @@ import (
"testing"

"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"
chain2 "github.com/ledgerwatch/erigon-lib/chain"
libcommon "github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon-lib/common/length"
"github.com/ledgerwatch/erigon-lib/kv"
"github.com/ledgerwatch/erigon-lib/kv/bitmapdb"
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/u256"
Expand All @@ -49,6 +49,7 @@ import (
"github.com/ledgerwatch/erigon/crypto"
"github.com/ledgerwatch/erigon/ethdb/prune"
"github.com/ledgerwatch/erigon/params"
"github.com/ledgerwatch/erigon/turbo/services"
"github.com/ledgerwatch/erigon/turbo/stages"
)

Expand Down Expand Up @@ -2128,12 +2129,12 @@ func TestEIP1559Transition(t *testing.T) {
chainID.SetFromBig(gspec.Config.ChainID)
var tx types.Transaction = &types.DynamicFeeTransaction{
CommonTx: types.CommonTx{
ChainID: &chainID,
Nonce: 0,
To: &aa,
Gas: 30000,
Data: []byte{},
Nonce: 0,
To: &aa,
Gas: 30000,
Data: []byte{},
},
ChainID: &chainID,
FeeCap: new(uint256.Int).Mul(new(uint256.Int).SetUint64(5), new(uint256.Int).SetUint64(params.GWei)),
Tip: u256.Num2,
AccessList: accesses,
Expand Down

0 comments on commit 862d7b6

Please sign in to comment.