/// <summary> /// Tries to perform mempool cleanup with the help of the backend. /// </summary> public async Task <bool> TryPerformMempoolCleanupAsync(Func <Uri> destAction, IPEndPoint torSocks) { // 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 { if (!TransactionHashes.Any()) { return(true); // There's nothing to cleanup. } Logger.LogInfo <MempoolService>("Start cleaning out mempool..."); using (var client = new WasabiClient(destAction, torSocks)) { var compactness = 10; var allMempoolHashes = await client.GetMempoolHashesAsync(compactness); var toRemove = TransactionHashes.Where(x => !allMempoolHashes.Contains(x.ToString().Substring(0, compactness))); int removedTxCount = 0; foreach (uint256 tx in toRemove) { if (TransactionHashes.TryRemove(tx)) { removedTxCount++; } } Logger.LogInfo <MempoolService>($"{removedTxCount} transactions were cleaned from mempool."); } return(true); } catch (Exception ex) { Logger.LogWarning <MempoolService>(ex); } finally { Interlocked.Exchange(ref _cleanupInProcess, 0); } return(false); }