Skip to content

Commit 545e85e

Browse files
committed
Add AssertLockHeld assertions in CWallet::ListCoins
1 parent 3cf76c2 commit 545e85e

File tree

2 files changed

+15
-13
lines changed

2 files changed

+15
-13
lines changed

src/wallet/test/wallet_tests.cpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,11 @@ BOOST_FIXTURE_TEST_CASE(ListCoins, ListCoinsTestingSetup)
317317

318318
// Confirm ListCoins initially returns 1 coin grouped under coinbaseKey
319319
// address.
320-
auto list = wallet->ListCoins();
320+
std::map<CTxDestination, std::vector<COutput>> list;
321+
{
322+
LOCK2(cs_main, wallet->cs_wallet);
323+
list = wallet->ListCoins();
324+
}
321325
BOOST_CHECK_EQUAL(list.size(), 1U);
322326
BOOST_CHECK_EQUAL(boost::get<CKeyID>(list.begin()->first).ToString(), coinbaseAddress);
323327
BOOST_CHECK_EQUAL(list.begin()->second.size(), 1U);
@@ -330,7 +334,10 @@ BOOST_FIXTURE_TEST_CASE(ListCoins, ListCoinsTestingSetup)
330334
// coinbaseKey pubkey, even though the change address has a different
331335
// pubkey.
332336
AddTx(CRecipient{GetScriptForRawPubKey({}), 1 * COIN, false /* subtract fee */});
333-
list = wallet->ListCoins();
337+
{
338+
LOCK2(cs_main, wallet->cs_wallet);
339+
list = wallet->ListCoins();
340+
}
334341
BOOST_CHECK_EQUAL(list.size(), 1U);
335342
BOOST_CHECK_EQUAL(boost::get<CKeyID>(list.begin()->first).ToString(), coinbaseAddress);
336343
BOOST_CHECK_EQUAL(list.begin()->second.size(), 2U);
@@ -356,7 +363,10 @@ BOOST_FIXTURE_TEST_CASE(ListCoins, ListCoinsTestingSetup)
356363
}
357364
// Confirm ListCoins still returns same result as before, despite coins
358365
// being locked.
359-
list = wallet->ListCoins();
366+
{
367+
LOCK2(cs_main, wallet->cs_wallet);
368+
list = wallet->ListCoins();
369+
}
360370
BOOST_CHECK_EQUAL(list.size(), 1U);
361371
BOOST_CHECK_EQUAL(boost::get<CKeyID>(list.begin()->first).ToString(), coinbaseAddress);
362372
BOOST_CHECK_EQUAL(list.begin()->second.size(), 2U);

src/wallet/wallet.cpp

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2364,20 +2364,12 @@ void CWallet::AvailableCoins(std::vector<COutput> &vCoins, bool fOnlySafe, const
23642364

23652365
std::map<CTxDestination, std::vector<COutput>> CWallet::ListCoins() const
23662366
{
2367-
// TODO: Add AssertLockHeld(cs_wallet) here.
2368-
//
2369-
// Because the return value from this function contains pointers to
2370-
// CWalletTx objects, callers to this function really should acquire the
2371-
// cs_wallet lock before calling it. However, the current caller doesn't
2372-
// acquire this lock yet. There was an attempt to add the missing lock in
2373-
// https://github.com/bitcoin/bitcoin/pull/10340, but that change has been
2374-
// postponed until after https://github.com/bitcoin/bitcoin/pull/10244 to
2375-
// avoid adding some extra complexity to the Qt code.
2367+
AssertLockHeld(cs_main);
2368+
AssertLockHeld(cs_wallet);
23762369

23772370
std::map<CTxDestination, std::vector<COutput>> result;
23782371
std::vector<COutput> availableCoins;
23792372

2380-
LOCK2(cs_main, cs_wallet);
23812373
AvailableCoins(availableCoins);
23822374

23832375
for (auto& coin : availableCoins) {

0 commit comments

Comments
 (0)