Skip to content

Commit f2880e2

Browse files
committed
Merge #18160: gui: Avoid Wallet::GetBalance in WalletModel::pollBalanceChanged
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 #16874 and should be fixed with a solution similar to #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 #17905 to be merged first. This PR can be simpler if it is based on #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
2 parents 965c0c3 + 0933a37 commit f2880e2

File tree

3 files changed

+15
-14
lines changed

3 files changed

+15
-14
lines changed

src/interfaces/wallet.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -368,16 +368,17 @@ class WalletImpl : public Wallet
368368
}
369369
return result;
370370
}
371-
bool tryGetBalances(WalletBalances& balances, int& num_blocks) override
371+
bool tryGetBalances(WalletBalances& balances, int& num_blocks, bool force, int cached_num_blocks) override
372372
{
373373
auto locked_chain = m_wallet->chain().lock(true /* try_lock */);
374374
if (!locked_chain) return false;
375+
num_blocks = locked_chain->getHeight().get_value_or(-1);
376+
if (!force && num_blocks == cached_num_blocks) return false;
375377
TRY_LOCK(m_wallet->cs_wallet, locked_wallet);
376378
if (!locked_wallet) {
377379
return false;
378380
}
379381
balances = getBalances();
380-
num_blocks = locked_chain->getHeight().get_value_or(-1);
381382
return true;
382383
}
383384
CAmount getBalance() override { return m_wallet->GetBalance().m_mine_trusted; }

src/interfaces/wallet.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,11 @@ class Wallet
201201
//! Get balances.
202202
virtual WalletBalances getBalances() = 0;
203203

204-
//! Get balances if possible without blocking.
205-
virtual bool tryGetBalances(WalletBalances& balances, int& num_blocks) = 0;
204+
//! Get balances if possible without waiting for chain and wallet locks.
205+
virtual bool tryGetBalances(WalletBalances& balances,
206+
int& num_blocks,
207+
bool force,
208+
int cached_num_blocks) = 0;
206209

207210
//! Get balance.
208211
virtual CAmount getBalance() = 0;

src/qt/walletmodel.cpp

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -78,21 +78,18 @@ void WalletModel::pollBalanceChanged()
7878
// rescan.
7979
interfaces::WalletBalances new_balances;
8080
int numBlocks = -1;
81-
if (!m_wallet->tryGetBalances(new_balances, numBlocks)) {
81+
if (!m_wallet->tryGetBalances(new_balances, numBlocks, fForceCheckBalanceChanged, cachedNumBlocks)) {
8282
return;
8383
}
8484

85-
if(fForceCheckBalanceChanged || numBlocks != cachedNumBlocks)
86-
{
87-
fForceCheckBalanceChanged = false;
85+
fForceCheckBalanceChanged = false;
8886

89-
// Balance and number of transactions might have changed
90-
cachedNumBlocks = numBlocks;
87+
// Balance and number of transactions might have changed
88+
cachedNumBlocks = numBlocks;
9189

92-
checkBalanceChanged(new_balances);
93-
if(transactionTableModel)
94-
transactionTableModel->updateConfirmations();
95-
}
90+
checkBalanceChanged(new_balances);
91+
if(transactionTableModel)
92+
transactionTableModel->updateConfirmations();
9693
}
9794

9895
void WalletModel::checkBalanceChanged(const interfaces::WalletBalances& new_balances)

0 commit comments

Comments
 (0)