Skip to content

Commit 1beea7a

Browse files
committed
[wallet] Make CWallet::ListCoins atomic
1 parent 4cad916 commit 1beea7a

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

src/wallet/test/wallet_tests.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -676,18 +676,24 @@ BOOST_FIXTURE_TEST_CASE(ListCoins, ListCoinsTestingSetup)
676676
BOOST_CHECK_EQUAL(list.begin()->second.size(), 2);
677677

678678
// Lock both coins. Confirm number of available coins drops to 0.
679-
std::vector<COutput> available;
680-
wallet->AvailableCoins(available);
681-
BOOST_CHECK_EQUAL(available.size(), 2);
679+
{
680+
LOCK2(cs_main, wallet->cs_wallet);
681+
std::vector<COutput> available;
682+
wallet->AvailableCoins(available);
683+
BOOST_CHECK_EQUAL(available.size(), 2);
684+
}
682685
for (const auto& group : list) {
683686
for (const auto& coin : group.second) {
684687
LOCK(wallet->cs_wallet);
685688
wallet->LockCoin(COutPoint(coin.tx->GetHash(), coin.i));
686689
}
687690
}
688-
wallet->AvailableCoins(available);
689-
BOOST_CHECK_EQUAL(available.size(), 0);
690-
691+
{
692+
LOCK2(cs_main, wallet->cs_wallet);
693+
std::vector<COutput> available;
694+
wallet->AvailableCoins(available);
695+
BOOST_CHECK_EQUAL(available.size(), 0);
696+
}
691697
// Confirm ListCoins still returns same result as before, despite coins
692698
// being locked.
693699
list = wallet->ListCoins();

src/wallet/wallet.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2198,11 +2198,12 @@ CAmount CWallet::GetAvailableBalance(const CCoinControl* coinControl) const
21982198

21992199
void CWallet::AvailableCoins(std::vector<COutput> &vCoins, bool fOnlySafe, const CCoinControl *coinControl, const CAmount &nMinimumAmount, const CAmount &nMaximumAmount, const CAmount &nMinimumSumAmount, const uint64_t nMaximumCount, const int nMinDepth, const int nMaxDepth) const
22002200
{
2201+
AssertLockHeld(cs_main);
2202+
AssertLockHeld(cs_wallet);
2203+
22012204
vCoins.clear();
22022205

22032206
{
2204-
LOCK2(cs_main, cs_wallet);
2205-
22062207
CAmount nTotal = 0;
22072208

22082209
for (const auto& entry : mapWallet)
@@ -2320,11 +2321,11 @@ std::map<CTxDestination, std::vector<COutput>> CWallet::ListCoins() const
23202321
// avoid adding some extra complexity to the Qt code.
23212322

23222323
std::map<CTxDestination, std::vector<COutput>> result;
2323-
23242324
std::vector<COutput> availableCoins;
2325-
AvailableCoins(availableCoins);
23262325

23272326
LOCK2(cs_main, cs_wallet);
2327+
AvailableCoins(availableCoins);
2328+
23282329
for (auto& coin : availableCoins) {
23292330
CTxDestination address;
23302331
if (coin.fSpendable &&

0 commit comments

Comments
 (0)