diff --git a/src/interfaces/chain.cpp b/src/interfaces/chain.cpp index 23b33ce076..a2bc2a006a 100644 --- a/src/interfaces/chain.cpp +++ b/src/interfaces/chain.cpp @@ -22,6 +22,7 @@ class LockImpl : public Chain::Lock { Optional getHeight() override { + LockAnnotation lock(::cs_main); int height = ::ChainActive().Height(); if (height >= 0) { return height; @@ -30,6 +31,7 @@ class LockImpl : public Chain::Lock } Optional getBlockHeight(const uint256& hash) override { + LockAnnotation lock(::cs_main); CBlockIndex* block = LookupBlockIndex(hash); if (block && ::ChainActive().Contains(block)) { return block->nHeight; @@ -44,29 +46,34 @@ class LockImpl : public Chain::Lock } uint256 getBlockHash(int height) override { + LockAnnotation lock(::cs_main); CBlockIndex* block = ::ChainActive()[height]; assert(block != nullptr); return block->GetBlockHash(); } int64_t getBlockTime(int height) override { + LockAnnotation lock(::cs_main); CBlockIndex* block = ::ChainActive()[height]; assert(block != nullptr); return block->GetBlockTime(); } int64_t getBlockMedianTimePast(int height) override { + LockAnnotation lock(::cs_main); CBlockIndex* block = ::ChainActive()[height]; assert(block != nullptr); return block->GetMedianTimePast(); } bool haveBlockOnDisk(int height) override { + LockAnnotation lock(::cs_main); CBlockIndex* block = ::ChainActive()[height]; return block && ((block->nStatus & BLOCK_HAVE_DATA) != 0) && block->nTx > 0; } Optional findFirstBlockWithTime(int64_t time, uint256* hash) override { + LockAnnotation lock(::cs_main); CBlockIndex* block = ::ChainActive().FindEarliestAtLeast(time); if (block) { if (hash) *hash = block->GetBlockHash(); @@ -90,6 +97,7 @@ class LockImpl : public Chain::Lock } Optional findPruned(int start_height, Optional stop_height) override { + LockAnnotation lock(::cs_main); if (::fPruneMode) { CBlockIndex* block = stop_height ? ::ChainActive()[*stop_height] : ::ChainActive().Tip(); while (block && block->nHeight >= start_height) { @@ -103,6 +111,7 @@ class LockImpl : public Chain::Lock } Optional findFork(const uint256& hash, Optional* height) override { + LockAnnotation lock(::cs_main); const CBlockIndex* block = LookupBlockIndex(hash); const CBlockIndex* fork = block ? ::ChainActive().FindFork(block) : nullptr; if (height) { @@ -119,11 +128,16 @@ class LockImpl : public Chain::Lock } bool isPotentialTip(const uint256& hash) override { + LockAnnotation lock(::cs_main); if (::ChainActive().Tip()->GetBlockHash() == hash) return true; CBlockIndex* block = LookupBlockIndex(hash); return block && block->GetAncestor(::ChainActive().Height()) == ::ChainActive().Tip(); } - CBlockLocator getTipLocator() override { return ::ChainActive().GetLocator(); } + CBlockLocator getTipLocator() override + { + LockAnnotation lock(::cs_main); + return ::ChainActive().GetLocator(); + } Optional findLocatorFork(const CBlockLocator& locator) override { LockAnnotation lock(::cs_main); diff --git a/src/wallet/test/wallet_tests.cpp b/src/wallet/test/wallet_tests.cpp index 3c3526eec2..5579ee30d8 100644 --- a/src/wallet/test/wallet_tests.cpp +++ b/src/wallet/test/wallet_tests.cpp @@ -14,6 +14,7 @@ #include #include #include +#include #include #include #include @@ -46,7 +47,7 @@ BOOST_FIXTURE_TEST_CASE(scan_for_wallet_transactions, TestChain100Setup) auto chain = interfaces::MakeChain(); auto locked_chain = chain->lock(); - LockAnnotation lock(::cs_main); // for PruneOneBlockFile + LockAnnotation lock(::cs_main); // Verify ScanForWalletTransactions accommodates a null start block. { @@ -117,14 +118,13 @@ BOOST_FIXTURE_TEST_CASE(scan_for_wallet_transactions, TestChain100Setup) BOOST_FIXTURE_TEST_CASE(importmulti_rescan, TestChain100Setup) { - auto chain = interfaces::MakeChain(); - // Cap last block file size, and mine new block in a new block file. CBlockIndex* oldTip = ::ChainActive().Tip(); GetBlockFileInfo(oldTip->GetBlockPos().nFile)->nSize = MAX_BLOCKFILE_SIZE; CreateAndProcessBlock({}, GetScriptForRawPubKey(coinbaseKey.GetPubKey())); CBlockIndex* newTip = ::ChainActive().Tip(); + auto chain = interfaces::MakeChain(); auto locked_chain = chain->lock(); LockAnnotation lock(::cs_main);