FindMostWorkChain() can insert duplicate entries into m_blocks_unlinked on a pruned node during reorgs. When the pruned data is later redownloaded, the same block is processed twice and re-inserted into setBlockIndexCandidates with a mutated nSequenceId mutating the sort key of a live std::set element which is undefined behavior.
On regtest it aborts on an assertion; on mainnet it's silent (crash, hang, or wrong chain selection). It only affects pruned nodes, and only under reorgs that redownload pruned stale-fork blocks.
Bug
In FindMostWorkChain(), when the node walks back a candidate chain and finds an ancestor whose data has been pruned (fMissingData), it parks the descendant blocks in m_blocks_unlinked so they can be reconsidered once the data returns:
} else if (fMissingData) {
// If we're missing data, then add back to m_blocks_unlinked,
// so that if the block arrives in the future we can try adding
// to setBlockIndexCandidates again.
m_blockman.m_blocks_unlinked.insert(
std::make_pair(pindexFailed->pprev, pindexFailed));
}The loop asserts every block here has
HaveNumChainTxs()(i.e.nChainTx > 0), which pruning does not clear onlyBLOCK_HAVE_DATAis cleared. So this branch is reachable only on pruned nodes, for a block whose data was received and later deleted.If
FindMostWorkChain()processes the same pruned block more than once across reorg churn (e.g. flipping between two competing chains), the same(pprev, pindexFailed)pair is inserted twice. When the pruned data is redownloaded,ReceivedBlockTransactions()walksm_blocks_unlinkedfor that parent and re-adds the children tosetBlockIndexCandidates, assigning each annSequenceId. A duplicate means the same block is processed twice and on the second pass itsnSequenceIdis changed while it is already an element of the set. That set is ordered byCBlockIndexWorkComparator, whose tiebreak isnSequenceId:
bool CBlockIndexWorkComparator::operator()(const CBlockIndex* pa, const CBlockIndex* pb) const {
if (pa->nChainWork > pb->nChainWork) return false;
if (pa->nChainWork < pb->nChainWork) return true;
if (pa->nSequenceId < pb->nSequenceId) return false; // ← sort key mutated under the bug
if (pa->nSequenceId > pb->nSequenceId) return true;
if (pa < pb) return false;
if (pa > pb) return true;
return false;
}Mutating a key that determines an element's position in a std::set is undefined behavior: the red-black tree's invariant is violated, and any subsequent find/insert/erase can return wrong results, loop, or crash. setBlockIndexCandidates is the structure Bitcoin uses to choose the active chain.
Fix
Deduplicate before inserting and never park the same block in m_blocks_unlinked twice:
} else if (fMissingData) {
// ... Avoid duplicate entries in m_blocks_unlinked. If the same entry is
// processed twice in ReceivedBlockTransactions(), it may be re-added to
// setBlockIndexCandidates with a modified nSequenceId, breaking ordering
// guarantees and leading to undefined behavior.
auto range = m_blockman.m_blocks_unlinked.equal_range(pindexFailed->pprev);
if (!std::any_of(range.first, range.second,
[&](const auto& p){ return p.second == pindexFailed; })) {
m_blockman.m_blocks_unlinked.emplace(pindexFailed->pprev, pindexFailed);
}
}Proof of concept
Bitcoin Core’s test/functional/feature_pruning.py exercises exactly this path: a pruned node (node2) that reorgs back across a fork whose block data was pruned, forcing a redownload. On regtest, DefaultConsistencyChecks is on, so CheckBlockIndex() runs and turns the duplicate into a clean abort.
Fixed (master):
TestFramework (INFO): Tests successful
v31.0:
bitcoind: ./validation.cpp:5366: void ChainstateManager::CheckBlockIndex() const:
Assertion `!foundInUnlinked' failed.
TestFramework (ERROR): Unexpected exception:
BrokenPipeError: [Errno 32] Broken pipe
TestFramework (ERROR): Test failed.
UpdateTip: new best=...height=1297
BlockDisconnected: block height=1299
BlockDisconnected: block height=1298
BlockDisconnected: block height=1297
BlockDisconnected: block height=1296
BlockDisconnected: block height=1295 The assertion is a regtest-only safety net. On mainnet CheckBlockIndex() is off by default, so the duplicate doesn’t announce itself and it silently corrupts setBlockIndexCandidates instead.
Impact during BIP 110 activation
Pruned nodes running knots 29.3 or core 31.0 could crash or worse could happen during the activation if there are enough miners on both sides (for and against BIP 110) and blocks are redownloaded in reorgs.
Pull requests
https://github.com/bitcoin/bitcoin/pull/35070/
https://github.com/bitcoinknots/bitcoin/pull/317
Credits
stratospher
mzumsande
Note: This only affects pruned nodes and only during a deep chain split. A pruned node deletes block data older than its most recent ~288 blocks. The bug fires only when that already-deleted data has to be fetched again which happens when a competing chain branches off more than ~288 blocks back (deeper still if you run a larger -prune), forcing your node to re-download the abandoned branch.


