diff --git a/src/validation.h b/src/validation.h --- a/src/validation.h +++ b/src/validation.h @@ -313,8 +313,6 @@ * Note that we guarantee that either the proof-of-work is valid on pblock, or * (and possibly also) BlockChecked will have been called. * - * Call without cs_main held. - * * @param[in] config The global config. * @param[in] pblock The block we want to process. * @param[in] fForceProcessing Process this block even if unrequested; used @@ -325,13 +323,12 @@ */ 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. * - * Call without cs_main held. - * * @param[in] config The config. * @param[in] block The block headers themselves. * @param[out] state This may be set to an Error state if any error @@ -345,7 +342,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). @@ -373,7 +371,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. @@ -587,12 +585,13 @@ /** * 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( const Config &config, CValidationState &state, const CBlock &block, CBlockIndex *pindexPrev, - BlockValidationOptions validationOptions = BlockValidationOptions()); + BlockValidationOptions validationOptions = BlockValidationOptions()) + EXCLUSIVE_LOCKS_REQUIRED(cs_main); /** * When there are blocks in the active chain with missing data, rewind the @@ -628,7 +627,7 @@ * Returns true if the provided block index successfully became the chain tip. */ bool PreciousBlock(const Config &config, CValidationState &state, - CBlockIndex *pindex); + CBlockIndex *pindex) LOCKS_EXCLUDED(cs_main); /** * Mark a block as finalized. @@ -639,14 +638,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 @@ -132,7 +132,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, @@ -144,11 +145,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, @@ -164,10 +167,11 @@ // 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); + bool ResetBlockFailureFlags(CBlockIndex *pindex) + EXCLUSIVE_LOCKS_REQUIRED(cs_main); template void UpdateFlagsForBlock(CBlockIndex *pindexBase, CBlockIndex *pindex, F f); template @@ -195,9 +199,11 @@ ConnectTrace &connectTrace, DisconnectedBlockTransactions &disconnectpool); - CBlockIndex *AddToBlockIndex(const CBlockHeader &block); + 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. * @@ -207,13 +213,15 @@ void CheckBlockIndex(const Consensus::Params &consensusParams); void InvalidBlockFound(CBlockIndex *pindex, const CValidationState &state); - CBlockIndex *FindMostWorkChain(); + CBlockIndex *FindMostWorkChain() EXCLUSIVE_LOCKS_REQUIRED(cs_main); bool ReceivedBlockTransactions(const CBlock &block, CValidationState &state, CBlockIndex *pindexNew, - const FlatFilePos &pos); + const FlatFilePos &pos) + EXCLUSIVE_LOCKS_REQUIRED(cs_main); bool RollforwardBlock(const CBlockIndex *pindex, CCoinsViewCache &inputs, - const Config &config); + const Config &config) + EXCLUSIVE_LOCKS_REQUIRED(cs_main); } g_chainstate; /** @@ -2826,7 +2834,7 @@ return true; } -static void NotifyHeaderTip() { +static void NotifyHeaderTip() LOCKS_EXCLUDED(cs_main) { bool fNotify = false; bool fInitialBlockDownload = false; static CBlockIndex *pindexHeaderOld = nullptr; @@ -3127,7 +3135,7 @@ } bool InvalidateBlock(const Config &config, CValidationState &state, - CBlockIndex *pindex) { + CBlockIndex *pindex) EXCLUSIVE_LOCKS_REQUIRED(cs_main) { return g_chainstate.UnwindBlock(config, state, pindex, true); } @@ -4409,9 +4417,9 @@ bool CChainState::LoadBlockIndex(const Config &config, CBlockTreeDB &blocktree) { - if (!blocktree.LoadBlockIndexGuts(config, [this](const uint256 &hash) { - return this->InsertBlockIndex(hash); - })) { + if (!blocktree.LoadBlockIndexGuts( + config, [this](const uint256 &hash) EXCLUSIVE_LOCKS_REQUIRED( + cs_main) { return this->InsertBlockIndex(hash); })) { return false; } @@ -4485,7 +4493,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; }