Skip to content

Commit b012bbe

Browse files
author
MarcoFalke
committed
Merge #10605: Add AssertLockHeld assertions in CWallet::ListCoins
62b6f0f Add EXCLUSIVE_LOCKS_REQUIRED to CWallet::ListCoins (Russell Yanofsky) 545e85e Add AssertLockHeld assertions in CWallet::ListCoins (Russell Yanofsky) Pull request description: Fixes TODO from #10295 Tree-SHA512: 2dd03a8217e5e1313aa2119cb530e0c0daf3ae3a751b6fdec611df57b8090201a90b52ff05f8f696e978a1344aaf21989d67a03beb5ef6ef79b77be38d04b451
2 parents 709a15b + 62b6f0f commit b012bbe

File tree

3 files changed

+16
-14
lines changed

3 files changed

+16
-14
lines changed

src/wallet/test/wallet_tests.cpp

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

321321
// Confirm ListCoins initially returns 1 coin grouped under coinbaseKey
322322
// address.
323-
auto list = wallet->ListCoins();
323+
std::map<CTxDestination, std::vector<COutput>> list;
324+
{
325+
LOCK2(cs_main, wallet->cs_wallet);
326+
list = wallet->ListCoins();
327+
}
324328
BOOST_CHECK_EQUAL(list.size(), 1U);
325329
BOOST_CHECK_EQUAL(boost::get<CKeyID>(list.begin()->first).ToString(), coinbaseAddress);
326330
BOOST_CHECK_EQUAL(list.begin()->second.size(), 1U);
@@ -333,7 +337,10 @@ BOOST_FIXTURE_TEST_CASE(ListCoins, ListCoinsTestingSetup)
333337
// coinbaseKey pubkey, even though the change address has a different
334338
// pubkey.
335339
AddTx(CRecipient{GetScriptForRawPubKey({}), 1 * COIN, false /* subtract fee */});
336-
list = wallet->ListCoins();
340+
{
341+
LOCK2(cs_main, wallet->cs_wallet);
342+
list = wallet->ListCoins();
343+
}
337344
BOOST_CHECK_EQUAL(list.size(), 1U);
338345
BOOST_CHECK_EQUAL(boost::get<CKeyID>(list.begin()->first).ToString(), coinbaseAddress);
339346
BOOST_CHECK_EQUAL(list.begin()->second.size(), 2U);
@@ -359,7 +366,10 @@ BOOST_FIXTURE_TEST_CASE(ListCoins, ListCoinsTestingSetup)
359366
}
360367
// Confirm ListCoins still returns same result as before, despite coins
361368
// being locked.
362-
list = wallet->ListCoins();
369+
{
370+
LOCK2(cs_main, wallet->cs_wallet);
371+
list = wallet->ListCoins();
372+
}
363373
BOOST_CHECK_EQUAL(list.size(), 1U);
364374
BOOST_CHECK_EQUAL(boost::get<CKeyID>(list.begin()->first).ToString(), coinbaseAddress);
365375
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
@@ -2252,20 +2252,12 @@ void CWallet::AvailableCoins(std::vector<COutput> &vCoins, bool fOnlySafe, const
22522252

22532253
std::map<CTxDestination, std::vector<COutput>> CWallet::ListCoins() const
22542254
{
2255-
// TODO: Add AssertLockHeld(cs_wallet) here.
2256-
//
2257-
// Because the return value from this function contains pointers to
2258-
// CWalletTx objects, callers to this function really should acquire the
2259-
// cs_wallet lock before calling it. However, the current caller doesn't
2260-
// acquire this lock yet. There was an attempt to add the missing lock in
2261-
// https://github.com/bitcoin/bitcoin/pull/10340, but that change has been
2262-
// postponed until after https://github.com/bitcoin/bitcoin/pull/10244 to
2263-
// avoid adding some extra complexity to the Qt code.
2255+
AssertLockHeld(cs_main);
2256+
AssertLockHeld(cs_wallet);
22642257

22652258
std::map<CTxDestination, std::vector<COutput>> result;
22662259
std::vector<COutput> availableCoins;
22672260

2268-
LOCK2(cs_main, cs_wallet);
22692261
AvailableCoins(availableCoins);
22702262

22712263
for (auto& coin : availableCoins) {

src/wallet/wallet.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -764,7 +764,7 @@ class CWallet final : public CCryptoKeyStore, public CValidationInterface
764764
/**
765765
* Return list of available coins and locked coins grouped by non-change output address.
766766
*/
767-
std::map<CTxDestination, std::vector<COutput>> ListCoins() const;
767+
std::map<CTxDestination, std::vector<COutput>> ListCoins() const EXCLUSIVE_LOCKS_REQUIRED(cs_main, cs_wallet);
768768

769769
/**
770770
* Find non-change parent output.

0 commit comments

Comments
 (0)