Skip to content

Commit c787b3b

Browse files
validation: avoid duplicates in m_blocks_unlinked
In a pruned node undergoing a deep reorg, FindMostWorkChain can insert duplicate entries into m_blocks_unlinked. This can happen when: - Traversing from one candidate tip to the fork point adds blocks whose parents have been pruned. - Traversing from another candidate tip over the same fork inserts the same pairs again, since the blocks are shared across both branches. When we finally download the missing parent from our peer and call ReceivedBlockTransactions to process m_blocks_unlinked, the same entry may be processed multiple times. This can lead to re-insertion into setBlockIndexCandidates with a modified nSequenceId, violating its ordering invariants and causing undefined behavior. So avoid duplicate insertions into m_blocks_unlinked here. Co-authored-by: Martin Zumsande <[email protected]>
1 parent e0fb41f commit c787b3b

1 file changed

Lines changed: 15 additions & 10 deletions

File tree

‎src/validation.cpp‎

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3141,12 +3141,18 @@ CBlockIndex* Chainstate::FindMostWorkChain()
31413141
}
31423142
// Remove the entire chain from the set.
31433143
for (CBlockIndex *pindexFailed = pindexNew; pindexFailed != pindexTest; pindexFailed = pindexFailed->pprev) {
3144+
// If we're missing data and not a descendant of an invalid block,
3145+
// then add back to m_blocks_unlinked, so that if the block arrives in the future
3146+
// we can try adding to setBlockIndexCandidates again.
31443147
if (fMissingData && !fFailedChain) {
3145-
// If we're missing data and not a descendant of an invalid block,
3146-
// then add back to m_blocks_unlinked, so that if the block arrives in the future
3147-
// we can try adding to setBlockIndexCandidates again.
3148-
m_blockman.m_blocks_unlinked.insert(
3149-
std::make_pair(pindexFailed->pprev, pindexFailed));
3148+
// Avoid duplicate entries in m_blocks_unlinked. If the same entry is
3149+
// processed twice in ReceivedBlockTransactions(), it may be re-added to
3150+
// setBlockIndexCandidates with a modified nSequenceId, breaking ordering
3151+
// guarantees and leading to undefined behavior.
3152+
auto range = m_blockman.m_blocks_unlinked.equal_range(pindexFailed->pprev);
3153+
if (!std::any_of(range.first, range.second, [&](const auto& p) { return p.second == pindexFailed; })) {
3154+
m_blockman.m_blocks_unlinked.emplace(pindexFailed->pprev, pindexFailed);
3155+
}
31503156
}
31513157
setBlockIndexCandidates.erase(pindexFailed);
31523158
}
@@ -5340,13 +5346,12 @@ void ChainstateManager::CheckBlockIndex() const
53405346
// Check whether this block is in m_blocks_unlinked.
53415347
auto rangeUnlinked{m_blockman.m_blocks_unlinked.equal_range(pindex->pprev)};
53425348
bool foundInUnlinked = false;
5343-
while (rangeUnlinked.first != rangeUnlinked.second) {
5344-
assert(rangeUnlinked.first->first == pindex->pprev);
5345-
if (rangeUnlinked.first->second == pindex) {
5349+
for (auto it = rangeUnlinked.first; it != rangeUnlinked.second; ++it) {
5350+
assert(it->first == pindex->pprev);
5351+
if (it->second == pindex) {
5352+
assert(!foundInUnlinked); // No duplicates in m_blocks_unlinked
53465353
foundInUnlinked = true;
5347-
break;
53485354
}
5349-
rangeUnlinked.first++;
53505355
}
53515356
if (pindex->pprev && (pindex->nStatus & BLOCK_HAVE_DATA) && pindexFirstNeverProcessed != nullptr && pindexFirstInvalid == nullptr) {
53525357
// If this block has block data available, some parent was never received, and has no invalid parents, it must be in m_blocks_unlinked.

0 commit comments

Comments
 (0)