Refactor mempool cleanup.
Interlocked things ->
Better logging.
MODIFY
WalletWasabi/Services/MemPoolService.cs
WalletWasabi/Services/MemPoolService.cs
@@ -7,6 +7,8 @@ using WalletWasabi.Logging;
using NBitcoin.Protocol;
using System.Collections.Generic;
using System.Linq;
+using System.Net;
+using WalletWasabi.WebClients.Wasabi;
namespace WalletWasabi.Services
{
@@ -24,16 +26,23 @@ namespace WalletWasabi.Services
_cleanupInProcess = 0;
}
- private long _cleanupInProcess;
+ private int _cleanupInProcess;
- public void TryPerformMempoolCleanup(IEnumerable<uint256> allMempoolHashes)
+ /// <summary>
+ /// Tries to perform mempool cleanup with the help of the backend.
+ /// </summary>
+ public async Task<bool> TryPerformMempoolCleanupAsync(Func<Uri> destAction, IPEndPoint torSocks)
{
- if (Interlocked.Read(ref _cleanupInProcess) == 1) return; // If already cleaning, then no need to run it that often.
- Interlocked.Exchange(ref _cleanupInProcess, 1);
+ // If already cleaning, then no need to run it that often.
+ if (Interlocked.CompareExchange(ref _cleanupInProcess, 1, 0) == 1) return false;
+
// This function is designed to prevent forever growing mempool.
try
{
Logger.LogInfo<MemPoolService>("Start cleaning out mempool...");
+ using (var client = new WasabiClient(destAction, torSocks))
+ {
+ var allMempoolHashes = await client.GetMempoolHashesAsync();
uint256[] toRemove = TransactionHashes.Except(allMempoolHashes).ToArray();
foreach (uint256 tx in toRemove)
@@ -42,6 +51,9 @@ namespace WalletWasabi.Services
}
Logger.LogInfo<MemPoolService>($"{toRemove.Count()} transactions were cleaned from mempool.");
}
+
+ return true;
+ }
catch (Exception ex)
{
Logger.LogWarning<MemPoolService>(ex);
@@ -50,22 +62,27 @@ namespace WalletWasabi.Services
{
Interlocked.Exchange(ref _cleanupInProcess, 0);
}
+
+ return false;
}
- public void TryPerformMempoolCleanupAsync(NodesGroup nodes, CancellationToken cancel)
- {
- if (Interlocked.Read(ref _cleanupInProcess) == 1) return; // If already cleaning, then no need to run it that often.
- Interlocked.Exchange(ref _cleanupInProcess, 1);
- Task.Run(async () =>
- {
+ /// <summary>
+ /// Tries to perform mempool cleanup with the help of the connected nodes.
+ /// NOTE: This results in heavy network activity! https://github.com/zkSNACKs/WalletWasabi/issues/1273
+ /// </summary>
+ public async Task<bool> TryPerformMempoolCleanupAsync(NodesGroup nodes, CancellationToken cancel)
+ {
+ // If already cleaning, then no need to run it that often.
+ if (Interlocked.CompareExchange(ref _cleanupInProcess, 1, 0) == 1) return false;
+
// This function is designed to prevent forever growing mempool.
try
{
var delay = TimeSpan.FromMinutes(1);
- if (nodes?.ConnectedNodes is null) return;
+ if (nodes?.ConnectedNodes is null) return false;
while (nodes.ConnectedNodes.Count != nodes.MaximumNodeConnection && nodes.ConnectedNodes.All(x => x.IsConnected))
{
- if (cancel.IsCancellationRequested) return;
+ if (cancel.IsCancellationRequested) return false;
Logger.LogInfo<MemPoolService>($"Not all nodes were in a connected state. Delaying mempool cleanup for {delay.TotalSeconds} seconds...");
await Task.Delay(delay, cancel);
}
@@ -79,11 +96,11 @@ namespace WalletWasabi.Services
{
if (!node.IsConnected) continue;
- if (cancel.IsCancellationRequested) return;
+ if (cancel.IsCancellationRequested) return false;
uint256[] txs = node.GetMempool(cancel);
- if (cancel.IsCancellationRequested) return;
+ if (cancel.IsCancellationRequested) return false;
allTxs.UnionWith(txs);
- if (cancel.IsCancellationRequested) return;
+ if (cancel.IsCancellationRequested) return false;
}
catch (Exception ex)
{
@@ -107,6 +124,8 @@ namespace WalletWasabi.Services
TransactionHashes.TryRemove(tx);
}
Logger.LogInfo<MemPoolService>($"{toRemove.Count()} transactions were cleaned from mempool.");
+
+ return true;
}
catch (Exception ex)
{
@@ -125,7 +144,8 @@ namespace WalletWasabi.Services
{
Interlocked.Exchange(ref _cleanupInProcess, 0);
}
- });
+
+ return false;
}
}
}

MODIFY
WalletWasabi/Services/WalletService.cs
WalletWasabi/Services/WalletService.cs
@@ -246,11 +246,7 @@ namespace WalletWasabi.Services
// Try perform mempool cleanup based on connected nodes' mempools.
if (Synchronizer != null && Synchronizer.GetFiltersLeft() == 0)
{
- using (var client = new WasabiClient(Synchronizer.WasabiClient.TorClient.DestinationUriAction, Synchronizer.WasabiClient.TorClient.TorSocks5EndPoint))
- {
- var mempoolHashes = await client.GetMempoolHashesAsync();
- MemPool?.TryPerformMempoolCleanup(mempoolHashes);
- }
+ await MemPool?.TryPerformMempoolCleanupAsync(Synchronizer.WasabiClient.TorClient.DestinationUriAction, Synchronizer.WasabiClient.TorClient.TorSocks5EndPoint);
}
}

Refactor mempool cleanup.
- Interlocked things -> https://github.com/zkSNACKs/WalletWasabi/issues/1276
- Better logging.