This is a series of diffs that fixes a minor bug related to m_best_header sometimes being incorrect.
PR description:
m_best_header (the most-work header not known to be on an invalid chain) can be wrong in the context of invalidation / reconsideration of blocks. This can happen naturally (a valid header is received and stored in our block tree db; when the full block arrives, it is found to be invalid) or triggered by the user with the invalidateblock / reconsiderblock rpc.
We don't currently use m_best_header for any critical things, so it being wrong affects mostly rpcs.
One alternative to this suggested in the past would be to introduce a continuous tracking of header tips (#12138).
While this might be more performant, it is also more complicated, and situations where we need this data are only be remotely triggerable by paying the cost of creating a valid PoW header for an invalid block.
I think it isn't necessary to optimise for performance here, plus the solution in this PR doesn't perform any extra steps in the normal node operation where no invalidated blocks are encountered.
https://github.com/bitcoin/bitcoin/pull/30666/commits/a51e91783aac0beefcb604be159eb1cb96a39051:
validation: add RecalculateBestHeader() function
It recalculates m_best_header by looping over the entire
block index. Even though this is not very performant, it
will only be used in rare situations that cannot be
triggered by others without a cost:
As part of to invalidateblock / reconsiderblock rpcs, or when a
block with an accepted header with valid PoW turns out to be invalid
later during full validation.
https://github.com/bitcoin/bitcoin/pull/30666/commits/9275e9689a426964f5eaee65e356754a0548d926:
rpc: call RecalculateBestHeader as part of reconsiderblock
Co-authored-by: Fabian Jahr <fjahr@protonmail.com>
This is a partial backport of core#30666
Depends on D17998