diff --git a/src/validation.h b/src/validation.h --- a/src/validation.h +++ b/src/validation.h @@ -333,8 +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 - * validationinterface callback. + * May not be called in a validationinterface callback. * * @param[in] config The global config. * @param[in] pblock The block we want to process. @@ -346,13 +345,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 - * validationinterface callback. + * May not be called in a validationinterface callback. * * @param[in] config The config. * @param[in] block The block headers themselves. @@ -367,7 +366,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 +395,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 +609,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 @@ -644,11 +645,10 @@ /** * Mark a block as precious and reorganize. * - * May not be called with cs_main held. May not be called in a - * validationinterface callback. + * 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. @@ -659,30 +659,33 @@ /** 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); + CBlockIndex *pindex) EXCLUSIVE_LOCKS_REQUIRED(cs_main); /** Remove invalidity status from a block and its descendants. */ -void ResetBlockFailureFlags(CBlockIndex *pindex); +void ResetBlockFailureFlags(CBlockIndex *pindex) + EXCLUSIVE_LOCKS_REQUIRED(cs_main); /** Remove parked status from a block and its descendants. */ -void UnparkBlockAndChildren(CBlockIndex *pindex); +void UnparkBlockAndChildren(CBlockIndex *pindex) + EXCLUSIVE_LOCKS_REQUIRED(cs_main); /** Remove parked status from a block. */ -void UnparkBlock(CBlockIndex *pindex); +void UnparkBlock(CBlockIndex *pindex) EXCLUSIVE_LOCKS_REQUIRED(cs_main); /** * Retrieve the topmost finalized block. */ -const CBlockIndex *GetFinalizedBlock(); +const CBlockIndex *GetFinalizedBlock() EXCLUSIVE_LOCKS_REQUIRED(cs_main); /** * Checks if a block is finalized. */ -bool IsBlockFinalized(const CBlockIndex *pindex); +bool IsBlockFinalized(const CBlockIndex *pindex) + EXCLUSIVE_LOCKS_REQUIRED(cs_main); /** The currently-connected chain of blocks (protected by cs_main). */ extern CChain &chainActive; 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); - void ResetBlockFailureFlags(CBlockIndex *pindex); + CBlockIndex *pindex, bool invalidate) + EXCLUSIVE_LOCKS_REQUIRED(cs_main); + void ResetBlockFailureFlags(CBlockIndex *pindex) + EXCLUSIVE_LOCKS_REQUIRED(cs_main); template void UpdateFlagsForBlock(CBlockIndex *pindexBase, CBlockIndex *pindex, F f); template @@ -176,7 +181,8 @@ void UpdateFlags(CBlockIndex *pindex, F f) EXCLUSIVE_LOCKS_REQUIRED(cs_main); /** Remove parked status from a block and its descendants. */ - void UnparkBlockImpl(CBlockIndex *pindex, bool fClearChildren); + void UnparkBlockImpl(CBlockIndex *pindex, bool fClearChildren) + EXCLUSIVE_LOCKS_REQUIRED(cs_main); bool ReplayBlocks(const Consensus::Params ¶ms, CCoinsView *view); bool RewindBlockIndex(const Config &config); @@ -202,7 +208,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 +227,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; /** @@ -2381,7 +2389,8 @@ }; static bool FinalizeBlockInternal(const Config &config, CValidationState &state, - const CBlockIndex *pindex) { + const CBlockIndex *pindex) + EXCLUSIVE_LOCKS_REQUIRED(cs_main) { AssertLockHeld(cs_main); if (pindex->nStatus.isInvalid()) { // We try to finalize an invalid block. @@ -2412,7 +2421,8 @@ } static const CBlockIndex *FindBlockToFinalize(const Config &config, - CBlockIndex *pindexNew) { + CBlockIndex *pindexNew) + EXCLUSIVE_LOCKS_REQUIRED(cs_main) { AssertLockHeld(cs_main); const int32_t maxreorgdepth = @@ -2859,7 +2869,7 @@ return true; } -static void NotifyHeaderTip() { +static void NotifyHeaderTip() LOCKS_EXCLUDED(cs_main) { bool fNotify = false; bool fInitialBlockDownload = false; static CBlockIndex *pindexHeaderOld = nullptr; @@ -3865,7 +3875,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) { @@ -4448,10 +4458,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; } @@ -4525,7 +4536,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; }