diff --git a/src/validation.h b/src/validation.h --- a/src/validation.h +++ b/src/validation.h @@ -333,7 +333,7 @@ * Note that we guarantee that either the proof-of-work is valid on pblock, or * (and possibly also) BlockChecked will have been called. * - * May not be called with cs_main held. May not be called in a + * May not be called in a * validationinterface callback. * * @param[in] config The global config. @@ -346,12 +346,13 @@ */ bool ProcessNewBlock(const Config &config, const std::shared_ptr pblock, - bool fForceProcessing, bool *fNewBlock); + bool fForceProcessing, bool *fNewBlock) + LOCKS_EXCLUDED(cs_main); /** * Process incoming block headers. * - * May not be called with cs_main held. May not be called in a + * May not be called in a * validationinterface callback. * * @param[in] config The config. @@ -367,7 +368,8 @@ const std::vector &block, CValidationState &state, const CBlockIndex **ppindex = nullptr, - CBlockHeader *first_invalid = nullptr); + CBlockHeader *first_invalid = nullptr) + LOCKS_EXCLUDED(cs_main); /** * Open a block file (blk?????.dat). @@ -395,7 +397,7 @@ * Load the block tree and coins database from disk, initializing state if we're * running with -reindex. */ -bool LoadBlockIndex(const Config &config); +bool LoadBlockIndex(const Config &config) EXCLUSIVE_LOCKS_REQUIRED(cs_main); /** * Update the chain tip based on database information. @@ -609,11 +611,12 @@ /** * Check a block is completely valid from start to finish (only works on top of - * our current best block, with cs_main held) + * our current best block) */ bool TestBlockValidity(CValidationState &state, const CChainParams ¶ms, const CBlock &block, CBlockIndex *pindexPrev, - BlockValidationOptions validationOptions); + BlockValidationOptions validationOptions) + EXCLUSIVE_LOCKS_REQUIRED(cs_main); /** * When there are blocks in the active chain with missing data, rewind the @@ -643,11 +646,11 @@ /** * Mark a block as precious and reorganize. * - * May not be called with cs_main held. May not be called in a + * May not be called in a * validationinterface callback. */ bool PreciousBlock(const Config &config, CValidationState &state, - CBlockIndex *pindex); + CBlockIndex *pindex) LOCKS_EXCLUDED(cs_main); /** * Mark a block as finalized. @@ -658,14 +661,15 @@ /** Mark a block as invalid. */ bool InvalidateBlock(const Config &config, CValidationState &state, - CBlockIndex *pindex); + CBlockIndex *pindex) EXCLUSIVE_LOCKS_REQUIRED(cs_main); /** Park a block. */ bool ParkBlock(const Config &config, CValidationState &state, CBlockIndex *pindex); /** Remove invalidity status from a block and its descendants. */ -bool ResetBlockFailureFlags(CBlockIndex *pindex); +bool ResetBlockFailureFlags(CBlockIndex *pindex) + EXCLUSIVE_LOCKS_REQUIRED(cs_main); /** Remove parked status from a block and its descendants. */ bool UnparkBlockAndChildren(CBlockIndex *pindex); diff --git a/src/validation.cpp b/src/validation.cpp --- a/src/validation.cpp +++ b/src/validation.cpp @@ -128,7 +128,8 @@ CBlockIndex *pindexBestInvalid = nullptr; CBlockIndex *pindexBestParked = nullptr; - bool LoadBlockIndex(const Config &config, CBlockTreeDB &blocktree); + bool LoadBlockIndex(const Config &config, CBlockTreeDB &blocktree) + EXCLUSIVE_LOCKS_REQUIRED(cs_main); bool ActivateBestChain( const Config &config, CValidationState &state, @@ -140,11 +141,13 @@ * mapBlockIndex. */ bool AcceptBlockHeader(const Config &config, const CBlockHeader &block, - CValidationState &state, CBlockIndex **ppindex); + CValidationState &state, CBlockIndex **ppindex) + EXCLUSIVE_LOCKS_REQUIRED(cs_main); bool AcceptBlock(const Config &config, const std::shared_ptr &pblock, CValidationState &state, bool fRequested, - const FlatFilePos *dbp, bool *fNewBlock); + const FlatFilePos *dbp, bool *fNewBlock) + EXCLUSIVE_LOCKS_REQUIRED(cs_main); // Block (dis)connection on a given view: DisconnectResult DisconnectBlock(const CBlock &block, @@ -163,10 +166,12 @@ // Manual block validity manipulation: bool PreciousBlock(const Config &config, CValidationState &state, - CBlockIndex *pindex); + CBlockIndex *pindex) LOCKS_EXCLUDED(cs_main); bool UnwindBlock(const Config &config, CValidationState &state, - CBlockIndex *pindex, bool invalidate); - bool ResetBlockFailureFlags(CBlockIndex *pindex); + CBlockIndex *pindex, bool invalidate) + EXCLUSIVE_LOCKS_REQUIRED(cs_main); + bool ResetBlockFailureFlags(CBlockIndex *pindex) + EXCLUSIVE_LOCKS_REQUIRED(cs_main); template void UpdateFlagsForBlock(CBlockIndex *pindexBase, CBlockIndex *pindex, F f); template @@ -202,7 +207,8 @@ CBlockIndex *AddToBlockIndex(const CBlockHeader &block) EXCLUSIVE_LOCKS_REQUIRED(cs_main); /** Create a new block index entry for a given block hash */ - CBlockIndex *InsertBlockIndex(const uint256 &hash); + CBlockIndex *InsertBlockIndex(const uint256 &hash) + EXCLUSIVE_LOCKS_REQUIRED(cs_main); /** * Make various assertions about the state of the block index. * @@ -220,7 +226,8 @@ EXCLUSIVE_LOCKS_REQUIRED(cs_main); bool RollforwardBlock(const CBlockIndex *pindex, CCoinsViewCache &inputs, - const Consensus::Params ¶ms); + const Consensus::Params ¶ms) + EXCLUSIVE_LOCKS_REQUIRED(cs_main); } g_chainstate; /** @@ -2849,7 +2856,7 @@ return true; } -static void NotifyHeaderTip() { +static void NotifyHeaderTip() LOCKS_EXCLUDED(cs_main) { bool fNotify = false; bool fInitialBlockDownload = false; static CBlockIndex *pindexHeaderOld = nullptr; @@ -3060,7 +3067,8 @@ } bool CChainState::UnwindBlock(const Config &config, CValidationState &state, - CBlockIndex *pindex, bool invalidate) { + CBlockIndex *pindex, bool invalidate) + EXCLUSIVE_LOCKS_REQUIRED(cs_main) { AssertLockHeld(cs_main); // We first disconnect backwards and then mark the blocks as invalid. @@ -3154,12 +3162,12 @@ } bool InvalidateBlock(const Config &config, CValidationState &state, - CBlockIndex *pindex) { + CBlockIndex *pindex) EXCLUSIVE_LOCKS_REQUIRED(cs_main) { return g_chainstate.UnwindBlock(config, state, pindex, true); } bool ParkBlock(const Config &config, CValidationState &state, - CBlockIndex *pindex) { + CBlockIndex *pindex) EXCLUSIVE_LOCKS_REQUIRED(cs_main) { return g_chainstate.UnwindBlock(config, state, pindex, false); } @@ -3859,7 +3867,7 @@ } // If the previous block index isn't valid, determine if it descends - // from any block which has been found invalid (g_failed_blocks), then + // from any block which has been found invalid (m_failed_blocks), then // mark pindexPrev and any blocks between them as failed. if (!pindexPrev->IsValid(BlockValidity::SCRIPTS)) { for (const CBlockIndex *failedit : m_failed_blocks) { @@ -4442,10 +4450,11 @@ bool CChainState::LoadBlockIndex(const Config &config, CBlockTreeDB &blocktree) { - if (!blocktree.LoadBlockIndexGuts(config.GetChainParams().GetConsensus(), - [this](const uint256 &hash) { - return this->InsertBlockIndex(hash); - })) { + if (!blocktree.LoadBlockIndexGuts( + config.GetChainParams().GetConsensus(), + [this](const uint256 &hash) EXCLUSIVE_LOCKS_REQUIRED(cs_main) { + return this->InsertBlockIndex(hash); + })) { return false; } @@ -4519,7 +4528,8 @@ return true; } -bool static LoadBlockIndexDB(const Config &config) { +bool static LoadBlockIndexDB(const Config &config) + EXCLUSIVE_LOCKS_REQUIRED(cs_main) { if (!g_chainstate.LoadBlockIndex(config, *pblocktree)) { return false; }