From 26ea3fa2b7fc0f18fc6570994a8f59451d86a78b Mon Sep 17 00:00:00 2001 From: woodser Date: Fri, 19 Jul 2019 23:24:56 -0400 Subject: [PATCH] separate lockAndSync() from syncAux() --- src/wallet/MoneroWallet.cpp | 32 ++++++++++++++++++++++---------- src/wallet/MoneroWallet.h | 4 ++-- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/src/wallet/MoneroWallet.cpp b/src/wallet/MoneroWallet.cpp index f751769..3850aa7 100644 --- a/src/wallet/MoneroWallet.cpp +++ b/src/wallet/MoneroWallet.cpp @@ -1033,22 +1033,22 @@ namespace monero { MoneroSyncResult MoneroWallet::sync() { cout << "sync()" << endl; - return syncAux(boost::none, boost::none); + return lockAndSync(); } MoneroSyncResult MoneroWallet::sync(MoneroSyncListener& listener) { cout << "sync(startHeight)" << endl; - return syncAux(boost::none, listener); + return lockAndSync(boost::none, listener); } MoneroSyncResult MoneroWallet::sync(uint64_t startHeight) { cout << "sync(startHeight)" << endl; - return syncAux(startHeight, boost::none); + return lockAndSync(startHeight); } MoneroSyncResult MoneroWallet::sync(uint64_t startHeight, MoneroSyncListener& listener) { cout << "sync(startHeight, listener)" << endl; - return syncAux(startHeight, listener); + return lockAndSync(startHeight, listener); } void MoneroWallet::setAutoSync(bool autoSync) { @@ -1065,9 +1065,11 @@ namespace monero { } } + // TODO: support arguments bool hard, bool refresh = true, bool keep_key_images = false void MoneroWallet::rescanBlockchain() { cout << "rescanBlockchain()" << endl; - wallet2->rescan_blockchain(false); // TODO: support arguments bool hard, bool refresh = true, bool keep_key_images = false + rescanOnSync = true; + syncAux(); } // isMultisigImportNeeded @@ -2325,22 +2327,32 @@ namespace monero { syncCV.wait(lock); } if (autoSyncEnabled) { - autoSyncAux(); + lockAndSync(); } } } - void MoneroWallet::autoSyncAux() { + MoneroSyncResult MoneroWallet::lockAndSync(boost::optional startHeight, boost::optional listener) { bool rescan = rescanOnSync.exchange(false); - boost::lock_guard guarg(syncMutex); + boost::lock_guard guarg(syncMutex); // synchronize sync() and syncAsync() + MoneroSyncResult result; + result.numBlocksFetched = 0; + result.receivedMoney = false; do { + // skip if daemon is not synced if (getIsDaemonSynced()) { + + // rescan blockchain if requested if (rescan) wallet2->rescan_blockchain(false); - syncAux(boost::none, boost::none); - if (!isSynced) isSynced = true; + + // sync wallet + result = syncAux(startHeight, listener); + + // find and save rings wallet2->find_and_save_rings(false); } } while (!rescan && (rescan = rescanOnSync.exchange(false))); // repeat if not rescanned and rescan was requested + return result; } MoneroSyncResult MoneroWallet::syncAux(boost::optional startHeight, boost::optional listener) { diff --git a/src/wallet/MoneroWallet.h b/src/wallet/MoneroWallet.h index c640163..13be4f6 100644 --- a/src/wallet/MoneroWallet.h +++ b/src/wallet/MoneroWallet.h @@ -1177,7 +1177,7 @@ namespace monero { 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 autoSyncAux(); // internal synchronized sync function - MoneroSyncResult syncAux(boost::optional startHeight, boost::optional listener); + MoneroSyncResult lockAndSync(boost::optional startHeight = boost::none, boost::optional listener = boost::none); // internal function to synchronize request to sync and rescan + MoneroSyncResult syncAux(boost::optional startHeight = boost::none, boost::optional listener = boost::none); // internal function to immediately block, sync, and report progress }; }