Add WalletState
MODIFY
WalletWasabi/Wallets/Wallet.cs
WalletWasabi/Wallets/Wallet.cs
@@ -34,7 +34,7 @@ using WalletWasabi.WebClients.Wasabi;
namespace WalletWasabi.Wallets
{
- public class Wallet : IHostedService
+ public class Wallet : BackgroundService
{
private Node _localBitcoinCoreNode = null;
@@ -88,6 +88,8 @@ namespace WalletWasabi.Wallets
BitcoinStore.IndexStore.NewFilter += IndexDownloader_NewFilterAsync;
BitcoinStore.IndexStore.Reorged += IndexDownloader_ReorgedAsync;
BitcoinStore.MempoolService.TransactionReceived += Mempool_TransactionReceived;
+
+ State = WalletState.Initialized;
}
public static event EventHandler<bool> DownloadingBlockChanged;
@@ -98,6 +100,7 @@ namespace WalletWasabi.Wallets
public event EventHandler<Block> NewBlockProcessed;
+ public WalletState State { get; private set; }
public BitcoinStore BitcoinStore { get; }
public KeyManager KeyManager { get; }
public WasabiSynchronizer Synchronizer { get; }
@@ -130,7 +133,6 @@ namespace WalletWasabi.Wallets
}
public IFeeProvider FeeProvider { get; }
- public bool IsStoppingOrStopped { get; private set; }
public CoreNode CoreNode { get; }
public FilterModel LastProcessedFilter { get; private set; }
private static Random Random { get; } = new Random();
@@ -141,7 +143,7 @@ namespace WalletWasabi.Wallets
private int NodeTimeouts { get; set; }
/// <inheritdoc/>
- public async Task StartAsync(CancellationToken cancel)
+ public override async Task StartAsync(CancellationToken cancel)
{
try
{
@@ -161,16 +163,25 @@ namespace WalletWasabi.Wallets
using (BenchmarkLogger.Measure())
{
- await RuntimeParams.LoadAsync();
+ await RuntimeParams.LoadAsync().ConfigureAwait(false);
ChaumianClient.Start();
- using (await HandleFiltersLock.LockAsync())
+ using (await HandleFiltersLock.LockAsync().ConfigureAwait(false))
{
- await LoadWalletStateAsync(cancel);
- await LoadDummyMempoolAsync();
+ await LoadWalletStateAsync(cancel).ConfigureAwait(false);
+ await LoadDummyMempoolAsync().ConfigureAwait(false);
+ }
}
+
+ await base.StartAsync(cancel).ConfigureAwait(false);
+
+ State = WalletState.Started;
}
+ catch
+ {
+ State = WalletState.Initialized;
+ throw;
}
finally
{
@@ -178,6 +189,9 @@ namespace WalletWasabi.Wallets
}
}
+ /// <inheritdoc />
+ protected override Task ExecuteAsync(CancellationToken stoppingToken) => Task.CompletedTask;
+
/// <param name="hash">Block hash of the desired block, represented as a 256 bit integer.</param>
/// <exception cref="OperationCanceledException"></exception>
public async Task<Block> FetchBlockAsync(uint256 hash, CancellationToken cancel)
@@ -275,20 +289,33 @@ namespace WalletWasabi.Wallets
.ToHashSet();
/// <inheritdoc/>
- public async Task StopAsync(CancellationToken cancel)
+ public async override Task StopAsync(CancellationToken cancel)
{
- IsStoppingOrStopped = true;
+ try
+ {
+ var prevState = State;
+ State = WalletState.Stopping;
+
+ await base.StopAsync(cancel).ConfigureAwait(false);
+ if (prevState >= WalletState.Initialized)
+ {
BitcoinStore.IndexStore.NewFilter -= IndexDownloader_NewFilterAsync;
BitcoinStore.IndexStore.Reorged -= IndexDownloader_ReorgedAsync;
BitcoinStore.MempoolService.TransactionReceived -= Mempool_TransactionReceived;
TransactionProcessor.WalletRelevantTransactionProcessed -= TransactionProcessor_WalletRelevantTransactionProcessedAsync;
+ }
DisconnectDisposeNullLocalBitcoinCoreNode();
await ChaumianClient.StopAsync(cancel).ConfigureAwait(false);
Logger.LogInfo($"{nameof(ChaumianClient)} is stopped.");
}
+ finally
+ {
+ State = WalletState.Stopped;
+ }
+ }
internal static LockTime InternalSelectLockTimeForTransaction(uint tipHeight, Random rnd)
{

MODIFY
WalletWasabi/Wallets/WalletManager.cs
WalletWasabi/Wallets/WalletManager.cs
@@ -44,7 +44,7 @@ namespace WalletWasabi.Wallets
private object Lock { get; }
private AsyncLock AddRemoveLock { get; }
- private IEnumerable<KeyValuePair<Wallet, HashSet<uint256>>> AliveWalletsNoLock => Wallets.Where(x => x.Key is { IsStoppingOrStopped: var isDisposed } && !isDisposed);
+ private IEnumerable<KeyValuePair<Wallet, HashSet<uint256>>> AliveWalletsNoLock => Wallets.Where(x => x.Key is { State: var state } && state < WalletState.Stopping);
private BitcoinStore BitcoinStore { get; set; }
private WasabiSynchronizer Synchronizer { get; set; }
@@ -195,6 +195,7 @@ namespace WalletWasabi.Wallets
Logger.LogInfo($"{nameof(wallet.KeyManager)} backup saved to `{backupWalletFilePath}`.");
}
await wallet.StopAsync(cancel).ConfigureAwait(false);
+ wallet?.Dispose();
Logger.LogInfo($"{nameof(Wallet)} is stopped.");
}
catch (Exception ex)

ADD
WalletWasabi/Wallets/WalletState.cs
+using System;
+using System.Collections.Generic;
+using System.Text;
+
+namespace WalletWasabi.Wallets
+{
+ public enum WalletState
+ {
+ Uninitialized,
+ Initialized,
+ Starting,
+ Started,
+ Stopping,
+ Stopped
+ }
+}

Add WalletState