diff --git a/src/wallet/MoneroWallet.cpp b/src/wallet/MoneroWallet.cpp index 3ca96a2..261569d 100644 --- a/src/wallet/MoneroWallet.cpp +++ b/src/wallet/MoneroWallet.cpp @@ -849,6 +849,7 @@ namespace monero { // init wallet2 and set daemon connection if (!wallet2->init(uri, login)) throw runtime_error("Failed to initialize wallet with daemon connection"); + getIsConnected(); // update isConnected cache // TODO: better naming? } void MoneroWallet::setDaemonConnection(const MoneroRpcConnection& connection) { @@ -1049,7 +1050,16 @@ namespace monero { void MoneroWallet::setAutoSync(bool autoSync) { cout << "setAutoSync(" << autoSync << ")" << endl; - //throw runtime_error("Not implemented"); + if (autoSync) { + if (!autoSyncEnabled) { + autoSyncEnabled = true; + syncCV.notify_one(); + } + } else { + if (!autoSyncThreadDone) { + autoSyncEnabled = false; + } + } } // rescanBlockchain @@ -2271,6 +2281,10 @@ namespace monero { void MoneroWallet::close() { cout << "close()" << endl; + autoSyncEnabled = false; + autoSyncThreadDone = true; + syncCV.notify_one(); + autoSyncThread.join(); wallet2->stop(); } @@ -2297,24 +2311,24 @@ namespace monero { boost::mutex::scoped_lock lock(autoSyncMutex); if (autoSyncThreadDone) break; if (autoSyncEnabled) { - boost::posix_time::milliseconds wait_for_ms(DEFAULT_REFRESH_INTERVAL_MILLIS); + boost::posix_time::milliseconds wait_for_ms(autoSyncInterval.load()); syncCV.timed_wait(lock, wait_for_ms); } else { syncCV.wait(lock); } if (autoSyncEnabled) { - doSync(); + autoSyncAux(); } } } - void MoneroWallet::doSync() { + void MoneroWallet::autoSyncAux() { bool rescan = rescanOnSync.exchange(false); boost::lock_guard guarg(syncMutex); do { if (getIsDaemonSynced()) { if (rescan) wallet2->rescan_blockchain(false); - wallet2->refresh(wallet2->is_trusted_daemon()); + sync(); if (!isSynced) isSynced = true; wallet2->find_and_save_rings(false); } @@ -2325,16 +2339,17 @@ namespace monero { cout << "syncAux()" << endl; // validate inputs - if (endHeight != boost::none) throw runtime_error("Monero core wallet2 does not support syncing to an end height"); // TODO: custom exception type + if (endHeight != boost::none) throw runtime_error("Monero core wallet2 does not support syncing to an end height"); // TODO: remove end height altogether // determine sync start height uint64_t syncStartHeight = startHeight == boost::none ? max(getHeight(), getRestoreHeight()) : *startHeight; if (syncStartHeight < getRestoreHeight()) setRestoreHeight(syncStartHeight); // TODO monero core: start height processed > requested start height unless restore height manually set - // sync wallet + // sync wallet and return result wallet2Listener->onSyncStart(syncStartHeight, listener); MoneroSyncResult result; wallet2->refresh(wallet2->is_trusted_daemon(), syncStartHeight, result.numBlocksFetched, result.receivedMoney, true); + if (!isSynced) isSynced = true; wallet2Listener->onSyncEnd(); return result; } diff --git a/src/wallet/MoneroWallet.h b/src/wallet/MoneroWallet.h index 5214101..1fd446c 100644 --- a/src/wallet/MoneroWallet.h +++ b/src/wallet/MoneroWallet.h @@ -1169,14 +1169,15 @@ namespace monero { mutable std::atomic isSynced; // whether or not wallet is synced mutable std::atomic isConnected; // cache connection status to avoid unecessary RPC calls boost::condition_variable syncCV; // to awaken sync threads - boost::mutex syncMutex; // synchronize sync() and syncAsync() requests + boost::mutex syncMutex; // synchronize sync() and syncAsync() requests std::atomic rescanOnSync; // whether or not to rescan on sync std::atomic autoSyncEnabled; // whether or not auto sync is enabled + std::atomic autoSyncInterval; // auto sync loop interval in milliseconds boost::thread autoSyncThread; // thread for auto sync loop - boost::mutex autoSyncMutex; // synchronize auto sync loop + boost::mutex autoSyncMutex; // synchronize auto sync loop std::atomic autoSyncThreadDone; // whether or not auto sync loop is done (cannot be re-started) void autoSyncThreadFunc(); // function to run auto sync loop thread - void doSync(); // internal synchronized sync function - MoneroSyncResult syncAux(boost::optional startHeight, boost::optional endHeight, boost::optional listener); // TODO: replace this with doSync() + void autoSyncAux(); // internal synchronized sync function + MoneroSyncResult syncAux(boost::optional startHeight, boost::optional endHeight, boost::optional listener); }; }