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 43f0be030a..4a4b7891bf 100644 --- a/tests/state_test_util.go +++ b/tests/state_test_util.go @@ -359,17 +359,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) + } +}