b335786 validation: Move most logic from InvalidChainFound into InvalidBlockFound:
InvalidBlockFound() and InvalidateBlock() both call SetBlockFailureFlags() and then update the best header if it points into the invalid branch.
We could put that header update at the end of the helper, since marking a branch invalid can also make the current best header unusable.
The flags still need to be set first so RecalculateBestHeader() excludes the invalid blocks.
If we inlcude the header update in that method, it should be renamed of course to reflect its new purpose.
InvalidChainFound() can stay separate, since it only tracks the invalid block with the most work.
I’d also call InvalidChainFound() before logging in InvalidBlockFound(), so the two callers follow the same order.
<details>
<summary>Propagate invalidity and update the best header</summary>
diff --git a/src/validation.cpp b/src/validation.cpp
index 7df03b6d29..0e1f575874 100644
--- a/src/validation.cpp
+++ b/src/validation.cpp
@@ -1972,10 +1972,8 @@ void Chainstate::InvalidBlockFound(CBlockIndex* pindex, const BlockValidationSta
pindex->nStatus |= BLOCK_FAILED_VALID;
m_blockman.m_dirty_blockindex.insert(pindex);
setBlockIndexCandidates.erase(pindex);
- SetBlockFailureFlags(pindex);
- if (m_chainman.m_best_header != nullptr && m_chainman.m_best_header->GetAncestor(pindex->nHeight) == pindex) {
- m_chainman.RecalculateBestHeader();
- }
+ PropagateBlockInvalidity(pindex);
+ UpdateBestInvalid(pindex);
LogInfo("%s: invalid block=%s height=%d log2_work=%f date=%s", __func__,
pindex->GetBlockHash().ToString(), pindex->nHeight,
@@ -1985,7 +1983,6 @@ void Chainstate::InvalidBlockFound(CBlockIndex* pindex, const BlockValidationSta
LogInfo("%s: current best=%s height=%d log2_work=%f date=%s", __func__,
tip->GetBlockHash().ToString(), m_chain.Height(), log(tip->nChainWork.getdouble()) / log(2.0),
FormatISO8601DateTime(tip->GetBlockTime()));
- UpdateBestInvalid(pindex);
CheckForkWarningConditions();
}
}
@@ -3753,13 +3750,10 @@ bool Chainstate::InvalidateBlock(BlockValidationState& state, CBlockIndex* const
}
}
- // Flag the remaining descendants as invalid: The marking in the disconnect loop only
- // covers blocks that were already present in highpow_outofchain_headers when it was
- // built, and it doesn't run at all if pindex never was in the active chain.
- SetBlockFailureFlags(to_mark_failed);
- if (m_chainman.m_best_header != nullptr && m_chainman.m_best_header->GetAncestor(to_mark_failed->nHeight) == to_mark_failed) {
- m_chainman.RecalculateBestHeader();
- }
+ // Flag the remaining descendants as invalid and update the best header: The marking in the disconnect
+ // loop only covers blocks that were already present in highpow_outofchain_headers when
+ // it was built, and it doesn't run at all if pindex never was in the active chain.
+ PropagateBlockInvalidity(to_mark_failed);
UpdateBestInvalid(to_mark_failed);
LogInfo("Invalidated block: hash=%s height=%d",
@@ -3790,7 +3784,7 @@ bool Chainstate::InvalidateBlock(BlockValidationState& state, CBlockIndex* const
return true;
}
-void Chainstate::SetBlockFailureFlags(CBlockIndex* invalid_block)
+void Chainstate::PropagateBlockInvalidity(CBlockIndex* invalid_block)
{
AssertLockHeld(cs_main);
@@ -3800,6 +3794,9 @@ void Chainstate::SetBlockFailureFlags(CBlockIndex* invalid_block)
m_blockman.m_dirty_blockindex.insert(&block_index);
}
}
+ if (m_chainman.m_best_header != nullptr && m_chainman.m_best_header->GetAncestor(invalid_block->nHeight) == invalid_block) {
+ m_chainman.RecalculateBestHeader();
+ }
}
void Chainstate::ResetBlockFailureFlags(CBlockIndex *pindex) {
diff --git a/src/validation.h b/src/validation.h
index 995dffab06..d3a8166623 100644
--- a/src/validation.h
+++ b/src/validation.h
@@ -806,8 +806,8 @@ public:
EXCLUSIVE_LOCKS_REQUIRED(!m_chainstate_mutex)
LOCKS_EXCLUDED(::cs_main);
- /** Set invalidity status to all descendants of a block */
- void SetBlockFailureFlags(CBlockIndex* pindex) EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
+ /** Set invalidity status to all descendants of an already invalid block, and recalculate the best header if it was in that subtree */
+ void PropagateBlockInvalidity(CBlockIndex* pindex) EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
/** Remove invalidity status from a block, its descendants and ancestors and reconsider them for activation */
void ResetBlockFailureFlags(CBlockIndex* pindex) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
</details>