From 7a8a5864cd54a57f733b6c38e9675e910b30b507 Mon Sep 17 00:00:00 2001 From: Dusan Nosovic Date: Wed, 13 Mar 2024 14:01:27 +0100 Subject: [PATCH 1/6] added contructor for all types of transactions --- types/access_list_tx.go | 6 +++++- types/dynamic_fee_tx.go | 6 +++++- types/legacy_tx.go | 6 +++++- types/state_tx.go | 6 +++++- types/transaction.go | 8 ++++---- 5 files changed, 24 insertions(+), 8 deletions(-) diff --git a/types/access_list_tx.go b/types/access_list_tx.go index e0b47b76ae..4832fec3fb 100644 --- a/types/access_list_tx.go +++ b/types/access_list_tx.go @@ -112,6 +112,10 @@ type AccessListTxn struct { AccessList TxAccessList } +func NewAccessListTx() *AccessListTxn { + return &AccessListTxn{BaseTx: &BaseTx{}} +} + func (tx *AccessListTxn) transactionType() TxType { return AccessListTxType } func (tx *AccessListTxn) chainID() *big.Int { return tx.ChainID } func (tx *AccessListTxn) gasPrice() *big.Int { return tx.GasPrice } @@ -297,7 +301,7 @@ func (tx *AccessListTxn) marshalRLPWith(arena *fastrlp.Arena) *fastrlp.Value { } func (tx *AccessListTxn) copy() TxData { - cpy := &AccessListTxn{} + cpy := NewAccessListTx() if tx.chainID() != nil { chainID := new(big.Int) diff --git a/types/dynamic_fee_tx.go b/types/dynamic_fee_tx.go index eeaa43ee28..df88bc6335 100644 --- a/types/dynamic_fee_tx.go +++ b/types/dynamic_fee_tx.go @@ -16,6 +16,10 @@ type DynamicFeeTx struct { AccessList TxAccessList } +func NewDynamicFeeTx() *DynamicFeeTx { + return &DynamicFeeTx{BaseTx: &BaseTx{}} +} + func (tx *DynamicFeeTx) transactionType() TxType { return DynamicFeeTxType } func (tx *DynamicFeeTx) chainID() *big.Int { return tx.ChainID } func (tx *DynamicFeeTx) gasPrice() *big.Int { return nil } @@ -208,7 +212,7 @@ func (tx *DynamicFeeTx) marshalRLPWith(arena *fastrlp.Arena) *fastrlp.Value { } func (tx *DynamicFeeTx) copy() TxData { - cpy := &DynamicFeeTx{} + cpy := NewDynamicFeeTx() if tx.chainID() != nil { chainID := new(big.Int) diff --git a/types/legacy_tx.go b/types/legacy_tx.go index e9d9cd23ef..233b5c29ae 100644 --- a/types/legacy_tx.go +++ b/types/legacy_tx.go @@ -12,6 +12,10 @@ type LegacyTx struct { GasPrice *big.Int } +func NewLegacyTx() *LegacyTx { + return &LegacyTx{BaseTx: &BaseTx{}} +} + func (tx *LegacyTx) transactionType() TxType { return LegacyTxType } func (tx *LegacyTx) chainID() *big.Int { return deriveChainID(tx.v()) } func (tx *LegacyTx) gasPrice() *big.Int { return tx.GasPrice } @@ -159,7 +163,7 @@ func (tx *LegacyTx) marshalRLPWith(arena *fastrlp.Arena) *fastrlp.Value { } func (tx *LegacyTx) copy() TxData { //nolint:dupl - cpy := &LegacyTx{BaseTx: &BaseTx{}} + cpy := NewLegacyTx() if tx.gasPrice() != nil { gasPrice := new(big.Int) diff --git a/types/state_tx.go b/types/state_tx.go index c6f7b6b150..639303b40c 100644 --- a/types/state_tx.go +++ b/types/state_tx.go @@ -12,6 +12,10 @@ type StateTx struct { GasPrice *big.Int } +func NewStateTx() *StateTx { + return &StateTx{BaseTx: &BaseTx{}} +} + func (tx *StateTx) transactionType() TxType { return StateTxType } func (tx *StateTx) chainID() *big.Int { return deriveChainID(tx.v()) } func (tx *StateTx) gasPrice() *big.Int { return tx.GasPrice } @@ -175,7 +179,7 @@ func (tx *StateTx) marshalRLPWith(arena *fastrlp.Arena) *fastrlp.Value { } func (tx *StateTx) copy() TxData { //nolint:dupl - cpy := &StateTx{} + cpy := NewStateTx() if tx.gasPrice() != nil { gasPrice := new(big.Int) diff --git a/types/transaction.go b/types/transaction.go index 00a6bc07e5..a500a6f074 100644 --- a/types/transaction.go +++ b/types/transaction.go @@ -74,13 +74,13 @@ func NewTx(inner TxData) *Transaction { func (t *Transaction) InitInnerData(txType TxType) { switch txType { case AccessListTxType: - t.Inner = &AccessListTxn{BaseTx: &BaseTx{}} + t.Inner = NewAccessListTx() case StateTxType: - t.Inner = &StateTx{BaseTx: &BaseTx{}} + t.Inner = NewStateTx() case LegacyTxType: - t.Inner = &LegacyTx{BaseTx: &BaseTx{}} + t.Inner = NewLegacyTx() default: - t.Inner = &DynamicFeeTx{BaseTx: &BaseTx{}} + t.Inner = NewDynamicFeeTx() } } From 335f9c848b590dbe96e81bfa4fd9cec4ee6779ad Mon Sep 17 00:00:00 2001 From: Dusan Nosovic Date: Wed, 13 Mar 2024 14:01:27 +0100 Subject: [PATCH 2/6] legacy transaction test fix --- blockchain/blockchain_test.go | 67 ++++++---------- blockchain/storage/testing.go | 40 +++++----- consensus/polybft/block_builder_test.go | 16 ++-- crypto/txsigner_eip155_test.go | 24 +++--- crypto/txsigner_frontier_test.go | 13 ++- crypto/txsigner_london_test.go | 12 ++- e2e/framework/testserver.go | 20 +++-- e2e/txpool_test.go | 73 ++++++++--------- helper/tests/testing.go | 23 +++--- jsonrpc/eth_blockchain_test.go | 4 +- jsonrpc/eth_endpoint_test.go | 80 +++++++++---------- jsonrpc/eth_txpool_test.go | 24 +++--- jsonrpc/filter_manager_fuzz_test.go | 30 +++---- jsonrpc/filter_manager_test.go | 6 +- jsonrpc/helper_test.go | 100 +++++++++++------------- jsonrpc/txpool_endpoint_test.go | 24 +++--- jsonrpc/types_test.go | 26 +++--- state/transition_test.go | 12 ++- tests/state_test_util.go | 20 +++-- types/access_list_tx.go | 10 ++- types/dynamic_fee_tx.go | 10 ++- types/legacy_tx.go | 10 ++- types/state_tx.go | 10 ++- types/transaction.go | 80 +++++++++++++++++++ 24 files changed, 374 insertions(+), 360 deletions(-) diff --git a/blockchain/blockchain_test.go b/blockchain/blockchain_test.go index 1932060ac3..8b66c81b80 100644 --- a/blockchain/blockchain_test.go +++ b/blockchain/blockchain_test.go @@ -590,13 +590,8 @@ func TestBlockchainWriteBody(t *testing.T) { t.Run("should succeed if tx has from field", func(t *testing.T) { t.Parallel() - tx := types.NewTx(&types.LegacyTx{ - BaseTx: &types.BaseTx{ - Value: big.NewInt(10), - V: big.NewInt(1), - From: addr, - }, - }) + tx := types.NewTx(types.NewLegacyTx(types.WithValue(big.NewInt(10)), + types.WithSignatureValues(big.NewInt(1), nil, nil), types.WithFrom(addr))) block := &types.Block{ Header: &types.Header{}, @@ -624,12 +619,8 @@ func TestBlockchainWriteBody(t *testing.T) { t.Run("should return error if tx doesn't have from and recovering address fails", func(t *testing.T) { t.Parallel() - tx := types.NewTx(&types.LegacyTx{ - BaseTx: &types.BaseTx{ - Value: big.NewInt(10), - V: big.NewInt(1), - }, - }) + tx := types.NewTx(types.NewLegacyTx(types.WithValue(big.NewInt(10)), + types.WithSignatureValues(big.NewInt(1), nil, nil))) block := &types.Block{ Header: &types.Header{}, @@ -658,12 +649,8 @@ func TestBlockchainWriteBody(t *testing.T) { t.Run("should recover from address and store to storage", func(t *testing.T) { t.Parallel() - tx := types.NewTx(&types.LegacyTx{ - BaseTx: &types.BaseTx{ - Value: big.NewInt(10), - V: big.NewInt(1), - }, - }) + tx := types.NewTx(types.NewLegacyTx(types.WithValue(big.NewInt(10)), + types.WithSignatureValues(big.NewInt(1), nil, nil))) block := &types.Block{ Header: &types.Header{}, @@ -721,8 +708,8 @@ func Test_recoverFromFieldsInBlock(t *testing.T) { }, } - tx1 := types.NewTx(&types.LegacyTx{BaseTx: &types.BaseTx{Nonce: 0, From: addr1}}) - tx2 := types.NewTx(&types.LegacyTx{BaseTx: &types.BaseTx{Nonce: 1, From: types.ZeroAddress}}) + tx1 := types.NewTx(types.NewLegacyTx(types.WithNonce(0), types.WithFrom(addr1))) + tx2 := types.NewTx(types.NewLegacyTx(types.WithNonce(1), types.WithFrom(types.ZeroAddress))) computeTxHashes(tx1, tx2) @@ -751,9 +738,9 @@ func Test_recoverFromFieldsInBlock(t *testing.T) { }, } - tx1 := types.NewTx(&types.LegacyTx{BaseTx: &types.BaseTx{Nonce: 0, From: types.ZeroAddress}}) - tx2 := types.NewTx(&types.LegacyTx{BaseTx: &types.BaseTx{Nonce: 1, From: types.ZeroAddress}}) - tx3 := types.NewTx(&types.LegacyTx{BaseTx: &types.BaseTx{Nonce: 2, From: types.ZeroAddress}}) + tx1 := types.NewTx(types.NewLegacyTx(types.WithNonce(0), types.WithFrom(types.ZeroAddress))) + tx2 := types.NewTx(types.NewLegacyTx(types.WithNonce(1), types.WithFrom(types.ZeroAddress))) + tx3 := types.NewTx(types.NewLegacyTx(types.WithNonce(2), types.WithFrom(types.ZeroAddress))) computeTxHashes(tx1, tx2, tx3) @@ -807,8 +794,8 @@ func Test_recoverFromFieldsInTransactions(t *testing.T) { }, } - tx1 := types.NewTx(&types.LegacyTx{BaseTx: &types.BaseTx{Nonce: 0, From: addr1}}) - tx2 := types.NewTx(&types.LegacyTx{BaseTx: &types.BaseTx{Nonce: 1, From: types.ZeroAddress}}) + tx1 := types.NewTx(types.NewLegacyTx(types.WithNonce(0), types.WithFrom(addr1))) + tx2 := types.NewTx(types.NewLegacyTx(types.WithNonce(1), types.WithFrom(types.ZeroAddress))) computeTxHashes(tx1, tx2) @@ -836,9 +823,9 @@ func Test_recoverFromFieldsInTransactions(t *testing.T) { }, } - tx1 := types.NewTx(&types.LegacyTx{BaseTx: &types.BaseTx{Nonce: 0, From: types.ZeroAddress}}) - tx2 := types.NewTx(&types.LegacyTx{BaseTx: &types.BaseTx{Nonce: 1, From: types.ZeroAddress}}) - tx3 := types.NewTx(&types.LegacyTx{BaseTx: &types.BaseTx{Nonce: 2, From: types.ZeroAddress}}) + tx1 := types.NewTx(types.NewLegacyTx(types.WithNonce(0), types.WithFrom(types.ZeroAddress))) + tx2 := types.NewTx(types.NewLegacyTx(types.WithNonce(1), types.WithFrom(types.ZeroAddress))) + tx3 := types.NewTx(types.NewLegacyTx(types.WithNonce(2), types.WithFrom(types.ZeroAddress))) computeTxHashes(tx1, tx2, tx3) @@ -870,8 +857,8 @@ func Test_recoverFromFieldsInTransactions(t *testing.T) { }, } - tx1 := types.NewTx(&types.LegacyTx{BaseTx: &types.BaseTx{Nonce: 0, From: addr1}}) - tx2 := types.NewTx(&types.LegacyTx{BaseTx: &types.BaseTx{Nonce: 1, From: addr2}}) + tx1 := types.NewTx(types.NewLegacyTx(types.WithNonce(0), types.WithFrom(addr1))) + tx2 := types.NewTx(types.NewLegacyTx(types.WithNonce(1), types.WithFrom(addr2))) computeTxHashes(tx1, tx2) @@ -906,12 +893,10 @@ func TestBlockchainReadBody(t *testing.T) { batchWriter := storage.NewBatchWriter(b.db) - tx := types.NewTx(&types.LegacyTx{ - BaseTx: &types.BaseTx{ - Value: big.NewInt(10), - V: big.NewInt(1), - }, - }) + tx := types.NewTx(types.NewLegacyTx( + types.WithValue(big.NewInt(10)), + types.WithSignatureValues(big.NewInt(1), nil, nil), + )) tx.ComputeHash() @@ -1610,11 +1595,9 @@ func TestBlockchain_WriteFullBlock(t *testing.T) { {GasUsed: 100}, {GasUsed: 200}, } - tx := types.NewTx(&types.LegacyTx{ - BaseTx: &types.BaseTx{ - Value: big.NewInt(1), - }, - }) + tx := types.NewTx(types.NewLegacyTx( + types.WithValue(big.NewInt(1)), + )) tx.ComputeHash() header.ComputeHash() diff --git a/blockchain/storage/testing.go b/blockchain/storage/testing.go index 6e243aa8e3..f073b55227 100644 --- a/blockchain/storage/testing.go +++ b/blockchain/storage/testing.go @@ -270,31 +270,27 @@ func testBody(t *testing.T, m PlaceholderStorage) { require.NoError(t, batch.WriteBatch()) addr1 := types.StringToAddress("11") - t0 := types.NewTx(&types.LegacyTx{ - GasPrice: big.NewInt(11), - BaseTx: &types.BaseTx{ - Nonce: 0, - To: &addr1, - Value: big.NewInt(1), - Gas: 11, - Input: []byte{1, 2}, - V: big.NewInt(1), - }, - }) + t0 := types.NewTx(types.NewLegacyTx( + types.WithGasPrice(big.NewInt(11)), + types.WithNonce(0), + types.WithTo(&addr1), + types.WithValue(big.NewInt(1)), + types.WithGas(11), + types.WithInput([]byte{1, 2}), + types.WithSignatureValues(big.NewInt(1), nil, nil), + )) t0.ComputeHash() addr2 := types.StringToAddress("22") - t1 := types.NewTx(&types.LegacyTx{ - GasPrice: big.NewInt(11), - BaseTx: &types.BaseTx{ - Nonce: 0, - To: &addr2, - Value: big.NewInt(1), - Gas: 22, - Input: []byte{4, 5}, - V: big.NewInt(2), - }, - }) + t1 := types.NewTx(types.NewLegacyTx( + types.WithGasPrice(big.NewInt(11)), + types.WithNonce(0), + types.WithTo(&addr2), + types.WithValue(big.NewInt(1)), + types.WithGas(22), + types.WithInput([]byte{4, 5}), + types.WithSignatureValues(big.NewInt(2), nil, nil), + )) t1.ComputeHash() block := types.Block{ diff --git a/consensus/polybft/block_builder_test.go b/consensus/polybft/block_builder_test.go index 4dbd7df083..041ddfb30a 100644 --- a/consensus/polybft/block_builder_test.go +++ b/consensus/polybft/block_builder_test.go @@ -93,15 +93,13 @@ func TestBlockBuilder_BuildBlockTxOneFailedTxAndOneTakesTooMuchGas(t *testing.T) gas = blockGasLimit - 1 } - tx := types.NewTx(&types.LegacyTx{ - GasPrice: big.NewInt(gasPrice), - BaseTx: &types.BaseTx{ - Value: big.NewInt(amount), - Gas: gas, - Nonce: 0, - To: &acc.address, - }, - }) + tx := types.NewTx(types.NewLegacyTx( + types.WithGasPrice(big.NewInt(gasPrice)), + types.WithValue(big.NewInt(amount)), + types.WithGas(gas), + types.WithNonce(0), + types.WithTo(&acc.address), + )) tx, err = signer.SignTx(tx, acc.privKey) require.NoError(t, err) diff --git a/crypto/txsigner_eip155_test.go b/crypto/txsigner_eip155_test.go index fdb88ef9b0..6489b6d110 100644 --- a/crypto/txsigner_eip155_test.go +++ b/crypto/txsigner_eip155_test.go @@ -70,13 +70,11 @@ func TestEIP155Signer_Sender(t *testing.T) { t.Fatalf("Unable to generate key") } - txn := types.NewTx(&types.LegacyTx{ - GasPrice: big.NewInt(0), - BaseTx: &types.BaseTx{ - To: &toAddress, - Value: big.NewInt(1), - }, - }) + txn := types.NewTx(types.NewLegacyTx( + types.WithGasPrice(big.NewInt(0)), + types.WithTo(&toAddress), + types.WithValue(big.NewInt(1)), + )) signer := NewEIP155Signer( testCase.chainID.Uint64(), @@ -107,13 +105,11 @@ func TestEIP155Signer_ChainIDMismatch(t *testing.T) { t.Fatalf("Unable to generate key") } - txn := types.NewTx(&types.LegacyTx{ - GasPrice: big.NewInt(0), - BaseTx: &types.BaseTx{ - To: &toAddress, - Value: big.NewInt(1), - }, - }) + txn := types.NewTx(types.NewLegacyTx( + types.WithGasPrice(big.NewInt(0)), + types.WithTo(&toAddress), + types.WithValue(big.NewInt(1)), + )) signer := NewEIP155Signer(chainIDTop) diff --git a/crypto/txsigner_frontier_test.go b/crypto/txsigner_frontier_test.go index 07ffda27ac..80dba4c6af 100644 --- a/crypto/txsigner_frontier_test.go +++ b/crypto/txsigner_frontier_test.go @@ -15,13 +15,12 @@ func TestFrontierSigner(t *testing.T) { key, err := GenerateECDSAKey() assert.NoError(t, err) - txn := types.NewTx(&types.LegacyTx{ - GasPrice: big.NewInt(0), - BaseTx: &types.BaseTx{ - To: &toAddress, - Value: big.NewInt(10), - }, - }) + txn := types.NewTx(types.NewLegacyTx( + types.WithGasPrice(big.NewInt(0)), + types.WithTo(&toAddress), + types.WithValue(big.NewInt(10)), + )) + signedTx, err := signer.SignTx(txn, key) assert.NoError(t, err) diff --git a/crypto/txsigner_london_test.go b/crypto/txsigner_london_test.go index 5f42c3a147..146de82fe1 100644 --- a/crypto/txsigner_london_test.go +++ b/crypto/txsigner_london_test.go @@ -83,13 +83,11 @@ func TestLondonSignerSender(t *testing.T) { }, }) case types.LegacyTxType: - txn = types.NewTx(&types.LegacyTx{ - GasPrice: big.NewInt(5), - BaseTx: &types.BaseTx{ - To: &recipient, - Value: big.NewInt(1), - }, - }) + txn = types.NewTx(types.NewLegacyTx( + types.WithGasPrice(big.NewInt(5)), + types.WithTo(&recipient), + types.WithValue(big.NewInt(1)), + )) case types.StateTxType: txn = types.NewTx(&types.StateTx{ GasPrice: big.NewInt(5), diff --git a/e2e/framework/testserver.go b/e2e/framework/testserver.go index f9bb407383..450ce6a6b0 100644 --- a/e2e/framework/testserver.go +++ b/e2e/framework/testserver.go @@ -520,17 +520,15 @@ func (t *TestServer) SendRawTx( return nil, err } - signedTx, err := t.SignTx(types.NewTx(&types.LegacyTx{ - GasPrice: tx.GasPrice, - BaseTx: &types.BaseTx{ - Gas: tx.Gas, - To: tx.To, - Value: tx.Value, - Input: tx.Input, - Nonce: nextNonce, - From: tx.From, - }, - }), signerKey) + signedTx, err := t.SignTx(types.NewTx(types.NewLegacyTx( + types.WithGasPrice(tx.GasPrice), + types.WithGas(tx.Gas), + types.WithTo(tx.To), + types.WithValue(tx.Value), + types.WithInput(tx.Input), + types.WithNonce(nextNonce), + types.WithFrom(tx.From), + )), signerKey) if err != nil { return nil, err } diff --git a/e2e/txpool_test.go b/e2e/txpool_test.go index 1762bdd178..72c642e960 100644 --- a/e2e/txpool_test.go +++ b/e2e/txpool_test.go @@ -46,14 +46,12 @@ func generateTx(params generateTxReqParams) *types.Transaction { var unsignedTx *types.Transaction if params.gasPrice != nil { - unsignedTx = types.NewTx(&types.LegacyTx{ - BaseTx: &types.BaseTx{ - Nonce: params.nonce, - To: ¶ms.toAddress, - Gas: 1000000, - Value: params.value, - }, - }) + unsignedTx = types.NewTx(types.NewLegacyTx( + types.WithNonce(params.nonce), + types.WithTo(¶ms.toAddress), + types.WithGas(1000000), + types.WithValue(params.value), + )) unsignedTx.SetGasPrice(params.gasPrice) } else { unsignedTx = types.NewTx(&types.DynamicFeeTx{ @@ -245,27 +243,23 @@ func TestTxPool_RecoverableError(t *testing.T) { _, receiverAddress := tests.GenerateKeyAndAddr(t) transactions := []*types.Transaction{ - types.NewTx(&types.LegacyTx{ - GasPrice: big.NewInt(framework.DefaultGasPrice), - BaseTx: &types.BaseTx{ - Nonce: 0, - Gas: 22000, - To: &receiverAddress, - Value: oneEth, - V: big.NewInt(27), - From: senderAddress, - }, - }), - types.NewTx(&types.LegacyTx{ - GasPrice: big.NewInt(framework.DefaultGasPrice), - BaseTx: &types.BaseTx{ - Nonce: 1, - Gas: 22000, - To: &receiverAddress, - Value: oneEth, - V: big.NewInt(27), - }, - }), + types.NewTx(types.NewLegacyTx( + types.WithGasPrice(big.NewInt(framework.DefaultGasPrice)), + types.WithNonce(0), + types.WithGas(22000), + types.WithTo(&receiverAddress), + types.WithValue(oneEth), + types.WithSignatureValues(big.NewInt(27), nil, nil), + types.WithFrom(senderAddress), + )), + types.NewTx(types.NewLegacyTx( + types.WithGasPrice(big.NewInt(framework.DefaultGasPrice)), + types.WithNonce(1), + types.WithGas(22000), + types.WithTo(&receiverAddress), + types.WithValue(oneEth), + types.WithSignatureValues(big.NewInt(27), nil, nil), + )), types.NewTx(&types.DynamicFeeTx{ ChainID: new(big.Int).SetUint64(defaultChainID), GasFeeCap: big.NewInt(framework.DefaultGasPrice), @@ -359,17 +353,16 @@ func TestTxPool_GetPendingTx(t *testing.T) { operator := server.TxnPoolOperator() client := server.JSONRPC() - signedTx, err := signer.SignTx(types.NewTx(&types.LegacyTx{ - GasPrice: big.NewInt(1000000000), - BaseTx: &types.BaseTx{ - Nonce: 0, - Gas: framework.DefaultGasLimit - 1, - To: &receiverAddress, - Value: oneEth, - V: big.NewInt(1), - From: types.ZeroAddress, - }, - }), senderKey) + signedTx, err := signer.SignTx(types.NewTx(types.NewLegacyTx( + types.WithGasPrice(big.NewInt(1000000000)), + types.WithNonce(0), + types.WithGas(framework.DefaultGasLimit-1), + types.WithTo(&receiverAddress), + types.WithValue(oneEth), + types.WithSignatureValues(big.NewInt(1), nil, nil), + types.WithFrom(types.ZeroAddress), + ), + ), senderKey) assert.NoError(t, err, "failed to sign transaction") // Add the transaction diff --git a/helper/tests/testing.go b/helper/tests/testing.go index d9c6f2ee15..f781e2ef85 100644 --- a/helper/tests/testing.go +++ b/helper/tests/testing.go @@ -238,19 +238,16 @@ type GenerateTxReqParams struct { func generateTx(params GenerateTxReqParams) (*types.Transaction, error) { signer := crypto.NewEIP155Signer(100) - signedTx, signErr := signer.SignTx(types.NewTx(&types.LegacyTx{ - GasPrice: params.GasPrice, - BaseTx: &types.BaseTx{ - Nonce: params.Nonce, - From: params.ReferenceAddr, - To: ¶ms.ToAddress, - Gas: 1000000, - Value: params.Value, - Input: params.Input, - V: big.NewInt(27), // it is necessary to encode in rlp - }, - }), params.ReferenceKey) - + signedTx, signErr := signer.SignTx(types.NewTx(types.NewLegacyTx( + types.WithGasPrice(params.GasPrice), + types.WithNonce(params.Nonce), + types.WithFrom(params.ReferenceAddr), + types.WithTo(¶ms.ToAddress), + types.WithGas(1000000), + types.WithValue(params.Value), + types.WithInput(params.Input), + types.WithSignatureValues(big.NewInt(27), nil, nil), + )), params.ReferenceKey) if signErr != nil { return nil, fmt.Errorf("unable to sign transaction, %w", signErr) } diff --git a/jsonrpc/eth_blockchain_test.go b/jsonrpc/eth_blockchain_test.go index ad1f1c2e0a..d4448af7be 100644 --- a/jsonrpc/eth_blockchain_test.go +++ b/jsonrpc/eth_blockchain_test.go @@ -89,7 +89,7 @@ func TestEth_Block_GetBlockTransactionCountByHash(t *testing.T) { for i := 0; i < 10; i++ { block.Transactions = append(block.Transactions, []*types.Transaction{ - types.NewTx(&types.LegacyTx{BaseTx: &types.BaseTx{Nonce: 0, From: addr0}})}...) + types.NewTx(types.NewLegacyTx(types.WithNonce(0), types.WithFrom(addr0)))}...) } store.add(block) @@ -108,7 +108,7 @@ func TestEth_Block_GetBlockTransactionCountByNumber(t *testing.T) { for i := 0; i < 10; i++ { block.Transactions = append(block.Transactions, []*types.Transaction{ - types.NewTx(&types.LegacyTx{BaseTx: &types.BaseTx{Nonce: 0, From: addr0}})}...) + types.NewTx(types.NewLegacyTx(types.WithNonce(0), types.WithFrom(addr0)))}...) } store.add(block) diff --git a/jsonrpc/eth_endpoint_test.go b/jsonrpc/eth_endpoint_test.go index 1954dd3c0e..6093b6161b 100644 --- a/jsonrpc/eth_endpoint_test.go +++ b/jsonrpc/eth_endpoint_test.go @@ -69,17 +69,15 @@ func TestEth_DecodeTxn(t *testing.T) { Value: toArgBytesPtr(oneEther.Bytes()), Data: nil, }, - res: types.NewTx(&types.LegacyTx{ - GasPrice: big.NewInt(10000), - BaseTx: &types.BaseTx{ - From: types.ZeroAddress, - To: &addr2, - Gas: 21000, - Value: oneEther, - Input: []byte{}, - Nonce: 0, - }, - }), + res: types.NewTx(types.NewLegacyTx( + types.WithGasPrice(big.NewInt(10000)), + types.WithFrom(types.ZeroAddress), + types.WithTo(&addr2), + types.WithGas(21000), + types.WithValue(oneEther), + types.WithInput([]byte{}), + types.WithNonce(0), + )), err: nil, }, { @@ -97,17 +95,15 @@ func TestEth_DecodeTxn(t *testing.T) { Value: toArgBytesPtr(oneEther.Bytes()), Data: nil, }, - res: types.NewTx(&types.LegacyTx{ - GasPrice: big.NewInt(10000), - BaseTx: &types.BaseTx{ - From: addr1, - To: &addr2, - Gas: 21000, - Value: oneEther, - Input: []byte{}, - Nonce: 10, - }, - }), + res: types.NewTx(types.NewLegacyTx( + types.WithGasPrice(big.NewInt(10000)), + types.WithFrom(addr1), + types.WithTo(&addr2), + types.WithGas(21000), + types.WithValue(oneEther), + types.WithInput([]byte{}), + types.WithNonce(10), + )), err: nil, }, { @@ -120,17 +116,15 @@ func TestEth_DecodeTxn(t *testing.T) { Data: nil, Nonce: toArgUint64Ptr(1), }, - res: types.NewTx(&types.LegacyTx{ - GasPrice: big.NewInt(10000), - BaseTx: &types.BaseTx{ - From: addr1, - To: &addr2, - Gas: 21000, - Value: new(big.Int).SetBytes([]byte{}), - Input: []byte{}, - Nonce: 1, - }, - }), + res: types.NewTx(types.NewLegacyTx( + types.WithGasPrice(big.NewInt(10000)), + types.WithFrom(addr1), + types.WithTo(&addr2), + types.WithGas(21000), + types.WithValue(new(big.Int).SetBytes([]byte{})), + types.WithInput([]byte{}), + types.WithNonce(1), + )), err: nil, }, { @@ -142,17 +136,15 @@ func TestEth_DecodeTxn(t *testing.T) { Data: nil, Nonce: toArgUint64Ptr(1), }, - res: types.NewTx(&types.LegacyTx{ - GasPrice: big.NewInt(10000), - BaseTx: &types.BaseTx{ - Value: new(big.Int).SetBytes([]byte{}), - Input: []byte{}, - Nonce: 1, - From: addr1, - To: &addr2, - Gas: 0, - }, - }), + res: types.NewTx(types.NewLegacyTx( + types.WithGasPrice(big.NewInt(10000)), + types.WithValue(new(big.Int).SetBytes([]byte{})), + types.WithInput([]byte{}), + types.WithNonce(1), + types.WithFrom(addr1), + types.WithTo(&addr2), + types.WithGas(0), + )), err: nil, }, } diff --git a/jsonrpc/eth_txpool_test.go b/jsonrpc/eth_txpool_test.go index c850c9287f..3e2aa54f2c 100644 --- a/jsonrpc/eth_txpool_test.go +++ b/jsonrpc/eth_txpool_test.go @@ -11,12 +11,10 @@ import ( func TestEth_TxnPool_SendRawTransaction(t *testing.T) { store := &mockStoreTxn{} eth := newTestEthEndpoint(store) - txn := types.NewTx(&types.LegacyTx{ - BaseTx: &types.BaseTx{ - From: addr0, - V: big.NewInt(1), - }, - }) + txn := types.NewTx(types.NewLegacyTx( + types.WithFrom(addr0), + types.WithSignatureValues(big.NewInt(1), nil, nil), + )) txn.ComputeHash() data := txn.MarshalRLP() @@ -34,14 +32,12 @@ func TestEth_TxnPool_SendTransaction(t *testing.T) { store := &mockStoreTxn{} store.AddAccount(addr0) eth := newTestEthEndpoint(store) - txToSend := types.NewTx(&types.LegacyTx{ - GasPrice: big.NewInt(int64(1)), - BaseTx: &types.BaseTx{ - From: addr0, - To: argAddrPtr(addr0), - Nonce: uint64(0), - }, - }) + txToSend := types.NewTx(types.NewLegacyTx( + types.WithGasPrice(big.NewInt(1)), + types.WithFrom(addr0), + types.WithTo(argAddrPtr(addr0)), + types.WithNonce(0), + )) _, err := eth.SendRawTransaction(txToSend.MarshalRLP()) assert.NoError(t, err) diff --git a/jsonrpc/filter_manager_fuzz_test.go b/jsonrpc/filter_manager_fuzz_test.go index 49b46d27dc..d9e7d68da9 100644 --- a/jsonrpc/filter_manager_fuzz_test.go +++ b/jsonrpc/filter_manager_fuzz_test.go @@ -33,27 +33,15 @@ func FuzzGetLogsForQuery(f *testing.F) { Hash: types.StringToHash(strconv.Itoa(i)), }, Transactions: []*types.Transaction{ - { - Inner: &types.LegacyTx{ - BaseTx: &types.BaseTx{ - Value: big.NewInt(10), - }, - }, - }, - { - Inner: &types.LegacyTx{ - BaseTx: &types.BaseTx{ - Value: big.NewInt(11), - }, - }, - }, - { - Inner: &types.LegacyTx{ - BaseTx: &types.BaseTx{ - Value: big.NewInt(12), - }, - }, - }, + types.NewTx(types.NewLegacyTx( + types.WithValue(big.NewInt(10)), + )), + types.NewTx(types.NewLegacyTx( + types.WithValue(big.NewInt(11)), + )), + types.NewTx(types.NewLegacyTx( + types.WithValue(big.NewInt(12)), + )), }, } } diff --git a/jsonrpc/filter_manager_test.go b/jsonrpc/filter_manager_test.go index 00693147e2..48e43aca9c 100644 --- a/jsonrpc/filter_manager_test.go +++ b/jsonrpc/filter_manager_test.go @@ -114,9 +114,9 @@ func Test_GetLogsForQuery(t *testing.T) { Hash: types.StringToHash(strconv.Itoa(i)), }, Transactions: []*types.Transaction{ - types.NewTx(&types.LegacyTx{BaseTx: &types.BaseTx{Value: big.NewInt(10)}}), - types.NewTx(&types.LegacyTx{BaseTx: &types.BaseTx{Value: big.NewInt(11)}}), - types.NewTx(&types.LegacyTx{BaseTx: &types.BaseTx{Value: big.NewInt(12)}}), + types.NewTx(types.NewLegacyTx(types.WithValue(big.NewInt(10)))), + types.NewTx(types.NewLegacyTx(types.WithValue(big.NewInt(11)))), + types.NewTx(types.NewLegacyTx(types.WithValue(big.NewInt(12)))), }, } } diff --git a/jsonrpc/helper_test.go b/jsonrpc/helper_test.go index fcb28b8464..f04f481ffb 100644 --- a/jsonrpc/helper_test.go +++ b/jsonrpc/helper_test.go @@ -14,18 +14,14 @@ import ( func createTestTransaction(hash types.Hash) *types.Transaction { recipient := types.StringToAddress("2") - return types.NewTx(&types.LegacyTx{ - GasPrice: big.NewInt(400), - BaseTx: &types.BaseTx{ - Hash: hash, - From: types.StringToAddress("1"), - To: &recipient, - Value: big.NewInt(100), - V: big.NewInt(1), - R: big.NewInt(2), - S: big.NewInt(3), - }, - }) + return types.NewTx(types.NewLegacyTx( + types.WithGasPrice(big.NewInt(400)), + types.WithHash(hash), + types.WithFrom(types.StringToAddress("1")), + types.WithTo(&recipient), + types.WithValue(big.NewInt(100)), + types.WithSignatureValues(big.NewInt(1), big.NewInt(2), big.NewInt(3)), + )) } func createTestHeader(height uint64, setterFn func(h *types.Header)) *types.Header { @@ -735,17 +731,15 @@ func TestDecodeTxn(t *testing.T) { Nonce: &nonce, }, store: &debugEndpointMockStore{}, - expected: types.NewTx(&types.LegacyTx{ - GasPrice: new(big.Int).SetBytes([]byte(gasPrice)), - BaseTx: &types.BaseTx{ - From: types.ZeroAddress, - To: &to, - Gas: uint64(gas), - Value: new(big.Int).SetBytes([]byte(value)), - Input: input, - Nonce: uint64(0), - }, - }), + expected: types.NewTx(types.NewLegacyTx( + types.WithGasPrice(new(big.Int).SetBytes([]byte(gasPrice))), + types.WithFrom(types.ZeroAddress), + types.WithTo(&to), + types.WithGas(uint64(gas)), + types.WithValue(new(big.Int).SetBytes([]byte(value))), + types.WithInput(input), + types.WithNonce(0), + )), err: false, }, { @@ -770,17 +764,15 @@ func TestDecodeTxn(t *testing.T) { }, nil }, }, - expected: types.NewTx(&types.LegacyTx{ - GasPrice: new(big.Int).SetBytes([]byte(gasPrice)), - BaseTx: &types.BaseTx{ - From: from, - To: &to, - Gas: uint64(gas), - Value: new(big.Int).SetBytes([]byte(value)), - Input: input, - Nonce: uint64(stateNonce), - }, - }), + expected: types.NewTx(types.NewLegacyTx( + types.WithGasPrice(new(big.Int).SetBytes([]byte(gasPrice))), + types.WithFrom(from), + types.WithTo(&to), + types.WithGas(uint64(gas)), + types.WithValue(new(big.Int).SetBytes([]byte(value))), + types.WithInput(input), + types.WithNonce(uint64(stateNonce)), + )), err: false, }, { @@ -796,17 +788,15 @@ func TestDecodeTxn(t *testing.T) { Nonce: &nonce, }, store: &debugEndpointMockStore{}, - expected: types.NewTx(&types.LegacyTx{ - GasPrice: new(big.Int).SetBytes([]byte(gasPrice)), - BaseTx: &types.BaseTx{ - From: from, - To: &to, - Gas: uint64(gas), - Value: new(big.Int).SetBytes([]byte(value)), - Input: data, - Nonce: uint64(nonce), - }, - }), + expected: types.NewTx(types.NewLegacyTx( + types.WithGasPrice(new(big.Int).SetBytes([]byte(gasPrice))), + types.WithFrom(from), + types.WithTo(&to), + types.WithGas(uint64(gas)), + types.WithValue(new(big.Int).SetBytes([]byte(value))), + types.WithInput(data), + types.WithNonce(uint64(nonce)), + )), err: false, }, { @@ -817,17 +807,15 @@ func TestDecodeTxn(t *testing.T) { Nonce: &nonce, }, store: &debugEndpointMockStore{}, - expected: types.NewTx(&types.LegacyTx{ - GasPrice: new(big.Int), - BaseTx: &types.BaseTx{ - From: from, - To: &to, - Gas: uint64(0), - Value: new(big.Int), - Input: []byte{}, - Nonce: uint64(nonce), - }, - }), + expected: types.NewTx(types.NewLegacyTx( + types.WithGasPrice(new(big.Int)), + types.WithFrom(from), + types.WithTo(&to), + types.WithGas(uint64(0)), + types.WithValue(new(big.Int)), + types.WithInput([]byte{}), + types.WithNonce(uint64(nonce)), + )), err: false, }, { diff --git a/jsonrpc/txpool_endpoint_test.go b/jsonrpc/txpool_endpoint_test.go index 3b935f0aef..03654ead6c 100644 --- a/jsonrpc/txpool_endpoint_test.go +++ b/jsonrpc/txpool_endpoint_test.go @@ -294,20 +294,16 @@ func (s *mockTxPoolStore) GetBaseFee() uint64 { } func newTestTransaction(nonce uint64, from types.Address) *types.Transaction { - txn := types.NewTx(&types.LegacyTx{ - GasPrice: big.NewInt(1), - BaseTx: &types.BaseTx{ - Nonce: nonce, - Gas: nonce * 100, - Value: big.NewInt(200), - Input: []byte{0xff}, - From: from, - To: &addr1, - V: big.NewInt(1), - R: big.NewInt(1), - S: big.NewInt(1), - }, - }) + txn := types.NewTx(types.NewLegacyTx( + types.WithGasPrice(big.NewInt(1)), + types.WithNonce(nonce), + types.WithGas(nonce*100), + types.WithValue(big.NewInt(200)), + types.WithInput([]byte{0xff}), + types.WithFrom(from), + types.WithTo(&addr1), + types.WithSignatureValues(big.NewInt(1), big.NewInt(1), big.NewInt(1)), + )) txn.ComputeHash() diff --git a/jsonrpc/types_test.go b/jsonrpc/types_test.go index 3b6cd06e57..5aec65a284 100644 --- a/jsonrpc/types_test.go +++ b/jsonrpc/types_test.go @@ -103,21 +103,17 @@ func TestToTransaction_Returns_V_R_S_ValuesWithoutLeading0(t *testing.T) { v, _ := hex.DecodeHex(hexWithLeading0) r, _ := hex.DecodeHex(hexWithLeading0) s, _ := hex.DecodeHex(hexWithLeading0) - txn := types.NewTx(&types.LegacyTx{ - GasPrice: big.NewInt(0), - BaseTx: &types.BaseTx{ - Nonce: 0, - Gas: 0, - To: nil, - Value: big.NewInt(0), - Input: nil, - V: new(big.Int).SetBytes(v), - R: new(big.Int).SetBytes(r), - S: new(big.Int).SetBytes(s), - Hash: types.Hash{}, - From: types.Address{}, - }, - }) + txn := types.NewTx(types.NewLegacyTx( + types.WithGasPrice(big.NewInt(0)), + types.WithNonce(0), + types.WithGas(0), + types.WithTo(nil), + types.WithValue(big.NewInt(0)), + types.WithInput(nil), + types.WithSignatureValues(new(big.Int).SetBytes(v), new(big.Int).SetBytes(r), new(big.Int).SetBytes(s)), + types.WithHash(types.Hash{}), + types.WithFrom(types.Address{}), + )) jsonTx := toTransaction(txn, nil, nil, nil) diff --git a/state/transition_test.go b/state/transition_test.go index 47a33ea978..c225652b87 100644 --- a/state/transition_test.go +++ b/state/transition_test.go @@ -70,13 +70,11 @@ func TestSubGasLimitPrice(t *testing.T) { t.Parallel() transition := newTestTransition(tt.preState) - msg := types.NewTx(&types.LegacyTx{ - GasPrice: big.NewInt(tt.gasPrice), - BaseTx: &types.BaseTx{ - From: tt.from, - Gas: tt.gas, - }, - }) + msg := types.NewTx(types.NewLegacyTx( + types.WithGasPrice(big.NewInt(tt.gasPrice)), + types.WithFrom(tt.from), + types.WithGas(tt.gas), + )) err := transition.subGasLimitPrice(msg) diff --git a/tests/state_test_util.go b/tests/state_test_util.go index cedc8db5cb..f7e3f291fa 100644 --- a/tests/state_test_util.go +++ b/tests/state_test_util.go @@ -358,17 +358,15 @@ func (t *stTransaction) At(i indexes, baseFee *big.Int) (*types.Transaction, err }, } } else { - txData = &types.LegacyTx{ - GasPrice: gasPrice, - BaseTx: &types.BaseTx{ - From: t.From, - To: t.To, - Nonce: t.Nonce, - Value: value, - Gas: t.GasLimit[i.Gas], - Input: hex.MustDecodeHex(t.Data[i.Data]), - }, - } + txData = types.NewLegacyTx( + types.WithGasPrice(gasPrice), + types.WithFrom(t.From), + types.WithTo(t.To), + types.WithNonce(t.Nonce), + types.WithValue(value), + types.WithGas(t.GasLimit[i.Gas]), + types.WithInput(hex.MustDecodeHex(t.Data[i.Data])), + ) } } diff --git a/types/access_list_tx.go b/types/access_list_tx.go index 4832fec3fb..6cfe91018b 100644 --- a/types/access_list_tx.go +++ b/types/access_list_tx.go @@ -112,8 +112,14 @@ type AccessListTxn struct { AccessList TxAccessList } -func NewAccessListTx() *AccessListTxn { - return &AccessListTxn{BaseTx: &BaseTx{}} +func NewAccessListTx(options ...Option) *AccessListTxn { + accessListTx := &AccessListTxn{BaseTx: &BaseTx{}} + + for _, opt := range options { + opt(accessListTx) + } + + return accessListTx } func (tx *AccessListTxn) transactionType() TxType { return AccessListTxType } diff --git a/types/dynamic_fee_tx.go b/types/dynamic_fee_tx.go index df88bc6335..94d58e6633 100644 --- a/types/dynamic_fee_tx.go +++ b/types/dynamic_fee_tx.go @@ -16,8 +16,14 @@ type DynamicFeeTx struct { AccessList TxAccessList } -func NewDynamicFeeTx() *DynamicFeeTx { - return &DynamicFeeTx{BaseTx: &BaseTx{}} +func NewDynamicFeeTx(options ...Option) *DynamicFeeTx { + dynamicTx := &DynamicFeeTx{BaseTx: &BaseTx{}} + + for _, opt := range options { + opt(dynamicTx) + } + + return dynamicTx } func (tx *DynamicFeeTx) transactionType() TxType { return DynamicFeeTxType } diff --git a/types/legacy_tx.go b/types/legacy_tx.go index 233b5c29ae..8b0ef4a4dc 100644 --- a/types/legacy_tx.go +++ b/types/legacy_tx.go @@ -12,8 +12,14 @@ type LegacyTx struct { GasPrice *big.Int } -func NewLegacyTx() *LegacyTx { - return &LegacyTx{BaseTx: &BaseTx{}} +func NewLegacyTx(options ...Option) *LegacyTx { + legacyTx := &LegacyTx{BaseTx: &BaseTx{}} + + for _, opt := range options { + opt(legacyTx) + } + + return legacyTx } func (tx *LegacyTx) transactionType() TxType { return LegacyTxType } diff --git a/types/state_tx.go b/types/state_tx.go index 639303b40c..4093b2c5f1 100644 --- a/types/state_tx.go +++ b/types/state_tx.go @@ -12,8 +12,14 @@ type StateTx struct { GasPrice *big.Int } -func NewStateTx() *StateTx { - return &StateTx{BaseTx: &BaseTx{}} +func NewStateTx(options ...Option) *StateTx { + stateTx := &StateTx{BaseTx: &BaseTx{}} + + for _, opt := range options { + opt(stateTx) + } + + return stateTx } func (tx *StateTx) transactionType() TxType { return StateTxType } diff --git a/types/transaction.go b/types/transaction.go index a500a6f074..f406b66478 100644 --- a/types/transaction.go +++ b/types/transaction.go @@ -391,3 +391,83 @@ func NewTxWithType(txType TxType) *Transaction { return tx } + +type Option func(TxData) + +func WithGasPrice(gasPrice *big.Int) Option { + return func(td TxData) { + td.setGasPrice(gasPrice) + } +} + +func WithNonce(nonce uint64) Option { + return func(td TxData) { + td.setNonce(nonce) + } +} + +func WithGas(gas uint64) Option { + return func(td TxData) { + td.setGas(gas) + } +} + +func WithTo(to *Address) Option { + return func(td TxData) { + td.setTo(to) + } +} + +func WithValue(value *big.Int) Option { + return func(td TxData) { + td.setValue(value) + } +} + +func WithInput(input []byte) Option { + return func(td TxData) { + td.setInput(input) + } +} + +func WithSignatureValues(v, r, s *big.Int) Option { + return func(td TxData) { + td.setSignatureValues(v, r, s) + } +} + +func WithHash(hash Hash) Option { + return func(td TxData) { + td.setHash(hash) + } +} + +func WithFrom(from Address) Option { + return func(td TxData) { + td.setFrom(from) + } +} + +func WithGasTipCap(gasTipCap *big.Int) Option { + return func(td TxData) { + td.setGasTipCap(gasTipCap) + } +} + +func WithGasFeeCap(gasFeeCap *big.Int) Option { + return func(td TxData) { + td.setGasFeeCap(gasFeeCap) + } +} + +func WithChainID(chainID *big.Int) Option { + return func(td TxData) { + td.setChainID(chainID) + } +} + +func WithAccessList(accessList TxAccessList) Option { + return func(td TxData) { + td.setAccessList(accessList) + } +} From 4dfd753d49438bd7463f4f09f35eeb280c84063b Mon Sep 17 00:00:00 2001 From: Dusan Nosovic Date: Wed, 13 Mar 2024 14:01:27 +0100 Subject: [PATCH 3/6] other transactions test fix --- blockchain/storage/leveldb/leveldb_test.go | 18 +++---- blockchain/storage/testing.go | 14 +++-- consensus/polybft/fsm.go | 16 +++--- consensus/polybft/fsm_test.go | 13 +++-- crypto/txsigner_london_test.go | 60 +++++++++------------- e2e/txpool_test.go | 37 ++++++------- gasprice/feehistory_test.go | 14 +++-- gasprice/gasprice_test.go | 36 ++++++------- jsonrpc/debug_endpoint_test.go | 22 ++++---- jsonrpc/eth_endpoint_test.go | 44 ++++++++-------- jsonrpc/helper_test.go | 22 ++++---- jsonrpc/txpool_endpoint_test.go | 28 +++++----- jsonrpc/types_test.go | 26 ++++------ state/executor_test.go | 40 +++++++-------- tests/state_test_util.go | 46 ++++++++--------- 15 files changed, 193 insertions(+), 243 deletions(-) diff --git a/blockchain/storage/leveldb/leveldb_test.go b/blockchain/storage/leveldb/leveldb_test.go index 17fe6418d0..d149c65c6a 100644 --- a/blockchain/storage/leveldb/leveldb_test.go +++ b/blockchain/storage/leveldb/leveldb_test.go @@ -54,16 +54,14 @@ func generateTxs(t *testing.T, startNonce, count int, from types.Address, to *ty txs := make([]*types.Transaction, count) for i := range txs { - tx := types.NewTx(&types.DynamicFeeTx{ - GasFeeCap: big.NewInt(100), - GasTipCap: big.NewInt(10), - BaseTx: &types.BaseTx{ - Gas: types.StateTransactionGasLimit, - Nonce: uint64(startNonce + i), - To: to, - Value: big.NewInt(2000), - }, - }) + tx := types.NewTx(types.NewDynamicFeeTx( + types.WithGasFeeCap(big.NewInt(100)), + types.WithGasTipCap(big.NewInt(10)), + types.WithGas(types.StateTransactionGasLimit), + types.WithNonce(uint64(startNonce+1)), + types.WithTo(to), + types.WithValue(big.NewInt(2000)), + )) input := make([]byte, 1000) _, err := rand.Read(input) diff --git a/blockchain/storage/testing.go b/blockchain/storage/testing.go index f073b55227..f6ce56840e 100644 --- a/blockchain/storage/testing.go +++ b/blockchain/storage/testing.go @@ -338,14 +338,12 @@ func testReceipts(t *testing.T, m PlaceholderStorage) { body := &types.Body{ Transactions: []*types.Transaction{ - types.NewTx(&types.StateTx{ - GasPrice: new(big.Int).SetUint64(100), - BaseTx: &types.BaseTx{ - Nonce: 1000, - Gas: 50, - V: big.NewInt(11), - }, - }), + types.NewTx(types.NewStateTx( + types.WithGasPrice(new(big.Int).SetUint64(100)), + types.WithNonce(1000), + types.WithGas(50), + types.WithSignatureValues(big.NewInt(11), nil, nil), + )), }, } receipts := []*types.Receipt{ diff --git a/consensus/polybft/fsm.go b/consensus/polybft/fsm.go index 994d5c29e1..e754c8920f 100644 --- a/consensus/polybft/fsm.go +++ b/consensus/polybft/fsm.go @@ -716,15 +716,13 @@ func validateHeaderFields(parent *types.Header, header *types.Header, blockTimeD // createStateTransactionWithData creates a state transaction // with provided target address and inputData parameter which is ABI encoded byte array. func createStateTransactionWithData(target types.Address, inputData []byte) *types.Transaction { - tx := types.NewTx(&types.StateTx{ - GasPrice: big.NewInt(0), - BaseTx: &types.BaseTx{ - From: contracts.SystemCaller, - To: &target, - Input: inputData, - Gas: types.StateTransactionGasLimit, - }, - }) + tx := types.NewTx(types.NewStateTx( + types.WithGasPrice(big.NewInt(0)), + types.WithFrom(contracts.SystemCaller), + types.WithTo(&target), + types.WithInput(inputData), + types.WithGas(types.StateTransactionGasLimit), + )) return tx.ComputeHash() } diff --git a/consensus/polybft/fsm_test.go b/consensus/polybft/fsm_test.go index 5ae5440f42..4f588dd03e 100644 --- a/consensus/polybft/fsm_test.go +++ b/consensus/polybft/fsm_test.go @@ -100,13 +100,12 @@ func TestFSM_verifyCommitEpochTx(t *testing.T) { assert.NoError(t, fsm.verifyCommitEpochTx(commitEpochTx)) // submit tampered commit epoch transaction to the epoch ending block - alteredCommitEpochTx := types.NewTx(&types.StateTx{ - BaseTx: &types.BaseTx{ - To: &contracts.EpochManagerContract, - Input: []byte{}, - Gas: 0, - }, - }) + alteredCommitEpochTx := types.NewTx(types.NewStateTx( + types.WithTo(&contracts.EpochManagerContract), + types.WithInput([]byte{}), + types.WithGas(0), + )) + assert.ErrorContains(t, fsm.verifyCommitEpochTx(alteredCommitEpochTx), "invalid commit epoch transaction") // submit validators commit epoch transaction to the non-epoch ending block diff --git a/crypto/txsigner_london_test.go b/crypto/txsigner_london_test.go index 146de82fe1..48067deb1e 100644 --- a/crypto/txsigner_london_test.go +++ b/crypto/txsigner_london_test.go @@ -74,14 +74,12 @@ func TestLondonSignerSender(t *testing.T) { switch tc.txType { case types.AccessListTxType: - txn = types.NewTx(&types.AccessListTxn{ - GasPrice: big.NewInt(5), - ChainID: tc.chainID, - BaseTx: &types.BaseTx{ - To: &recipient, - Value: big.NewInt(1), - }, - }) + txn = types.NewTx(types.NewAccessListTx( + types.WithGasPrice(big.NewInt(5)), + types.WithChainID(tc.chainID), + types.WithTo(&recipient), + types.WithValue(big.NewInt(1)), + )) case types.LegacyTxType: txn = types.NewTx(types.NewLegacyTx( types.WithGasPrice(big.NewInt(5)), @@ -89,21 +87,17 @@ func TestLondonSignerSender(t *testing.T) { types.WithValue(big.NewInt(1)), )) case types.StateTxType: - txn = types.NewTx(&types.StateTx{ - GasPrice: big.NewInt(5), - BaseTx: &types.BaseTx{ - To: &recipient, - Value: big.NewInt(1), - }, - }) + txn = types.NewTx(types.NewStateTx( + types.WithGasPrice(big.NewInt(5)), + types.WithTo(&recipient), + types.WithValue(big.NewInt(1)), + )) case types.DynamicFeeTxType: - txn = types.NewTx(&types.DynamicFeeTx{ - ChainID: tc.chainID, - BaseTx: &types.BaseTx{ - To: &recipient, - Value: big.NewInt(1), - }, - }) + txn = types.NewTx(types.NewDynamicFeeTx( + types.WithChainID(tc.chainID), + types.WithTo(&recipient), + types.WithValue(big.NewInt(1)), + )) } chainID := tc.chainID.Uint64() @@ -140,19 +134,15 @@ func Test_LondonSigner_Sender(t *testing.T) { }{ { name: "sender is 0x85dA99c8a7C2C95964c8EfD687E95E632Fc533D6", - tx: types.NewTx(&types.DynamicFeeTx{ - ChainID: big.NewInt(100), - GasTipCap: ethgo.Gwei(1), - GasFeeCap: ethgo.Gwei(10), - BaseTx: &types.BaseTx{ - Gas: 21000, - To: &to, - Value: big.NewInt(100000000000000), - V: big.NewInt(0), - R: r, - S: s, - }, - }), + tx: types.NewTx(types.NewDynamicFeeTx( + types.WithChainID(big.NewInt(100)), + types.WithGasTipCap(ethgo.Gwei(1)), + types.WithGasFeeCap(ethgo.Gwei(10)), + types.WithGas(21000), + types.WithTo(&to), + types.WithValue(big.NewInt(100000000000000)), + types.WithSignatureValues(big.NewInt(0), r, s), + )), sender: types.StringToAddress("0x85dA99c8a7C2C95964c8EfD687E95E632Fc533D6"), }, } diff --git a/e2e/txpool_test.go b/e2e/txpool_test.go index 72c642e960..55d67cb7b8 100644 --- a/e2e/txpool_test.go +++ b/e2e/txpool_test.go @@ -54,15 +54,12 @@ func generateTx(params generateTxReqParams) *types.Transaction { )) unsignedTx.SetGasPrice(params.gasPrice) } else { - unsignedTx = types.NewTx(&types.DynamicFeeTx{ - ChainID: new(big.Int).SetUint64(defaultChainID), - BaseTx: &types.BaseTx{ - Nonce: params.nonce, - To: ¶ms.toAddress, - Gas: 1000000, - Value: params.value, - }, - }) + unsignedTx = types.NewTx(types.NewDynamicFeeTx( + types.WithNonce(params.nonce), + types.WithTo(¶ms.toAddress), + types.WithGas(1000000), + types.WithValue(params.value), + )) unsignedTx.SetGasFeeCap(params.gasFeeCap) unsignedTx.SetGasTipCap(params.gasTipCap) } @@ -260,18 +257,16 @@ func TestTxPool_RecoverableError(t *testing.T) { types.WithValue(oneEth), types.WithSignatureValues(big.NewInt(27), nil, nil), )), - types.NewTx(&types.DynamicFeeTx{ - ChainID: new(big.Int).SetUint64(defaultChainID), - GasFeeCap: big.NewInt(framework.DefaultGasPrice), - GasTipCap: big.NewInt(1000000000), - BaseTx: &types.BaseTx{ - Nonce: 2, - Gas: 22000, - To: &receiverAddress, - Value: oneEth, - V: big.NewInt(27), - }, - }), + types.NewTx(types.NewDynamicFeeTx( + types.WithChainID(new(big.Int).SetUint64(defaultChainID)), + types.WithGasFeeCap(big.NewInt(framework.DefaultGasPrice)), + types.WithGasTipCap(big.NewInt(1000000000)), + types.WithNonce(2), + types.WithGas(22000), + types.WithTo(&receiverAddress), + types.WithValue(oneEth), + types.WithSignatureValues(big.NewInt(27), nil, nil), + )), } server := framework.NewTestServers(t, 1, func(config *framework.TestServerConfig) { diff --git a/gasprice/feehistory_test.go b/gasprice/feehistory_test.go index a0251c74d2..68f2115168 100644 --- a/gasprice/feehistory_test.go +++ b/gasprice/feehistory_test.go @@ -194,14 +194,12 @@ func TestGasHelper_FeeHistory(t *testing.T) { b.Header.Miner = sender.Bytes() for i := 0; i < 3; i++ { - tx := types.NewTx(&types.DynamicFeeTx{ - GasTipCap: ethgo.Gwei(uint64(200)), - GasFeeCap: ethgo.Gwei(uint64(200 + 200)), - BaseTx: &types.BaseTx{ - Value: ethgo.Ether(1), - To: &types.ZeroAddress, - }, - }) + tx := types.NewTx(types.NewDynamicFeeTx( + types.WithGasTipCap(ethgo.Gwei(200)), + types.WithGasFeeCap(ethgo.Gwei(200+200)), + types.WithValue(ethgo.Ether(1)), + types.WithTo(&types.ZeroAddress), + )) tx, err := signer.SignTx(tx, senderKey) require.NoError(t, err) diff --git a/gasprice/gasprice_test.go b/gasprice/gasprice_test.go index a9ed00d8e7..aba8a0c274 100644 --- a/gasprice/gasprice_test.go +++ b/gasprice/gasprice_test.go @@ -84,16 +84,14 @@ func TestGasHelper_MaxPriorityFeePerGas(t *testing.T) { b.Header.Miner = sender.Bytes() for i := 0; i < 3; i++ { - tx := types.NewTx(&types.DynamicFeeTx{ - GasTipCap: ethgo.Gwei(uint64(rand.Intn(200))), - GasFeeCap: ethgo.Gwei(uint64(rand.Intn(200) + 200)), - ChainID: big.NewInt(backend.Config().ChainID), - BaseTx: &types.BaseTx{ - From: sender, - Value: ethgo.Ether(1), - To: &types.ZeroAddress, - }, - }) + tx := types.NewTx(types.NewDynamicFeeTx( + types.WithGasTipCap(ethgo.Gwei(uint64(rand.Intn(200)))), + types.WithGasFeeCap(ethgo.Gwei(uint64(rand.Intn(200)+200))), + types.WithChainID(big.NewInt(backend.Config().ChainID)), + types.WithFrom(sender), + types.WithValue(ethgo.Ether(1)), + types.WithTo(&types.ZeroAddress), + )) tx, err := signer.SignTx(tx, senderKey) require.NoError(t, err) @@ -221,16 +219,14 @@ func createTestTxs(t *testing.T, backend *backendMock, numOfTxsPerBlock, txCap i for i := 0; i < numOfTxsPerBlock; i++ { senderKey, sender := tests.GenerateKeyAndAddr(t) - tx := types.NewTx(&types.DynamicFeeTx{ - GasTipCap: ethgo.Gwei(uint64(rand.Intn(txCap))), - GasFeeCap: ethgo.Gwei(uint64(rand.Intn(txCap) + txCap)), - ChainID: big.NewInt(backend.Config().ChainID), - BaseTx: &types.BaseTx{ - From: sender, - Value: ethgo.Ether(1), - To: &types.ZeroAddress, - }, - }) + tx := types.NewTx(types.NewDynamicFeeTx( + types.WithGasTipCap(ethgo.Gwei(uint64(rand.Intn(txCap)))), + types.WithGasFeeCap(ethgo.Gwei(uint64(rand.Intn(txCap)+txCap))), + types.WithChainID(big.NewInt(backend.Config().ChainID)), + types.WithFrom(sender), + types.WithValue(ethgo.Ether(1)), + types.WithTo(&types.ZeroAddress), + )) tx, err := signer.SignTx(tx, senderKey) require.NoError(t, err) diff --git a/jsonrpc/debug_endpoint_test.go b/jsonrpc/debug_endpoint_test.go index 778ce207f0..35a2a3653a 100644 --- a/jsonrpc/debug_endpoint_test.go +++ b/jsonrpc/debug_endpoint_test.go @@ -605,18 +605,16 @@ func TestTraceCall(t *testing.T) { Nonce: &nonce, Type: toArgUint64Ptr(uint64(types.DynamicFeeTxType)), } - decodedTx = types.NewTx(&types.DynamicFeeTx{ - GasTipCap: new(big.Int).SetBytes([]byte(gasTipCap)), - GasFeeCap: new(big.Int).SetBytes([]byte(gasFeeCap)), - BaseTx: &types.BaseTx{ - Nonce: uint64(nonce), - Gas: uint64(gas), - To: &to, - Value: new(big.Int).SetBytes([]byte(value)), - Input: data, - From: from, - }, - }) + decodedTx = types.NewTx(types.NewDynamicFeeTx( + types.WithGasTipCap(new(big.Int).SetBytes([]byte(gasTipCap))), + types.WithGasFeeCap(new(big.Int).SetBytes([]byte(gasFeeCap))), + types.WithNonce(uint64(nonce)), + types.WithGas(uint64(gas)), + types.WithTo(&to), + types.WithValue(new(big.Int).SetBytes([]byte(value))), + types.WithInput(data), + types.WithFrom(from), + )) ) decodedTx.ComputeHash() diff --git a/jsonrpc/eth_endpoint_test.go b/jsonrpc/eth_endpoint_test.go index 6093b6161b..c35dd3694d 100644 --- a/jsonrpc/eth_endpoint_test.go +++ b/jsonrpc/eth_endpoint_test.go @@ -46,18 +46,16 @@ func TestEth_DecodeTxn(t *testing.T) { Nonce: toArgUint64Ptr(0), Type: toArgUint64Ptr(uint64(types.DynamicFeeTxType)), }, - res: types.NewTx(&types.DynamicFeeTx{ - GasTipCap: big.NewInt(10000), - GasFeeCap: big.NewInt(10000), - BaseTx: &types.BaseTx{ - From: addr1, - To: &addr2, - Gas: 21000, - Value: oneEther, - Input: []byte{}, - Nonce: 0, - }, - }), + res: types.NewTx(types.NewDynamicFeeTx( + types.WithGasTipCap(big.NewInt(10000)), + types.WithGasFeeCap(big.NewInt(10000)), + types.WithFrom(addr1), + types.WithTo(&addr2), + types.WithGas(21000), + types.WithValue(oneEther), + types.WithInput([]byte{}), + types.WithNonce(0), + )), err: nil, }, { @@ -271,18 +269,16 @@ func TestEth_TxnType(t *testing.T) { Type: toArgUint64Ptr(uint64(types.DynamicFeeTxType)), } - expectedRes := types.NewTx(&types.DynamicFeeTx{ - GasTipCap: big.NewInt(10000), - GasFeeCap: big.NewInt(10000), - BaseTx: &types.BaseTx{ - From: addr1, - To: &addr2, - Gas: 21000, - Value: oneEther, - Input: []byte{}, - Nonce: 0, - }, - }) + expectedRes := types.NewTx(types.NewDynamicFeeTx( + types.WithGasTipCap(big.NewInt(10000)), + types.WithGasFeeCap(big.NewInt(10000)), + types.WithFrom(addr1), + types.WithTo(&addr2), + types.WithGas(21000), + types.WithValue(oneEther), + types.WithInput([]byte{}), + types.WithNonce(0), + )) res, err := DecodeTxn(args, 1, store, false) expectedRes.ComputeHash() diff --git a/jsonrpc/helper_test.go b/jsonrpc/helper_test.go index f04f481ffb..19f521d6c7 100644 --- a/jsonrpc/helper_test.go +++ b/jsonrpc/helper_test.go @@ -706,18 +706,16 @@ func TestDecodeTxn(t *testing.T) { Type: toArgUint64Ptr(uint64(types.DynamicFeeTxType)), }, store: &debugEndpointMockStore{}, - expected: types.NewTx(&types.DynamicFeeTx{ - GasTipCap: new(big.Int).SetBytes([]byte(gasTipCap)), - GasFeeCap: new(big.Int).SetBytes([]byte(gasFeeCap)), - BaseTx: &types.BaseTx{ - Value: new(big.Int).SetBytes([]byte(value)), - Input: input, - Nonce: uint64(nonce), - From: from, - To: &to, - Gas: uint64(gas), - }, - }), + expected: types.NewTx(types.NewDynamicFeeTx( + types.WithGasTipCap(new(big.Int).SetBytes([]byte(gasTipCap))), + types.WithGasFeeCap(new(big.Int).SetBytes([]byte(gasFeeCap))), + types.WithValue(new(big.Int).SetBytes([]byte(value))), + types.WithInput(input), + types.WithNonce(uint64(nonce)), + types.WithFrom(from), + types.WithTo(&to), + types.WithGas(uint64(gas)), + )), err: false, }, { diff --git a/jsonrpc/txpool_endpoint_test.go b/jsonrpc/txpool_endpoint_test.go index 03654ead6c..a46dae8e17 100644 --- a/jsonrpc/txpool_endpoint_test.go +++ b/jsonrpc/txpool_endpoint_test.go @@ -311,22 +311,18 @@ func newTestTransaction(nonce uint64, from types.Address) *types.Transaction { } func newTestDynamicFeeTransaction(nonce uint64, from types.Address) *types.Transaction { - txn := types.NewTx(&types.DynamicFeeTx{ - GasTipCap: big.NewInt(2), - GasFeeCap: big.NewInt(4), - ChainID: big.NewInt(100), - BaseTx: &types.BaseTx{ - Nonce: nonce, - Gas: nonce * 100, - Value: big.NewInt(200), - Input: []byte{0xff}, - From: from, - To: &addr1, - V: big.NewInt(1), - R: big.NewInt(1), - S: big.NewInt(1), - }, - }) + txn := types.NewTx(types.NewDynamicFeeTx( + types.WithGasTipCap(big.NewInt(2)), + types.WithGasFeeCap(big.NewInt(4)), + types.WithChainID(big.NewInt(100)), + types.WithNonce(nonce), + types.WithGas(nonce*100), + types.WithValue(big.NewInt(200)), + types.WithInput([]byte{0xff}), + types.WithFrom(from), + types.WithTo(&addr1), + types.WithSignatureValues(big.NewInt(1), big.NewInt(1), big.NewInt(1)), + )) txn.ComputeHash() diff --git a/jsonrpc/types_test.go b/jsonrpc/types_test.go index 5aec65a284..3b6da3a0ba 100644 --- a/jsonrpc/types_test.go +++ b/jsonrpc/types_test.go @@ -132,22 +132,16 @@ func TestToTransaction_EIP1559(t *testing.T) { v, _ := hex.DecodeHex(hexWithLeading0) r, _ := hex.DecodeHex(hexWithLeading0) s, _ := hex.DecodeHex(hexWithLeading0) - txn := types.NewTx(&types.DynamicFeeTx{ - GasTipCap: big.NewInt(10), - GasFeeCap: big.NewInt(10), - BaseTx: &types.BaseTx{ - Nonce: 0, - Gas: 0, - To: nil, - Value: big.NewInt(0), - Input: nil, - V: new(big.Int).SetBytes(v), - R: new(big.Int).SetBytes(r), - S: new(big.Int).SetBytes(s), - Hash: types.Hash{}, - From: types.Address{}, - }, - }) + txn := types.NewTx(types.NewDynamicFeeTx( + types.WithGasTipCap(big.NewInt(10)), + types.WithGasFeeCap(big.NewInt(10)), + types.WithNonce(0), + types.WithGas(0), + types.WithValue(big.NewInt(0)), + types.WithSignatureValues(new(big.Int).SetBytes(v), new(big.Int).SetBytes(r), new(big.Int).SetBytes(s)), + types.WithHash(types.Hash{}), + types.WithFrom(types.Address{}), + )) jsonTx := toTransaction(txn, nil, nil, nil) diff --git a/state/executor_test.go b/state/executor_test.go index 42e5eb4237..1ad4cb6c16 100644 --- a/state/executor_test.go +++ b/state/executor_test.go @@ -80,11 +80,11 @@ func Test_Transition_checkDynamicFees(t *testing.T) { { name: "happy path", baseFee: big.NewInt(100), - tx: types.NewTx(&types.DynamicFeeTx{ - GasFeeCap: big.NewInt(100), - GasTipCap: big.NewInt(100), - BaseTx: &types.BaseTx{From: types.ZeroAddress}, - }), + tx: types.NewTx(types.NewDynamicFeeTx( + types.WithGasFeeCap(big.NewInt(100)), + types.WithGasTipCap(big.NewInt(100)), + types.WithFrom(types.ZeroAddress), + )), wantErr: func(t assert.TestingT, err error, i ...interface{}) bool { assert.NoError(t, err, i) @@ -94,11 +94,11 @@ func Test_Transition_checkDynamicFees(t *testing.T) { { name: "happy path with empty values", baseFee: big.NewInt(0), - tx: types.NewTx(&types.DynamicFeeTx{ - GasFeeCap: big.NewInt(0), - GasTipCap: big.NewInt(0), - BaseTx: &types.BaseTx{From: types.ZeroAddress}, - }), + tx: types.NewTx(types.NewDynamicFeeTx( + types.WithGasFeeCap(big.NewInt(0)), + types.WithGasTipCap(big.NewInt(0)), + types.WithFrom(types.ZeroAddress), + )), wantErr: func(t assert.TestingT, err error, i ...interface{}) bool { assert.NoError(t, err, i) @@ -108,11 +108,11 @@ func Test_Transition_checkDynamicFees(t *testing.T) { { name: "gas fee cap less than base fee", baseFee: big.NewInt(20), - tx: types.NewTx(&types.DynamicFeeTx{ - GasFeeCap: big.NewInt(10), - GasTipCap: big.NewInt(0), - BaseTx: &types.BaseTx{From: types.ZeroAddress}, - }), + tx: types.NewTx(types.NewDynamicFeeTx( + types.WithGasFeeCap(big.NewInt(10)), + types.WithGasTipCap(big.NewInt(0)), + types.WithFrom(types.ZeroAddress), + )), wantErr: func(t assert.TestingT, err error, i ...interface{}) bool { expectedError := fmt.Sprintf("max fee per gas less than block base fee: "+ "address %s, GasFeeCap/GasPrice: 10, BaseFee: 20", types.ZeroAddress) @@ -124,11 +124,11 @@ func Test_Transition_checkDynamicFees(t *testing.T) { { name: "gas fee cap less than tip cap", baseFee: big.NewInt(5), - tx: types.NewTx(&types.DynamicFeeTx{ - GasFeeCap: big.NewInt(10), - GasTipCap: big.NewInt(15), - BaseTx: &types.BaseTx{From: types.ZeroAddress}, - }), + tx: types.NewTx(types.NewDynamicFeeTx( + types.WithGasFeeCap(big.NewInt(10)), + types.WithGasTipCap(big.NewInt(15)), + types.WithFrom(types.ZeroAddress), + )), wantErr: func(t assert.TestingT, err error, i ...interface{}) bool { expectedError := fmt.Sprintf("max priority fee per gas higher than max fee per gas: "+ "address %s, GasTipCap: 15, GasFeeCap: 10", types.ZeroAddress) diff --git a/tests/state_test_util.go b/tests/state_test_util.go index f7e3f291fa..69d79273db 100644 --- a/tests/state_test_util.go +++ b/tests/state_test_util.go @@ -328,35 +328,31 @@ func (t *stTransaction) At(i indexes, baseFee *big.Int) (*types.Transaction, err // if tx is not dynamic and accessList is not nil, create an access list transaction if !isDynamiFeeTx && accessList != nil { - txData = &types.AccessListTxn{ - GasPrice: gasPrice, - AccessList: accessList, - BaseTx: &types.BaseTx{ - From: t.From, - To: t.To, - Nonce: t.Nonce, - Value: value, - Gas: t.GasLimit[i.Gas], - Input: hex.MustDecodeHex(t.Data[i.Data]), - }, - } + txData = types.NewAccessListTx( + types.WithGasPrice(gasPrice), + types.WithAccessList(accessList), + types.WithFrom(t.From), + types.WithTo(t.To), + types.WithNonce(t.Nonce), + types.WithValue(value), + types.WithGas(t.GasLimit[i.Gas]), + types.WithInput(hex.MustDecodeHex(t.Data[i.Data])), + ) } if txData == nil { if isDynamiFeeTx { - txData = &types.DynamicFeeTx{ - GasFeeCap: t.MaxFeePerGas, - GasTipCap: t.MaxPriorityFeePerGas, - AccessList: accessList, - BaseTx: &types.BaseTx{ - From: t.From, - To: t.To, - Nonce: t.Nonce, - Value: value, - Gas: t.GasLimit[i.Gas], - Input: hex.MustDecodeHex(t.Data[i.Data]), - }, - } + txData = + types.NewDynamicFeeTx( + types.WithGasFeeCap(t.MaxFeePerGas), + types.WithGasTipCap(t.MaxPriorityFeePerGas), + types.WithFrom(t.From), + types.WithTo(t.To), + types.WithNonce(t.Nonce), + types.WithValue(value), + types.WithGas(t.GasLimit[i.Gas]), + types.WithInput(hex.MustDecodeHex(t.Data[i.Data])), + ) } else { txData = types.NewLegacyTx( types.WithGasPrice(gasPrice), From 4dec3877258845a035bfd9d291c939ce99a5c1a1 Mon Sep 17 00:00:00 2001 From: Dusan Nosovic Date: Wed, 13 Mar 2024 14:01:27 +0100 Subject: [PATCH 4/6] legacy test fix --- e2e/txpool_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/e2e/txpool_test.go b/e2e/txpool_test.go index 55d67cb7b8..1c744011bd 100644 --- a/e2e/txpool_test.go +++ b/e2e/txpool_test.go @@ -55,6 +55,7 @@ func generateTx(params generateTxReqParams) *types.Transaction { unsignedTx.SetGasPrice(params.gasPrice) } else { unsignedTx = types.NewTx(types.NewDynamicFeeTx( + types.WithChainID(new(big.Int).SetUint64(defaultChainID)), types.WithNonce(params.nonce), types.WithTo(¶ms.toAddress), types.WithGas(1000000), From d5377e3b8a14ab50d01348898c6d4de3862b17f9 Mon Sep 17 00:00:00 2001 From: Dusan Nosovic Date: Wed, 13 Mar 2024 14:01:27 +0100 Subject: [PATCH 5/6] state test fix --- tests/state_test_util.go | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/state_test_util.go b/tests/state_test_util.go index 69d79273db..88fad90f35 100644 --- a/tests/state_test_util.go +++ b/tests/state_test_util.go @@ -346,6 +346,7 @@ func (t *stTransaction) At(i indexes, baseFee *big.Int) (*types.Transaction, err types.NewDynamicFeeTx( types.WithGasFeeCap(t.MaxFeePerGas), types.WithGasTipCap(t.MaxPriorityFeePerGas), + types.WithAccessList(accessList), types.WithFrom(t.From), types.WithTo(t.To), types.WithNonce(t.Nonce), From 1d2db1c923fe8c0afb6f8d71a8d9c6cac4e217b6 Mon Sep 17 00:00:00 2001 From: Dusan Nosovic Date: Wed, 13 Mar 2024 20:43:41 +0100 Subject: [PATCH 6/6] comment fix --- types/access_list_tx.go | 2 +- types/dynamic_fee_tx.go | 2 +- types/legacy_tx.go | 2 +- types/state_tx.go | 2 +- types/transaction.go | 28 ++++++++++++++-------------- 5 files changed, 18 insertions(+), 18 deletions(-) diff --git a/types/access_list_tx.go b/types/access_list_tx.go index 6cfe91018b..1df0b26bf7 100644 --- a/types/access_list_tx.go +++ b/types/access_list_tx.go @@ -112,7 +112,7 @@ type AccessListTxn struct { AccessList TxAccessList } -func NewAccessListTx(options ...Option) *AccessListTxn { +func NewAccessListTx(options ...TxOption) *AccessListTxn { accessListTx := &AccessListTxn{BaseTx: &BaseTx{}} for _, opt := range options { diff --git a/types/dynamic_fee_tx.go b/types/dynamic_fee_tx.go index 94d58e6633..b5c33eefd8 100644 --- a/types/dynamic_fee_tx.go +++ b/types/dynamic_fee_tx.go @@ -16,7 +16,7 @@ type DynamicFeeTx struct { AccessList TxAccessList } -func NewDynamicFeeTx(options ...Option) *DynamicFeeTx { +func NewDynamicFeeTx(options ...TxOption) *DynamicFeeTx { dynamicTx := &DynamicFeeTx{BaseTx: &BaseTx{}} for _, opt := range options { diff --git a/types/legacy_tx.go b/types/legacy_tx.go index 8b0ef4a4dc..668d905e3b 100644 --- a/types/legacy_tx.go +++ b/types/legacy_tx.go @@ -12,7 +12,7 @@ type LegacyTx struct { GasPrice *big.Int } -func NewLegacyTx(options ...Option) *LegacyTx { +func NewLegacyTx(options ...TxOption) *LegacyTx { legacyTx := &LegacyTx{BaseTx: &BaseTx{}} for _, opt := range options { diff --git a/types/state_tx.go b/types/state_tx.go index 4093b2c5f1..0ff7c48fa2 100644 --- a/types/state_tx.go +++ b/types/state_tx.go @@ -12,7 +12,7 @@ type StateTx struct { GasPrice *big.Int } -func NewStateTx(options ...Option) *StateTx { +func NewStateTx(options ...TxOption) *StateTx { stateTx := &StateTx{BaseTx: &BaseTx{}} for _, opt := range options { diff --git a/types/transaction.go b/types/transaction.go index f406b66478..e5bb986234 100644 --- a/types/transaction.go +++ b/types/transaction.go @@ -392,81 +392,81 @@ func NewTxWithType(txType TxType) *Transaction { return tx } -type Option func(TxData) +type TxOption func(TxData) -func WithGasPrice(gasPrice *big.Int) Option { +func WithGasPrice(gasPrice *big.Int) TxOption { return func(td TxData) { td.setGasPrice(gasPrice) } } -func WithNonce(nonce uint64) Option { +func WithNonce(nonce uint64) TxOption { return func(td TxData) { td.setNonce(nonce) } } -func WithGas(gas uint64) Option { +func WithGas(gas uint64) TxOption { return func(td TxData) { td.setGas(gas) } } -func WithTo(to *Address) Option { +func WithTo(to *Address) TxOption { return func(td TxData) { td.setTo(to) } } -func WithValue(value *big.Int) Option { +func WithValue(value *big.Int) TxOption { return func(td TxData) { td.setValue(value) } } -func WithInput(input []byte) Option { +func WithInput(input []byte) TxOption { return func(td TxData) { td.setInput(input) } } -func WithSignatureValues(v, r, s *big.Int) Option { +func WithSignatureValues(v, r, s *big.Int) TxOption { return func(td TxData) { td.setSignatureValues(v, r, s) } } -func WithHash(hash Hash) Option { +func WithHash(hash Hash) TxOption { return func(td TxData) { td.setHash(hash) } } -func WithFrom(from Address) Option { +func WithFrom(from Address) TxOption { return func(td TxData) { td.setFrom(from) } } -func WithGasTipCap(gasTipCap *big.Int) Option { +func WithGasTipCap(gasTipCap *big.Int) TxOption { return func(td TxData) { td.setGasTipCap(gasTipCap) } } -func WithGasFeeCap(gasFeeCap *big.Int) Option { +func WithGasFeeCap(gasFeeCap *big.Int) TxOption { return func(td TxData) { td.setGasFeeCap(gasFeeCap) } } -func WithChainID(chainID *big.Int) Option { +func WithChainID(chainID *big.Int) TxOption { return func(td TxData) { td.setChainID(chainID) } } -func WithAccessList(accessList TxAccessList) Option { +func WithAccessList(accessList TxAccessList) TxOption { return func(td TxData) { td.setAccessList(accessList) }