Skip to content

Commit 4279da4

Browse files
committed
[wallet] GetBalance can take an isminefilter filter.
GetBalance() can now take an ismine filter, which is passed down to GetAvailableCredit. This allows GetBalance to be used to get watch-only balances.
1 parent d96bdd7 commit 4279da4

File tree

2 files changed

+23
-11
lines changed

2 files changed

+23
-11
lines changed

src/wallet/wallet.cpp

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1968,7 +1968,7 @@ CAmount CWalletTx::GetImmatureCredit(bool fUseCache) const
19681968
return 0;
19691969
}
19701970

1971-
CAmount CWalletTx::GetAvailableCredit(bool fUseCache) const
1971+
CAmount CWalletTx::GetAvailableCredit(bool fUseCache, const isminefilter& filter) const
19721972
{
19731973
if (pwallet == nullptr)
19741974
return 0;
@@ -1977,8 +1977,17 @@ CAmount CWalletTx::GetAvailableCredit(bool fUseCache) const
19771977
if (IsCoinBase() && GetBlocksToMaturity() > 0)
19781978
return 0;
19791979

1980-
if (fUseCache && fAvailableCreditCached)
1981-
return nAvailableCreditCached;
1980+
CAmount* cache = nullptr;
1981+
bool* cache_used = nullptr;
1982+
1983+
if (filter == ISMINE_SPENDABLE) {
1984+
cache = &nAvailableCreditCached;
1985+
cache_used = &fAvailableCreditCached;
1986+
}
1987+
1988+
if (fUseCache && cache_used && *cache_used) {
1989+
return *cache;
1990+
}
19821991

19831992
CAmount nCredit = 0;
19841993
uint256 hashTx = GetHash();
@@ -1987,14 +1996,16 @@ CAmount CWalletTx::GetAvailableCredit(bool fUseCache) const
19871996
if (!pwallet->IsSpent(hashTx, i))
19881997
{
19891998
const CTxOut &txout = tx->vout[i];
1990-
nCredit += pwallet->GetCredit(txout, ISMINE_SPENDABLE);
1999+
nCredit += pwallet->GetCredit(txout, filter);
19912000
if (!MoneyRange(nCredit))
19922001
throw std::runtime_error(std::string(__func__) + " : value out of range");
19932002
}
19942003
}
19952004

1996-
nAvailableCreditCached = nCredit;
1997-
fAvailableCreditCached = true;
2005+
if (cache) {
2006+
*cache = nCredit;
2007+
*cache_used = true;
2008+
}
19982009
return nCredit;
19992010
}
20002011

@@ -2154,16 +2165,17 @@ void CWallet::ResendWalletTransactions(int64_t nBestBlockTime, CConnman* connman
21542165
*/
21552166

21562167

2157-
CAmount CWallet::GetBalance() const
2168+
CAmount CWallet::GetBalance(const isminefilter& filter) const
21582169
{
21592170
CAmount nTotal = 0;
21602171
{
21612172
LOCK2(cs_main, cs_wallet);
21622173
for (const auto& entry : mapWallet)
21632174
{
21642175
const CWalletTx* pcoin = &entry.second;
2165-
if (pcoin->IsTrusted())
2166-
nTotal += pcoin->GetAvailableCredit();
2176+
if (pcoin->IsTrusted()) {
2177+
nTotal += pcoin->GetAvailableCredit(true, filter);
2178+
}
21672179
}
21682180
}
21692181

src/wallet/wallet.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ class CWalletTx : public CMerkleTx
460460
CAmount GetDebit(const isminefilter& filter) const;
461461
CAmount GetCredit(const isminefilter& filter) const;
462462
CAmount GetImmatureCredit(bool fUseCache=true) const;
463-
CAmount GetAvailableCredit(bool fUseCache=true) const;
463+
CAmount GetAvailableCredit(bool fUseCache=true, const isminefilter& filter=ISMINE_SPENDABLE) const;
464464
CAmount GetImmatureWatchOnlyCredit(const bool fUseCache=true) const;
465465
CAmount GetAvailableWatchOnlyCredit(const bool fUseCache=true) const;
466466
CAmount GetChange() const;
@@ -944,7 +944,7 @@ class CWallet final : public CCryptoKeyStore, public CValidationInterface
944944
void ResendWalletTransactions(int64_t nBestBlockTime, CConnman* connman) override;
945945
// ResendWalletTransactionsBefore may only be called if fBroadcastTransactions!
946946
std::vector<uint256> ResendWalletTransactionsBefore(int64_t nTime, CConnman* connman);
947-
CAmount GetBalance() const;
947+
CAmount GetBalance(const isminefilter& filter=ISMINE_SPENDABLE) const;
948948
CAmount GetUnconfirmedBalance() const;
949949
CAmount GetImmatureBalance() const;
950950
CAmount GetWatchOnlyBalance() const;

0 commit comments

Comments
 (0)