Skip to content

Commit 96e3264

Browse files
committed
GUI: use cached balance in overviewpage and sendcoinsdialog
Plus, calculate the cached balance right when the wallet model, so the wallet widgets don't need to redo the same balance calculation multiple times when they are waiting for the model balance polling timer. ---------------------------------------------------------------------- test wise: `WalletTests` now need to trigger the walletModel balance changed manually. So the model updates its internal state and can be used by the widgets. This is because the test does not start the balance polling timer, in the same way as does not initialize several parts of the GUI workflow. All the objects (wallet, models, views, etc) that are used on this test are manually created instead of using the `WalletController` class flow. Rationale is that this unit test is focused on verifying the GUI widgets/views behavior only: update the presented information, etc. when they receive different signals and/or function calls from outside (in other words, focus is on the signal slots/receiver side). It's not about whether the wallet balance polling timer is functioning as expected or not (which we definitely create a new test case for it in a follow-up work).
1 parent 321335b commit 96e3264

File tree

4 files changed

+21
-20
lines changed

4 files changed

+21
-20
lines changed

src/qt/overviewpage.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -277,14 +277,13 @@ void OverviewPage::setWalletModel(WalletModel *model)
277277
ui->listTransactions->setModelColumn(TransactionTableModel::ToAddress);
278278

279279
// Keep up to date with wallet
280-
interfaces::Wallet& wallet = model->wallet();
281-
interfaces::WalletBalances balances = wallet.getBalances();
282-
setBalance(balances);
280+
setBalance(model->getCachedBalance());
283281
connect(model, &WalletModel::balanceChanged, this, &OverviewPage::setBalance);
284282

285283
connect(model->getOptionsModel(), &OptionsModel::displayUnitChanged, this, &OverviewPage::updateDisplayUnit);
286284

287-
updateWatchOnlyLabels(wallet.haveWatchOnly() && !model->wallet().privateKeysDisabled());
285+
interfaces::Wallet& wallet = model->wallet();
286+
updateWatchOnlyLabels(wallet.haveWatchOnly() && !wallet.privateKeysDisabled());
288287
connect(model, &WalletModel::notifyWatchonlyChanged, [this](bool showWatchOnly) {
289288
updateWatchOnlyLabels(showWatchOnly && !walletModel->wallet().privateKeysDisabled());
290289
});

src/qt/sendcoinsdialog.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -718,7 +718,7 @@ void SendCoinsDialog::setBalance(const interfaces::WalletBalances& balances)
718718

719719
void SendCoinsDialog::refreshBalance()
720720
{
721-
setBalance(model->wallet().getBalances());
721+
setBalance(model->getCachedBalance());
722722
ui->customFee->setDisplayUnit(model->getOptionsModel()->getDisplayUnit());
723723
updateSmartFeeLabel();
724724
}

src/qt/test/wallettests.cpp

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,13 @@ void BumpFee(TransactionView& view, const uint256& txid, bool expectDisabled, st
129129
QVERIFY(text.indexOf(QString::fromStdString(expectError)) != -1);
130130
}
131131

132+
void CompareBalance(WalletModel& walletModel, CAmount expected_balance, QLabel* balance_label_to_check)
133+
{
134+
BitcoinUnit unit = walletModel.getOptionsModel()->getDisplayUnit();
135+
QString balanceComparison = BitcoinUnits::formatWithUnit(unit, expected_balance, false, BitcoinUnits::SeparatorStyle::ALWAYS);
136+
QCOMPARE(balance_label_to_check->text().trimmed(), balanceComparison);
137+
}
138+
132139
//! Simple qt wallet tests.
133140
//
134141
// Test widgets can be debugged interactively calling show() on them and
@@ -193,15 +200,10 @@ void TestGUI(interfaces::Node& node)
193200
sendCoinsDialog.setModel(&walletModel);
194201
transactionView.setModel(&walletModel);
195202

196-
{
197-
// Check balance in send dialog
198-
QLabel* balanceLabel = sendCoinsDialog.findChild<QLabel*>("labelBalance");
199-
QString balanceText = balanceLabel->text();
200-
BitcoinUnit unit = walletModel.getOptionsModel()->getDisplayUnit();
201-
CAmount balance = walletModel.wallet().getBalance();
202-
QString balanceComparison = BitcoinUnits::formatWithUnit(unit, balance, false, BitcoinUnits::SeparatorStyle::ALWAYS);
203-
QCOMPARE(balanceText, balanceComparison);
204-
}
203+
// Update walletModel cached balance which will trigger an update for the 'labelBalance' QLabel.
204+
walletModel.pollBalanceChanged();
205+
// Check balance in send dialog
206+
CompareBalance(walletModel, walletModel.wallet().getBalance(), sendCoinsDialog.findChild<QLabel*>("labelBalance"));
205207

206208
// Send two transactions, and verify they are added to transaction list.
207209
TransactionTableModel* transactionTableModel = walletModel.getTransactionTableModel();
@@ -221,12 +223,8 @@ void TestGUI(interfaces::Node& node)
221223
// Check current balance on OverviewPage
222224
OverviewPage overviewPage(platformStyle.get());
223225
overviewPage.setWalletModel(&walletModel);
224-
QLabel* balanceLabel = overviewPage.findChild<QLabel*>("labelBalance");
225-
QString balanceText = balanceLabel->text().trimmed();
226-
BitcoinUnit unit = walletModel.getOptionsModel()->getDisplayUnit();
227-
CAmount balance = walletModel.wallet().getBalance();
228-
QString balanceComparison = BitcoinUnits::formatWithUnit(unit, balance, false, BitcoinUnits::SeparatorStyle::ALWAYS);
229-
QCOMPARE(balanceText, balanceComparison);
226+
walletModel.pollBalanceChanged(); // Manual balance polling update
227+
CompareBalance(walletModel, walletModel.wallet().getBalance(), overviewPage.findChild<QLabel*>("labelBalance"));
230228

231229
// Check Request Payment button
232230
ReceiveCoinsDialog receiveCoinsDialog(platformStyle.get());

src/qt/walletmodel.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ WalletModel::~WalletModel()
6767

6868
void WalletModel::startPollBalance()
6969
{
70+
// Update the cached balance right away, so every view can make use of it,
71+
// so them don't need to waste resources recalculating it.
72+
pollBalanceChanged();
73+
7074
// This timer will be fired repeatedly to update the balance
7175
// Since the QTimer::timeout is a private signal, it cannot be used
7276
// in the GUIUtil::ExceptionSafeConnect directly.

0 commit comments

Comments
 (0)