From eb909e1ae77ea842ef95e7b352cfeb9e58dddcfc Mon Sep 17 00:00:00 2001 From: Jaquee Date: Tue, 31 Jan 2017 10:32:41 +0100 Subject: async connectionStatus and init --- src/libwalletqt/Wallet.cpp | 79 ++++++++++++++++++++++++++++++++++++---------- 1 file changed, 63 insertions(+), 16 deletions(-) (limited to 'src/libwalletqt/Wallet.cpp') diff --git a/src/libwalletqt/Wallet.cpp b/src/libwalletqt/Wallet.cpp index 2ade69bb..0e4e09a3 100644 --- a/src/libwalletqt/Wallet.cpp +++ b/src/libwalletqt/Wallet.cpp @@ -16,6 +16,8 @@ #include #include #include +#include +#include namespace { static const int DAEMON_BLOCKCHAIN_HEIGHT_CACHE_TTL_SECONDS = 10; @@ -54,7 +56,7 @@ public: virtual void newBlock(uint64_t height) { // qDebug() << __FUNCTION__; - emit m_wallet->newBlock(height); + emit m_wallet->newBlock(height, m_wallet->daemonBlockChainTargetHeight()); } virtual void updated() @@ -103,17 +105,35 @@ bool Wallet::testnet() const return m_walletImpl->testnet(); } -Wallet::ConnectionStatus Wallet::connected() const + +void Wallet::updateConnectionStatusAsync() { - // cache connection status - if (!m_initialized || m_connectionStatusTime.elapsed() / 1000 > m_connectionStatusTtl) { - m_initialized = true; - ConnectionStatus newStatus = static_cast(m_walletImpl->connected()); + QFuture future = QtConcurrent::run(m_walletImpl, &Monero::Wallet::connected); + QFutureWatcher *connectionWatcher = new QFutureWatcher(); + + connect(connectionWatcher, &QFutureWatcher::finished, [=]() { + QFuture future = connectionWatcher->future(); + connectionWatcher->deleteLater(); + ConnectionStatus newStatus = static_cast(future.result()); if (newStatus != m_connectionStatus) { m_connectionStatus = newStatus; - emit connectionStatusChanged(); + emit connectionStatusChanged(newStatus); } + // Release lock + m_connectionStatusRunning = false; + }); + connectionWatcher->setFuture(future); +} + +Wallet::ConnectionStatus Wallet::connected(bool forceCheck) +{ + qDebug() << "Checking connection status"; + // cache connection status + if (forceCheck || !m_initialized || (m_connectionStatusTime.elapsed() / 1000 > m_connectionStatusTtl && !m_connectionStatusRunning) || m_connectionStatusTime.elapsed() > 30000) { + m_connectionStatusRunning = true; + m_initialized = true; m_connectionStatusTime.restart(); + updateConnectionStatusAsync(); } return m_connectionStatus; @@ -151,17 +171,38 @@ bool Wallet::store(const QString &path) bool Wallet::init(const QString &daemonAddress, quint64 upperTransactionLimit, bool isRecovering, quint64 restoreHeight) { - return m_walletImpl->init(daemonAddress.toStdString(), upperTransactionLimit); -} - -void Wallet::initAsync(const QString &daemonAddress, quint64 upperTransactionLimit, bool isRecovering, quint64 restoreHeight) -{ + qDebug() << "init non async"; if (isRecovering){ qDebug() << "RESTORING"; m_walletImpl->setRecoveringFromSeed(true); m_walletImpl->setRefreshFromBlockHeight(restoreHeight); } - m_walletImpl->initAsync(daemonAddress.toStdString(), upperTransactionLimit); + m_walletImpl->init(daemonAddress.toStdString(), upperTransactionLimit); + return true; +} + +void Wallet::initAsync(const QString &daemonAddress, quint64 upperTransactionLimit, bool isRecovering, quint64 restoreHeight) +{ + qDebug() << "initAsync: " + daemonAddress; + m_connectionStatus = Wallet::ConnectionStatus_Disconnected; + emit connectionStatusChanged(m_connectionStatus); + + QFuture future = QtConcurrent::run(this, &Wallet::init, + daemonAddress, upperTransactionLimit, isRecovering, restoreHeight); + QFutureWatcher * watcher = new QFutureWatcher(); + + connect(watcher, &QFutureWatcher::finished, + this, [this, watcher, daemonAddress, upperTransactionLimit, isRecovering, restoreHeight]() { + QFuture future = watcher->future(); + watcher->deleteLater(); + if(future.result()){ + qDebug() << "init async finished - starting refresh"; + connected(true); + m_walletImpl->startRefresh(); + + } + }); + watcher->setFuture(future); } //! create a view only wallet @@ -220,6 +261,7 @@ quint64 Wallet::daemonBlockChainTargetHeight() const if (m_daemonBlockChainTargetHeight == 0 || m_daemonBlockChainTargetHeightTime.elapsed() / 1000 > m_daemonBlockChainTargetHeightTtl) { m_daemonBlockChainTargetHeight = m_walletImpl->daemonBlockChainTargetHeight(); + // Target height is set to 0 if daemon is synced. // Use current height from daemon when target height < current height if (m_daemonBlockChainTargetHeight < m_daemonBlockChainHeight){ @@ -242,6 +284,7 @@ bool Wallet::refresh() void Wallet::refreshAsync() { + qDebug() << "refresh async"; m_walletImpl->refreshAsync(); } @@ -273,13 +316,14 @@ void Wallet::createTransactionAsync(const QString &dst_addr, const QString &paym QFuture future = QtConcurrent::run(this, &Wallet::createTransaction, dst_addr, payment_id,amount, mixin_count, priority); QFutureWatcher * watcher = new QFutureWatcher(); - watcher->setFuture(future); + connect(watcher, &QFutureWatcher::finished, this, [this, watcher,dst_addr,payment_id,mixin_count]() { QFuture future = watcher->future(); watcher->deleteLater(); emit transactionCreated(future.result(),dst_addr,payment_id,mixin_count); }); + watcher->setFuture(future); } PendingTransaction *Wallet::createTransactionAll(const QString &dst_addr, const QString &payment_id, @@ -299,13 +343,14 @@ void Wallet::createTransactionAllAsync(const QString &dst_addr, const QString &p QFuture future = QtConcurrent::run(this, &Wallet::createTransactionAll, dst_addr, payment_id, mixin_count, priority); QFutureWatcher * watcher = new QFutureWatcher(); - watcher->setFuture(future); + connect(watcher, &QFutureWatcher::finished, this, [this, watcher,dst_addr,payment_id,mixin_count]() { QFuture future = watcher->future(); watcher->deleteLater(); emit transactionCreated(future.result(),dst_addr,payment_id,mixin_count); }); + watcher->setFuture(future); } PendingTransaction *Wallet::createSweepUnmixableTransaction() @@ -319,13 +364,14 @@ void Wallet::createSweepUnmixableTransactionAsync() { QFuture future = QtConcurrent::run(this, &Wallet::createSweepUnmixableTransaction); QFutureWatcher * watcher = new QFutureWatcher(); - watcher->setFuture(future); + connect(watcher, &QFutureWatcher::finished, this, [this, watcher]() { QFuture future = watcher->future(); watcher->deleteLater(); emit transactionCreated(future.result(),"","",0); }); + watcher->setFuture(future); } UnsignedTransaction * Wallet::loadTxFile(const QString &fileName) @@ -542,6 +588,7 @@ Wallet::Wallet(Monero::Wallet *w, QObject *parent) m_daemonBlockChainHeightTime.restart(); m_daemonBlockChainTargetHeightTime.restart(); m_initialized = false; + m_connectionStatusRunning = false; } Wallet::~Wallet() -- cgit v1.2.3 From 2586404339bf361ec978f8c91fd76ac50b0bdc2e Mon Sep 17 00:00:00 2001 From: Jaquee Date: Tue, 31 Jan 2017 10:34:18 +0100 Subject: sync progress improvements --- components/ProgressBar.qml | 2 +- main.qml | 33 +++++++++++++++------------------ pages/Transfer.qml | 1 - src/libwalletqt/Wallet.cpp | 2 +- 4 files changed, 17 insertions(+), 21 deletions(-) (limited to 'src/libwalletqt/Wallet.cpp') diff --git a/components/ProgressBar.qml b/components/ProgressBar.qml index 33f2c585..ec53ec61 100644 --- a/components/ProgressBar.qml +++ b/components/ProgressBar.qml @@ -42,7 +42,7 @@ Item { var progressLevel = ((currentBlock/targetBlock) * 100).toFixed(0); fillLevel = progressLevel progressText.text = qsTr("Synchronizing blocks %1/%2").arg(currentBlock.toFixed(0)).arg(targetBlock.toFixed(0)); - item.visible = (currentWallet.connected !== Wallet.ConnectionStatus_Disconnected) && (currentBlock < targetBlock) + progressBar.visible = currentBlock < targetBlock } } diff --git a/main.qml b/main.qml index e0f48142..4296fe26 100644 --- a/main.qml +++ b/main.qml @@ -54,7 +54,6 @@ ApplicationWindow { property var transaction; property var transactionDescription; property alias password : passwordDialog.password - property int splashCounter: 0 property bool isNewWallet: false property int restoreHeight:0 property bool daemonSynced: false @@ -261,10 +260,12 @@ ApplicationWindow { return path.replace(/.*[\/\\]/, '').replace(/\.keys$/, '') } - function onWalletConnectionStatusChanged(){ + function onWalletConnectionStatusChanged(status){ console.log("Wallet connection status changed") middlePanel.updateStatus(); - } + leftPanel.networkStatus.connected = status + leftPanel.progressBar.visible = (status === Wallet.ConnectionStatus_Connected) && !daemonSynced + } function onWalletOpened(wallet) { walletName = usefulName(wallet.path) @@ -306,7 +307,6 @@ ApplicationWindow { console.log(">>> wallet updated") middlePanel.unlockedBalanceText = leftPanel.unlockedBalanceText = walletManager.displayAmount(currentWallet.unlockedBalance); middlePanel.balanceText = leftPanel.balanceText = walletManager.displayAmount(currentWallet.balance); - console.log("time to unlock: ", currentWallet.history.minutesToUnlock); // Update history if new block found since last update and balance is locked. if(foundNewBlock && currentWallet.history.locked) { foundNewBlock = false; @@ -332,11 +332,16 @@ ApplicationWindow { // Daemon fully synced // TODO: implement onDaemonSynced or similar in wallet API and don't start refresh thread before daemon is synced - daemonSynced = (currentWallet.connected != Wallet.ConnectionStatus_Disconnected && dCurrentBlock >= dTargetBlock) + daemonSynced = dCurrentBlock >= dTargetBlock + // Update daemon sync progress leftPanel.progressBar.updateProgress(dCurrentBlock,dTargetBlock); - updateSyncing((currentWallet.connected !== Wallet.ConnectionStatus_Disconnected) && (dCurrentBlock < dTargetBlock)) + leftPanel.progressBar.visible = !daemonSynced && currentWallet.connected !== Wallet.ConnectionStatus_Disconnected + // Update wallet sync progress + updateSyncing((currentWallet.connected !== Wallet.ConnectionStatus_Disconnected) && !daemonSynced) + // Update transfer page status middlePanel.updateStatus(); + // If wallet isnt connected and no daemon is running - Ask if(currentWallet.connected === Wallet.ConnectionStatus_Disconnected && !daemonManager.running() && !walletInitialized){ daemonManagerDialog.open(); @@ -388,17 +393,10 @@ ApplicationWindow { daemonRunning = false; } - function onWalletNewBlock(blockHeight) { - // Update progress bar - var currHeight = blockHeight - //fast refresh until restoreHeight is reached - var increment = ((restoreHeight == 0) || currHeight < restoreHeight)? 1000 : 10 - - if(currHeight > splashCounter + increment){ - splashCounter = currHeight - leftPanel.progressBar.updateProgress(currHeight,currentWallet.daemonBlockChainTargetHeight()); - } - foundNewBlock = true; + function onWalletNewBlock(blockHeight, targetHeight) { + // Update progress bar + leftPanel.progressBar.updateProgress(blockHeight,targetHeight); + foundNewBlock = true; } function onWalletMoneyReceived(txId, amount) { @@ -708,7 +706,6 @@ ApplicationWindow { // close wallet and show wizard function showWizard(){ walletInitialized = false; - splashCounter = 0; closeWallet(); currentWallet = undefined; wizard.restart(); diff --git a/pages/Transfer.qml b/pages/Transfer.qml index 74e5d114..05e78c12 100644 --- a/pages/Transfer.qml +++ b/pages/Transfer.qml @@ -655,7 +655,6 @@ Rectangle { //TODO: enable send page when we're connected and daemon is synced function updateStatus() { - console.log("updated transfer page status") if(typeof currentWallet === "undefined") { statusText.text = qsTr("Wallet is not connected to daemon.") + "
" + root.startLinkText return; diff --git a/src/libwalletqt/Wallet.cpp b/src/libwalletqt/Wallet.cpp index 0e4e09a3..56121370 100644 --- a/src/libwalletqt/Wallet.cpp +++ b/src/libwalletqt/Wallet.cpp @@ -127,9 +127,9 @@ void Wallet::updateConnectionStatusAsync() Wallet::ConnectionStatus Wallet::connected(bool forceCheck) { - qDebug() << "Checking connection status"; // cache connection status if (forceCheck || !m_initialized || (m_connectionStatusTime.elapsed() / 1000 > m_connectionStatusTtl && !m_connectionStatusRunning) || m_connectionStatusTime.elapsed() > 30000) { + qDebug() << "Checking connection status"; m_connectionStatusRunning = true; m_initialized = true; m_connectionStatusTime.restart(); -- cgit v1.2.3