Skip to content

Commit 3639112

Browse files
MarcoFalkePastaPastaPasta
MarcoFalke
authored andcommitted
Merge bitcoin#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 bitcoin#10295 Tree-SHA512: 2dd03a8217e5e1313aa2119cb530e0c0daf3ae3a751b6fdec611df57b8090201a90b52ff05f8f696e978a1344aaf21989d67a03beb5ef6ef79b77be38d04b451
1 parent b5a3283 commit 3639112

File tree

3 files changed

+16
-14
lines changed

3 files changed

+16
-14
lines changed

src/wallet/test/wallet_tests.cpp

+13-3
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,11 @@ BOOST_FIXTURE_TEST_CASE(ListCoins, ListCoinsTestingSetup)
324324

325325
// Confirm ListCoins initially returns 1 coin grouped under coinbaseKey
326326
// address.
327-
auto list = wallet->ListCoins();
327+
std::map<CTxDestination, std::vector<COutput>> list;
328+
{
329+
LOCK2(cs_main, wallet->cs_wallet);
330+
list = wallet->ListCoins();
331+
}
328332
BOOST_CHECK_EQUAL(list.size(), 1U);
329333
BOOST_CHECK_EQUAL(boost::get<CKeyID>(list.begin()->first).ToString(), coinbaseAddress);
330334
BOOST_CHECK_EQUAL(list.begin()->second.size(), 1U);
@@ -337,7 +341,10 @@ BOOST_FIXTURE_TEST_CASE(ListCoins, ListCoinsTestingSetup)
337341
// coinbaseKey pubkey, even though the change address has a different
338342
// pubkey.
339343
AddTx(CRecipient{GetScriptForRawPubKey({}), 1 * COIN, false /* subtract fee */});
340-
list = wallet->ListCoins();
344+
{
345+
LOCK2(cs_main, wallet->cs_wallet);
346+
list = wallet->ListCoins();
347+
}
341348
BOOST_CHECK_EQUAL(list.size(), 1U);
342349
BOOST_CHECK_EQUAL(boost::get<CKeyID>(list.begin()->first).ToString(), coinbaseAddress);
343350
BOOST_CHECK_EQUAL(list.begin()->second.size(), 2U);
@@ -363,7 +370,10 @@ BOOST_FIXTURE_TEST_CASE(ListCoins, ListCoinsTestingSetup)
363370
}
364371
// Confirm ListCoins still returns same result as before, despite coins
365372
// being locked.
366-
list = wallet->ListCoins();
373+
{
374+
LOCK2(cs_main, wallet->cs_wallet);
375+
list = wallet->ListCoins();
376+
}
367377
BOOST_CHECK_EQUAL(list.size(), 1U);
368378
BOOST_CHECK_EQUAL(boost::get<CKeyID>(list.begin()->first).ToString(), coinbaseAddress);
369379
BOOST_CHECK_EQUAL(list.begin()->second.size(), 2U);

src/wallet/wallet.cpp

+2-10
Original file line numberDiff line numberDiff line change
@@ -3010,20 +3010,12 @@ void CWallet::AvailableCoins(std::vector<COutput> &vCoins, bool fOnlySafe, const
30103010

30113011
std::map<CTxDestination, std::vector<COutput>> CWallet::ListCoins() const
30123012
{
3013-
// TODO: Add AssertLockHeld(cs_wallet) here.
3014-
//
3015-
// Because the return value from this function contains pointers to
3016-
// CWalletTx objects, callers to this function really should acquire the
3017-
// cs_wallet lock before calling it. However, the current caller doesn't
3018-
// acquire this lock yet. There was an attempt to add the missing lock in
3019-
// https://github.com/bitcoin/bitcoin/pull/10340, but that change has been
3020-
// postponed until after https://github.com/bitcoin/bitcoin/pull/10244 to
3021-
// avoid adding some extra complexity to the Qt code.
3013+
AssertLockHeld(cs_main);
3014+
AssertLockHeld(cs_wallet);
30223015

30233016
std::map<CTxDestination, std::vector<COutput>> result;
30243017
std::vector<COutput> availableCoins;
30253018

3026-
LOCK2(cs_main, cs_wallet);
30273019
AvailableCoins(availableCoins);
30283020

30293021
for (auto& coin : availableCoins) {

src/wallet/wallet.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -887,7 +887,7 @@ class CWallet final : public CCryptoKeyStore, public CValidationInterface
887887
/**
888888
* Return list of available coins and locked coins grouped by non-change output address.
889889
*/
890-
std::map<CTxDestination, std::vector<COutput>> ListCoins() const;
890+
std::map<CTxDestination, std::vector<COutput>> ListCoins() const EXCLUSIVE_LOCKS_REQUIRED(cs_main, cs_wallet);
891891

892892
/**
893893
* Find non-change parent output.

0 commit comments

Comments
 (0)