Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move txPool work from Processing loop event to txPool thread #7469

Merged
merged 9 commits into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/Nethermind/Nethermind.TxPool.Test/TxPoolTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,12 @@ public void should_not_ignore_insufficient_funds_for_eip1559_transactions()
txPool.GetPendingTransactionsCount().Should().Be(0);
result.Should().Be(AcceptTxResult.InsufficientFunds);
EnsureSenderBalance(tx.SenderAddress, tx.Value);

var headProcessed = new ManualResetEventSlim(false);
txPool.HeadChanged += (s, a) => headProcessed.Set();
_blockTree.BlockAddedToMain += Raise.EventWith(_blockTree, new BlockReplacementEventArgs(Build.A.Block.WithGasLimit(10000000).TestObject));

headProcessed.Wait();
result = txPool.SubmitTx(tx, TxHandlingOptions.PersistentBroadcast);
result.Should().Be(AcceptTxResult.InsufficientFunds);
txPool.GetPendingTransactionsCount().Should().Be(0);
Expand Down Expand Up @@ -719,7 +724,12 @@ public void should_remove_txHash_from_hashCache_when_tx_removed_because_of_txPoo
EnsureSenderBalance(higherPriorityTx);
_txPool.SubmitTx(higherPriorityTx, TxHandlingOptions.PersistentBroadcast);

var headProcessed = new ManualResetEventSlim(false);
_txPool.HeadChanged += (s, a) => headProcessed.Set();

_blockTree.BlockAddedToMain += Raise.EventWith(new BlockReplacementEventArgs(Build.A.Block.TestObject));

headProcessed.Wait();
_txPool.IsKnown(higherPriorityTx.Hash).Should().BeTrue();
_txPool.IsKnown(transaction.Hash).Should().BeFalse();
}
Expand Down
11 changes: 7 additions & 4 deletions src/Nethermind/Nethermind.TxPool/TxPool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -191,10 +191,6 @@ private void OnHeadChange(object? sender, BlockReplacementEventArgs e)
{
try
{
// Clear snapshot
_transactionSnapshot = null;
_blobTransactionSnapshot = null;
LukaszRozmej marked this conversation as resolved.
Show resolved Hide resolved
_hashCache.ClearCurrentBlockCache();
benaadams marked this conversation as resolved.
Show resolved Hide resolved
_headBlocksChannel.Writer.TryWrite(e);
}
catch (Exception exception)
Expand All @@ -205,14 +201,20 @@ private void OnHeadChange(object? sender, BlockReplacementEventArgs e)
}
}

public event EventHandler<BlockReplacementEventArgs>? HeadChanged;
benaadams marked this conversation as resolved.
Show resolved Hide resolved

private void ProcessNewHeads()
{
Task.Factory.StartNew(async () =>
{
while (await _headBlocksChannel.Reader.WaitToReadAsync())
{
_hashCache.ClearCurrentBlockCache();
while (_headBlocksChannel.Reader.TryRead(out BlockReplacementEventArgs? args))
{
// Clear snapshot
_transactionSnapshot = null;
_blobTransactionSnapshot = null;
try
{
ArrayPoolList<AddressAsKey>? accountChanges = args.Block.AccountChanges;
Expand All @@ -236,6 +238,7 @@ private void ProcessNewHeads()
RemoveProcessedTransactions(args.Block);
UpdateBuckets();
_broadcaster.OnNewHead();
HeadChanged?.Invoke(this, args);
Metrics.TransactionCount = _transactions.Count;
Metrics.BlobTransactionCount = _blobTransactions.Count;
}
Expand Down