From f52b950650cd5383f59567fd2ac981d9b464c208 Mon Sep 17 00:00:00 2001 From: sneurlax Date: Mon, 22 Jan 2024 21:24:30 -0600 Subject: [PATCH 1/2] avoid updating wallet info before finishing opening remove unused var --- lib/wallets/wallet/impl/monero_wallet.dart | 7 +++++++ lib/wallets/wallet/intermediate/cryptonote_wallet.dart | 2 ++ .../wallet/wallet_mixin_interfaces/cw_based_interface.dart | 3 +++ 3 files changed, 12 insertions(+) diff --git a/lib/wallets/wallet/impl/monero_wallet.dart b/lib/wallets/wallet/impl/monero_wallet.dart index 1dad7355a..5e4bc2940 100644 --- a/lib/wallets/wallet/impl/monero_wallet.dart +++ b/lib/wallets/wallet/impl/monero_wallet.dart @@ -55,6 +55,7 @@ class MoneroWallet extends CryptonoteWallet with CwBasedInterface { @override Future exitCwWallet() async { + walletOpen = false; (cwWalletBase as MoneroWalletBase?)?.onNewBlock = null; (cwWalletBase as MoneroWalletBase?)?.onNewTransaction = null; (cwWalletBase as MoneroWalletBase?)?.syncStatusChanged = null; @@ -63,6 +64,8 @@ class MoneroWallet extends CryptonoteWallet with CwBasedInterface { @override Future open() async { + walletOpen = false; + String? password; try { password = await cwKeysStorage.getWalletPassword(walletName: walletId); @@ -87,6 +90,8 @@ class MoneroWallet extends CryptonoteWallet with CwBasedInterface { const Duration(seconds: 193), (_) async => await cwWalletBase?.save(), ); + + walletOpen = true; } @override @@ -152,6 +157,8 @@ class MoneroWallet extends CryptonoteWallet with CwBasedInterface { @override Future updateTransactions() async { + if (!walletOpen) return; + await (cwWalletBase as MoneroWalletBase?)?.updateTransactions(); final transactions = (cwWalletBase as MoneroWalletBase?)?.transactionHistory?.transactions; diff --git a/lib/wallets/wallet/intermediate/cryptonote_wallet.dart b/lib/wallets/wallet/intermediate/cryptonote_wallet.dart index ab988eb23..72c49e342 100644 --- a/lib/wallets/wallet/intermediate/cryptonote_wallet.dart +++ b/lib/wallets/wallet/intermediate/cryptonote_wallet.dart @@ -7,6 +7,8 @@ abstract class CryptonoteWallet extends Wallet with MnemonicInterface { CryptonoteWallet(T currency) : super(currency); + bool walletOpen = false; + // ========== Overrides ====================================================== @override diff --git a/lib/wallets/wallet/wallet_mixin_interfaces/cw_based_interface.dart b/lib/wallets/wallet/wallet_mixin_interfaces/cw_based_interface.dart index 9dc0c0b7d..7f4508d80 100644 --- a/lib/wallets/wallet/wallet_mixin_interfaces/cw_based_interface.dart +++ b/lib/wallets/wallet/wallet_mixin_interfaces/cw_based_interface.dart @@ -244,6 +244,8 @@ mixin CwBasedInterface on CryptonoteWallet @override Future updateBalance() async { + if (!walletOpen) return; + final total = await totalBalance; final available = await availableBalance; @@ -300,6 +302,7 @@ mixin CwBasedInterface on CryptonoteWallet @override Future exit() async { if (!_hasCalledExit) { + walletOpen = false; _hasCalledExit = true; autoSaveTimer?.cancel(); await exitCwWallet(); From 7f6b069017e5f09756067eb2a814c2d1a89435fc Mon Sep 17 00:00:00 2001 From: sneurlax Date: Tue, 23 Jan 2024 14:12:27 -0600 Subject: [PATCH 2/2] replace simple return with an await open --- lib/wallets/wallet/impl/monero_wallet.dart | 13 +++++++++---- .../wallet/intermediate/cryptonote_wallet.dart | 16 +++++++++++++++- .../cw_based_interface.dart | 11 +++++++++-- 3 files changed, 33 insertions(+), 7 deletions(-) diff --git a/lib/wallets/wallet/impl/monero_wallet.dart b/lib/wallets/wallet/impl/monero_wallet.dart index 5e4bc2940..f4d654b08 100644 --- a/lib/wallets/wallet/impl/monero_wallet.dart +++ b/lib/wallets/wallet/impl/monero_wallet.dart @@ -55,7 +55,7 @@ class MoneroWallet extends CryptonoteWallet with CwBasedInterface { @override Future exitCwWallet() async { - walletOpen = false; + resetWalletOpenCompleter(); (cwWalletBase as MoneroWalletBase?)?.onNewBlock = null; (cwWalletBase as MoneroWalletBase?)?.onNewTransaction = null; (cwWalletBase as MoneroWalletBase?)?.syncStatusChanged = null; @@ -64,7 +64,7 @@ class MoneroWallet extends CryptonoteWallet with CwBasedInterface { @override Future open() async { - walletOpen = false; + resetWalletOpenCompleter(); String? password; try { @@ -91,7 +91,7 @@ class MoneroWallet extends CryptonoteWallet with CwBasedInterface { (_) async => await cwWalletBase?.save(), ); - walletOpen = true; + walletOpenCompleter?.complete(); } @override @@ -157,7 +157,12 @@ class MoneroWallet extends CryptonoteWallet with CwBasedInterface { @override Future updateTransactions() async { - if (!walletOpen) return; + try { + await waitForWalletOpen().timeout(const Duration(seconds: 30)); + } catch (e, s) { + Logging.instance + .log("Failed to wait for wallet open: $e\n$s", level: LogLevel.Fatal); + } await (cwWalletBase as MoneroWalletBase?)?.updateTransactions(); final transactions = diff --git a/lib/wallets/wallet/intermediate/cryptonote_wallet.dart b/lib/wallets/wallet/intermediate/cryptonote_wallet.dart index 72c49e342..61a86aece 100644 --- a/lib/wallets/wallet/intermediate/cryptonote_wallet.dart +++ b/lib/wallets/wallet/intermediate/cryptonote_wallet.dart @@ -1,3 +1,5 @@ +import 'dart:async'; + import 'package:stackwallet/wallets/crypto_currency/intermediate/cryptonote_currency.dart'; import 'package:stackwallet/wallets/models/tx_data.dart'; import 'package:stackwallet/wallets/wallet/wallet.dart'; @@ -7,7 +9,19 @@ abstract class CryptonoteWallet extends Wallet with MnemonicInterface { CryptonoteWallet(T currency) : super(currency); - bool walletOpen = false; + Completer? walletOpenCompleter; + + void resetWalletOpenCompleter() { + if (walletOpenCompleter == null || walletOpenCompleter!.isCompleted) { + walletOpenCompleter = Completer(); + } + } + + Future waitForWalletOpen() async { + if (walletOpenCompleter != null && !walletOpenCompleter!.isCompleted) { + await walletOpenCompleter!.future; + } + } // ========== Overrides ====================================================== diff --git a/lib/wallets/wallet/wallet_mixin_interfaces/cw_based_interface.dart b/lib/wallets/wallet/wallet_mixin_interfaces/cw_based_interface.dart index 7f4508d80..47778a56b 100644 --- a/lib/wallets/wallet/wallet_mixin_interfaces/cw_based_interface.dart +++ b/lib/wallets/wallet/wallet_mixin_interfaces/cw_based_interface.dart @@ -47,6 +47,8 @@ mixin CwBasedInterface on CryptonoteWallet Timer? autoSaveTimer; + static bool walletOperationWaiting = false; + Future pathForWalletDir({ required String name, required WalletType type, @@ -244,7 +246,12 @@ mixin CwBasedInterface on CryptonoteWallet @override Future updateBalance() async { - if (!walletOpen) return; + try { + await waitForWalletOpen().timeout(const Duration(seconds: 30)); + } catch (e, s) { + Logging.instance + .log("Failed to wait for wallet open: $e\n$s", level: LogLevel.Fatal); + } final total = await totalBalance; final available = await availableBalance; @@ -302,7 +309,7 @@ mixin CwBasedInterface on CryptonoteWallet @override Future exit() async { if (!_hasCalledExit) { - walletOpen = false; + resetWalletOpenCompleter(); _hasCalledExit = true; autoSaveTimer?.cancel(); await exitCwWallet();