@@ -154,6 +154,10 @@ class CChainState {
154154
155155 bool ActivateBestChain (CValidationState &state, const CChainParams& chainparams, std::shared_ptr<const CBlock> pblock);
156156
157+ /* *
158+ * If a block header hasn't already been seen, call CheckBlockHeader on it, ensure
159+ * that it doesn't descend from an invalid block, and then add it to mapBlockIndex.
160+ */
157161 bool AcceptBlockHeader (const CBlockHeader& block, CValidationState& state, const CChainParams& chainparams, CBlockIndex** ppindex);
158162 bool AcceptBlock (const std::shared_ptr<const CBlock>& pblock, CValidationState& state, const CChainParams& chainparams, CBlockIndex** ppindex, bool fRequested , const CDiskBlockPos* dbp, bool * fNewBlock );
159163
@@ -185,6 +189,11 @@ class CChainState {
185189 CBlockIndex* AddToBlockIndex (const CBlockHeader& block);
186190 /* * Create a new block index entry for a given block hash */
187191 CBlockIndex * InsertBlockIndex (const uint256& hash);
192+ /* *
193+ * Make various assertions about the state of the block index.
194+ *
195+ * By default this only executes fully when using the Regtest chain; see: fCheckBlockIndex.
196+ */
188197 void CheckBlockIndex (const Consensus::Params& consensusParams);
189198
190199 void InvalidBlockFound (CBlockIndex *pindex, const CValidationState &state);
@@ -2625,6 +2634,10 @@ static void NotifyHeaderTip() {
26252634 * Make the best chain active, in multiple steps. The result is either failure
26262635 * or an activated best chain. pblock is either nullptr or a pointer to a block
26272636 * that is already loaded (to avoid loading it again from disk).
2637+ *
2638+ * ActivateBestChain is split into steps (see ActivateBestChainStep) so that
2639+ * we avoid holding cs_main for an extended period of time; the length of this
2640+ * call may be quite long during reindexing or a substantial reorg.
26282641 */
26292642bool CChainState::ActivateBestChain (CValidationState &state, const CChainParams& chainparams, std::shared_ptr<const CBlock> pblock) {
26302643 // Note that while we're often called here from ProcessNewBlock, this is
@@ -3323,6 +3336,9 @@ bool CChainState::AcceptBlockHeader(const CBlockHeader& block, CValidationState&
33233336 if (!ContextualCheckBlockHeader (block, state, chainparams, pindexPrev, GetAdjustedTime ()))
33243337 return error (" %s: Consensus::ContextualCheckBlockHeader: %s, %s" , __func__, hash.ToString (), FormatStateMessage (state));
33253338
3339+ // If the previous block index isn't valid, determine if it descends from any block which
3340+ // has been found invalid (g_failed_blocks), then mark pindexPrev and any blocks
3341+ // between them as failed.
33263342 if (!pindexPrev->IsValid (BLOCK_VALID_SCRIPTS)) {
33273343 for (const CBlockIndex* failedit : g_failed_blocks) {
33283344 if (pindexPrev->GetAncestor (failedit->nHeight ) == failedit) {
0 commit comments