From 30a28146ac23aa3a9e510c5d6ab9a8d2c5b8177e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Barbosa?= Date: Sun, 16 Feb 2020 11:38:49 +0000 Subject: gui: Avoid Wallet::GetBalance in WalletModel::pollBalanceChanged Github-Pull: #18160 Rebased-From: 0933a37078e1ce3a3d70983c3e7f4b3ac6c3fa37 --- src/interfaces/wallet.cpp | 5 +++-- src/interfaces/wallet.h | 7 +++++-- src/qt/walletmodel.cpp | 17 +++++++---------- 3 files changed, 15 insertions(+), 14 deletions(-) diff --git a/src/interfaces/wallet.cpp b/src/interfaces/wallet.cpp index 0c8d92eba5..dbf0acbfbe 100644 --- a/src/interfaces/wallet.cpp +++ b/src/interfaces/wallet.cpp @@ -359,16 +359,17 @@ public: } return result; } - bool tryGetBalances(WalletBalances& balances, int& num_blocks) override + bool tryGetBalances(WalletBalances& balances, int& num_blocks, bool force, int cached_num_blocks) override { auto locked_chain = m_wallet->chain().lock(true /* try_lock */); if (!locked_chain) return false; + num_blocks = locked_chain->getHeight().get_value_or(-1); + if (!force && num_blocks == cached_num_blocks) return false; TRY_LOCK(m_wallet->cs_wallet, locked_wallet); if (!locked_wallet) { return false; } balances = getBalances(); - num_blocks = locked_chain->getHeight().get_value_or(-1); return true; } CAmount getBalance() override { return m_wallet->GetBalance().m_mine_trusted; } diff --git a/src/interfaces/wallet.h b/src/interfaces/wallet.h index 89e056b18b..36ceb538fb 100644 --- a/src/interfaces/wallet.h +++ b/src/interfaces/wallet.h @@ -198,8 +198,11 @@ public: //! Get balances. virtual WalletBalances getBalances() = 0; - //! Get balances if possible without blocking. - virtual bool tryGetBalances(WalletBalances& balances, int& num_blocks) = 0; + //! Get balances if possible without waiting for chain and wallet locks. + virtual bool tryGetBalances(WalletBalances& balances, + int& num_blocks, + bool force, + int cached_num_blocks) = 0; //! Get balance. virtual CAmount getBalance() = 0; diff --git a/src/qt/walletmodel.cpp b/src/qt/walletmodel.cpp index cb8dd3cc10..506d17ac17 100644 --- a/src/qt/walletmodel.cpp +++ b/src/qt/walletmodel.cpp @@ -77,21 +77,18 @@ void WalletModel::pollBalanceChanged() // rescan. interfaces::WalletBalances new_balances; int numBlocks = -1; - if (!m_wallet->tryGetBalances(new_balances, numBlocks)) { + if (!m_wallet->tryGetBalances(new_balances, numBlocks, fForceCheckBalanceChanged, cachedNumBlocks)) { return; } - if(fForceCheckBalanceChanged || numBlocks != cachedNumBlocks) - { - fForceCheckBalanceChanged = false; + fForceCheckBalanceChanged = false; - // Balance and number of transactions might have changed - cachedNumBlocks = numBlocks; + // Balance and number of transactions might have changed + cachedNumBlocks = numBlocks; - checkBalanceChanged(new_balances); - if(transactionTableModel) - transactionTableModel->updateConfirmations(); - } + checkBalanceChanged(new_balances); + if(transactionTableModel) + transactionTableModel->updateConfirmations(); } void WalletModel::checkBalanceChanged(const interfaces::WalletBalances& new_balances) -- cgit v1.2.3