From 5ac90f425aec6c63d4fef87a3e055b06a9df4021 Mon Sep 17 00:00:00 2001 From: facuMH Date: Wed, 27 Nov 2024 09:49:11 +0100 Subject: [PATCH] changes requested in review --- tests/gas_price_suggestion_test.go | 24 +++++--- tests/rejected_tx_test.go | 97 ++++++++++++++++-------------- 2 files changed, 67 insertions(+), 54 deletions(-) diff --git a/tests/gas_price_suggestion_test.go b/tests/gas_price_suggestion_test.go index 9b01ad229..124b274df 100644 --- a/tests/gas_price_suggestion_test.go +++ b/tests/gas_price_suggestion_test.go @@ -71,25 +71,25 @@ func TestGasPrice_UnderpricedTransactionsAreRejected(t *testing.T) { for _, extra := range []int{-10, 0, baseFee / 100, 4 * baseFee / 100} { feeCap := int64(baseFee + extra) - err = send(factory.makeLegacyTransactionWithPrice(t, nonce, feeCap)) + err = send(factory.makeLegacyTransactionWithPrice(t, nonce, feeCap, 0)) require.ErrorContains(err, "transaction underpriced") - err = send(factory.makeAccessListTransactionWithPrice(t, nonce, feeCap)) + err = send(factory.makeAccessListTransactionWithPrice(t, nonce, feeCap, 0)) require.ErrorContains(err, "transaction underpriced") - err = send(factory.makeDynamicFeeTransactionWithPrice(t, nonce, feeCap)) + err = send(factory.makeDynamicFeeTransactionWithPrice(t, nonce, feeCap, 0)) require.ErrorContains(err, "transaction underpriced") - err = send(factory.makeBlobTransactionWithPrice(t, nonce, feeCap)) + err = send(factory.makeBlobTransactionWithPrice(t, nonce, feeCap, 0)) require.ErrorContains(err, "transaction underpriced") } // Everything over ~5% above the base fee should be accepted. feeCap := int64(baseFee + 7*baseFee/100) - require.NoError(send(factory.makeLegacyTransactionWithPrice(t, nonce, feeCap))) - require.NoError(send(factory.makeAccessListTransactionWithPrice(t, nonce+1, feeCap))) - require.NoError(send(factory.makeDynamicFeeTransactionWithPrice(t, nonce+2, feeCap))) - require.NoError(send(factory.makeBlobTransactionWithPrice(t, nonce+3, feeCap))) + require.NoError(send(factory.makeLegacyTransactionWithPrice(t, nonce, feeCap, 0))) + require.NoError(send(factory.makeAccessListTransactionWithPrice(t, nonce+1, feeCap, 0))) + require.NoError(send(factory.makeDynamicFeeTransactionWithPrice(t, nonce+2, feeCap, 0))) + require.NoError(send(factory.makeBlobTransactionWithPrice(t, nonce+3, feeCap, 0))) } func makeNetAndClient(t *testing.T) (*IntegrationTestNet, *ethclient.Client) { @@ -113,12 +113,14 @@ func (f *txFactory) makeLegacyTransactionWithPrice( t *testing.T, nonce uint64, price int64, + value int64, ) *types.Transaction { transaction, err := types.SignTx(types.NewTx(&types.LegacyTx{ Gas: 21_000, GasPrice: big.NewInt(price), To: &common.Address{}, Nonce: nonce, + Value: big.NewInt(value), }), types.NewEIP155Signer(f.chainId), f.senderKey) require.NoError(t, err, "failed to sign transaction") return transaction @@ -128,6 +130,7 @@ func (f *txFactory) makeAccessListTransactionWithPrice( t *testing.T, nonce uint64, price int64, + value int64, ) *types.Transaction { transaction, err := types.SignTx(types.NewTx(&types.AccessListTx{ ChainID: f.chainId, @@ -135,6 +138,7 @@ func (f *txFactory) makeAccessListTransactionWithPrice( GasPrice: big.NewInt(price), To: &common.Address{}, Nonce: nonce, + Value: big.NewInt(value), }), types.NewEIP2930Signer(f.chainId), f.senderKey) require.NoError(t, err, "failed to sign transaction:") return transaction @@ -144,6 +148,7 @@ func (f *txFactory) makeDynamicFeeTransactionWithPrice( t *testing.T, nonce uint64, price int64, + value int64, ) *types.Transaction { transaction, err := types.SignTx(types.NewTx(&types.DynamicFeeTx{ ChainID: f.chainId, @@ -152,6 +157,7 @@ func (f *txFactory) makeDynamicFeeTransactionWithPrice( GasTipCap: big.NewInt(0), To: &common.Address{}, Nonce: nonce, + Value: big.NewInt(value), }), types.NewLondonSigner(f.chainId), f.senderKey) require.NoError(t, err, "failed to sign transaction:") return transaction @@ -161,6 +167,7 @@ func (f *txFactory) makeBlobTransactionWithPrice( t *testing.T, nonce uint64, price int64, + value int64, ) *types.Transaction { transaction, err := types.SignTx(types.NewTx(&types.BlobTx{ ChainID: uint256.MustFromBig(f.chainId), @@ -168,6 +175,7 @@ func (f *txFactory) makeBlobTransactionWithPrice( GasFeeCap: uint256.MustFromBig(big.NewInt(price)), GasTipCap: uint256.MustFromBig(big.NewInt(0)), Nonce: nonce, + Value: uint256.MustFromBig(big.NewInt(value)), BlobFeeCap: uint256.NewInt(3e10), // fee cap for the blob data BlobHashes: nil, // blob hashes in the transaction Sidecar: nil, // sidecar data in the transaction diff --git a/tests/rejected_tx_test.go b/tests/rejected_tx_test.go index 72d7dd1ce..84f1cb779 100644 --- a/tests/rejected_tx_test.go +++ b/tests/rejected_tx_test.go @@ -2,6 +2,7 @@ package tests import ( "context" + "fmt" "math/big" "testing" @@ -22,53 +23,57 @@ func TestRejectedTx(t *testing.T) { // create a client client, err := net.GetClient() require.NoError(err, "failed to get client") + defer client.Close() - chainId := getChainId(t, net) - - testCases := []struct { - name string - txMaker func(t *testing.T, account *Account, nonce uint64, price int64) *types.Transaction - }{ - {name: "LegacyTx", - txMaker: func(t *testing.T, account *Account, nonce uint64, price int64) *types.Transaction { - factory := &txFactory{ - senderKey: account.PrivateKey, - chainId: chainId, - } - return factory.makeLegacyTransactionWithPrice(t, nonce, price) - }, - }, {name: "AccessListTx", - txMaker: func(t *testing.T, account *Account, nonce uint64, price int64) *types.Transaction { - factory := &txFactory{ - senderKey: account.PrivateKey, - chainId: chainId, - } - return factory.makeAccessListTransactionWithPrice(t, nonce, price) - }, - }, {name: "DynamicTx", - txMaker: func(t *testing.T, account *Account, nonce uint64, price int64) *types.Transaction { - factory := &txFactory{ - senderKey: account.PrivateKey, - chainId: chainId, - } - return factory.makeDynamicFeeTransactionWithPrice(t, nonce, price) + chainId := getChainId(t, client) + + type testCase struct { + makeTx func(t *testing.T, account *Account, nonce uint64, price int64) *types.Transaction + } + + makeTestCaseWithAllTypesOfTx := func(value int64) map[string]testCase { + valueStr := fmt.Sprint(value) + cases := map[string]testCase{ + "LegacyTxWithValue" + valueStr: { + makeTx: func(t *testing.T, account *Account, nonce uint64, price int64) *types.Transaction { + factory := &txFactory{senderKey: account.PrivateKey, chainId: chainId} + return factory.makeLegacyTransactionWithPrice(t, nonce, price, value) + }, }, - }, {name: "BlobTx", - txMaker: func(t *testing.T, account *Account, nonce uint64, price int64) *types.Transaction { - factory := &txFactory{ - senderKey: account.PrivateKey, - chainId: chainId, - } - return factory.makeBlobTransactionWithPrice(t, nonce, price) + "AccessListTxWithValue" + valueStr: { + makeTx: func(t *testing.T, account *Account, nonce uint64, price int64) *types.Transaction { + factory := &txFactory{senderKey: account.PrivateKey, chainId: chainId} + return factory.makeAccessListTransactionWithPrice(t, nonce, price, value) + }, + }, "DynamicTxWithValue" + valueStr: { + makeTx: func(t *testing.T, account *Account, nonce uint64, price int64) *types.Transaction { + factory := &txFactory{senderKey: account.PrivateKey, chainId: chainId} + return factory.makeDynamicFeeTransactionWithPrice(t, nonce, price, value) + }, + }, "BlobTxWithValue" + valueStr: { + makeTx: func(t *testing.T, account *Account, nonce uint64, price int64) *types.Transaction { + factory := &txFactory{senderKey: account.PrivateKey, chainId: chainId} + return factory.makeBlobTransactionWithPrice(t, nonce, price, value) + }, }, - }, + } + return cases + } + + testCases := map[string]testCase{} + for name, test := range makeTestCaseWithAllTypesOfTx(0) { + testCases[name] = test + } + for name, test := range makeTestCaseWithAllTypesOfTx(42) { + testCases[name] = test } - for _, tc := range testCases { - t.Run(tc.name, func(t *testing.T) { + + for name, tc := range testCases { + t.Run(name, func(t *testing.T) { maxFeeCap := getMaxFee(t, client) newAccount := NewAccount() nonce := getNonce(t, client, newAccount.Address()) - testRejectedTx(t, net, newAccount.Address(), tc.txMaker(t, newAccount, nonce, maxFeeCap)) + testRejectedTx(t, net, newAccount.Address(), tc.makeTx(t, newAccount, nonce, maxFeeCap)) }) } } @@ -78,7 +83,10 @@ func testRejectedTx(t *testing.T, net *IntegrationTestNet, account common.Addres // verify estimated cost estimatedCost := tx.Gas()*tx.GasFeeCap().Uint64() + tx.Value().Uint64() - require.Equal(tx.Cost().Uint64(), estimatedCost, "cost of transaction is not equal to balance") + if tx.Type() == types.BlobTxType { + estimatedCost += tx.BlobGasFeeCap().Uint64() * tx.BlobGas() + } + require.Equal(tx.Cost().Int64(), int64(estimatedCost), "transaction estimation is not equal to balance") // provide just enough balance to NOT cover the cost _, err := net.EndowAccount(account, int64(estimatedCost-1)) @@ -104,10 +112,7 @@ func testRejectedTx(t *testing.T, net *IntegrationTestNet, account common.Addres require.GreaterOrEqual(tx.Cost().Uint64(), receipt.EffectiveGasPrice.Uint64()*receipt.GasUsed) } -func getChainId(t *testing.T, net *IntegrationTestNet) *big.Int { - client, err := net.GetClient() - require.NoError(t, err, "failed to get client") - +func getChainId(t *testing.T, client *ethclient.Client) *big.Int { chainId, err := client.ChainID(context.Background()) require.NoError(t, err, "failed to get chain ID::") return chainId @@ -117,7 +122,7 @@ func getMaxFee(t *testing.T, client *ethclient.Client) (maxFeeCap int64) { block, err := client.BlockByNumber(context.Background(), nil) require.NoError(t, err, "failed to get block by number") baseFee := block.BaseFee().Int64() - maxFeeCap = int64(float64(baseFee) * 1.06) + maxFeeCap = int64(float64(baseFee) * 1.05) return }