CAKE-149

M committed Nov 3, 2020 at 18:57 UTC 76581505a0b9f8175108ef40e0a202ba426c7b08
3 files changed +43 -68
cw_monero/ios/Classes/monero_api.cpp
+6
@@ -373,6 +373,7 @@ extern "C"
373
374 bool connect_to_node(char *error)
375 {
376 + nice(19);
377 bool is_connected = get_current_wallet()->connectToDaemon();
378
379 if (!is_connected)
@@ -385,6 +386,7 @@ extern "C"
386
387 bool setup_node(char *address, char *login, char *password, bool use_ssl, bool is_light_wallet, char *error)
388 {
389 + nice(19);
390 Monero::Wallet *wallet = get_current_wallet();
391
392 std::string _login = "";
@@ -494,6 +496,8 @@ extern "C"
496 return committed;
497 }
498
499 + // START
500 +
501 uint64_t get_node_height_or_update(uint64_t base_eight)
502 {
503 if (m_cached_syncing_blockchain_height < base_eight) {
@@ -578,6 +582,8 @@ extern "C"
582 get_current_wallet()->setListener(m_listener);
583 }
584
585 + // END
586 +
587 int64_t *subaddrress_get_all()
588 {
589 std::vector<Monero::SubaddressRow *> _subaddresses = m_subaddress->getAll();
cw_monero/lib/wallet.dart
+18 -35
@@ -212,19 +212,16 @@ String getPublicSpendKey() =>
212 convertUTF8ToString(pointer: getPublicSpendKeyNative());
213
214 class SyncListener {
215 - SyncListener({this.onNewBlock, this.onNeedToRefresh, this.onNewTransaction});
215 + SyncListener({this.onNewBlock});
216
217 void Function(int, int, double) onNewBlock;
218 - void Function() onNeedToRefresh;
219 - void Function() onNewTransaction;
218
219 Timer _updateSyncInfoTimer;
220 int _cachedBlockchainHeight = 0;
221 int _lastKnownBlockHeight = 0;
224 - int _initialSyncHeight = 0;
222
223 Future<int> getNodeHeightOrUpdate(int baseHeight) async {
227 - if (_cachedBlockchainHeight < baseHeight) {
224 + if (_cachedBlockchainHeight < baseHeight || _cachedBlockchainHeight == 0) {
225 _cachedBlockchainHeight = await getNodeHeight();
226 }
227
@@ -234,50 +231,36 @@ class SyncListener {
231 void start() {
232 _cachedBlockchainHeight = 0;
233 _lastKnownBlockHeight = 0;
237 - _initialSyncHeight = 0;
234 _updateSyncInfoTimer ??=
235 Timer.periodic(Duration(milliseconds: 1200), (_) async {
240 - final syncHeight = getSyncingHeight();
241 - final needToRefresh = isNeededToRefresh();
242 - final newTransactionExist = isNewTransactionExist();
243 - final bchHeight = await getNodeHeightOrUpdate(syncHeight);
236 + var syncHeight = getSyncingHeight();
237
245 - if (_lastKnownBlockHeight != syncHeight && syncHeight != null) {
246 - if (_initialSyncHeight <= 0) {
247 - _initialSyncHeight = syncHeight;
248 - }
249 -
250 - _lastKnownBlockHeight = syncHeight;
251 - final line = bchHeight - _initialSyncHeight;
252 - final diff = line - (bchHeight - syncHeight);
253 - final ptc = diff <= 0 ? 0.0 : diff / line;
254 - final left = bchHeight - syncHeight;
255 - // 1. Actual new height; 2. Blocks left to finish; 3. Progress in percents;
256 - onNewBlock?.call(syncHeight, left, ptc);
238 + if (syncHeight <= 0) {
239 + syncHeight = getCurrentHeight();
240 }
241
259 - if (newTransactionExist) {
260 - onNewTransaction?.call();
261 - }
242 + final bchHeight = await getNodeHeightOrUpdate(syncHeight);
243
263 - if (needToRefresh) {
264 - onNeedToRefresh?.call();
244 + if (_lastKnownBlockHeight == syncHeight || syncHeight == null) {
245 + return;
246 }
247 +
248 + _lastKnownBlockHeight = syncHeight;
249 + final track = bchHeight - syncHeight;
250 + final diff = track - (bchHeight - syncHeight);
251 + final ptc = diff <= 0 ? 0.0 : diff / track;
252 + final left = bchHeight - syncHeight;
253 + // 1. Actual new height; 2. Blocks left to finish; 3. Progress in percents;
254 + onNewBlock?.call(syncHeight, left, ptc);
255 });
256 }
257
258 void stop() => _updateSyncInfoTimer?.cancel();
259 }
260
272 -SyncListener setListeners(void Function(int, int, double) onNewBlock,
273 - void Function() onNeedToRefresh, void Function() onNewTransaction) {
274 - final listener = SyncListener(
275 - onNewBlock: onNewBlock,
276 - onNeedToRefresh: onNeedToRefresh,
277 - onNewTransaction: onNewTransaction);
278 -
261 +SyncListener setListeners(void Function(int, int, double) onNewBlock) {
262 + final listener = SyncListener(onNewBlock: onNewBlock);
263 setListenerNative();
280 -
264 return listener;
265 }
266
lib/monero/monero_wallet.dart
+19 -33
@@ -95,7 +95,7 @@ abstract class MoneroWalletBase extends WalletBase<MoneroBalance> with Store {
95 address = subaddress.address;
96 _setListeners();
97 await transactionHistory.update();
98 -
98 + print('walletInfo.isRecovery ${walletInfo.isRecovery}');
99 if (walletInfo.isRecovery) {
100 monero_wallet.setRecoveringFromSeed(isRecovery: walletInfo.isRecovery);
101
@@ -233,8 +233,7 @@ abstract class MoneroWalletBase extends WalletBase<MoneroBalance> with Store {
233
234 void _setListeners() {
235 _listener?.stop();
236 - _listener = monero_wallet.setListeners(
237 - _onNewBlock, _onNeedToRefresh, _onNewTransaction);
236 + _listener = monero_wallet.setListeners(_onNewBlock);
237 _listener.start();
238 }
239
@@ -293,43 +292,30 @@ abstract class MoneroWalletBase extends WalletBase<MoneroBalance> with Store {
292 int _getUnlockedBalance() =>
293 monero_wallet.getUnlockedBalance(accountIndex: account.id);
294
296 - void _onNewBlock(int height, int blocksLeft, double ptc) =>
297 - syncStatus = SyncingSyncStatus(blocksLeft, ptc);
295 + int _lastAutosaveTimestamp = 0;
296 + final int _autosaveInterval = 60000;
297 +
298 + Future<void> _autoSave() async {
299 + final nowTimestamp = DateTime.now().millisecondsSinceEpoch;
300 + final sum = _lastAutosaveTimestamp + _autosaveInterval;
301
299 - Future _onNeedToRefresh() async {
300 - if (syncStatus is FailedSyncStatus) {
302 + if (_lastAutosaveTimestamp != 0 && sum < nowTimestamp) {
303 return;
304 }
305
304 - if (walletInfo.isRecovery) {
305 - _askForUpdateTransactionHistory();
306 - _askForUpdateBalance();
307 - }
306 + _lastAutosaveTimestamp = nowTimestamp + _autosaveInterval;
307 + await save();
308 + }
309
309 - final currentHeight = getCurrentHeight();
310 - final nodeHeight = monero_wallet.getNodeHeightSync();
310 + void _onNewBlock(int height, int blocksLeft, double ptc) async {
311 + _askForUpdateTransactionHistory();
312 + _askForUpdateBalance();
313
312 - if (nodeHeight - currentHeight < moneroBlockSize) {
314 + if (blocksLeft < moneroBlockSize) {
315 syncStatus = SyncedSyncStatus();
314 -
315 - if (walletInfo.isRecovery) {
316 - await setAsRecovered();
317 - }
318 - }
319 -
320 - // if (walletInfo.isRecovery &&
321 - // (nodeHeight - currentHeight < moneroBlockSize)) {
322 - // await setAsRecovered();
323 - // }
324 -
325 - if (currentHeight - _cachedRefreshHeight > moneroBlockSize) {
326 - _cachedRefreshHeight = currentHeight;
327 - await save();
316 + await _autoSave();
317 + } else {
318 + syncStatus = SyncingSyncStatus(blocksLeft, ptc);
319 }
320 }
330 -
331 - void _onNewTransaction() {
332 - _askForUpdateBalance();
333 - _askForUpdateTransactionHistory();
334 - }
321 }