WasabiSynchronizer: Rename `Cancel` -> `StopCts` & make API contract clear.
MODIFY
WalletWasabi/Services/WasabiSynchronizer.cs
WalletWasabi/Services/WasabiSynchronizer.cs
@@ -52,7 +52,7 @@ namespace WalletWasabi.Services
WasabiClientFactory = wasabiClientFactory;
WasabiClient = wasabiClientFactory.NewBackendClient();
- Cancel = new CancellationTokenSource();
+ StopCts = new CancellationTokenSource();
}
#region EventsPropertiesMembers
@@ -110,7 +110,10 @@ namespace WalletWasabi.Services
public bool IsRunning => Interlocked.Read(ref _running) == StateRunning;
- private CancellationTokenSource Cancel { get; set; }
+ /// <summary>
+ /// Cancellation token source for stopping <see cref="WasabiSynchronizer"/>.
+ /// </summary>
+ private CancellationTokenSource StopCts { get; }
public bool AreRequestsBlocked() => Interlocked.Read(ref _blockRequests) == 1;
@@ -153,7 +156,7 @@ namespace WalletWasabi.Services
{
while (AreRequestsBlocked())
{
- await Task.Delay(3000, Cancel.Token).ConfigureAwait(false);
+ await Task.Delay(3000, StopCts.Token).ConfigureAwait(false);
}
EstimateSmartFeeMode? estimateMode = null;
@@ -173,8 +176,8 @@ namespace WalletWasabi.Services
return;
}
- response = await WasabiClient.GetSynchronizeAsync(hashChain.TipHash, maxFiltersToSyncAtInitialization, estimateMode, Cancel.Token)
- .WithAwaitCancellationAsync(Cancel.Token, 300)
+ response = await WasabiClient.GetSynchronizeAsync(hashChain.TipHash, maxFiltersToSyncAtInitialization, estimateMode, StopCts.Token)
+ .WithAwaitCancellationAsync(StopCts.Token, 300)
.ConfigureAwait(false);
// NOT GenSocksServErr
@@ -203,7 +206,7 @@ namespace WalletWasabi.Services
try
{
// Backend API version might be updated meanwhile. Trying to update the versions.
- var result = await WasabiClient.CheckUpdatesAsync(Cancel.Token).ConfigureAwait(false);
+ var result = await WasabiClient.CheckUpdatesAsync(StopCts.Token).ConfigureAwait(false);
// If the backend is compatible and the Api version updated then we just used the wrong API.
if (result.BackendCompatible && lastUsedApiVersion != WasabiClient.ApiVersion)
@@ -271,11 +274,11 @@ namespace WalletWasabi.Services
$"{nameof(firstFilter)}.{nameof(firstFilter.Header)}.{nameof(firstFilter.Header.BlockHash)}:{firstFilter.Header.BlockHash}{Environment.NewLine}" +
$"{nameof(firstFilter)}.{nameof(firstFilter.Header)}.{nameof(firstFilter.Header.Height)}:{firstFilter.Header.Height}");
- await BitcoinStore.IndexStore.RemoveAllImmmatureFiltersAsync(Cancel.Token, deleteAndCrashIfMature: true).ConfigureAwait(false);
+ await BitcoinStore.IndexStore.RemoveAllImmmatureFiltersAsync(StopCts.Token, deleteAndCrashIfMature: true).ConfigureAwait(false);
}
else
{
- await BitcoinStore.IndexStore.AddNewFiltersAsync(filters, Cancel.Token).ConfigureAwait(false);
+ await BitcoinStore.IndexStore.AddNewFiltersAsync(filters, StopCts.Token).ConfigureAwait(false);
if (filters.Count() == 1)
{
@@ -291,7 +294,7 @@ namespace WalletWasabi.Services
{
// Reorg happened
// 1. Rollback index
- FilterModel reorgedFilter = await BitcoinStore.IndexStore.RemoveLastFilterAsync(Cancel.Token).ConfigureAwait(false);
+ FilterModel reorgedFilter = await BitcoinStore.IndexStore.RemoveLastFilterAsync(StopCts.Token).ConfigureAwait(false);
Logger.LogInfo($"REORG Invalid Block: {reorgedFilter.Header.BlockHash}.");
ignoreRequestInterval = true;
@@ -303,7 +306,7 @@ namespace WalletWasabi.Services
if (response.BestHeight > hashChain.TipHeight) // If the server's tip height is larger than ours, we're missing a filter, our index got corrupted.
{
// If still bad delete filters and crash the software?
- await BitcoinStore.IndexStore.RemoveAllImmmatureFiltersAsync(Cancel.Token, deleteAndCrashIfMature: true).ConfigureAwait(false);
+ await BitcoinStore.IndexStore.RemoveAllImmmatureFiltersAsync(StopCts.Token, deleteAndCrashIfMature: true).ConfigureAwait(false);
}
}
@@ -319,7 +322,7 @@ namespace WalletWasabi.Services
Logger.LogError(ex);
try
{
- await Task.Delay(3000, Cancel.Token).ConfigureAwait(false); // Give other threads time to do stuff.
+ await Task.Delay(3000, StopCts.Token).ConfigureAwait(false); // Give other threads time to do stuff.
}
catch (TaskCanceledException ex2)
{
@@ -341,7 +344,7 @@ namespace WalletWasabi.Services
try
{
int delay = (int)Math.Min(requestInterval.TotalMilliseconds, MaxRequestIntervalForMixing.TotalMilliseconds);
- await Task.Delay(delay, Cancel.Token).ConfigureAwait(false); // Ask for new index in every requestInterval.
+ await Task.Delay(delay, StopCts.Token).ConfigureAwait(false); // Ask for new index in every requestInterval.
}
catch (TaskCanceledException ex)
{
@@ -393,12 +396,16 @@ namespace WalletWasabi.Services
#endregion Methods
+ /// <summary>
+ /// Stops <see cref="WasabiSynchronizer"/>.
+ /// </summary>
+ /// <remarks>The method is supposed to be called just once.</remarks>
public async Task StopAsync()
{
Logger.LogTrace(">");
Interlocked.CompareExchange(ref _running, StateStopping, StateRunning); // If running, make it stopping.
- Cancel.Cancel();
+ StopCts.Cancel();
while (Interlocked.CompareExchange(ref _running, StateStopped, StateNotStarted) == StateStopping)
{
@@ -406,7 +413,7 @@ namespace WalletWasabi.Services
}
WasabiClient.Dispose();
- Cancel?.Dispose();
+ StopCts.Dispose();
EnableRequests(); // Enable requests (it's possible something is being blocked outside the class by AreRequestsBlocked.

WasabiSynchronizer: Rename `Cancel` -> `StopCts` & make API contract clear.