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.
This commit is contained in:
Martin Zumsande 2024-04-23 18:08:35 -04:00
parent 1d5b2406bb
commit a51e91783a
2 changed files with 16 additions and 0 deletions

View File

@ -6396,6 +6396,17 @@ std::optional<int> ChainstateManager::GetSnapshotBaseHeight() const
return base ? std::make_optional(base->nHeight) : std::nullopt; return base ? std::make_optional(base->nHeight) : std::nullopt;
} }
void ChainstateManager::RecalculateBestHeader()
{
AssertLockHeld(cs_main);
m_best_header = ActiveChain().Tip();
for (auto& entry : m_blockman.m_block_index) {
if (!(entry.second.nStatus & BLOCK_FAILED_MASK) && m_best_header->nChainWork < entry.second.nChainWork) {
m_best_header = &entry.second;
}
}
}
bool ChainstateManager::ValidatedSnapshotCleanup() bool ChainstateManager::ValidatedSnapshotCleanup()
{ {
AssertLockHeld(::cs_main); AssertLockHeld(::cs_main);

View File

@ -1321,6 +1321,11 @@ public:
//! nullopt. //! nullopt.
std::optional<int> GetSnapshotBaseHeight() const EXCLUSIVE_LOCKS_REQUIRED(::cs_main); std::optional<int> GetSnapshotBaseHeight() const EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
//! If, due to invalidation / reconsideration of blocks, the previous
//! best header is no longer valid / guaranteed to be the most-work
//! header in our block-index not known to be invalid, recalculate it.
void RecalculateBestHeader() EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
CCheckQueue<CScriptCheck>& GetCheckQueue() { return m_script_check_queue; } CCheckQueue<CScriptCheck>& GetCheckQueue() { return m_script_check_queue; }
~ChainstateManager(); ~ChainstateManager();