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/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 6e243aa8e3..f6ce56840e 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{ @@ -342,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/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/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_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..48067deb1e 100644 --- a/crypto/txsigner_london_test.go +++ b/crypto/txsigner_london_test.go @@ -74,38 +74,30 @@ 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.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), - 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() @@ -142,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/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..1c744011bd 100644 --- a/e2e/txpool_test.go +++ b/e2e/txpool_test.go @@ -46,25 +46,21 @@ 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{ - 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.WithChainID(new(big.Int).SetUint64(defaultChainID)), + types.WithNonce(params.nonce), + types.WithTo(¶ms.toAddress), + types.WithGas(1000000), + types.WithValue(params.value), + )) unsignedTx.SetGasFeeCap(params.gasFeeCap) unsignedTx.SetGasTipCap(params.gasTipCap) } @@ -245,39 +241,33 @@ 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.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.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.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) { @@ -359,17 +349,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/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/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/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_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..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, }, { @@ -69,17 +67,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 +93,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 +114,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 +134,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, }, } @@ -279,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/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..19f521d6c7 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 { @@ -710,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, }, { @@ -735,17 +729,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 +762,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 +786,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 +805,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..a46dae8e17 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() @@ -315,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 3b6cd06e57..3b6da3a0ba 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) @@ -136,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/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/evm_benchmark_test.go b/tests/evm_benchmark_test.go index 01e94bd90d..3c333f8526 100644 --- a/tests/evm_benchmark_test.go +++ b/tests/evm_benchmark_test.go @@ -5,6 +5,8 @@ import ( "fmt" "math/big" "os" + "path/filepath" + "strings" "testing" "time" @@ -18,48 +20,47 @@ import ( ) const ( - benchmarksDir = "evm-benchmarks/benchmarks" - chainID = 10 + benchmarksDir = "evm-benchmarks" + testsExtension = ".json" + chainID = 10 ) func BenchmarkEVM(b *testing.B) { - folders, err := listFolders([]string{benchmarksDir}) + files, err := listFiles(benchmarksDir, testsExtension) require.NoError(b, err) - for _, folder := range folders { - files, err := listFiles(folder, ".json") - require.NoError(b, err) + for _, file := range files { + name := filepath.ToSlash(strings.TrimPrefix(strings.TrimSuffix(file, testsExtension), benchmarksDir+string(filepath.Separator))) - for _, file := range files { - name := getTestName(file) + b.Run(name, func(b *testing.B) { + data, err := os.ReadFile(file) + require.NoError(b, err) - b.Run(name, func(b *testing.B) { - data, err := os.ReadFile(file) - require.NoError(b, err) + var testCases map[string]testCase + if err = json.Unmarshal(data, &testCases); err != nil { + b.Fatalf("failed to unmarshal %s: %v", file, err) + } - var testCases map[string]testCase - if err = json.Unmarshal(data, &testCases); err != nil { - b.Fatalf("failed to unmarshal %s: %v", file, err) - } - - for _, tc := range testCases { - for fork, postState := range tc.Post { - forks, exists := Forks[fork] - if !exists { - b.Logf("%s fork is not supported, skipping test case.", fork) - continue - } + for _, tc := range testCases { + for fork, postState := range tc.Post { + forks, exists := Forks[fork] + if !exists { + b.Logf("%s fork is not supported, skipping test case.", fork) + continue + } - fc := &forkConfig{name: fork, forks: forks} + fc := &forkConfig{name: fork, forks: forks} - for idx, postStateEntry := range postState { + for idx, postStateEntry := range postState { + key := fmt.Sprintf("%s/%d", fork, idx) + b.Run(key, func(b *testing.B) { err := runBenchmarkTest(b, tc, fc, postStateEntry) require.NoError(b, err, fmt.Sprintf("test %s (case#%d) execution failed", name, idx)) - } + }) } } - }) - } + } + }) } } diff --git a/tests/state_test.go b/tests/state_test.go index e4fe770177..ebc6a287c7 100644 --- a/tests/state_test.go +++ b/tests/state_test.go @@ -52,60 +52,55 @@ func TestState(t *testing.T) { "CALLBlake2f_MaxRounds", } - folders, err := listFolders([]string{stateTestsDir}) + files, err := listFiles(stateTestsDir, ".json") require.NoError(t, err) - for _, folder := range folders { - files, err := listFiles(folder, ".json") - require.NoError(t, err) + for _, file := range files { + if contains(long, file) && testing.Short() { + t.Logf("Long test '%s' is skipped in short mode\n", file) - for _, file := range files { - if contains(long, file) && testing.Short() { - t.Logf("Long test '%s' is skipped in short mode\n", file) - - continue - } + continue + } - if contains(skip, file) { - t.Logf("Test '%s' is skipped\n", file) + if contains(skip, file) { + t.Logf("Test '%s' is skipped\n", file) - continue - } + continue + } - file := file - t.Run(file, func(t *testing.T) { - t.Parallel() + file := file + t.Run(file, func(t *testing.T) { + t.Parallel() - data, err := os.ReadFile(file) - require.NoError(t, err) + data, err := os.ReadFile(file) + require.NoError(t, err) - var testCases map[string]testCase - if err = json.Unmarshal(data, &testCases); err != nil { - t.Fatalf("failed to unmarshal %s: %v", file, err) - } + var testCases map[string]testCase + if err = json.Unmarshal(data, &testCases); err != nil { + t.Fatalf("failed to unmarshal %s: %v", file, err) + } - for _, tc := range testCases { - for fork, postState := range tc.Post { - forks, exists := Forks[fork] - if !exists { - t.Logf("%s fork is not supported, skipping test case.", fork) - continue - } + for _, tc := range testCases { + for fork, postState := range tc.Post { + forks, exists := Forks[fork] + if !exists { + t.Logf("%s fork is not supported, skipping test case.", fork) + continue + } - fc := &forkConfig{name: fork, forks: forks} + fc := &forkConfig{name: fork, forks: forks} - for idx, postStateEntry := range postState { - start := time.Now() - err := runSpecificTestCase(t, file, tc, fc, idx, postStateEntry) + for idx, postStateEntry := range postState { + start := time.Now() + err := runSpecificTestCase(t, file, tc, fc, idx, postStateEntry) - t.Logf("'%s' executed. Fork: %s. Case: %d, Duration=%v\n", file, fork, idx, time.Since(start)) + t.Logf("'%s' executed. Fork: %s. Case: %d, Duration=%v\n", file, fork, idx, time.Since(start)) - require.NoError(t, tc.checkError(fork, idx, err)) - } + require.NoError(t, tc.checkError(fork, idx, err)) } } - }) - } + } + }) } } diff --git a/tests/state_test_util.go b/tests/state_test_util.go index 43f0be030a..88fad90f35 100644 --- a/tests/state_test_util.go +++ b/tests/state_test_util.go @@ -6,7 +6,6 @@ import ( "fmt" "io/fs" "math/big" - "os" "path/filepath" "strings" "testing" @@ -329,47 +328,42 @@ 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.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])), + ) } 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])), + ) } } @@ -615,57 +609,28 @@ func contains(l []string, name string) bool { return false } -func listFolders(paths []string) ([]string, error) { - var folders []string - - for _, rootPath := range paths { - err := filepath.WalkDir(rootPath, func(path string, d fs.DirEntry, err error) error { - if err != nil { - return err - } - - if d.IsDir() { - files, err := os.ReadDir(path) - if err != nil { - return err - } - - if len(files) > 0 { - folders = append(folders, path) - } - } - - return nil - }) - - if err != nil { - return nil, err - } - } - - return folders, nil -} - func listFiles(folder string, extensions ...string) ([]string, error) { var files []string - err := filepath.WalkDir(folder, func(path string, d fs.DirEntry, err error) error { + err := filepath.Walk(folder, func(path string, info fs.FileInfo, err error) error { if err != nil { return err } - if !d.IsDir() { - if len(extensions) > 0 { - // filter files by extensions - for _, ext := range extensions { - if strings.HasSuffix(path, ext) { - files = append(files, path) - } + if info.IsDir() { + return nil + } + + if len(extensions) > 0 { + // filter files by extensions + for _, ext := range extensions { + if fileExt := filepath.Ext(path); fileExt == ext { + files = append(files, path) } - } else { - // if no extensions filter is provided, add all files - files = append(files, path) } + } else { + // if no extensions filter is provided, add all files + files = append(files, path) } return nil diff --git a/types/access_list_tx.go b/types/access_list_tx.go index e0b47b76ae..1df0b26bf7 100644 --- a/types/access_list_tx.go +++ b/types/access_list_tx.go @@ -112,6 +112,16 @@ type AccessListTxn struct { AccessList TxAccessList } +func NewAccessListTx(options ...TxOption) *AccessListTxn { + accessListTx := &AccessListTxn{BaseTx: &BaseTx{}} + + for _, opt := range options { + opt(accessListTx) + } + + return accessListTx +} + 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 +307,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..b5c33eefd8 100644 --- a/types/dynamic_fee_tx.go +++ b/types/dynamic_fee_tx.go @@ -16,6 +16,16 @@ type DynamicFeeTx struct { AccessList TxAccessList } +func NewDynamicFeeTx(options ...TxOption) *DynamicFeeTx { + dynamicTx := &DynamicFeeTx{BaseTx: &BaseTx{}} + + for _, opt := range options { + opt(dynamicTx) + } + + return dynamicTx +} + 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 +218,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..668d905e3b 100644 --- a/types/legacy_tx.go +++ b/types/legacy_tx.go @@ -12,6 +12,16 @@ type LegacyTx struct { GasPrice *big.Int } +func NewLegacyTx(options ...TxOption) *LegacyTx { + legacyTx := &LegacyTx{BaseTx: &BaseTx{}} + + for _, opt := range options { + opt(legacyTx) + } + + return legacyTx +} + 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 +169,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..0ff7c48fa2 100644 --- a/types/state_tx.go +++ b/types/state_tx.go @@ -12,6 +12,16 @@ type StateTx struct { GasPrice *big.Int } +func NewStateTx(options ...TxOption) *StateTx { + stateTx := &StateTx{BaseTx: &BaseTx{}} + + for _, opt := range options { + opt(stateTx) + } + + return stateTx +} + 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 +185,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..e5bb986234 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() } } @@ -391,3 +391,83 @@ func NewTxWithType(txType TxType) *Transaction { return tx } + +type TxOption func(TxData) + +func WithGasPrice(gasPrice *big.Int) TxOption { + return func(td TxData) { + td.setGasPrice(gasPrice) + } +} + +func WithNonce(nonce uint64) TxOption { + return func(td TxData) { + td.setNonce(nonce) + } +} + +func WithGas(gas uint64) TxOption { + return func(td TxData) { + td.setGas(gas) + } +} + +func WithTo(to *Address) TxOption { + return func(td TxData) { + td.setTo(to) + } +} + +func WithValue(value *big.Int) TxOption { + return func(td TxData) { + td.setValue(value) + } +} + +func WithInput(input []byte) TxOption { + return func(td TxData) { + td.setInput(input) + } +} + +func WithSignatureValues(v, r, s *big.Int) TxOption { + return func(td TxData) { + td.setSignatureValues(v, r, s) + } +} + +func WithHash(hash Hash) TxOption { + return func(td TxData) { + td.setHash(hash) + } +} + +func WithFrom(from Address) TxOption { + return func(td TxData) { + td.setFrom(from) + } +} + +func WithGasTipCap(gasTipCap *big.Int) TxOption { + return func(td TxData) { + td.setGasTipCap(gasTipCap) + } +} + +func WithGasFeeCap(gasFeeCap *big.Int) TxOption { + return func(td TxData) { + td.setGasFeeCap(gasFeeCap) + } +} + +func WithChainID(chainID *big.Int) TxOption { + return func(td TxData) { + td.setChainID(chainID) + } +} + +func WithAccessList(accessList TxAccessList) TxOption { + return func(td TxData) { + td.setAccessList(accessList) + } +}