Skip to content

Commit

Permalink
wallet: single loop to calculate the currently required balances.
Browse files Browse the repository at this point in the history
future: add watch-only and CS balances distinction when/if they are needed (at the moment, they aren't).

Inspired in btc@fa57411fcba00556ba25d45bca53cc04623da051 .
  • Loading branch information
furszy committed Mar 11, 2021
1 parent 32538ae commit e9c160a
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/interfaces/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ namespace interfaces {

WalletBalances Wallet::getBalances() {
WalletBalances result;
result.balance = m_wallet.GetAvailableBalance();
result.unconfirmed_balance = m_wallet.GetUnconfirmedBalance(ISMINE_SPENDABLE_TRANSPARENT);
result.immature_balance = m_wallet.GetImmatureBalance();
CWallet::Balance balance = m_wallet.GetBalance();
result.balance = balance.m_mine_trusted + balance.m_mine_trusted_shield;
result.unconfirmed_balance = balance.m_mine_untrusted_pending;
result.immature_balance = balance.m_mine_immature;
result.have_watch_only = m_wallet.HaveWatchOnly();
if (result.have_watch_only) {
result.watch_only_balance = m_wallet.GetWatchOnlyBalance();
Expand All @@ -23,8 +24,8 @@ namespace interfaces {
result.delegate_balance = m_wallet.GetDelegatedBalance();
result.coldstaked_balance = m_wallet.GetColdStakingBalance();
}
result.shielded_balance = m_wallet.GetAvailableShieldedBalance();
result.unconfirmed_shielded_balance = m_wallet.GetUnconfirmedShieldedBalance();
result.shielded_balance = balance.m_mine_trusted_shield;
result.unconfirmed_shielded_balance = balance.m_mine_untrusted_shielded_balance;
return result;
}

Expand Down
26 changes: 26 additions & 0 deletions src/wallet/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2055,6 +2055,32 @@ void CWallet::ResendWalletTransactions(CConnman* connman)
* @{
*/

CWallet::Balance CWallet::GetBalance(const int min_depth) const
{
Balance ret;
{
LOCK(cs_wallet);
std::set<uint256> trusted_parents;
for (const auto& entry : mapWallet) {
const CWalletTx& wtx = entry.second;
const bool is_trusted{wtx.IsTrusted()};
const int tx_depth{wtx.GetDepthInMainChain()};
const CAmount tx_credit_mine{wtx.GetAvailableCredit(/* fUseCache */ true, ISMINE_SPENDABLE_TRANSPARENT)};
const CAmount tx_credit_shield_mine{wtx.GetAvailableCredit(/* fUseCache */ true, ISMINE_SPENDABLE_SHIELDED)};
if (is_trusted && tx_depth >= min_depth) {
ret.m_mine_trusted += tx_credit_mine;
ret.m_mine_trusted_shield += tx_credit_shield_mine;
}
if (!is_trusted && tx_depth == 0 && wtx.InMempool()) {
ret.m_mine_untrusted_pending += tx_credit_mine;
ret.m_mine_untrusted_shielded_balance += tx_credit_shield_mine;
}
ret.m_mine_immature += wtx.GetImmatureCredit();
}
}
return ret;
}

CAmount CWallet::loopTxsBalance(std::function<void(const uint256&, const CWalletTx&, CAmount&)> method) const
{
CAmount nTotal = 0;
Expand Down
9 changes: 9 additions & 0 deletions src/wallet/wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -954,6 +954,15 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface
void ReacceptWalletTransactions(bool fFirstLoad = false);
void ResendWalletTransactions(CConnman* connman) override;

struct Balance {
CAmount m_mine_trusted{0}; //!< Trusted, at depth=GetBalance.min_depth or more
CAmount m_mine_untrusted_pending{0}; //!< Untrusted, but in mempool (pending)
CAmount m_mine_immature{0}; //!< Immature coinbases/coinstakes in the main chain
CAmount m_mine_trusted_shield{0}; //!< Trusted shield, at depth=GetBalance.min_depth or more
CAmount m_mine_untrusted_shielded_balance{0}; //!< Untrusted shield, but in mempool (pending)
};
Balance GetBalance(int min_depth = 0) const;

CAmount loopTxsBalance(std::function<void(const uint256&, const CWalletTx&, CAmount&)>method) const;
CAmount GetAvailableBalance(bool fIncludeDelegated = true, bool fIncludeShielded = true) const;
CAmount GetAvailableBalance(isminefilter& filter, bool useCache = false, int minDepth = 1) const;
Expand Down

0 comments on commit e9c160a

Please sign in to comment.