Add new state to the Wallet class
MODIFY
WalletWasabi.Gui/Controls/WalletExplorer/ClosedWalletViewModel.cs
WalletWasabi.Gui/Controls/WalletExplorer/ClosedWalletViewModel.cs
@@ -24,11 +24,6 @@ namespace WalletWasabi.Gui.Controls.WalletExplorer
{
var global = Locator.Current.GetService<Global>();
- if (!await global.WaitForInitializationCompletedAsync(CancellationToken.None))
- {
- return;
- }
-
await global.WalletManager.StartWalletAsync(Wallet);
}
catch (Exception e)

MODIFY
WalletWasabi.Gui/Tabs/WalletManager/HardwareWallets/ConnectHardwareWalletViewModel.cs
WalletWasabi.Gui/Tabs/WalletManager/HardwareWallets/ConnectHardwareWalletViewModel.cs
@@ -392,12 +392,6 @@ namespace WalletWasabi.Gui.Tabs.WalletManager.HardwareWallets
try
{
- bool isSuccessful = await Global.WaitForInitializationCompletedAsync(CancellationToken.None);
- if (!isSuccessful)
- {
- return;
- }
-
var wallet = await Task.Run(async () => await Global.WalletManager.StartWalletAsync(keyManager));
// Successfully initialized.
Owner.OnClose();

MODIFY
WalletWasabi.Gui/Tabs/WalletManager/LoadWallets/LoadWalletViewModel.cs
WalletWasabi.Gui/Tabs/WalletManager/LoadWallets/LoadWalletViewModel.cs
@@ -247,12 +247,6 @@ namespace WalletWasabi.Gui.Tabs.WalletManager.LoadWallets
try
{
- bool isSuccessful = await Global.WaitForInitializationCompletedAsync(CancellationToken.None);
- if (!isSuccessful)
- {
- return;
- }
-
var wallet = await Task.Run(async () => await Global.WalletManager.StartWalletAsync(keyManager));
// Successfully initialized.
Owner.OnClose();

MODIFY
WalletWasabi/Wallets/Wallet.cs
WalletWasabi/Wallets/Wallet.cs
@@ -149,9 +149,9 @@ namespace WalletWasabi.Wallets
IFeeProvider feeProvider,
CoreNode coreNode = null)
{
- if (State != WalletState.Uninitialized)
+ if (State >= WalletState.WaitingForInit)
{
- throw new InvalidOperationException($"{nameof(State)} must be {WalletState.Uninitialized}. Current state: {State}.");
+ throw new InvalidOperationException($"{nameof(State)} must be {WalletState.Uninitialized} or {WalletState.WaitingForInit}. Current state: {State}.");
}
BitcoinStore = Guard.NotNull(nameof(bitcoinStore), bitcoinStore);
@@ -909,6 +909,15 @@ namespace WalletWasabi.Wallets
Logger.LogInfo($"Current timeout value used on block download is: {timeout} seconds.");
}
+ public void SetInitalizingState()
+ {
+ if (State != WalletState.Uninitialized)
+ {
+ throw new InvalidOperationException($"{nameof(State)} must be {WalletState.Uninitialized}. Current state: {State}.");
+ }
+ State = WalletState.WaitingForInit;
+ }
+
public static Wallet CreateAndRegisterServices(Network network, BitcoinStore bitcoinStore, KeyManager keyManager, WasabiSynchronizer synchronizer, NodesGroup nodes, string dataDir, ServiceConfiguration serviceConfiguration, IFeeProvider feeProvider, CoreNode bitcoinCoreNode = null)
{
var wallet = new Wallet(dataDir, network, keyManager);

MODIFY
WalletWasabi/Wallets/WalletManager.cs
WalletWasabi/Wallets/WalletManager.cs
@@ -70,6 +70,7 @@ namespace WalletWasabi.Wallets
private WasabiSynchronizer Synchronizer { get; set; }
private NodesGroup Nodes { get; set; }
private ServiceConfiguration ServiceConfiguration { get; set; }
+ private bool IsInitialized { get; set; }
private IFeeProvider FeeProvider { get; set; }
private CoreNode BitcoinCoreNode { get; set; }
@@ -178,7 +179,18 @@ namespace WalletWasabi.Wallets
Wallets.Single(x => x.Key == wallet);
}
+ wallet.SetInitalizingState();
+
+ // Wait for the WalletManager to be initialized.
+ while (!IsInitialized)
+ {
+ await Task.Delay(100, CancelAllInitialization.Token).ConfigureAwait(false);
+ }
+
+ if (wallet.State == WalletState.WaitingForInit)
+ {
wallet.RegisterServices(BitcoinStore, Synchronizer, Nodes, ServiceConfiguration, FeeProvider, BitcoinCoreNode);
+ }
using (await StartStopWalletLock.LockAsync(CancelAllInitialization.Token).ConfigureAwait(false))
{
@@ -432,6 +444,13 @@ namespace WalletWasabi.Wallets
ServiceConfiguration = serviceConfiguration;
FeeProvider = feeProvider;
BitcoinCoreNode = bitcoinCoreNode;
+
+ foreach (var wallet in GetWallets().Where(w => w.State <= WalletState.WaitingForInit))
+ {
+ wallet.RegisterServices(BitcoinStore, Synchronizer, Nodes, ServiceConfiguration, FeeProvider, BitcoinCoreNode);
+ }
+
+ IsInitialized = true;
}
/// <param name="refreshWalletList">Refreshes wallet list from files.</param>

MODIFY
WalletWasabi/Wallets/WalletState.cs
WalletWasabi/Wallets/WalletState.cs
@@ -7,6 +7,7 @@ namespace WalletWasabi.Wallets
public enum WalletState
{
Uninitialized,
+ WaitingForInit,
Initialized,
Starting,
Started,

Add new state to the Wallet class