| 2351 | } |
| 2352 | |
| 2353 | std::map<CTxDestination, std::vector<COutput>> CWallet::ListCoins() const |
| 2354 | { |
| 2355 | // TODO: Add AssertLockHeld(cs_wallet) here. |
| 2356 | // |
| 2357 | // Because the return value from this function contains pointers to |
| 2358 | // CWalletTx objects, callers to this function really should acquire the |
| 2359 | // cs_wallet lock before calling it. However, the current caller doesn't |
| 2360 | // acquire this lock yet. There was an attempt to add the missing lock in |
| 2361 | // https://github.com/bitcoin/bitcoin/pull/10340, but that change has been |
| 2362 | // postponed until after https://github.com/bitcoin/bitcoin/pull/10244 to |
| 2363 | // avoid adding some extra complexity to the Qt code. |
| 2364 | |
| 2365 | std::map<CTxDestination, std::vector<COutput>> result; |
| 2366 | std::vector<COutput> availableCoins; |
| 2367 | |
| 2368 | LOCK2(cs_main, cs_wallet); |
| 2369 | AvailableCoins(availableCoins); |
| 2370 | |
| 2371 | for (auto& coin : availableCoins) { |
| 2372 | CTxDestination address; |
| 2373 | if (coin.fSpendable && |
| 2374 | ExtractDestination(FindNonChangeParentOutput(*coin.tx->tx, coin.i).scriptPubKey, address)) { |
| 2375 | result[address].emplace_back(std::move(coin)); |
| 2376 | } |
| 2377 | } |
| 2378 | |
| 2379 | std::vector<COutPoint> lockedCoins; |
| 2380 | ListLockedCoins(lockedCoins); |
| 2381 | for (const auto& output : lockedCoins) { |
| 2382 | auto it = mapWallet.find(output.hash); |
| 2383 | if (it != mapWallet.end()) { |
| 2384 | int depth = it->second.GetDepthInMainChain(); |
| 2385 | if (depth >= 0 && output.n < it->second.tx->vout.size() && |
| 2386 | IsMine(it->second.tx->vout[output.n]) == ISMINE_SPENDABLE) { |
| 2387 | CTxDestination address; |
| 2388 | if (ExtractDestination(FindNonChangeParentOutput(*it->second.tx, output.n).scriptPubKey, address)) { |
| 2389 | result[address].emplace_back( |
| 2390 | &it->second, output.n, depth, true /* spendable */, true /* solvable */, false /* safe */); |
| 2391 | } |
| 2392 | } |
| 2393 | } |
| 2394 | } |
| 2395 | |
| 2396 | return result; |
| 2397 | } |
| 2398 | |
| 2399 | const CTxOut& CWallet::FindNonChangeParentOutput(const CTransaction& tx, int output) const |
| 2400 | { |