Skip to content

Commit d635993

Browse files
validation: check invariants when inserting into m_blocks_unlinked
For an entry A -> B in m_blocks_unlinked, the entry B was added into m_blocks_unlinked either because: - some ancestor of B was never received (or) - some ancestor of B was pruned away. Every insert must satisfy two invariants: 1. B has BLOCK_HAVE_DATA set. 2. No duplicate A -> B entries in m_blocks_unlinked (this is UB zone if this entry gets popped twice in ReceivedBlockTransactions and happens to be in setBlockIndexCandidates) 2 bugs (#35070 and #35168) discovered recently stemmed from the m_blocks_unlinked insertion sites not enforcing these invariants. So add a helper which wraps around insertion sites of m_blocks_unlinked with these invariants. Co-authored-by: Martin Zumsande <[email protected]>
1 parent 0852925 commit d635993

3 files changed

Lines changed: 16 additions & 6 deletions

File tree

‎src/node/blockstorage.cpp‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,18 @@ CBlockIndex* BlockManager::AddToBlockIndex(const CBlockHeader& block, CBlockInde
255255
return pindexNew;
256256
}
257257

258+
void BlockManager::AddUnlinkedBlock(CBlockIndex* block)
259+
{
260+
AssertLockHeld(cs_main);
261+
Assume(block != nullptr);
262+
Assume(block->nStatus & BLOCK_HAVE_DATA);
263+
auto range = m_blocks_unlinked.equal_range(block->pprev);
264+
for (auto it = range.first; it != range.second; ++it) {
265+
if (it->second == block) return; // don't insert duplicates
266+
}
267+
m_blocks_unlinked.emplace(block->pprev, block);
268+
}
269+
258270
void BlockManager::PruneOneBlockFile(const int fileNumber)
259271
{
260272
AssertLockHeld(cs_main);
@@ -487,7 +499,7 @@ bool BlockManager::LoadBlockIndex(const std::optional<uint256>& snapshot_blockha
487499
} else {
488500
pindex->m_chain_tx_count = 0;
489501
if (pindex->nStatus & BLOCK_HAVE_DATA) {
490-
m_blocks_unlinked.insert(std::make_pair(pindex->pprev, pindex));
502+
AddUnlinkedBlock(pindex);
491503
}
492504
}
493505
} else {

‎src/node/blockstorage.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,7 @@ class BlockManager
354354
* All pairs A->B, where A (or one of its ancestors) misses transactions, but B has transactions.
355355
*/
356356
std::multimap<CBlockIndex*, CBlockIndex*> m_blocks_unlinked;
357+
void AddUnlinkedBlock(CBlockIndex* block) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
357358

358359
std::unique_ptr<BlockTreeDB> m_block_tree_db GUARDED_BY(::cs_main);
359360

‎src/validation.cpp‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3149,10 +3149,7 @@ CBlockIndex* Chainstate::FindMostWorkChain()
31493149
// processed twice in ReceivedBlockTransactions(), it may be re-added to
31503150
// setBlockIndexCandidates with a modified nSequenceId, breaking ordering
31513151
// 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-
}
3152+
m_blockman.AddUnlinkedBlock(pindexFailed);
31563153
}
31573154
setBlockIndexCandidates.erase(pindexFailed);
31583155
}
@@ -3816,7 +3813,7 @@ void ChainstateManager::ReceivedBlockTransactions(const CBlock& block, CBlockInd
38163813
}
38173814
} else {
38183815
if (pindexNew->pprev && pindexNew->pprev->IsValid(BLOCK_VALID_TREE)) {
3819-
m_blockman.m_blocks_unlinked.insert(std::make_pair(pindexNew->pprev, pindexNew));
3816+
m_blockman.AddUnlinkedBlock(pindexNew);
38203817
}
38213818
}
38223819
}

0 commit comments

Comments
 (0)