Skip to content

Commit

Permalink
Merge bitcoin#18160: gui: Avoid Wallet::GetBalance in WalletModel::po…
Browse files Browse the repository at this point in the history
…llBalanceChanged

0933a37 gui: Avoid Wallet::GetBalance in WalletModel::pollBalanceChanged (João Barbosa)

Pull request description:

  Each 250ms the slot `WalletModel::pollBalanceChanged` is called which, at worst case, calls `Wallet::GetBalance`. This is a waste of resources since most of the time there aren't new transactions or new blocks. Fix this by early checking if cache is dirty or not.

  The actual balance computation can still hang the GUI thread but that is tracked in bitcoin#16874 and should be fixed with a solution similar to bitcoin#17135.

ACKs for top commit:
  hebasto:
    ACK 0933a37, I have not tested the code, but I have reviewed it and it looks OK, I agree it can be merged.
  jonasschnelli:
    utACK 0933a37
  instagibbs:
    ACK 0933a37
  ryanofsky:
    Code review ACK 0933a37, but I would prefer (not strongly) for bitcoin#17905 to be merged first. This PR can be simpler if it is based on bitcoin#17905, so tryGetBalances can just be left alone instead of changing into to a more complicated tryGetBalancesIfNeeded function, and then getting changed back later when we want to optimize it out.
  jonatack:
    ACK 0933a37 based primarily on code review, despite a lot of manual testing with a large 177MB wallet.

Tree-SHA512: 18db35bf33a7577666658c8cb0b57308c8474baa5ea95bf1468cd8531a69857d8915584f6ac505874717aa6aabeb1b506ac77630f8acdb6651afab89275e38a1
  • Loading branch information
laanwj authored and knst committed Jul 24, 2023
1 parent 5382d05 commit 40214ee
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 15 deletions.
7 changes: 5 additions & 2 deletions src/interfaces/wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,11 @@ class Wallet
//! Get balances.
virtual WalletBalances getBalances() = 0;

//! Get balances if possible without blocking.
virtual bool tryGetBalances(WalletBalances& balances, uint256& block_hash) = 0;
//! Get balances if possible without waiting for chain and wallet locks.
virtual bool tryGetBalances(WalletBalances& balances,
uint256& block_hash,
bool force,
const uint256& cached_last_update_tip) = 0;

//! Get balance.
virtual CAmount getBalance() = 0;
Expand Down
19 changes: 8 additions & 11 deletions src/qt/walletmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,22 +101,19 @@ void WalletModel::pollBalanceChanged()
// rescan.
interfaces::WalletBalances new_balances;
uint256 block_hash;
if (!m_wallet->tryGetBalances(new_balances, block_hash)) {
if (!m_wallet->tryGetBalances(new_balances, block_hash, fForceCheckBalanceChanged, m_cached_last_update_tip)) {
return;
}

if (fForceCheckBalanceChanged || block_hash != m_cached_last_update_tip || node().coinJoinOptions().getRounds() != cachedCoinJoinRounds)
{
fForceCheckBalanceChanged = false;
fForceCheckBalanceChanged = false;

// Balance and number of transactions might have changed
m_cached_last_update_tip = block_hash;
cachedCoinJoinRounds = node().coinJoinOptions().getRounds();
// Balance and number of transactions might have changed
m_cached_last_update_tip = block_hash;
cachedCoinJoinRounds = node().coinJoinOptions().getRounds();

checkBalanceChanged(new_balances);
if(transactionTableModel)
transactionTableModel->updateConfirmations();
}
checkBalanceChanged(new_balances);
if(transactionTableModel)
transactionTableModel->updateConfirmations();
}

void WalletModel::checkBalanceChanged(const interfaces::WalletBalances& new_balances)
Expand Down
5 changes: 3 additions & 2 deletions src/wallet/interfaces.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -434,13 +434,14 @@ class WalletImpl : public Wallet
}
return result;
}
bool tryGetBalances(WalletBalances& balances, uint256& block_hash) override
bool tryGetBalances(WalletBalances& balances, uint256& block_hash, bool force, const uint256& cached_last_update_tip) override
{
block_hash = m_wallet->GetLastBlockHash();
if (!force && block_hash == cached_last_update_tip) return false;
TRY_LOCK(m_wallet->cs_wallet, locked_wallet);
if (!locked_wallet) {
return false;
}
block_hash = m_wallet->GetLastBlockHash();
balances = getBalances();
return true;
}
Expand Down

0 comments on commit 40214ee

Please sign in to comment.