Async all the way up
MODIFY
WalletWasabi/Services/WalletService.cs
WalletWasabi/Services/WalletService.cs
@@ -139,7 +139,7 @@ namespace WalletWasabi.Services
BitcoinStore.IndexStore.NewFilter += IndexDownloader_NewFilterAsync;
BitcoinStore.IndexStore.Reorged += IndexDownloader_ReorgedAsync;
- MemPool.TransactionReceived += MemPool_TransactionReceived;
+ MemPool.TransactionReceived += MemPool_TransactionReceivedAsync;
}
private void Coins_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
@@ -147,18 +147,15 @@ namespace WalletWasabi.Services
RefreshCoinHistories();
}
- private static object TransactionProcessingLock { get; } = new object();
+ private static AsyncLock TransactionProcessingLock { get; } = new AsyncLock();
- private void MemPool_TransactionReceived(object sender, SmartTransaction tx)
+ private async void MemPool_TransactionReceivedAsync(object sender, SmartTransaction tx)
{
try
{
- lock (TransactionProcessingLock)
+ using (await TransactionProcessingLock.LockAsync())
{
- var prevCount = TransactionCache.Count;
- ProcessTransaction(tx);
-
- if (prevCount != TransactionCache.Count)
+ if (await ProcessTransactionAsync(tx))
{
SerializeTransactionCache();
}
@@ -263,7 +260,7 @@ namespace WalletWasabi.Services
{
var block = await GetOrDownloadBlockAsync(blockstate.BlockHash, cancel);
- ProcessBlock(blockstate.BlockHeight, block, blockstate.TransactionIndices);
+ await ProcessBlockAsync(blockstate.BlockHeight, block, blockstate.TransactionIndices);
}
// Go through the filters and que to download the matches.
@@ -311,7 +308,7 @@ namespace WalletWasabi.Services
if (mempoolHashes.Contains(tx.GetHash().ToString().Substring(0, compactness)))
{
tx.SetHeight(Height.MemPool);
- ProcessTransaction(tx);
+ await ProcessTransactionAsync(tx);
MemPool.TransactionHashes.TryAdd(tx.GetHash());
Logger.LogInfo<WalletService>($"Transaction was successfully tested against the backend's mempool hashes: {tx.GetHash()}.");
@@ -325,7 +322,7 @@ namespace WalletWasabi.Services
foreach (var tx in transactions)
{
tx.SetHeight(Height.MemPool);
- ProcessTransaction(tx);
+ await ProcessTransactionAsync(tx);
MemPool.TransactionHashes.TryAdd(tx.GetHash());
}
@@ -358,7 +355,7 @@ namespace WalletWasabi.Services
Block currentBlock = await GetOrDownloadBlockAsync(filterModel.BlockHash, cancel); // Wait until not downloaded.
- ProcessBlock(filterModel.BlockHeight, currentBlock);
+ await ProcessBlockAsync(filterModel.BlockHeight, currentBlock);
}
public HdPubKey GetReceiveKey(string label, IEnumerable<HdPubKey> dontTouch = null)
@@ -455,7 +452,7 @@ namespace WalletWasabi.Services
return clusters;
}
- private void ProcessBlock(Height height, Block block, IEnumerable<int> filterByTxIds = null)
+ private async Task ProcessBlockAsync(Height height, Block block, IEnumerable<int> filterByTxIds = null)
{
if (filterByTxIds is null)
{
@@ -463,7 +460,7 @@ namespace WalletWasabi.Services
for (int i = 0; i < block.Transactions.Count; i++)
{
Transaction tx = block.Transactions[i];
- if (ProcessTransaction(new SmartTransaction(tx, height)))
+ if (await ProcessTransactionAsync(new SmartTransaction(tx, height)))
{
relevantIndicies.Add(i);
}
@@ -484,7 +481,7 @@ namespace WalletWasabi.Services
foreach (var i in filterByTxIds.OrderBy(x => x))
{
Transaction tx = block.Transactions[i];
- ProcessTransaction(new SmartTransaction(tx, height));
+ await ProcessTransactionAsync(new SmartTransaction(tx, height));
}
}
@@ -493,7 +490,7 @@ namespace WalletWasabi.Services
NewBlockProcessed?.Invoke(this, block);
}
- private bool ProcessTransaction(SmartTransaction tx)
+ private async Task<bool> ProcessTransactionAsync(SmartTransaction tx)
{
uint256 txId = tx.GetHash();
var walletRelevant = false;
@@ -620,8 +617,6 @@ namespace WalletWasabi.Services
// If it's being mixed and anonset is not sufficient, then queue it.
if (newCoin.Unspent && ChaumianClient.HasIngredients && newCoin.AnonymitySet < ServiceConfiguration.MixUntilAnonymitySet && ChaumianClient.State.Contains(newCoin.SpentOutputs))
- {
- Task.Run(async () =>
{
try
{
@@ -631,7 +626,6 @@ namespace WalletWasabi.Services
{
Logger.LogError<WalletService>(ex);
}
- });
}
// Make sure there's always 21 clean keys generated and indexed.
@@ -1487,7 +1481,7 @@ namespace WalletWasabi.Services
}
}
- ProcessTransaction(new SmartTransaction(transaction.Transaction, Height.MemPool));
+ await ProcessTransactionAsync(new SmartTransaction(transaction.Transaction, Height.MemPool));
MemPool.TransactionHashes.TryAdd(transaction.GetHash());
Logger.LogInfo<WalletService>($"Transaction is successfully broadcasted to backend: {transaction.GetHash()}.");
@@ -1622,12 +1616,8 @@ namespace WalletWasabi.Services
{
BitcoinStore.IndexStore.NewFilter -= IndexDownloader_NewFilterAsync;
BitcoinStore.IndexStore.Reorged -= IndexDownloader_ReorgedAsync;
- MemPool.TransactionReceived -= MemPool_TransactionReceived;
+ MemPool.TransactionReceived -= MemPool_TransactionReceivedAsync;
Coins.CollectionChanged -= Coins_CollectionChanged;
- lock (TransactionProcessingLock)
- {
- SerializeTransactionCache();
- }
DisconnectDisposeNullLocalBitcoinCoreNode();
}

Async all the way up