Changed way for saving of monero wallet.

M committed Aug 16, 2021 at 13:01 UTC ad95ae323225ea6ae2c16633f2f859d8e05f847d
3 files changed +26 -75
cw_monero/ios/Classes/monero_api.cpp
+11
@@ -4,6 +4,7 @@
4 #include <functional>
5 #include <iostream>
6 #include <unistd.h>
7 +#include <mutex>
8 #include "thread"
9 #include "CwWalletListener.h"
10 #if __APPLE__
@@ -182,6 +183,8 @@ extern "C"
183 Monero::SubaddressAccount *m_account;
184 uint64_t m_last_known_wallet_height;
185 uint64_t m_cached_syncing_blockchain_height = 0;
186 + std::mutex store_lock;
187 + bool is_storing = false;
188
189 void change_current_wallet(Monero::Wallet *wallet)
190 {
@@ -452,7 +455,15 @@ extern "C"
455
456 void store(char *path)
457 {
458 + store_lock.lock();
459 + if (is_storing) {
460 + return;
461 + }
462 +
463 + is_storing = true;
464 get_current_wallet()->store(std::string(path));
465 + is_storing = false;
466 + store_lock.unlock();
467 }
468
469 bool transaction_create(char *address, char *payment_id, char *amount,
cw_monero/lib/wallet.dart
+1 -16
@@ -112,8 +112,6 @@ final rescanBlockchainAsyncNative = moneroApi
112 .lookup<NativeFunction<rescan_blockchain>>('rescan_blockchain')
113 .asFunction<RescanBlockchainAsync>();
114
115 -bool isStoring = false;
116 -
115 int getSyncingHeight() => getSyncingHeightNative();
116
117 bool isNeededToRefresh() => isNeededToRefreshNative() != 0;
@@ -285,20 +283,7 @@ SyncListener setListeners(void Function(int, int, double) onNewBlock,
283
284 void onStartup() => onStartupNative();
285
288 -void _storeSync(Object _) {
289 - if (isStoring) {
290 - return;
291 - }
292 -
293 - try {
294 - isStoring = true;
295 - storeSync();
296 - isStoring = false;
297 - } catch (e) {
298 - isStoring = false;
299 - rethrow;
300 - }
301 -}
286 +void _storeSync(Object _) => storeSync();
287
288 bool _setupNodeSync(Map args) {
289 final address = args['address'] as String;
lib/monero/monero_wallet.dart
+14 -59
@@ -41,11 +41,8 @@ abstract class MoneroWalletBase extends WalletBase<MoneroBalance,
41 balance = MoneroBalance(
42 fullBalance: monero_wallet.getFullBalance(accountIndex: 0),
43 unlockedBalance: monero_wallet.getFullBalance(accountIndex: 0));
44 - _lastAutosaveTimestamp = 0;
45 - _lastSaveTimestamp = 0;
46 - _isSavingAfterSync = false;
47 - _isSavingAfterNewTransaction = false;
44 _isTransactionUpdating = false;
45 + _hasSyncAfterStartup = false;
46 walletAddresses = MoneroWalletAddresses(walletInfo);
47 _onAccountChangeReaction = reaction((_) => walletAddresses.account,
48 (Account account) {
@@ -57,7 +54,7 @@ abstract class MoneroWalletBase extends WalletBase<MoneroBalance,
54 });
55 }
56
60 - static const int _autoAfterSyncSaveInterval = 60000;
57 + static const int _autoSaveInterval = 30;
58
59 @override
60 MoneroWalletAddresses walletAddresses;
@@ -82,11 +79,9 @@ abstract class MoneroWalletBase extends WalletBase<MoneroBalance,
79
80 SyncListener _listener;
81 ReactionDisposer _onAccountChangeReaction;
85 - int _lastAutosaveTimestamp;
86 - bool _isSavingAfterSync;
87 - bool _isSavingAfterNewTransaction;
82 bool _isTransactionUpdating;
89 - int _lastSaveTimestamp;
83 + bool _hasSyncAfterStartup;
84 + Timer _autoSaveTimer;
85
86 Future<void> init() async {
87 await walletAddresses.init();
@@ -105,12 +100,17 @@ abstract class MoneroWalletBase extends WalletBase<MoneroBalance,
100 height: walletInfo.restoreHeight);
101 }
102 }
103 +
104 + _autoSaveTimer = Timer.periodic(
105 + Duration(seconds: _autoSaveInterval),
106 + (_) async => await save());
107 }
108
109 @override
110 void close() {
111 _listener?.stop();
112 _onAccountChangeReaction?.reaction?.dispose();
113 + _autoSaveTimer?.cancel();
114 }
115
116 @override
@@ -240,15 +240,7 @@ abstract class MoneroWalletBase extends WalletBase<MoneroBalance,
240 @override
241 Future<void> save() async {
242 await walletAddresses.updateAddressesInBox();
243 -
244 - final now = DateTime.now().millisecondsSinceEpoch;
245 -
246 - if (now - _lastSaveTimestamp < Duration(seconds: 10).inMilliseconds) {
247 - return;
248 - }
249 -
243 await backupWalletFiles(name);
251 - _lastSaveTimestamp = now;
244 await monero_wallet.store();
245 }
246
@@ -373,46 +365,6 @@ abstract class MoneroWalletBase extends WalletBase<MoneroBalance,
365 int _getUnlockedBalance() =>
366 monero_wallet.getUnlockedBalance(accountIndex: walletAddresses.account.id);
367
376 - Future<void> _afterSyncSave() async {
377 - try {
378 - if (_isSavingAfterSync) {
379 - return;
380 - }
381 -
382 - _isSavingAfterSync = true;
383 -
384 - final nowTimestamp = DateTime.now().millisecondsSinceEpoch;
385 - final sum = _lastAutosaveTimestamp + _autoAfterSyncSaveInterval;
386 -
387 - if (_lastAutosaveTimestamp > 0 && sum < nowTimestamp) {
388 - return;
389 - }
390 -
391 - await save();
392 - _lastAutosaveTimestamp = nowTimestamp + _autoAfterSyncSaveInterval;
393 - } catch (e) {
394 - print(e.toString());
395 - }
396 -
397 - _isSavingAfterSync = false;
398 - }
399 -
400 - Future<void> _afterNewTransactionSave() async {
401 - try {
402 - if (_isSavingAfterNewTransaction) {
403 - return;
404 - }
405 -
406 - _isSavingAfterNewTransaction = true;
407 -
408 - await save();
409 - } catch (e) {
410 - print(e.toString());
411 - }
412 -
413 - _isSavingAfterNewTransaction = false;
414 - }
415 -
368 void _onNewBlock(int height, int blocksLeft, double ptc) async {
369 try {
370 if (walletInfo.isRecovery) {
@@ -426,7 +378,11 @@ abstract class MoneroWalletBase extends WalletBase<MoneroBalance,
378 _askForUpdateBalance();
379 walletAddresses.accountList.update();
380 syncStatus = SyncedSyncStatus();
429 - await _afterSyncSave();
381 +
382 + if (!_hasSyncAfterStartup) {
383 + _hasSyncAfterStartup = true;
384 + await save();
385 + }
386
387 if (walletInfo.isRecovery) {
388 await setAsRecovered();
@@ -444,7 +400,6 @@ abstract class MoneroWalletBase extends WalletBase<MoneroBalance,
400 await _askForUpdateTransactionHistory();
401 _askForUpdateBalance();
402 await Future<void>.delayed(Duration(seconds: 1));
447 - await _afterNewTransactionSave();
403 } catch (e) {
404 print(e.toString());
405 }