Skip to content

Commit a587f85

Browse files
committed
Merge #18587: gui: Avoid wallet tryGetBalances calls in WalletModel::pollBalanceChanged
d3a56be Revert "gui: Avoid Wallet::GetBalance in WalletModel::pollBalanceChanged" (Russell Yanofsky) bf0a510 gui: Avoid wallet tryGetBalances calls before TransactionChanged or BlockTip notifications (Russell Yanofsky) 2bc9b92 Cancel wallet balance timer when shutdown requested (Russell Yanofsky) 83f69fa 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). ACKs for top commit: jonasschnelli: utACK d3a56be Tree-SHA512: 3cd31ca515e77c3bd7160d3f1ea0dce5050d4038b2aa441b6f66b8599bd413d81ca5542a197806e773d6092dd1d26830932b1cecbc95298b1f1ab41099e2f12f
2 parents 0aa2ff0 + d3a56be commit a587f85

File tree

6 files changed

+32
-20
lines changed

6 files changed

+32
-20
lines changed

src/interfaces/wallet.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -351,14 +351,13 @@ class WalletImpl : public Wallet
351351
}
352352
return result;
353353
}
354-
bool tryGetBalances(WalletBalances& balances, int& num_blocks, bool force, int cached_num_blocks) override
354+
bool tryGetBalances(WalletBalances& balances, int& num_blocks) override
355355
{
356356
TRY_LOCK(m_wallet->cs_wallet, locked_wallet);
357357
if (!locked_wallet) {
358358
return false;
359359
}
360360
num_blocks = m_wallet->GetLastBlockHeight();
361-
if (!force && num_blocks == cached_num_blocks) return false;
362361
balances = getBalances();
363362
return true;
364363
}

src/interfaces/wallet.h

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

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

211208
//! Get balance.
212209
virtual CAmount getBalance() = 0;

src/qt/transactiontablemodel.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -664,7 +664,7 @@ QVariant TransactionTableModel::headerData(int section, Qt::Orientation orientat
664664
QModelIndex TransactionTableModel::index(int row, int column, const QModelIndex &parent) const
665665
{
666666
Q_UNUSED(parent);
667-
TransactionRecord *data = priv->index(walletModel->wallet(), walletModel->clientModel().getNumBlocks(), row);
667+
TransactionRecord *data = priv->index(walletModel->wallet(), walletModel->getNumBlocks(), row);
668668
if(data)
669669
{
670670
return createIndex(row, column, data);

src/qt/walletmodel.cpp

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,15 @@
3939
WalletModel::WalletModel(std::unique_ptr<interfaces::Wallet> wallet, ClientModel& client_model, const PlatformStyle *platformStyle, QObject *parent) :
4040
QObject(parent),
4141
m_wallet(std::move(wallet)),
42-
m_client_model(client_model),
42+
m_client_model(&client_model),
4343
m_node(client_model.node()),
4444
optionsModel(client_model.getOptionsModel()),
4545
addressTableModel(nullptr),
4646
transactionTableModel(nullptr),
4747
recentRequestsTableModel(nullptr),
4848
cachedEncryptionStatus(Unencrypted),
49-
cachedNumBlocks(0)
49+
cachedNumBlocks(0),
50+
timer(new QTimer(this))
5051
{
5152
fHaveWatchOnly = m_wallet->haveWatchOnly();
5253
addressTableModel = new AddressTableModel(this);
@@ -64,11 +65,16 @@ WalletModel::~WalletModel()
6465
void WalletModel::startPollBalance()
6566
{
6667
// This timer will be fired repeatedly to update the balance
67-
QTimer* timer = new QTimer(this);
6868
connect(timer, &QTimer::timeout, this, &WalletModel::pollBalanceChanged);
6969
timer->start(MODEL_UPDATE_DELAY);
7070
}
7171

72+
void WalletModel::setClientModel(ClientModel* client_model)
73+
{
74+
m_client_model = client_model;
75+
if (!m_client_model) timer->stop();
76+
}
77+
7278
void WalletModel::updateStatus()
7379
{
7480
EncryptionStatus newEncryptionStatus = getEncryptionStatus();
@@ -80,24 +86,31 @@ void WalletModel::updateStatus()
8086

8187
void WalletModel::pollBalanceChanged()
8288
{
89+
// Avoid recomputing wallet balances unless a TransactionChanged or
90+
// BlockTip notification was received.
91+
if (!fForceCheckBalanceChanged && cachedNumBlocks == m_client_model->getNumBlocks()) return;
92+
8393
// Try to get balances and return early if locks can't be acquired. This
8494
// avoids the GUI from getting stuck on periodical polls if the core is
8595
// holding the locks for a longer time - for example, during a wallet
8696
// rescan.
8797
interfaces::WalletBalances new_balances;
8898
int numBlocks = -1;
89-
if (!m_wallet->tryGetBalances(new_balances, numBlocks, fForceCheckBalanceChanged, cachedNumBlocks)) {
99+
if (!m_wallet->tryGetBalances(new_balances, numBlocks)) {
90100
return;
91101
}
92102

93-
fForceCheckBalanceChanged = false;
103+
if(fForceCheckBalanceChanged || numBlocks != cachedNumBlocks)
104+
{
105+
fForceCheckBalanceChanged = false;
94106

95-
// Balance and number of transactions might have changed
96-
cachedNumBlocks = numBlocks;
107+
// Balance and number of transactions might have changed
108+
cachedNumBlocks = numBlocks;
97109

98-
checkBalanceChanged(new_balances);
99-
if(transactionTableModel)
100-
transactionTableModel->updateConfirmations();
110+
checkBalanceChanged(new_balances);
111+
if(transactionTableModel)
112+
transactionTableModel->updateConfirmations();
113+
}
101114
}
102115

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

src/qt/walletmodel.h

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

145145
interfaces::Node& node() const { return m_node; }
146146
interfaces::Wallet& wallet() const { return *m_wallet; }
147-
ClientModel& clientModel() const { return m_client_model; }
147+
void setClientModel(ClientModel* client_model);
148+
int getNumBlocks() const { return cachedNumBlocks; }
148149

149150
QString getWalletName() const;
150151
QString getDisplayName() const;
@@ -161,7 +162,7 @@ class WalletModel : public QObject
161162
std::unique_ptr<interfaces::Handler> m_handler_show_progress;
162163
std::unique_ptr<interfaces::Handler> m_handler_watch_only_changed;
163164
std::unique_ptr<interfaces::Handler> m_handler_can_get_addrs_changed;
164-
ClientModel& m_client_model;
165+
ClientModel* m_client_model;
165166
interfaces::Node& m_node;
166167

167168
bool fHaveWatchOnly;
@@ -179,6 +180,7 @@ class WalletModel : public QObject
179180
interfaces::WalletBalances m_cached_balances;
180181
EncryptionStatus cachedEncryptionStatus;
181182
int cachedNumBlocks;
183+
QTimer* timer;
182184

183185
void subscribeToCoreSignals();
184186
void unsubscribeFromCoreSignals();

src/qt/walletview.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ void WalletView::setClientModel(ClientModel *_clientModel)
9797

9898
overviewPage->setClientModel(_clientModel);
9999
sendCoinsPage->setClientModel(_clientModel);
100+
if (walletModel) walletModel->setClientModel(_clientModel);
100101
}
101102

102103
void WalletView::setWalletModel(WalletModel *_walletModel)

0 commit comments

Comments
 (0)