mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-11-11 06:28:31 +01:00
Merge bitcoin-core/gui#598: Avoid recalculating the wallet balance - use model cache
4584d300a4GUI: remove now unneeded 'm_balances' field from overviewpage (furszy)050e8b1391GUI: 'getAvailableBalance', use cached balance if the user did not select UTXO manually (furszy)96e3264a82GUI: use cached balance in overviewpage and sendcoinsdialog (furszy)321335bf02GUI: add getter for WalletModel::m_cached_balances field (furszy)e62958dc81GUI: sendCoinsDialog, remove duplicate wallet().getBalances() call (furszy) Pull request description: As per the title says, we are recalculating the entire wallet balance on different situations calling to `wallet().getBalances()`, when should instead make use of the wallet model cached balance. This has the benefits of (1) not spending resources calculating a balance that we already have cached, and (2) avoid blocking the main thread for a long time, in case of big wallets, walking through the entire wallet's tx map more than what it's really needed. Changes: 1) Fix: `SendCoinsDialog` was calling `wallet().getBalances()` twice during `setModel`. 2) Use the cached balance if the user did not select any UTXO manually inside the wallet model `getAvailableBalance` call. ----------------------- As an extra note, this work born in [#25005](https://github.com/bitcoin/bitcoin/pull/25005) but grew out of scope of it. ACKs for top commit: jarolrod: ACK4584d300a4hebasto: re-ACK4584d300a4, only suggested changes and commit message formatting since my [recent](https://github.com/bitcoin-core/gui/pull/598#pullrequestreview-1071268192) review. Tree-SHA512: 6633ce7f9a82a3e46e75aa7295df46c80a4cd4a9f3305427af203c9bc8670573fa8a1927f14a279260c488cc975a08d238faba2e9751588086fea1dcf8ea2b28
This commit is contained in:
@@ -67,6 +67,10 @@ WalletModel::~WalletModel()
|
||||
|
||||
void WalletModel::startPollBalance()
|
||||
{
|
||||
// Update the cached balance right away, so every view can make use of it,
|
||||
// so them don't need to waste resources recalculating it.
|
||||
pollBalanceChanged();
|
||||
|
||||
// This timer will be fired repeatedly to update the balance
|
||||
// Since the QTimer::timeout is a private signal, it cannot be used
|
||||
// in the GUIUtil::ExceptionSafeConnect directly.
|
||||
@@ -120,12 +124,17 @@ void WalletModel::pollBalanceChanged()
|
||||
|
||||
void WalletModel::checkBalanceChanged(const interfaces::WalletBalances& new_balances)
|
||||
{
|
||||
if(new_balances.balanceChanged(m_cached_balances)) {
|
||||
if (new_balances.balanceChanged(m_cached_balances)) {
|
||||
m_cached_balances = new_balances;
|
||||
Q_EMIT balanceChanged(new_balances);
|
||||
}
|
||||
}
|
||||
|
||||
interfaces::WalletBalances WalletModel::getCachedBalance() const
|
||||
{
|
||||
return m_cached_balances;
|
||||
}
|
||||
|
||||
void WalletModel::updateTransaction()
|
||||
{
|
||||
// Balance and number of transactions might have changed
|
||||
@@ -194,7 +203,9 @@ WalletModel::SendCoinsReturn WalletModel::prepareTransaction(WalletModelTransact
|
||||
return DuplicateAddress;
|
||||
}
|
||||
|
||||
CAmount nBalance = m_wallet->getAvailableBalance(coinControl);
|
||||
// If no coin was manually selected, use the cached balance
|
||||
// Future: can merge this call with 'createTransaction'.
|
||||
CAmount nBalance = getAvailableBalance(&coinControl);
|
||||
|
||||
if(total > nBalance)
|
||||
{
|
||||
@@ -602,3 +613,8 @@ uint256 WalletModel::getLastBlockProcessed() const
|
||||
{
|
||||
return m_client_model ? m_client_model->getBestBlockHash() : uint256{};
|
||||
}
|
||||
|
||||
CAmount WalletModel::getAvailableBalance(const CCoinControl* control)
|
||||
{
|
||||
return control && control->HasSelected() ? wallet().getAvailableBalance(*control) : getCachedBalance().balance;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user