diff --git a/cw_monero/ios/Classes/monero_api.cpp b/cw_monero/ios/Classes/monero_api.cpp index bc653da10..4398043d0 100644 --- a/cw_monero/ios/Classes/monero_api.cpp +++ b/cw_monero/ios/Classes/monero_api.cpp @@ -373,6 +373,7 @@ extern "C" bool connect_to_node(char *error) { + nice(19); bool is_connected = get_current_wallet()->connectToDaemon(); if (!is_connected) @@ -385,6 +386,7 @@ extern "C" bool setup_node(char *address, char *login, char *password, bool use_ssl, bool is_light_wallet, char *error) { + nice(19); Monero::Wallet *wallet = get_current_wallet(); std::string _login = ""; @@ -494,6 +496,8 @@ extern "C" return committed; } + // START + uint64_t get_node_height_or_update(uint64_t base_eight) { if (m_cached_syncing_blockchain_height < base_eight) { @@ -578,6 +582,8 @@ extern "C" get_current_wallet()->setListener(m_listener); } + // END + int64_t *subaddrress_get_all() { std::vector _subaddresses = m_subaddress->getAll(); diff --git a/cw_monero/lib/wallet.dart b/cw_monero/lib/wallet.dart index 8a8731d9e..4348c8809 100644 --- a/cw_monero/lib/wallet.dart +++ b/cw_monero/lib/wallet.dart @@ -212,19 +212,16 @@ String getPublicSpendKey() => convertUTF8ToString(pointer: getPublicSpendKeyNative()); class SyncListener { - SyncListener({this.onNewBlock, this.onNeedToRefresh, this.onNewTransaction}); + SyncListener({this.onNewBlock}); void Function(int, int, double) onNewBlock; - void Function() onNeedToRefresh; - void Function() onNewTransaction; Timer _updateSyncInfoTimer; int _cachedBlockchainHeight = 0; int _lastKnownBlockHeight = 0; - int _initialSyncHeight = 0; Future getNodeHeightOrUpdate(int baseHeight) async { - if (_cachedBlockchainHeight < baseHeight) { + if (_cachedBlockchainHeight < baseHeight || _cachedBlockchainHeight == 0) { _cachedBlockchainHeight = await getNodeHeight(); } @@ -234,50 +231,36 @@ class SyncListener { void start() { _cachedBlockchainHeight = 0; _lastKnownBlockHeight = 0; - _initialSyncHeight = 0; _updateSyncInfoTimer ??= Timer.periodic(Duration(milliseconds: 1200), (_) async { - final syncHeight = getSyncingHeight(); - final needToRefresh = isNeededToRefresh(); - final newTransactionExist = isNewTransactionExist(); + var syncHeight = getSyncingHeight(); + + if (syncHeight <= 0) { + syncHeight = getCurrentHeight(); + } + final bchHeight = await getNodeHeightOrUpdate(syncHeight); - if (_lastKnownBlockHeight != syncHeight && syncHeight != null) { - if (_initialSyncHeight <= 0) { - _initialSyncHeight = syncHeight; - } - - _lastKnownBlockHeight = syncHeight; - final line = bchHeight - _initialSyncHeight; - final diff = line - (bchHeight - syncHeight); - final ptc = diff <= 0 ? 0.0 : diff / line; - final left = bchHeight - syncHeight; - // 1. Actual new height; 2. Blocks left to finish; 3. Progress in percents; - onNewBlock?.call(syncHeight, left, ptc); + if (_lastKnownBlockHeight == syncHeight || syncHeight == null) { + return; } - if (newTransactionExist) { - onNewTransaction?.call(); - } - - if (needToRefresh) { - onNeedToRefresh?.call(); - } + _lastKnownBlockHeight = syncHeight; + final track = bchHeight - syncHeight; + final diff = track - (bchHeight - syncHeight); + final ptc = diff <= 0 ? 0.0 : diff / track; + final left = bchHeight - syncHeight; + // 1. Actual new height; 2. Blocks left to finish; 3. Progress in percents; + onNewBlock?.call(syncHeight, left, ptc); }); } void stop() => _updateSyncInfoTimer?.cancel(); } -SyncListener setListeners(void Function(int, int, double) onNewBlock, - void Function() onNeedToRefresh, void Function() onNewTransaction) { - final listener = SyncListener( - onNewBlock: onNewBlock, - onNeedToRefresh: onNeedToRefresh, - onNewTransaction: onNewTransaction); - +SyncListener setListeners(void Function(int, int, double) onNewBlock) { + final listener = SyncListener(onNewBlock: onNewBlock); setListenerNative(); - return listener; } diff --git a/lib/monero/monero_wallet.dart b/lib/monero/monero_wallet.dart index 9840eb2c5..8ce53f8d1 100644 --- a/lib/monero/monero_wallet.dart +++ b/lib/monero/monero_wallet.dart @@ -95,7 +95,7 @@ abstract class MoneroWalletBase extends WalletBase with Store { address = subaddress.address; _setListeners(); await transactionHistory.update(); - + print('walletInfo.isRecovery ${walletInfo.isRecovery}'); if (walletInfo.isRecovery) { monero_wallet.setRecoveringFromSeed(isRecovery: walletInfo.isRecovery); @@ -233,8 +233,7 @@ abstract class MoneroWalletBase extends WalletBase with Store { void _setListeners() { _listener?.stop(); - _listener = monero_wallet.setListeners( - _onNewBlock, _onNeedToRefresh, _onNewTransaction); + _listener = monero_wallet.setListeners(_onNewBlock); _listener.start(); } @@ -293,43 +292,30 @@ abstract class MoneroWalletBase extends WalletBase with Store { int _getUnlockedBalance() => monero_wallet.getUnlockedBalance(accountIndex: account.id); - void _onNewBlock(int height, int blocksLeft, double ptc) => - syncStatus = SyncingSyncStatus(blocksLeft, ptc); + int _lastAutosaveTimestamp = 0; + final int _autosaveInterval = 60000; - Future _onNeedToRefresh() async { - if (syncStatus is FailedSyncStatus) { + Future _autoSave() async { + final nowTimestamp = DateTime.now().millisecondsSinceEpoch; + final sum = _lastAutosaveTimestamp + _autosaveInterval; + + if (_lastAutosaveTimestamp != 0 && sum < nowTimestamp) { return; } - if (walletInfo.isRecovery) { - _askForUpdateTransactionHistory(); - _askForUpdateBalance(); - } - - final currentHeight = getCurrentHeight(); - final nodeHeight = monero_wallet.getNodeHeightSync(); - - if (nodeHeight - currentHeight < moneroBlockSize) { - syncStatus = SyncedSyncStatus(); - - if (walletInfo.isRecovery) { - await setAsRecovered(); - } - } - - // if (walletInfo.isRecovery && - // (nodeHeight - currentHeight < moneroBlockSize)) { - // await setAsRecovered(); - // } - - if (currentHeight - _cachedRefreshHeight > moneroBlockSize) { - _cachedRefreshHeight = currentHeight; - await save(); - } + _lastAutosaveTimestamp = nowTimestamp + _autosaveInterval; + await save(); } - void _onNewTransaction() { - _askForUpdateBalance(); + void _onNewBlock(int height, int blocksLeft, double ptc) async { _askForUpdateTransactionHistory(); + _askForUpdateBalance(); + + if (blocksLeft < moneroBlockSize) { + syncStatus = SyncedSyncStatus(); + await _autoSave(); + } else { + syncStatus = SyncingSyncStatus(blocksLeft, ptc); + } } }