- Prerequisite PR: #12431 (it wouldn’t be catastrophic to merge this before that, but preferable to get that in first.)
In trying to fix the fork warning code (validation.cpp:CheckForkWarningConditions*
), it became apparent that we are marking some (but not all) invalid blocks as invalid (via nStatus
) when they are received and subsequently dropped. The fact that we never mark some invalid blocks as such prevents us from e.g. detecting and warning on invalid chains with significant work.
This change has ProcessNewBlock
call InvalidateBlock
on the invalid block to do the expected bookkeeping in mapBlockIndex
before dropping the block.
This change also consolidates the setting of CBlockIndex’s nStatus |= BLOCK_FAILED_VALID
to a single function (InvalidBlockFound
) since there’s peripheral bookkeeping (e.g. g_failed_blocks.insert()
) that we want to do consistently but is duplicated in some places or not done in other cases when it apparently should be.
One such replacement with InvalidBlockFound ensures addition of invalid blocks to g_failed_blocks
and so (in theory) reduces CPU burden when being spammed with descendants of an invalid block, since we no longer have to walk mapBlockIndex
to determine its invalidity. Based on reading usage of g_failed_blocks
I can’t tell if this savings is real, but in any case it seems worthwhile to keep that set consistent (i.e. 1-1 with blocks marked BLOCK_FAILED_VALID since last restart).