InvalidChainFound() is called from 3 spots:
- from
InvalidBlockFound() - from
ActivateBestChain(), with the highest connectable block of the chain as an arg (after having calledInvalidBlockFound()already for the block that failed inConnectTip()) - from
InvalidateBlock()(rpc-only)
Most of the logic in InvalidChainFound() is not required for 2) and 3):
In ActivateBestChain(), the previous call to InvalidChainFound() before via InvalidBlockFound did the flag updates, best header calculation and logging, so only m_best_invalid could need additional updating.
In InvalidateBlock() (which doesn’t call InvalidBlockFound()), we do most of the accounting manually.
Therefore move of all logic but the m_best_invalid update into InvalidBlockFound(), avoiding duplicate logging and unnecessary work (the loops over the block index in SetBlockFailureFlags() and RecalculateBestHeader()
were done twice before).
The second commit renames InvalidChainFound to UpdateBestInvalid to adjust to its remaining role.
This addresses past discussions about repeated work in InvalidateBlock() (https://github.com/bitcoin/bitcoin/pull/31405#discussion_r2135717500) and ActivateBestChain() (https://github.com/bitcoin/bitcoin/pull/31533#discussion_r2607189790)