Skip to content

Commit cfa948d

Browse files
committed
Bugfix: Wallet: When getting balance w/ min_conf, include UTXOs that were spent more recently
1 parent 4996d98 commit cfa948d

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

src/wallet/wallet.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,7 @@ void CWallet::SyncMetaData(std::pair<TxSpends::iterator, TxSpends::iterator> ran
594594
* Outpoint is spent if any non-conflicted transaction
595595
* spends it:
596596
*/
597-
bool CWallet::IsSpent(interfaces::Chain::Lock& locked_chain, const uint256& hash, unsigned int n) const
597+
bool CWallet::IsSpent(interfaces::Chain::Lock& locked_chain, const uint256& hash, unsigned int n, const int min_depth) const
598598
{
599599
const COutPoint outpoint(hash, n);
600600
std::pair<TxSpends::const_iterator, TxSpends::const_iterator> range;
@@ -606,6 +606,7 @@ bool CWallet::IsSpent(interfaces::Chain::Lock& locked_chain, const uint256& hash
606606
std::map<uint256, CWalletTx>::const_iterator mit = mapWallet.find(wtxid);
607607
if (mit != mapWallet.end()) {
608608
int depth = mit->second.GetDepthInMainChain(locked_chain);
609+
if (depth < min_depth) continue;
609610
if (depth > 0 || (depth == 0 && !mit->second.isAbandoned()))
610611
return true; // Spent
611612
}
@@ -1861,7 +1862,7 @@ CAmount CWalletTx::GetImmatureCredit(interfaces::Chain::Lock& locked_chain, bool
18611862
return 0;
18621863
}
18631864

1864-
CAmount CWalletTx::GetAvailableCredit(interfaces::Chain::Lock& locked_chain, bool fUseCache, const isminefilter& filter) const
1865+
CAmount CWalletTx::GetAvailableCredit(interfaces::Chain::Lock& locked_chain, bool fUseCache, const isminefilter& filter, const int min_depth) const
18651866
{
18661867
if (pwallet == nullptr)
18671868
return 0;
@@ -1889,7 +1890,7 @@ CAmount CWalletTx::GetAvailableCredit(interfaces::Chain::Lock& locked_chain, boo
18891890
uint256 hashTx = GetHash();
18901891
for (unsigned int i = 0; i < tx->vout.size(); i++)
18911892
{
1892-
if (!pwallet->IsSpent(locked_chain, hashTx, i))
1893+
if (!pwallet->IsSpent(locked_chain, hashTx, i, min_depth))
18931894
{
18941895
const CTxOut &txout = tx->vout[i];
18951896
nCredit += pwallet->GetCredit(txout, filter);
@@ -2054,7 +2055,7 @@ CAmount CWallet::GetBalance(const isminefilter& filter, const int min_depth, con
20542055

20552056
if (pcoin->GetDepthInMainChain(*locked_chain) < min_depth) continue;
20562057

2057-
nTotal += pcoin->GetAvailableCredit(*locked_chain, true, filter);
2058+
nTotal += pcoin->GetAvailableCredit(*locked_chain, true, filter, min_depth);
20582059
}
20592060
}
20602061

src/wallet/wallet.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ class CWalletTx : public CMerkleTx
485485
// annotation "EXCLUSIVE_LOCKS_REQUIRED(cs_main, pwallet->cs_wallet)". The
486486
// annotation "NO_THREAD_SAFETY_ANALYSIS" was temporarily added to avoid
487487
// having to resolve the issue of member access into incomplete type CWallet.
488-
CAmount GetAvailableCredit(interfaces::Chain::Lock& locked_chain, bool fUseCache=true, const isminefilter& filter=ISMINE_SPENDABLE) const NO_THREAD_SAFETY_ANALYSIS;
488+
CAmount GetAvailableCredit(interfaces::Chain::Lock& locked_chain, bool fUseCache=true, const isminefilter& filter=ISMINE_SPENDABLE, int min_depth = 0) const NO_THREAD_SAFETY_ANALYSIS;
489489
CAmount GetImmatureWatchOnlyCredit(interfaces::Chain::Lock& locked_chain, const bool fUseCache=true) const;
490490
CAmount GetChange() const;
491491

@@ -815,7 +815,7 @@ class CWallet final : public CCryptoKeyStore, public CValidationInterface
815815
bool SelectCoinsMinConf(const CAmount& nTargetValue, const CoinEligibilityFilter& eligibility_filter, std::vector<OutputGroup> groups,
816816
std::set<CInputCoin>& setCoinsRet, CAmount& nValueRet, const CoinSelectionParams& coin_selection_params, bool& bnb_used) const;
817817

818-
bool IsSpent(interfaces::Chain::Lock& locked_chain, const uint256& hash, unsigned int n) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
818+
bool IsSpent(interfaces::Chain::Lock& locked_chain, const uint256& hash, unsigned int n, int min_depth = 0) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
819819
std::vector<OutputGroup> GroupOutputs(const std::vector<COutput>& outputs, bool single_coin) const;
820820

821821
bool IsLockedCoin(uint256 hash, unsigned int n) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);

0 commit comments

Comments
 (0)