Skip to content

Commit a51e917

Browse files
committed
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.
1 parent 1d5b240 commit a51e917

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

src/validation.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6396,6 +6396,17 @@ std::optional<int> ChainstateManager::GetSnapshotBaseHeight() const
63966396
return base ? std::make_optional(base->nHeight) : std::nullopt;
63976397
}
63986398

6399+
void ChainstateManager::RecalculateBestHeader()
6400+
{
6401+
AssertLockHeld(cs_main);
6402+
m_best_header = ActiveChain().Tip();
6403+
for (auto& entry : m_blockman.m_block_index) {
6404+
if (!(entry.second.nStatus & BLOCK_FAILED_MASK) && m_best_header->nChainWork < entry.second.nChainWork) {
6405+
m_best_header = &entry.second;
6406+
}
6407+
}
6408+
}
6409+
63996410
bool ChainstateManager::ValidatedSnapshotCleanup()
64006411
{
64016412
AssertLockHeld(::cs_main);

src/validation.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1321,6 +1321,11 @@ class ChainstateManager
13211321
//! nullopt.
13221322
std::optional<int> GetSnapshotBaseHeight() const EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
13231323

1324+
//! If, due to invalidation / reconsideration of blocks, the previous
1325+
//! best header is no longer valid / guaranteed to be the most-work
1326+
//! header in our block-index not known to be invalid, recalculate it.
1327+
void RecalculateBestHeader() EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
1328+
13241329
CCheckQueue<CScriptCheck>& GetCheckQueue() { return m_script_check_queue; }
13251330

13261331
~ChainstateManager();

0 commit comments

Comments
 (0)