diff --git a/src/Nethermind/Nethermind.State/StateProvider.cs b/src/Nethermind/Nethermind.State/StateProvider.cs index 39981c9c5a94..9121b5bb0c2a 100644 --- a/src/Nethermind/Nethermind.State/StateProvider.cs +++ b/src/Nethermind/Nethermind.State/StateProvider.cs @@ -547,6 +547,7 @@ public void Commit(IReleaseSpec releaseSpec, IWorldStateTracer stateTracer, bool { FlushToTree(); } + return; } diff --git a/src/Nethermind/Nethermind.TxPool.Test/Collections/DistinctValueSortedPoolTests.cs b/src/Nethermind/Nethermind.TxPool.Test/Collections/DistinctValueSortedPoolTests.cs index 19511d76d916..8d96891b8ba5 100644 --- a/src/Nethermind/Nethermind.TxPool.Test/Collections/DistinctValueSortedPoolTests.cs +++ b/src/Nethermind/Nethermind.TxPool.Test/Collections/DistinctValueSortedPoolTests.cs @@ -24,7 +24,7 @@ namespace Nethermind.TxPool.Test.Collections { [TestFixture] - [Parallelizable(ParallelScope.None)] + [Parallelizable(ParallelScope.Self)] public class DistinctValueSortedPoolTests { private const int Capacity = 16; @@ -73,13 +73,12 @@ public static IEnumerable DistinctTestCases [TestCaseSource(nameof(DistinctTestCases))] public void Distinct_transactions_are_all_added(Transaction[] transactions, int expectedCount) { - var pool = new TxDistinctSortedPool(Capacity, _transactionComparerProvider.GetDefaultComparer(), LimboLogs.Instance); - foreach (var transaction in transactions) + Parallel.ForEach(transactions, transaction => { pool.TryInsert(transaction.Hash, transaction); - } + }); pool.Count.Should().Be(expectedCount); } @@ -219,14 +218,11 @@ public void Capacity_can_shrink_to_given_value(int shrinkValue, int expectedCapa int capacityMultiplier = 10; int expectedAllCount = Capacity * capacityMultiplier; - WithFinalizer newOne; - for (int i = 0; i < expectedAllCount; i++) + Parallel.For(0, expectedAllCount, i => { - newOne = new WithFinalizer(); + WithFinalizer newOne = new(); pool.TryInsert(newOne.Index, newOne); - } - - newOne = null; + }); CollectAndFinalize(); diff --git a/src/Nethermind/Nethermind.TxPool.Test/NonceManagerTests.cs b/src/Nethermind/Nethermind.TxPool.Test/NonceManagerTests.cs index cf3add450cbc..dce8e3bde40c 100644 --- a/src/Nethermind/Nethermind.TxPool.Test/NonceManagerTests.cs +++ b/src/Nethermind/Nethermind.TxPool.Test/NonceManagerTests.cs @@ -191,7 +191,7 @@ public void should_reuse_nonce_if_tx_rejected() } [Test] - [Repeat(10)] + [Repeat(2)] public void should_lock_on_same_account() { using NonceLocker locker = _nonceManager.ReserveNonce(TestItem.AddressA, out UInt256 nonce); @@ -200,7 +200,7 @@ public void should_lock_on_same_account() { using NonceLocker locker = _nonceManager.ReserveNonce(TestItem.AddressA, out UInt256 _); }); - TimeSpan ts = TimeSpan.FromMilliseconds(1000); + TimeSpan ts = TimeSpan.FromMilliseconds(100); task.Wait(ts); task.IsCompleted.Should().Be(false); } diff --git a/src/Nethermind/Nethermind.TxPool.Test/TxBroadcasterTests.cs b/src/Nethermind/Nethermind.TxPool.Test/TxBroadcasterTests.cs index 07328103f408..82718dea4c71 100644 --- a/src/Nethermind/Nethermind.TxPool.Test/TxBroadcasterTests.cs +++ b/src/Nethermind/Nethermind.TxPool.Test/TxBroadcasterTests.cs @@ -76,7 +76,7 @@ public async Task should_not_broadcast_persisted_tx_to_peer_too_quickly() int addedTxsCount = TestItem.PrivateKeys.Length; Transaction[] transactions = new Transaction[addedTxsCount]; - for (int i = 0; i < addedTxsCount; i++) + Parallel.For(0, addedTxsCount, i => { transactions[i] = Build.A.Transaction .WithGasPrice(i.GWei()) @@ -84,7 +84,7 @@ public async Task should_not_broadcast_persisted_tx_to_peer_too_quickly() .TestObject; _broadcaster.Broadcast(transactions[i], true); - } + }); _broadcaster.GetSnapshot().Length.Should().Be(addedTxsCount); @@ -126,7 +126,7 @@ public void should_pick_best_persistent_txs_to_broadcast([Values(1, 2, 99, 100, int addedTxsCount = TestItem.PrivateKeys.Length; Transaction[] transactions = new Transaction[addedTxsCount]; - for (int i = 0; i < addedTxsCount; i++) + Parallel.For(0, addedTxsCount, i => { transactions[i] = Build.A.Transaction .WithGasPrice(i.GWei()) @@ -134,7 +134,7 @@ public void should_pick_best_persistent_txs_to_broadcast([Values(1, 2, 99, 100, .TestObject; _broadcaster.Broadcast(transactions[i], true); - } + }); _broadcaster.GetSnapshot().Length.Should().Be(addedTxsCount); @@ -210,7 +210,7 @@ public void should_skip_large_txs_when_picking_best_persistent_txs_to_broadcast( int addedTxsCount = TestItem.PrivateKeys.Length; Transaction[] transactions = new Transaction[addedTxsCount]; - for (int i = 0; i < addedTxsCount; i++) + Parallel.For(0, addedTxsCount, i => { bool isLarge = i % 10 == 0; transactions[i] = Build.A.Transaction @@ -221,7 +221,7 @@ public void should_skip_large_txs_when_picking_best_persistent_txs_to_broadcast( .TestObject; _broadcaster.Broadcast(transactions[i], true); - } + }); _broadcaster.GetSnapshot().Length.Should().Be(addedTxsCount); @@ -256,7 +256,7 @@ public void should_skip_blob_txs_when_picking_best_persistent_txs_to_broadcast([ int addedTxsCount = TestItem.PrivateKeys.Length; Transaction[] transactions = new Transaction[addedTxsCount]; - for (int i = 0; i < addedTxsCount; i++) + Parallel.For(0, addedTxsCount, i => { bool isBlob = i % 10 == 0; transactions[i] = Build.A.Transaction @@ -267,7 +267,7 @@ public void should_skip_blob_txs_when_picking_best_persistent_txs_to_broadcast([ .TestObject; _broadcaster.Broadcast(transactions[i], true); - } + }); _broadcaster.GetSnapshot().Length.Should().Be(addedTxsCount); @@ -308,7 +308,7 @@ public void should_not_pick_txs_with_GasPrice_lower_than_CurrentBaseFee([Values( int addedTxsCount = TestItem.PrivateKeys.Length; Transaction[] transactions = new Transaction[addedTxsCount]; - for (int i = 0; i < addedTxsCount; i++) + Parallel.For(0, addedTxsCount, i => { transactions[i] = Build.A.Transaction .WithGasPrice(i.GWei()) @@ -316,7 +316,7 @@ public void should_not_pick_txs_with_GasPrice_lower_than_CurrentBaseFee([Values( .TestObject; _broadcaster.Broadcast(transactions[i], true); - } + }); _broadcaster.GetSnapshot().Length.Should().Be(addedTxsCount); @@ -382,7 +382,7 @@ public void should_not_pick_1559_txs_with_MaxFeePerGas_lower_than_CurrentBaseFee int addedTxsCount = TestItem.PrivateKeys.Length; Transaction[] transactions = new Transaction[addedTxsCount]; - for (int i = 0; i < addedTxsCount; i++) + Parallel.For(0, addedTxsCount, i => { transactions[i] = Build.A.Transaction .WithType(TxType.EIP1559) @@ -391,7 +391,7 @@ public void should_not_pick_1559_txs_with_MaxFeePerGas_lower_than_CurrentBaseFee .TestObject; _broadcaster.Broadcast(transactions[i], true); - } + }); _broadcaster.GetSnapshot().Length.Should().Be(addedTxsCount); @@ -423,14 +423,17 @@ public void should_not_pick_blob_txs_with_MaxFeePerBlobGas_lower_than_CurrentFee int addedTxsCount = TestItem.PrivateKeys.Length; Transaction[] transactions = new Transaction[addedTxsCount]; - for (int i = 0; i < addedTxsCount; i++) + Parallel.For(0, addedTxsCount, i => { transactions[i] = Build.A.Transaction .WithShardBlobTxTypeAndFields() .WithMaxFeePerBlobGas(i.GWei()) .SignedAndResolved(_ethereumEcdsa, TestItem.PrivateKeys[i]) .TestObject; + }); + for (int i = 0; i < addedTxsCount; i++) + { _broadcaster.Broadcast(transactions[i], true); } @@ -466,7 +469,7 @@ public void should_pick_tx_with_lowest_nonce_from_bucket() const int addedTxsCount = 5; Transaction[] transactions = new Transaction[addedTxsCount]; - for (int i = 0; i < addedTxsCount; i++) + Parallel.For(0, addedTxsCount, i => { transactions[i] = Build.A.Transaction .WithNonce((UInt256)i) @@ -475,7 +478,7 @@ public void should_pick_tx_with_lowest_nonce_from_bucket() .TestObject; _broadcaster.Broadcast(transactions[i], true); - } + }); _broadcaster.GetSnapshot().Length.Should().Be(addedTxsCount); IList pickedTxs = _broadcaster.GetPersistentTxsToSend().TransactionsToSend; @@ -676,13 +679,13 @@ public void should_rebroadcast_all_persistent_transactions_if_PeerNotificationTh _txPoolConfig = new TxPoolConfig() { Size = 100, PeerNotificationThreshold = shouldBroadcastAll ? 100 : 5 }; _broadcaster = new TxBroadcaster(_comparer, TimerFactory.Default, _txPoolConfig, _headInfo, _logManager); - for (int i = 0; i < _txPoolConfig.Size; i++) + Parallel.For(0, _txPoolConfig.Size, i => { Transaction tx = Build.A.Transaction .WithNonce((UInt256)i) .SignedAndResolved(TestItem.PrivateKeyA).TestObject; _broadcaster.Broadcast(tx, true); - } + }); Transaction[] pickedTxs = _broadcaster.GetPersistentTxsToSend().TransactionsToSend.ToArray(); pickedTxs.Length.Should().Be(shouldBroadcastAll ? 100 : 1); diff --git a/src/Nethermind/Nethermind.TxPool.Test/TxPoolTests.Blobs.cs b/src/Nethermind/Nethermind.TxPool.Test/TxPoolTests.Blobs.cs index ea37e41afa26..5d1e4ff7aaaf 100644 --- a/src/Nethermind/Nethermind.TxPool.Test/TxPoolTests.Blobs.cs +++ b/src/Nethermind/Nethermind.TxPool.Test/TxPoolTests.Blobs.cs @@ -132,17 +132,22 @@ public void should_reject_txs_with_nonce_too_far_in_future(TxType txType, int ma }; _txPool = CreatePool(txPoolConfig, GetCancunSpecProvider()); EnsureSenderBalance(TestItem.AddressA, UInt256.MaxValue); - for (int nonce = 0; nonce < txPoolConfig.Size; nonce++) + + Transaction[] txs = new Transaction[txPoolConfig.Size]; + Parallel.For(0, txPoolConfig.Size, (nonce) => { - Transaction tx = Build.A.Transaction + txs[nonce] = Build.A.Transaction .WithNonce((UInt256)nonce) .WithType(txType) .WithShardBlobTxTypeAndFieldsIfBlobTx() .WithMaxFeePerGas(1.GWei()) .WithMaxPriorityFeePerGas(1.GWei()) .SignedAndResolved(_ethereumEcdsa, TestItem.PrivateKeyA).TestObject; + }); - _txPool.SubmitTx(tx, TxHandlingOptions.None).Should().Be(nonce > expectedNumberOfAcceptedTxs + for (int nonce = 0; nonce < txPoolConfig.Size; nonce++) + { + _txPool.SubmitTx(txs[nonce], TxHandlingOptions.None).Should().Be(nonce > expectedNumberOfAcceptedTxs ? AcceptTxResult.NonceTooFarInFuture : AcceptTxResult.Accepted); } @@ -775,7 +780,8 @@ public void should_index_blobs_when_adding_txs([Values(true, false)] bool isPers public void should_handle_indexing_blobs_when_adding_txs_in_parallel([Values(true, false)] bool isPersistentStorage) { const int txsPerSender = 10; - int poolSize = TestItem.PrivateKeys.Length * txsPerSender; + PrivateKey[] testPrivateKeys = TestItem.PrivateKeys[..64]; + int poolSize = testPrivateKeys.Length * txsPerSender; TxPoolConfig txPoolConfig = new() { BlobsSupport = isPersistentStorage ? BlobsSupportMode.Storage : BlobsSupportMode.InMemory, @@ -791,13 +797,13 @@ public void should_handle_indexing_blobs_when_adding_txs_in_parallel([Values(tru byte[] expectedBlobVersionedHash = null; - foreach (PrivateKey privateKey in TestItem.PrivateKeys) + foreach (PrivateKey privateKey in testPrivateKeys) { EnsureSenderBalance(privateKey.Address, UInt256.MaxValue); } // adding, getting and removing txs in parallel - Parallel.ForEach(TestItem.PrivateKeys, privateKey => + Parallel.ForEach(testPrivateKeys, privateKey => { for (int i = 0; i < txsPerSender; i++) { diff --git a/src/Nethermind/Nethermind.TxPool.Test/TxPoolTests.cs b/src/Nethermind/Nethermind.TxPool.Test/TxPoolTests.cs index 0e6e00b4c795..823d402adea2 100644 --- a/src/Nethermind/Nethermind.TxPool.Test/TxPoolTests.cs +++ b/src/Nethermind/Nethermind.TxPool.Test/TxPoolTests.cs @@ -15,6 +15,7 @@ using Nethermind.Consensus.Transactions; using Nethermind.Consensus.Validators; using Nethermind.Core; +using Nethermind.Core.Collections; using Nethermind.Core.Crypto; using Nethermind.Core.Events; using Nethermind.Core.Extensions; @@ -563,7 +564,7 @@ public void should_not_add_tx_if_already_pending_lower_nonces_are_exhausting_bal EnsureSenderBalance(TestItem.AddressA, (UInt256)(oneTxPrice * numberOfTxsPossibleToExecuteBeforeGasExhaustion)); - for (int i = 0; i < 10; i++) + Parallel.For(0, 10, i => { transactions[i] = Build.A.Transaction .WithSenderAddress(TestItem.AddressA) @@ -572,6 +573,10 @@ public void should_not_add_tx_if_already_pending_lower_nonces_are_exhausting_bal .WithGasLimit(_txGasLimit) .WithValue(value) .SignedAndResolved(_ethereumEcdsa, TestItem.PrivateKeyA).TestObject; + }); + + for (int i = 0; i < 10; i++) + { _txPool.SubmitTx(transactions[i], TxHandlingOptions.PersistentBroadcast); } @@ -591,16 +596,22 @@ public void should_not_count_txs_with_stale_nonces_when_calculating_cumulative_c EnsureSenderBalance(TestItem.AddressA, (UInt256)(oneTxPrice * numberOfTxsPossibleToExecuteBeforeGasExhaustion)); - for (int i = 0; i < numberOfTxsPossibleToExecuteBeforeGasExhaustion * 2; i++) + int count = numberOfTxsPossibleToExecuteBeforeGasExhaustion * 2; + using ArrayPoolList transactions = new(count, count); + Parallel.For(0, count, i => { - Transaction tx = Build.A.Transaction + transactions[i] = Build.A.Transaction .WithSenderAddress(TestItem.AddressA) .WithNonce((UInt256)i) .WithGasPrice((UInt256)gasPrice) .WithGasLimit(_txGasLimit) .WithValue(value) .SignedAndResolved(_ethereumEcdsa, TestItem.PrivateKeyA).TestObject; - _txPool.SubmitTx(tx, TxHandlingOptions.PersistentBroadcast); + }); + + for (int i = 0; i < count; i++) + { + _txPool.SubmitTx(transactions[i], TxHandlingOptions.PersistentBroadcast); if (i < numberOfStaleTxsInBucket) { @@ -617,15 +628,16 @@ public void should_not_count_txs_with_stale_nonces_when_calculating_cumulative_c [Test] public void should_add_tx_if_cost_of_executing_all_txs_in_bucket_exceeds_balance_but_these_with_lower_nonces_doesnt() { + const int count = 10; const int gasPrice = 10; const int value = 1; int oneTxPrice = _txGasLimit * gasPrice + value; _txPool = CreatePool(); - Transaction[] transactions = new Transaction[10]; + Transaction[] transactions = new Transaction[count]; EnsureSenderBalance(TestItem.AddressA, (UInt256)(oneTxPrice * 8)); - for (int i = 0; i < 10; i++) + Parallel.For(0, count, i => { transactions[i] = Build.A.Transaction .WithSenderAddress(TestItem.AddressA) @@ -634,6 +646,10 @@ public void should_add_tx_if_cost_of_executing_all_txs_in_bucket_exceeds_balance .WithGasLimit(_txGasLimit) .WithValue(value) .SignedAndResolved(_ethereumEcdsa, TestItem.PrivateKeyA).TestObject; + }); + + for (int i = 0; i < count; i++) + { if (i != 7) { _txPool.SubmitTx(transactions[i], TxHandlingOptions.PersistentBroadcast); @@ -662,7 +678,7 @@ public void should_discard_tx_because_of_overflow_of_cumulative_cost_of_this_tx_ UInt256.MaxValue.Divide(GasCostOf.Transaction * 2, out UInt256 halfOfMaxGasPriceWithoutOverflow); - for (int i = 0; i < 3; i++) + Parallel.For(0, 3, i => { transactions[i] = Build.A.Transaction .WithSenderAddress(TestItem.AddressA) @@ -670,11 +686,10 @@ public void should_discard_tx_because_of_overflow_of_cumulative_cost_of_this_tx_ .WithGasPrice(halfOfMaxGasPriceWithoutOverflow) .WithGasLimit(GasCostOf.Transaction) .SignedAndResolved(_ethereumEcdsa, TestItem.PrivateKeyA).TestObject; - if (i != 2) - { - _txPool.SubmitTx(transactions[i], TxHandlingOptions.PersistentBroadcast); - } - } + }); + + _txPool.SubmitTx(transactions[0], TxHandlingOptions.PersistentBroadcast); + _txPool.SubmitTx(transactions[1], TxHandlingOptions.PersistentBroadcast); transactions[2].GasPrice = 5; _txPool.GetPendingTransactionsCount().Should().Be(2); @@ -689,7 +704,7 @@ public async Task should_not_dump_GasBottleneck_of_all_txs_in_bucket_if_first_tx EnsureSenderBalance(TestItem.AddressA, UInt256.MaxValue); - for (int i = 0; i < 5; i++) + Parallel.For(0, 5, i => { transactions[i] = Build.A.Transaction .WithSenderAddress(TestItem.AddressA) @@ -697,7 +712,7 @@ public async Task should_not_dump_GasBottleneck_of_all_txs_in_bucket_if_first_tx .WithGasPrice((UInt256)(i + 2)) .SignedAndResolved(_ethereumEcdsa, TestItem.PrivateKeyA).TestObject; _txPool.SubmitTx(transactions[i], TxHandlingOptions.PersistentBroadcast); - } + }); for (int i = 0; i < 3; i++) { @@ -720,7 +735,7 @@ public async Task should_not_fail_if_there_is_no_current_nonce_in_bucket() EnsureSenderBalance(TestItem.AddressA, UInt256.MaxValue); - for (int i = 0; i < 3; i++) + Parallel.For(0, 3, i => { transactions[i] = Build.A.Transaction .WithSenderAddress(TestItem.AddressA) @@ -728,7 +743,7 @@ public async Task should_not_fail_if_there_is_no_current_nonce_in_bucket() .WithGasPrice((UInt256)(i + 2)) .SignedAndResolved(_ethereumEcdsa, TestItem.PrivateKeyA).TestObject; _txPool.SubmitTx(transactions[i], TxHandlingOptions.PersistentBroadcast); - } + }); for (int i = 0; i < 3; i++) { @@ -775,13 +790,17 @@ public void should_calculate_gasBottleneck_properly() _txPool = CreatePool(); Transaction[] transactions = new Transaction[5]; - for (int i = 0; i < 5; i++) + Parallel.For(0, 5, i => { transactions[i] = Build.A.Transaction .WithSenderAddress(TestItem.AddressA) .WithNonce((UInt256)i) .WithGasPrice((UInt256)(i + 2)) .SignedAndResolved(_ethereumEcdsa, TestItem.PrivateKeyA).TestObject; + }); + + for (int i = 0; i < 5; i++) + { EnsureSenderBalance(transactions[i]); _txPool.SubmitTx(transactions[i], TxHandlingOptions.PersistentBroadcast); } @@ -797,7 +816,7 @@ public async Task should_remove_txs_with_old_nonces_when_updating_GasBottleneck( Transaction[] transactions = new Transaction[5]; EnsureSenderBalance(TestItem.AddressA, UInt256.MaxValue); - for (int i = 0; i < 5; i++) + Parallel.For(0, 5, i => { transactions[i] = Build.A.Transaction .WithSenderAddress(TestItem.AddressA) @@ -805,7 +824,7 @@ public async Task should_remove_txs_with_old_nonces_when_updating_GasBottleneck( .WithGasPrice((UInt256)(i + 2)) .SignedAndResolved(_ethereumEcdsa, TestItem.PrivateKeyA).TestObject; _txPool.SubmitTx(transactions[i], TxHandlingOptions.PersistentBroadcast); - } + }); _txPool.GetPendingTransactionsCount().Should().Be(5); for (int i = 0; i < 3; i++) @@ -853,7 +872,7 @@ public void should_remove_stale_txs_from_persistent_transactions(int numberOfTxs Transaction[] transactions = new Transaction[numberOfTxs]; EnsureSenderBalance(TestItem.AddressA, UInt256.MaxValue); - for (int i = 0; i < numberOfTxs; i++) + Parallel.For(0, numberOfTxs, i => { transactions[i] = Build.A.Transaction .WithNonce((UInt256)i) @@ -862,7 +881,7 @@ public void should_remove_stale_txs_from_persistent_transactions(int numberOfTxs .SignedAndResolved(_ethereumEcdsa, TestItem.PrivateKeyA) .TestObject; _txPool.SubmitTx(transactions[i], TxHandlingOptions.PersistentBroadcast); - } + }); _txPool.GetOwnPendingTransactions().Length.Should().Be(numberOfTxs); Block block = Build.A.Block.WithTransactions(transactions[nonceIncludedInBlock]).TestObject; @@ -1554,16 +1573,21 @@ public void Should_not_replace_better_txs_by_worse_ones() TxPoolConfig txPoolConfig = new TxPoolConfig { Size = 128 }; _txPool = CreatePool(txPoolConfig); + using ArrayPoolList transactions = new(txPoolConfig.Size, txPoolConfig.Size); // send (size - 1) standard txs from different senders - for (int i = 0; i < txPoolConfig.Size - 1; i++) + Parallel.For(0, txPoolConfig.Size, i => { - Transaction tx = Build.A.Transaction + transactions[i] = Build.A.Transaction .WithNonce(0) .WithValue(0) .WithGasPrice(10) .WithTo(TestItem.AddressB) .SignedAndResolved(_ethereumEcdsa, TestItem.PrivateKeys[i]).TestObject; + }); + for (int i = 0; i < txPoolConfig.Size - 1; i++) + { + Transaction tx = transactions[i]; EnsureSenderBalance(TestItem.PrivateKeys[i].Address, UInt256.MaxValue); AcceptTxResult result = _txPool.SubmitTx(tx, TxHandlingOptions.PersistentBroadcast); @@ -1589,15 +1613,22 @@ public void Should_not_replace_better_txs_by_worse_ones() _txPool.GetPendingTransactionsCount().Should().Be(txPoolConfig.Size); _txPool.GetPendingTransactionsBySender().Keys.Count.Should().Be(txPoolConfig.Size); - // send (size - 1) expensive txs from sender X - for (int i = 0; i < txPoolConfig.Size - 1; i++) + using ArrayPoolList txs = new(txPoolConfig.Size, txPoolConfig.Size); + // send (size - 1) standard txs from different senders + Parallel.For(0, txPoolConfig.Size, i => { - Transaction tx = Build.A.Transaction + txs[i] = Build.A.Transaction .WithNonce((UInt256)(i + 1)) .WithValue(0) .WithGasPrice(1000) .WithTo(TestItem.AddressB) .SignedAndResolved(_ethereumEcdsa, privateKeyOfAttacker).TestObject; + }); + + // send (size - 1) expensive txs from sender X + for (int i = 0; i < txPoolConfig.Size - 1; i++) + { + Transaction tx = txs[i]; AcceptTxResult result = _txPool.SubmitTx(tx, TxHandlingOptions.PersistentBroadcast); result.Should().Be(AcceptTxResult.FeeTooLowToCompete); @@ -1615,13 +1646,20 @@ public void Should_not_replace_ready_txs_by_nonce_gap_ones() TxPoolConfig txPoolConfig = new() { Size = 128 }; _txPool = CreatePool(txPoolConfig); + using ArrayPoolList txs = new(txPoolConfig.Size, txPoolConfig.Size); // send (size - 1) standard txs from different senders - for (int i = 0; i < txPoolConfig.Size - 1; i++) + Parallel.For(0, txPoolConfig.Size, i => { - Transaction tx = Build.A.Transaction + txs[i] = Build.A.Transaction .WithNonce(0) .WithGasPrice(10) .SignedAndResolved(_ethereumEcdsa, TestItem.PrivateKeys[i]).TestObject; + }); + + // send (size - 1) standard txs from different senders + for (int i = 0; i < txPoolConfig.Size - 1; i++) + { + Transaction tx = txs[i]; EnsureSenderBalance(TestItem.PrivateKeys[i].Address, UInt256.MaxValue); _txPool.SubmitTx(tx, TxHandlingOptions.PersistentBroadcast).Should().Be(AcceptTxResult.Accepted); @@ -1644,14 +1682,18 @@ public void Should_not_replace_ready_txs_by_nonce_gap_ones() _txPool.GetPendingTransactionsCount().Should().Be(txPoolConfig.Size); _txPool.GetPendingTransactionsBySender().Keys.Count.Should().Be(txPoolConfig.Size); - // send (size - 1) expensive txs from sender X with consecutive nonces - for (int i = 0; i < txPoolConfig.Size - 1; i++) + using ArrayPoolList txs2 = new(txPoolConfig.Size, txPoolConfig.Size); + Parallel.For(0, txPoolConfig.Size, i => { - Transaction tx = Build.A.Transaction + txs2[i] = Build.A.Transaction .WithNonce((UInt256)(i + 1 + nonceGap)) .WithGasPrice(1000) .SignedAndResolved(_ethereumEcdsa, privateKeyOfAttacker).TestObject; - + }); + // send (size - 1) expensive txs from sender X with consecutive nonces + for (int i = 0; i < txPoolConfig.Size - 1; i++) + { + Transaction tx = txs2[i]; _txPool.SubmitTx(tx, TxHandlingOptions.PersistentBroadcast).Should().Be(AcceptTxResult.FeeTooLowToCompete); // newly coming txs should evict themselves @@ -1668,16 +1710,21 @@ public void Should_not_add_underpaid_tx_even_if_lower_nonces_are_expensive(int g TxPoolConfig txPoolConfig = new TxPoolConfig { Size = 128 }; _txPool = CreatePool(txPoolConfig); - // send standard txs from different senders - for (int i = 1; i < txPoolConfig.Size; i++) + using ArrayPoolList txs = new(txPoolConfig.Size, txPoolConfig.Size); + Parallel.For(1, txPoolConfig.Size, i => { - Transaction tx = Build.A.Transaction + txs[i] = Build.A.Transaction .WithNonce(0) .WithValue(0) .WithGasPrice(10) .WithTo(TestItem.AddressB) .SignedAndResolved(_ethereumEcdsa, TestItem.PrivateKeys[i]).TestObject; + }); + // send standard txs from different senders + for (int i = 1; i < txPoolConfig.Size; i++) + { + Transaction tx = txs[i]; EnsureSenderBalance(TestItem.PrivateKeys[i].Address, UInt256.MaxValue); _txPool.SubmitTx(tx, TxHandlingOptions.PersistentBroadcast).Should().Be(AcceptTxResult.Accepted); } @@ -2083,13 +2130,13 @@ public void SetCode_tx_can_be_replaced_and_remove_pending_delegation_restriction result.Should().Be(AcceptTxResult.Accepted); Transaction thirdTx = Build.A.Transaction - .WithNonce(1) - .WithType(TxType.EIP1559) - .WithMaxFeePerGas(9.GWei()) - .WithMaxPriorityFeePerGas(9.GWei()) - .WithGasLimit(GasCostOf.Transaction) - .WithTo(TestItem.AddressB) - .SignedAndResolved(_ethereumEcdsa, signer).TestObject; + .WithNonce(1) + .WithType(TxType.EIP1559) + .WithMaxFeePerGas(9.GWei()) + .WithMaxPriorityFeePerGas(9.GWei()) + .WithGasLimit(GasCostOf.Transaction) + .WithTo(TestItem.AddressB) + .SignedAndResolved(_ethereumEcdsa, signer).TestObject; result = _txPool.SubmitTx(thirdTx, TxHandlingOptions.PersistentBroadcast);