diff --git a/src/chain.h b/src/chain.h --- a/src/chain.h +++ b/src/chain.h @@ -262,9 +262,9 @@ } }; -typedef std::unordered_map BlockMap; -extern BlockMap &mapBlockIndex; extern CCriticalSection cs_main; +typedef std::unordered_map BlockMap; +extern BlockMap &mapBlockIndex GUARDED_BY(cs_main); inline CBlockIndex *LookupBlockIndex(const BlockHash &hash) { AssertLockHeld(cs_main); diff --git a/src/validation.h b/src/validation.h --- a/src/validation.h +++ b/src/validation.h @@ -425,7 +425,7 @@ /** * Mark one block file as pruned. */ -void PruneOneBlockFile(const int fileNumber); +void PruneOneBlockFile(const int fileNumber) EXCLUSIVE_LOCKS_REQUIRED(cs_main); /** * Actually unlink the specified files diff --git a/src/validation.cpp b/src/validation.cpp --- a/src/validation.cpp +++ b/src/validation.cpp @@ -124,7 +124,7 @@ public: CChain chainActive; - BlockMap mapBlockIndex; + BlockMap mapBlockIndex GUARDED_BY(cs_main); std::multimap mapBlocksUnlinked; CBlockIndex *pindexBestInvalid = nullptr; CBlockIndex *pindexBestParked = nullptr; diff --git a/src/wallet/test/wallet_tests.cpp b/src/wallet/test/wallet_tests.cpp --- a/src/wallet/test/wallet_tests.cpp +++ b/src/wallet/test/wallet_tests.cpp @@ -33,6 +33,8 @@ wallet.AddKeyPubKey(key, key.GetPubKey()); } +// Note: when backporting PR14957, see PR15321 and ensure both LockAnnotations +// are in place. BOOST_FIXTURE_TEST_CASE(rescan, TestChain100Setup) { auto chain = interfaces::MakeChain(); @@ -43,6 +45,7 @@ CreateAndProcessBlock({}, GetScriptForRawPubKey(coinbaseKey.GetPubKey())); CBlockIndex *newTip = chainActive.Tip(); + LockAnnotation lock(::cs_main); auto locked_chain = chain->lock(); // Verify ScanForWalletTransactions picks up transactions in both the old @@ -235,6 +238,7 @@ SetMockTime(mockTime); CBlockIndex *block = nullptr; if (blockTime > 0) { + LockAnnotation lock(::cs_main); auto locked_chain = wallet.chain().lock(); auto inserted = mapBlockIndex.emplace(BlockHash(GetRandHash()), new CBlockIndex);