MCPcopy Create free account
hub / github.com/bitcoin/bitcoin / GetBalance

Function GetBalance

src/wallet/receive.cpp:245–295  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

243}
244
245Balance GetBalance(const CWallet& wallet, const int min_depth, bool avoid_reuse, bool include_nonmempool)
246{
247 Balance ret;
248 bool allow_used_addresses = !avoid_reuse || !wallet.IsWalletFlagSet(WALLET_FLAG_AVOID_REUSE);
249 {
250 LOCK(wallet.cs_wallet);
251 std::set<Txid> trusted_parents;
252 for (const auto& [outpoint, txo] : wallet.GetTXOs()) {
253 const CWalletTx& wtx = txo.GetWalletTx();
254
255 const bool is_trusted{CachedTxIsTrusted(wallet, wtx, trusted_parents)};
256 const int tx_depth{wallet.GetTxDepthInMainChain(wtx)};
257
258 bool nonmempool_spent = false;
259 switch (wallet.HowSpent(outpoint)) {
260 case CWallet::SpendType::CONFIRMED:
261 case CWallet::SpendType::MEMPOOL:
262 // treat as spent; ignore
263 break;
264 case CWallet::SpendType::NONMEMPOOL:
265 if (!include_nonmempool) break;
266 nonmempool_spent = true;
267 [[fallthrough]];
268 case CWallet::SpendType::UNSPENT:
269 CAmount* bucket = nullptr;
270
271 // Set the amounts in the return object
272 if (wallet.IsTxImmatureCoinBase(wtx) && wtx.isConfirmed()) {
273 bucket = &ret.m_mine_immature;
274 } else if (is_trusted && tx_depth >= min_depth) {
275 bucket = &ret.m_mine_trusted;
276 } else if (!is_trusted && wtx.InMempool()) {
277 bucket = &ret.m_mine_untrusted_pending;
278 }
279 if (bucket) {
280 // Get the amounts for mine
281 CAmount credit_mine = txo.GetTxOut().nValue;
282
283 if (!allow_used_addresses && wallet.IsSpentKey(txo.GetTxOut().scriptPubKey)) {
284 bucket = &ret.m_mine_used;
285 }
286 *bucket += credit_mine;
287 if (nonmempool_spent) {
288 ret.m_mine_nonmempool -= credit_mine;
289 }
290 }
291 }
292 }
293 }
294 return ret;
295}
296
297std::map<CTxDestination, CAmount> GetAddressBalances(const CWallet& wallet)
298{

Callers 6

getBalancesMethod · 0.85
getBalanceMethod · 0.85
BOOST_FIXTURE_TEST_CASEFunction · 0.85
getbalanceFunction · 0.85
getbalancesFunction · 0.85
WalletBalanceFunction · 0.85

Calls 10

CachedTxIsTrustedFunction · 0.85
IsWalletFlagSetMethod · 0.80
GetWalletTxMethod · 0.80
GetTxDepthInMainChainMethod · 0.80
HowSpentMethod · 0.80
IsTxImmatureCoinBaseMethod · 0.80
isConfirmedMethod · 0.80
InMempoolMethod · 0.80
GetTxOutMethod · 0.80
IsSpentKeyMethod · 0.80

Tested by 1

BOOST_FIXTURE_TEST_CASEFunction · 0.68