Skip to content

Commit 94640cf

Browse files
jonasschnellimajcosta
authored andcommitted
[backport#18587] gui: Avoid wallet tryGetBalances calls in WalletModel::pollBalanceChanged
Summary: d3a56be77a9d112cde4baef4314882170b9f228f Revert "gui: Avoid Wallet::GetBalance in WalletModel::pollBalanceChanged" (Russell Yanofsky) bf0a510981ddc28c754881ca21c50ab18e5f2b59 gui: Avoid wallet tryGetBalances calls before TransactionChanged or BlockTip notifications (Russell Yanofsky) 2bc9b92ed8b7736ad67876398a0bb8287f57e9b3 Cancel wallet balance timer when shutdown requested (Russell Yanofsky) 83f69fab3a1ae97c5cff8ba1e6fd191b0fa264bb Switch transaction table to use wallet height not node height (Russell Yanofsky) Pull request description: Main commit `gui: Avoid wallet tryGetBalances calls` is one-line change to `WalletModel::pollBalanceChanged` that returns early if there hasn't been a new `TransactionChanged` or `BlockTip` notification since the previous poll call. This is the same behavior that was implemented in #18160, now implemented in a simpler way. The other commits are a straight revert of #18160, and two tweaks to avoid relying on `WalletModel::m_client_model` lifetime which were causing travis failures with earlier versions of this PR. Motivation for this change is to be able to revert #18160 and cut down on unnecessary cross-process calls that happen when #18160 is combined with #10102 This PR is part of the [process separation project](https://github.com/bitcoin/bitcoin/projects/10). --- Backport of Core [[bitcoin/bitcoin#18587 | PR18587]] Test Plan: ninja all check-check-functional Reviewers: #bitcoin_abc, Fabien Reviewed By: #bitcoin_abc, Fabien Differential Revision: https://reviews.bitcoinabc.org/D9168
1 parent 1bc6f81 commit 94640cf

File tree

6 files changed

+39
-23
lines changed

6 files changed

+39
-23
lines changed

src/interfaces/wallet.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -331,16 +331,13 @@ namespace {
331331
}
332332
return result;
333333
}
334-
bool tryGetBalances(WalletBalances &balances, int &num_blocks,
335-
bool force, int cached_num_blocks) override {
334+
bool tryGetBalances(WalletBalances &balances,
335+
int &num_blocks) override {
336336
TRY_LOCK(m_wallet->cs_wallet, locked_wallet);
337337
if (!locked_wallet) {
338338
return false;
339339
}
340340
num_blocks = m_wallet->GetLastBlockHeight();
341-
if (!force && num_blocks == cached_num_blocks) {
342-
return false;
343-
}
344341
balances = getBalances();
345342
return true;
346343
}

src/interfaces/wallet.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -198,9 +198,8 @@ class Wallet {
198198
//! Get balances.
199199
virtual WalletBalances getBalances() = 0;
200200

201-
//! Get balances if possible without waiting for chain and wallet locks.
202-
virtual bool tryGetBalances(WalletBalances &balances, int &num_blocks,
203-
bool force, int cached_num_blocks) = 0;
201+
//! Get balances if possible without blocking.
202+
virtual bool tryGetBalances(WalletBalances &balances, int &num_blocks) = 0;
204203

205204
//! Get balance.
206205
virtual Amount getBalance() = 0;

src/qt/transactiontablemodel.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -671,8 +671,8 @@ QVariant TransactionTableModel::headerData(int section,
671671
QModelIndex TransactionTableModel::index(int row, int column,
672672
const QModelIndex &parent) const {
673673
Q_UNUSED(parent);
674-
TransactionRecord *data = priv->index(
675-
walletModel->wallet(), walletModel->clientModel().getNumBlocks(), row);
674+
TransactionRecord *data =
675+
priv->index(walletModel->wallet(), walletModel->getNumBlocks(), row);
676676
if (data) {
677677
return createIndex(row, column, data);
678678
}

src/qt/walletmodel.cpp

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,11 @@ WalletModel::WalletModel(std::unique_ptr<interfaces::Wallet> wallet,
3131
ClientModel &client_model,
3232
const PlatformStyle *platformStyle, QObject *parent)
3333
: QObject(parent), m_wallet(std::move(wallet)),
34-
m_client_model(client_model), m_node(client_model.node()),
34+
m_client_model(&client_model), m_node(client_model.node()),
3535
optionsModel(client_model.getOptionsModel()), addressTableModel(nullptr),
3636
transactionTableModel(nullptr), recentRequestsTableModel(nullptr),
37-
cachedEncryptionStatus(Unencrypted), cachedNumBlocks(0) {
37+
cachedEncryptionStatus(Unencrypted), cachedNumBlocks(0),
38+
timer(new QTimer(this)) {
3839
fHaveWatchOnly = m_wallet->haveWatchOnly();
3940
addressTableModel = new AddressTableModel(this);
4041
transactionTableModel = new TransactionTableModel(platformStyle, this);
@@ -49,11 +50,17 @@ WalletModel::~WalletModel() {
4950

5051
void WalletModel::startPollBalance() {
5152
// This timer will be fired repeatedly to update the balance
52-
QTimer *timer = new QTimer(this);
5353
connect(timer, &QTimer::timeout, this, &WalletModel::pollBalanceChanged);
5454
timer->start(MODEL_UPDATE_DELAY);
5555
}
5656

57+
void WalletModel::setClientModel(ClientModel *client_model) {
58+
m_client_model = client_model;
59+
if (!m_client_model) {
60+
timer->stop();
61+
}
62+
}
63+
5764
void WalletModel::updateStatus() {
5865
EncryptionStatus newEncryptionStatus = getEncryptionStatus();
5966

@@ -63,25 +70,33 @@ void WalletModel::updateStatus() {
6370
}
6471

6572
void WalletModel::pollBalanceChanged() {
73+
// Avoid recomputing wallet balances unless a TransactionChanged or
74+
// BlockTip notification was received.
75+
if (!fForceCheckBalanceChanged &&
76+
cachedNumBlocks == m_client_model->getNumBlocks()) {
77+
return;
78+
}
79+
6680
// Try to get balances and return early if locks can't be acquired. This
6781
// avoids the GUI from getting stuck on periodical polls if the core is
6882
// holding the locks for a longer time - for example, during a wallet
6983
// rescan.
7084
interfaces::WalletBalances new_balances;
7185
int numBlocks = -1;
72-
if (!m_wallet->tryGetBalances(new_balances, numBlocks,
73-
fForceCheckBalanceChanged, cachedNumBlocks)) {
86+
if (!m_wallet->tryGetBalances(new_balances, numBlocks)) {
7487
return;
7588
}
7689

77-
fForceCheckBalanceChanged = false;
90+
if (fForceCheckBalanceChanged || numBlocks != cachedNumBlocks) {
91+
fForceCheckBalanceChanged = false;
7892

79-
// Balance and number of transactions might have changed
80-
cachedNumBlocks = numBlocks;
93+
// Balance and number of transactions might have changed
94+
cachedNumBlocks = numBlocks;
8195

82-
checkBalanceChanged(new_balances);
83-
if (transactionTableModel) {
84-
transactionTableModel->updateConfirmations();
96+
checkBalanceChanged(new_balances);
97+
if (transactionTableModel) {
98+
transactionTableModel->updateConfirmations();
99+
}
85100
}
86101
}
87102

src/qt/walletmodel.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,8 @@ class WalletModel : public QObject {
148148

149149
interfaces::Node &node() const { return m_node; }
150150
interfaces::Wallet &wallet() const { return *m_wallet; }
151-
ClientModel &clientModel() const { return m_client_model; }
151+
void setClientModel(ClientModel *client_model);
152+
int getNumBlocks() const { return cachedNumBlocks; }
152153

153154
const CChainParams &getChainParams() const;
154155

@@ -170,7 +171,7 @@ class WalletModel : public QObject {
170171
std::unique_ptr<interfaces::Handler> m_handler_show_progress;
171172
std::unique_ptr<interfaces::Handler> m_handler_watch_only_changed;
172173
std::unique_ptr<interfaces::Handler> m_handler_can_get_addrs_changed;
173-
ClientModel &m_client_model;
174+
ClientModel *m_client_model;
174175
interfaces::Node &m_node;
175176

176177
bool fHaveWatchOnly;
@@ -188,6 +189,7 @@ class WalletModel : public QObject {
188189
interfaces::WalletBalances m_cached_balances;
189190
EncryptionStatus cachedEncryptionStatus;
190191
int cachedNumBlocks;
192+
QTimer *timer;
191193

192194
void subscribeToCoreSignals();
193195
void unsubscribeFromCoreSignals();

src/qt/walletview.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@ void WalletView::setClientModel(ClientModel *_clientModel) {
111111

112112
overviewPage->setClientModel(_clientModel);
113113
sendCoinsPage->setClientModel(_clientModel);
114+
if (walletModel) {
115+
walletModel->setClientModel(_clientModel);
116+
}
114117
}
115118

116119
void WalletView::setWalletModel(WalletModel *_walletModel) {

0 commit comments

Comments
 (0)