diff --git a/cw_monero/ios/Classes/monero_api.cpp b/cw_monero/ios/Classes/monero_api.cpp index 7ab1780c8..eefdede60 100644 --- a/cw_monero/ios/Classes/monero_api.cpp +++ b/cw_monero/ios/Classes/monero_api.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include "thread" #include "CwWalletListener.h" #if __APPLE__ @@ -182,6 +183,8 @@ extern "C" Monero::SubaddressAccount *m_account; uint64_t m_last_known_wallet_height; uint64_t m_cached_syncing_blockchain_height = 0; + std::mutex store_lock; + bool is_storing = false; void change_current_wallet(Monero::Wallet *wallet) { @@ -452,7 +455,15 @@ extern "C" void store(char *path) { + store_lock.lock(); + if (is_storing) { + return; + } + + is_storing = true; get_current_wallet()->store(std::string(path)); + is_storing = false; + store_lock.unlock(); } bool transaction_create(char *address, char *payment_id, char *amount, diff --git a/cw_monero/lib/wallet.dart b/cw_monero/lib/wallet.dart index 5c239dd99..34f94b275 100644 --- a/cw_monero/lib/wallet.dart +++ b/cw_monero/lib/wallet.dart @@ -112,8 +112,6 @@ final rescanBlockchainAsyncNative = moneroApi .lookup>('rescan_blockchain') .asFunction(); -bool isStoring = false; - int getSyncingHeight() => getSyncingHeightNative(); bool isNeededToRefresh() => isNeededToRefreshNative() != 0; @@ -285,20 +283,7 @@ SyncListener setListeners(void Function(int, int, double) onNewBlock, void onStartup() => onStartupNative(); -void _storeSync(Object _) { - if (isStoring) { - return; - } - - try { - isStoring = true; - storeSync(); - isStoring = false; - } catch (e) { - isStoring = false; - rethrow; - } -} +void _storeSync(Object _) => storeSync(); bool _setupNodeSync(Map args) { final address = args['address'] as String; diff --git a/lib/monero/monero_wallet.dart b/lib/monero/monero_wallet.dart index a76b03ba8..3aade21e5 100644 --- a/lib/monero/monero_wallet.dart +++ b/lib/monero/monero_wallet.dart @@ -41,11 +41,8 @@ abstract class MoneroWalletBase extends WalletBase walletAddresses.account, (Account account) { @@ -57,7 +54,7 @@ abstract class MoneroWalletBase extends WalletBase init() async { await walletAddresses.init(); @@ -105,12 +100,17 @@ abstract class MoneroWalletBase extends WalletBase await save()); } @override void close() { _listener?.stop(); _onAccountChangeReaction?.reaction?.dispose(); + _autoSaveTimer?.cancel(); } @override @@ -240,15 +240,7 @@ abstract class MoneroWalletBase extends WalletBase save() async { await walletAddresses.updateAddressesInBox(); - - final now = DateTime.now().millisecondsSinceEpoch; - - if (now - _lastSaveTimestamp < Duration(seconds: 10).inMilliseconds) { - return; - } - await backupWalletFiles(name); - _lastSaveTimestamp = now; await monero_wallet.store(); } @@ -373,46 +365,6 @@ abstract class MoneroWalletBase extends WalletBase monero_wallet.getUnlockedBalance(accountIndex: walletAddresses.account.id); - Future _afterSyncSave() async { - try { - if (_isSavingAfterSync) { - return; - } - - _isSavingAfterSync = true; - - final nowTimestamp = DateTime.now().millisecondsSinceEpoch; - final sum = _lastAutosaveTimestamp + _autoAfterSyncSaveInterval; - - if (_lastAutosaveTimestamp > 0 && sum < nowTimestamp) { - return; - } - - await save(); - _lastAutosaveTimestamp = nowTimestamp + _autoAfterSyncSaveInterval; - } catch (e) { - print(e.toString()); - } - - _isSavingAfterSync = false; - } - - Future _afterNewTransactionSave() async { - try { - if (_isSavingAfterNewTransaction) { - return; - } - - _isSavingAfterNewTransaction = true; - - await save(); - } catch (e) { - print(e.toString()); - } - - _isSavingAfterNewTransaction = false; - } - void _onNewBlock(int height, int blocksLeft, double ptc) async { try { if (walletInfo.isRecovery) { @@ -426,7 +378,11 @@ abstract class MoneroWalletBase extends WalletBase.delayed(Duration(seconds: 1)); - await _afterNewTransactionSave(); } catch (e) { print(e.toString()); }