From 2524cc179e985cd6abfb7cb4b4c7e6a064188cef Mon Sep 17 00:00:00 2001 From: xiphon Date: Thu, 13 Jun 2019 12:19:52 +0000 Subject: [PATCH] Wallet: async fetching wallet, daemon and target height --- main.qml | 24 ++++++++++++------------ src/libwalletqt/Wallet.cpp | 13 +++++++++++++ src/libwalletqt/Wallet.h | 22 +++++++++++++--------- 3 files changed, 38 insertions(+), 21 deletions(-) diff --git a/main.qml b/main.qml index 3299c629..d4bb6838 100644 --- a/main.qml +++ b/main.qml @@ -293,6 +293,7 @@ ApplicationWindow { // Disconnect all listeners if (typeof currentWallet !== "undefined" && currentWallet !== null) { + currentWallet.heightRefreshed.disconnect(onHeightRefreshed); currentWallet.refreshed.disconnect(onWalletRefresh) currentWallet.updated.disconnect(onWalletUpdate) currentWallet.newBlock.disconnect(onWalletNewBlock) @@ -351,6 +352,7 @@ ApplicationWindow { } // connect handlers + currentWallet.heightRefreshed.connect(onHeightRefreshed); currentWallet.refreshed.connect(onWalletRefresh) currentWallet.updated.connect(onWalletUpdate) currentWallet.newBlock.connect(onWalletNewBlock) @@ -621,18 +623,7 @@ ApplicationWindow { remoteNodeConnected = false; } - function onWalletRefresh() { - console.log(">>> wallet refreshed") - - // Daemon connected - leftPanel.networkStatus.connected = currentWallet.connected() - - // Wallet height - var bcHeight = currentWallet.blockChainHeight(); - - // Check daemon status - var dCurrentBlock = currentWallet.daemonBlockChainHeight(); - var dTargetBlock = currentWallet.daemonBlockChainTargetHeight(); + function onHeightRefreshed(bcHeight, dCurrentBlock, dTargetBlock) { // Daemon fully synced // TODO: implement onDaemonSynced or similar in wallet API and don't start refresh thread before daemon is synced // targetBlock = currentBlock = 1 before network connection is established. @@ -683,6 +674,15 @@ ApplicationWindow { onWalletUpdate(); } + function onWalletRefresh() { + console.log(">>> wallet refreshed") + + // Daemon connected + leftPanel.networkStatus.connected = currentWallet.connected() + + currentWallet.refreshHeightAsync(); + } + function startDaemon(flags){ // Pause refresh while starting daemon currentWallet.pauseRefresh(); diff --git a/src/libwalletqt/Wallet.cpp b/src/libwalletqt/Wallet.cpp index 2f106aed..629106da 100644 --- a/src/libwalletqt/Wallet.cpp +++ b/src/libwalletqt/Wallet.cpp @@ -348,6 +348,19 @@ void Wallet::setSubaddressLabel(quint32 accountIndex, quint32 addressIndex, cons m_walletImpl->setSubaddressLabel(accountIndex, addressIndex, label.toStdString()); } +void Wallet::refreshHeightAsync() const +{ + QtConcurrent::run([this] { + QFuture daemonHeight = QtConcurrent::run([this] { + return daemonBlockChainHeight(); + }); + QFuture targetHeight = QtConcurrent::run([this] { + return daemonBlockChainTargetHeight(); + }); + emit heightRefreshed(blockChainHeight(), daemonHeight.result(), targetHeight.result()); + }); +} + quint64 Wallet::blockChainHeight() const { return m_walletImpl->blockChainHeight(); diff --git a/src/libwalletqt/Wallet.h b/src/libwalletqt/Wallet.h index 39df6cc8..ba12d1de 100644 --- a/src/libwalletqt/Wallet.h +++ b/src/libwalletqt/Wallet.h @@ -181,15 +181,7 @@ public: //! returns if view only wallet Q_INVOKABLE bool viewOnly() const; - //! returns current wallet's block height - //! (can be less than daemon's blockchain height when wallet sync in progress) - Q_INVOKABLE quint64 blockChainHeight() const; - - //! returns daemon's blockchain height - Q_INVOKABLE quint64 daemonBlockChainHeight() const; - - //! returns daemon's blockchain target height - Q_INVOKABLE quint64 daemonBlockChainTargetHeight() const; + Q_INVOKABLE void refreshHeightAsync() const; //! export/import key images Q_INVOKABLE bool exportKeyImages(const QString& path); @@ -355,6 +347,7 @@ signals: void deviceButtonRequest(quint64 buttonCode); void deviceButtonPressed(); void transactionCommitted(bool status, PendingTransaction *t, QStringList txid); + void heightRefreshed(quint64 walletHeight, quint64 daemonHeight, quint64 targetHeight) const; // emitted when transaction is created async void transactionCreated(PendingTransaction * transaction, QString address, QString paymentId, quint32 mixinCount); @@ -365,6 +358,17 @@ private: Wallet(QObject * parent = nullptr); Wallet(Monero::Wallet *w, QObject * parent = 0); ~Wallet(); + + //! returns current wallet's block height + //! (can be less than daemon's blockchain height when wallet sync in progress) + quint64 blockChainHeight() const; + + //! returns daemon's blockchain height + quint64 daemonBlockChainHeight() const; + + //! returns daemon's blockchain target height + quint64 daemonBlockChainTargetHeight() const; + private: friend class WalletManager; friend class WalletListenerImpl;