Skip to content

Commit a1390c3

Browse files
committed
wallet balances, cache total delegated balance and calculate it only once.
gui send screen: remove on-demand delegated balance calculation
1 parent 02eb781 commit a1390c3

File tree

5 files changed

+33
-17
lines changed

5 files changed

+33
-17
lines changed

src/interfaces/wallet.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ namespace interfaces {
2020
result.unconfirmed_watch_only_balance = m_wallet.GetUnconfirmedWatchOnlyBalance();
2121
result.immature_watch_only_balance = m_wallet.GetImmatureWatchOnlyBalance();
2222
}
23-
if (result.have_coldstaking) { // At the moment, the GUI is not using these two balances.
24-
result.delegate_balance = m_wallet.GetDelegatedBalance();
23+
result.delegate_balance = balance.m_mine_cs_delegated_trusted;
24+
if (result.have_coldstaking) { // At the moment, the GUI is not using the cold staked balance.
2525
result.coldstaked_balance = m_wallet.GetColdStakingBalance();
2626
}
2727
result.shielded_balance = balance.m_mine_trusted_shield;

src/qt/pivx/send.cpp

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ void SendWidget::refreshAmounts()
145145
nDisplayUnit = walletModel->getOptionsModel()->getDisplayUnit();
146146

147147
CAmount totalAmount = 0;
148+
CAmount delegatedBalance = 0;
148149
QString titleTotalRemaining;
149150
if (coinControlDialog->coinControl->HasSelected()) {
150151
// Set remaining balance to the sum of the coinControl selected inputs
@@ -157,9 +158,17 @@ void SendWidget::refreshAmounts()
157158
totalAmount = selectedBalance - total;
158159
titleTotalRemaining = tr("Total remaining from the selected UTXO");
159160
} else {
160-
// Wallet's unlocked balance.
161-
totalAmount = isTransparent ? (walletModel->getUnlockedBalance(nullptr, fDelegationsChecked, false) - total)
162-
: (walletModel->GetWalletBalances().shielded_balance - total);
161+
interfaces::WalletBalances balances = walletModel->GetWalletBalances();
162+
if (isTransparent) {
163+
totalAmount = balances.balance - balances.shielded_balance - walletModel->getLockedBalance() - total;
164+
if (!fDelegationsChecked) {
165+
totalAmount -= balances.delegate_balance;
166+
}
167+
// show delegated balance if exist
168+
delegatedBalance = balances.delegate_balance;
169+
} else {
170+
totalAmount = balances.shielded_balance - total;
171+
}
163172
titleTotalRemaining = tr("Unlocked remaining");
164173
}
165174

@@ -168,16 +177,20 @@ void SendWidget::refreshAmounts()
168177
QMetaObject::invokeMethod(this, "updateAmounts", Qt::QueuedConnection,
169178
Q_ARG(QString, titleTotalRemaining),
170179
Q_ARG(QString, GUIUtil::formatBalance(total, nDisplayUnit, false)),
171-
Q_ARG(QString, labelAmountRemaining));
180+
Q_ARG(QString, labelAmountRemaining),
181+
Q_ARG(CAmount, delegatedBalance));
172182
}
173183

174-
void SendWidget::updateAmounts(const QString& _titleTotalRemaining, const QString& _labelAmountSend, const QString& _labelAmountRemaining)
184+
void SendWidget::updateAmounts(const QString& _titleTotalRemaining,
185+
const QString& _labelAmountSend,
186+
const QString& _labelAmountRemaining,
187+
CAmount _delegationBalance)
175188
{
176189
ui->labelTitleTotalRemaining->setText(_titleTotalRemaining);
177190
ui->labelAmountSend->setText(_labelAmountSend);
178191
ui->labelAmountRemaining->setText(_labelAmountRemaining);
179192
// show or hide delegations checkbox if need be
180-
showHideCheckBoxDelegations();
193+
showHideCheckBoxDelegations(_delegationBalance);
181194
}
182195

183196
void SendWidget::loadClientModel()
@@ -338,19 +351,19 @@ void SendWidget::setFocusOnLastEntry()
338351
if (!entries.isEmpty()) entries.last()->setFocus();
339352
}
340353

341-
void SendWidget::showHideCheckBoxDelegations()
354+
void SendWidget::showHideCheckBoxDelegations(CAmount delegationBalance)
342355
{
343356
// Show checkbox only when there is any available owned delegation and
344357
// coincontrol is not selected, and we are trying to spend transparent PIVs.
345358
const bool isCControl = coinControlDialog ? coinControlDialog->coinControl->HasSelected() : false;
346-
const bool hasDel = cachedDelegatedBalance > 0;
359+
const bool hasDel = delegationBalance > 0;
347360

348361
const bool showCheckBox = isTransparent && !isCControl && hasDel;
349362
ui->checkBoxDelegations->setVisible(showCheckBox);
350363
if (showCheckBox)
351364
ui->checkBoxDelegations->setToolTip(
352365
tr("Possibly spend coins delegated for cold-staking (currently available: %1").arg(
353-
GUIUtil::formatBalance(cachedDelegatedBalance, nDisplayUnit, false))
366+
GUIUtil::formatBalance(delegationBalance, nDisplayUnit, false))
354367
);
355368
}
356369

@@ -521,9 +534,6 @@ bool SendWidget::sendFinalStep()
521534
);
522535

523536
if (sendStatus.status == WalletModel::OK) {
524-
// if delegations were spent, update cachedBalance
525-
if (fStakeDelegationVoided)
526-
cachedDelegatedBalance = walletModel->getDelegatedBalance();
527537
clearAll(false);
528538
inform(tr("Transaction sent"));
529539
dialog->deleteLater();

src/qt/pivx/send.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,10 @@ public Q_SLOTS:
5757
void onValueChanged();
5858
void refreshAmounts();
5959
void changeTheme(bool isLightTheme, QString &theme) override;
60-
void updateAmounts(const QString& titleTotalRemaining, const QString& labelAmountSend, const QString& labelAmountRemaining);
60+
void updateAmounts(const QString& titleTotalRemaining,
61+
const QString& labelAmountSend,
62+
const QString& labelAmountRemaining,
63+
CAmount _delegationBalance);
6164

6265
protected:
6366
void resizeEvent(QResizeEvent *event) override;
@@ -88,7 +91,6 @@ private Q_SLOTS:
8891
SendCustomFeeDialog* customFeeDialog = nullptr;
8992
bool isCustomFeeSelected = false;
9093
bool fDelegationsChecked = false;
91-
CAmount cachedDelegatedBalance{0};
9294

9395
int nDisplayUnit;
9496
QList<SendMultiRow*> entries;
@@ -117,7 +119,7 @@ private Q_SLOTS:
117119
OperationResult prepareTransparent(WalletModelTransaction* tx);
118120
bool sendFinalStep();
119121
void setFocusOnLastEntry();
120-
void showHideCheckBoxDelegations();
122+
void showHideCheckBoxDelegations(CAmount delegationBalance);
121123
void updateEntryLabels(const QList<SendCoinsRecipient>& recipients);
122124
void setCustomFeeSelected(bool isSelected, const CAmount& customFee = DEFAULT_TRANSACTION_FEE);
123125
void setCoinControlPayAmounts();

src/wallet/wallet.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2070,6 +2070,9 @@ CWallet::Balance CWallet::GetBalance(const int min_depth) const
20702070
if (is_trusted && tx_depth >= min_depth) {
20712071
ret.m_mine_trusted += tx_credit_mine;
20722072
ret.m_mine_trusted_shield += tx_credit_shield_mine;
2073+
if (wtx.tx->HasP2CSOutputs()) {
2074+
ret.m_mine_cs_delegated_trusted += wtx.GetStakeDelegationCredit();
2075+
}
20732076
}
20742077
if (!is_trusted && tx_depth == 0 && wtx.InMempool()) {
20752078
ret.m_mine_untrusted_pending += tx_credit_mine;

src/wallet/wallet.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -960,6 +960,7 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface
960960
CAmount m_mine_immature{0}; //!< Immature coinbases/coinstakes in the main chain
961961
CAmount m_mine_trusted_shield{0}; //!< Trusted shield, at depth=GetBalance.min_depth or more
962962
CAmount m_mine_untrusted_shielded_balance{0}; //!< Untrusted shield, but in mempool (pending)
963+
CAmount m_mine_cs_delegated_trusted{0}; //!< Trusted, at depth=GetBalance.min_depth or more. Part of m_mine_trusted as well
963964
};
964965
Balance GetBalance(int min_depth = 0) const;
965966

0 commit comments

Comments
 (0)