From 5332495c2404ab4b102c98c22419e70143b1dc90 Mon Sep 17 00:00:00 2001 From: j-berman Date: Thu, 16 Nov 2023 16:38:52 -0800 Subject: Implement background sync when locked --- src/libwalletqt/Wallet.cpp | 63 ++++++++++++++++++++++++++++++++++++++++++++++ src/libwalletqt/Wallet.h | 22 ++++++++++++++++ 2 files changed, 85 insertions(+) (limited to 'src') diff --git a/src/libwalletqt/Wallet.cpp b/src/libwalletqt/Wallet.cpp index cec4ff63..a5ac6925 100644 --- a/src/libwalletqt/Wallet.cpp +++ b/src/libwalletqt/Wallet.cpp @@ -529,6 +529,69 @@ bool Wallet::scanTransactions(const QVector &txids) return m_walletImpl->scanTransactions(c); } +void Wallet::setupBackgroundSync(const Wallet::BackgroundSyncType background_sync_type, const QString &wallet_password) +{ + qDebug() << "Setting up background sync"; + bool refreshEnabled = m_refreshEnabled; + pauseRefresh(); + + // run inside scheduler because of lag when stopping/starting refresh + m_scheduler.run([this, refreshEnabled, background_sync_type, &wallet_password] { + m_walletImpl->setupBackgroundSync( + static_cast(background_sync_type), + wallet_password.toStdString(), + Monero::optional()); + if (refreshEnabled) + startRefresh(); + emit backgroundSyncSetup(); + }); +} + +Wallet::BackgroundSyncType Wallet::getBackgroundSyncType() const +{ + return static_cast(m_walletImpl->getBackgroundSyncType()); +} + +bool Wallet::isBackgroundWallet() const +{ + return m_walletImpl->isBackgroundWallet(); +} + +bool Wallet::isBackgroundSyncing() const +{ + return m_walletImpl->isBackgroundSyncing(); +} + +void Wallet::startBackgroundSync() +{ + qDebug() << "Starting background sync"; + bool refreshEnabled = m_refreshEnabled; + pauseRefresh(); + + // run inside scheduler because of lag when stopping/starting refresh + m_scheduler.run([this, refreshEnabled] { + m_walletImpl->startBackgroundSync(); + if (refreshEnabled) + startRefresh(); + emit backgroundSyncStarted(); + }); +} + +void Wallet::stopBackgroundSync(const QString &password) +{ + qDebug() << "Stopping background sync"; + bool refreshEnabled = m_refreshEnabled; + pauseRefresh(); + + // run inside scheduler because of lag when stopping/starting refresh + m_scheduler.run([this, password, refreshEnabled] { + m_walletImpl->stopBackgroundSync(password.toStdString()); + if (refreshEnabled) + startRefresh(); + emit backgroundSyncStopped(); + }); +} + bool Wallet::refresh(bool historyAndSubaddresses /* = true */) { refreshingSet(true); diff --git a/src/libwalletqt/Wallet.h b/src/libwalletqt/Wallet.h index 398d19c6..a5bb913c 100644 --- a/src/libwalletqt/Wallet.h +++ b/src/libwalletqt/Wallet.h @@ -112,6 +112,14 @@ public: Q_ENUM(ConnectionStatus) + enum BackgroundSyncType { + BackgroundSync_Off = Monero::Wallet::BackgroundSync_Off, + BackgroundSync_ReusePassword = Monero::Wallet::BackgroundSync_ReusePassword, + BackgroundSync_CustomPassword = Monero::Wallet::BackgroundSync_CustomPassword + }; + + Q_ENUM(BackgroundSyncType) + //! returns mnemonic seed QString getSeed() const; @@ -215,6 +223,17 @@ public: //! scan transactions Q_INVOKABLE bool scanTransactions(const QVector &txids); + Q_INVOKABLE void setupBackgroundSync(const BackgroundSyncType background_sync_type, const QString &wallet_password); + Q_INVOKABLE BackgroundSyncType getBackgroundSyncType() const; + Q_INVOKABLE bool isBackgroundWallet() const; + Q_INVOKABLE bool isBackgroundSyncing() const; + + //! scan in the background with just the view key (wipe the spend key) + Q_INVOKABLE void startBackgroundSync(); + + //! bring the spend key back and process background synced txs + Q_INVOKABLE void stopBackgroundSync(const QString &password); + //! refreshes the wallet Q_INVOKABLE bool refresh(bool historyAndSubaddresses = true); @@ -369,6 +388,9 @@ signals: void moneyReceived(const QString &txId, quint64 amount); void unconfirmedMoneyReceived(const QString &txId, quint64 amount); void newBlock(quint64 height, quint64 targetHeight); + void backgroundSyncSetup() const; + void backgroundSyncStarted() const; + void backgroundSyncStopped() const; void addressBookChanged() const; void historyModelChanged() const; void walletCreationHeightChanged(); -- cgit v1.2.3