Skip to content

Commit e9c160a

Browse files
committed
wallet: single loop to calculate the currently required balances.
future: add watch-only and CS balances distinction when/if they are needed (at the moment, they aren't). Inspired in btc@fa57411fcba00556ba25d45bca53cc04623da051 .
1 parent 32538ae commit e9c160a

File tree

3 files changed

+41
-5
lines changed

3 files changed

+41
-5
lines changed

src/interfaces/wallet.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ namespace interfaces {
1010

1111
WalletBalances Wallet::getBalances() {
1212
WalletBalances result;
13-
result.balance = m_wallet.GetAvailableBalance();
14-
result.unconfirmed_balance = m_wallet.GetUnconfirmedBalance(ISMINE_SPENDABLE_TRANSPARENT);
15-
result.immature_balance = m_wallet.GetImmatureBalance();
13+
CWallet::Balance balance = m_wallet.GetBalance();
14+
result.balance = balance.m_mine_trusted + balance.m_mine_trusted_shield;
15+
result.unconfirmed_balance = balance.m_mine_untrusted_pending;
16+
result.immature_balance = balance.m_mine_immature;
1617
result.have_watch_only = m_wallet.HaveWatchOnly();
1718
if (result.have_watch_only) {
1819
result.watch_only_balance = m_wallet.GetWatchOnlyBalance();
@@ -23,8 +24,8 @@ namespace interfaces {
2324
result.delegate_balance = m_wallet.GetDelegatedBalance();
2425
result.coldstaked_balance = m_wallet.GetColdStakingBalance();
2526
}
26-
result.shielded_balance = m_wallet.GetAvailableShieldedBalance();
27-
result.unconfirmed_shielded_balance = m_wallet.GetUnconfirmedShieldedBalance();
27+
result.shielded_balance = balance.m_mine_trusted_shield;
28+
result.unconfirmed_shielded_balance = balance.m_mine_untrusted_shielded_balance;
2829
return result;
2930
}
3031

src/wallet/wallet.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2055,6 +2055,32 @@ void CWallet::ResendWalletTransactions(CConnman* connman)
20552055
* @{
20562056
*/
20572057

2058+
CWallet::Balance CWallet::GetBalance(const int min_depth) const
2059+
{
2060+
Balance ret;
2061+
{
2062+
LOCK(cs_wallet);
2063+
std::set<uint256> trusted_parents;
2064+
for (const auto& entry : mapWallet) {
2065+
const CWalletTx& wtx = entry.second;
2066+
const bool is_trusted{wtx.IsTrusted()};
2067+
const int tx_depth{wtx.GetDepthInMainChain()};
2068+
const CAmount tx_credit_mine{wtx.GetAvailableCredit(/* fUseCache */ true, ISMINE_SPENDABLE_TRANSPARENT)};
2069+
const CAmount tx_credit_shield_mine{wtx.GetAvailableCredit(/* fUseCache */ true, ISMINE_SPENDABLE_SHIELDED)};
2070+
if (is_trusted && tx_depth >= min_depth) {
2071+
ret.m_mine_trusted += tx_credit_mine;
2072+
ret.m_mine_trusted_shield += tx_credit_shield_mine;
2073+
}
2074+
if (!is_trusted && tx_depth == 0 && wtx.InMempool()) {
2075+
ret.m_mine_untrusted_pending += tx_credit_mine;
2076+
ret.m_mine_untrusted_shielded_balance += tx_credit_shield_mine;
2077+
}
2078+
ret.m_mine_immature += wtx.GetImmatureCredit();
2079+
}
2080+
}
2081+
return ret;
2082+
}
2083+
20582084
CAmount CWallet::loopTxsBalance(std::function<void(const uint256&, const CWalletTx&, CAmount&)> method) const
20592085
{
20602086
CAmount nTotal = 0;

src/wallet/wallet.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -954,6 +954,15 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface
954954
void ReacceptWalletTransactions(bool fFirstLoad = false);
955955
void ResendWalletTransactions(CConnman* connman) override;
956956

957+
struct Balance {
958+
CAmount m_mine_trusted{0}; //!< Trusted, at depth=GetBalance.min_depth or more
959+
CAmount m_mine_untrusted_pending{0}; //!< Untrusted, but in mempool (pending)
960+
CAmount m_mine_immature{0}; //!< Immature coinbases/coinstakes in the main chain
961+
CAmount m_mine_trusted_shield{0}; //!< Trusted shield, at depth=GetBalance.min_depth or more
962+
CAmount m_mine_untrusted_shielded_balance{0}; //!< Untrusted shield, but in mempool (pending)
963+
};
964+
Balance GetBalance(int min_depth = 0) const;
965+
957966
CAmount loopTxsBalance(std::function<void(const uint256&, const CWalletTx&, CAmount&)>method) const;
958967
CAmount GetAvailableBalance(bool fIncludeDelegated = true, bool fIncludeShielded = true) const;
959968
CAmount GetAvailableBalance(isminefilter& filter, bool useCache = false, int minDepth = 1) const;

0 commit comments

Comments
 (0)