Skip to content

Commit

Permalink
[wallet] Make CWallet::ListCoins atomic
Browse files Browse the repository at this point in the history
  • Loading branch information
promag committed Feb 8, 2018
1 parent 4cad916 commit 1beea7a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
18 changes: 12 additions & 6 deletions src/wallet/test/wallet_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -676,18 +676,24 @@ BOOST_FIXTURE_TEST_CASE(ListCoins, ListCoinsTestingSetup)
BOOST_CHECK_EQUAL(list.begin()->second.size(), 2);

// Lock both coins. Confirm number of available coins drops to 0.
std::vector<COutput> available;
wallet->AvailableCoins(available);
BOOST_CHECK_EQUAL(available.size(), 2);
{
LOCK2(cs_main, wallet->cs_wallet);
std::vector<COutput> available;
wallet->AvailableCoins(available);
BOOST_CHECK_EQUAL(available.size(), 2);
}
for (const auto& group : list) {
for (const auto& coin : group.second) {
LOCK(wallet->cs_wallet);
wallet->LockCoin(COutPoint(coin.tx->GetHash(), coin.i));
}
}
wallet->AvailableCoins(available);
BOOST_CHECK_EQUAL(available.size(), 0);

{
LOCK2(cs_main, wallet->cs_wallet);
std::vector<COutput> available;
wallet->AvailableCoins(available);
BOOST_CHECK_EQUAL(available.size(), 0);
}
// Confirm ListCoins still returns same result as before, despite coins
// being locked.
list = wallet->ListCoins();
Expand Down
9 changes: 5 additions & 4 deletions src/wallet/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2198,11 +2198,12 @@ CAmount CWallet::GetAvailableBalance(const CCoinControl* coinControl) const

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
{
AssertLockHeld(cs_main);
AssertLockHeld(cs_wallet);

vCoins.clear();

{
LOCK2(cs_main, cs_wallet);

CAmount nTotal = 0;

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

std::map<CTxDestination, std::vector<COutput>> result;

std::vector<COutput> availableCoins;
AvailableCoins(availableCoins);

LOCK2(cs_main, cs_wallet);
AvailableCoins(availableCoins);

for (auto& coin : availableCoins) {
CTxDestination address;
if (coin.fSpendable &&
Expand Down

0 comments on commit 1beea7a

Please sign in to comment.