validation: Leave pruned blocks in `m_blocks_unlinked`, fix another `nSequenceId` issue #36149

pull mzumsande wants to merge 4 commits into bitcoin:master from mzumsande:202609_fix_cbi_pruning_unlinked changing 5 files +69 −37
  1. mzumsande commented at 2:33 PM on September 2, 2026: contributor

    m_blocks_unlinked serves two purposes:

    1. finding blocks to setBlockIndexCandidates once their parents are received
    2. setting m_chain_tx_count once the parents are received

    In case the parent block of a previously pruned block is received, 2. is currently not being done, because pruning removed the entries from m_blocks_unlinked. This can result in CheckBlockIndex failures (#31512, #36021) and incorrect rpc results, plus we are in a temporarily inconsistent state (m_chain_tx_count is currently not set, but would get set if we restarted).

    This scenario is unlikely to happen during normal node operation unless there are huge reorgs, but it can happen, for example, in getblockfrompeer scenarios. Fix this by leaving pruned blocks in m_chain_tx_count, and also adding them on restart.

    This was one of two reasons, the fuzz target block_index_tree couldn't cover pruning of blocks not in the main chain.

    The other one is similar to #34521 and #35070 - if we receive a block a second time (after pruning), we could change nSequenceId while the block is in setBlockIndexCandidates - fix this by attempting to remove it from the set before re-adding it.

    After the two issues are fixed, the restriction from block_index_tree fuzz target is removed.

    Fixes #36021 Fixes #31512

  2. DrahtBot added the label Validation on Sep 2, 2026
  3. DrahtBot commented at 2:33 PM on September 2, 2026: contributor

    <!--e57a25ab6845829454e8d69fc972939a-->

    The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

    <!--006a51241073e994b41acfe9ec718e94-->

    Code Coverage & Benchmarks

    For details see: https://corecheck.dev/bitcoin/bitcoin/pulls/36149.

    <!--021abf342d371248e50ceaed478a90ca-->

    Reviews

    See the guideline and AI policy for information on the review process.

    Type Reviewers
    Concept ACK l0rinc

    If your review is incorrectly listed, please copy-paste <code>&lt;!--meta-tag:bot-skip--&gt;</code> into the comment that the bot should ignore.

    <!--5faf32d7da4f0f540f40219e4f7537a3-->

  4. validation: keep pruned blocks in m_blocks_unlinked
    m_blocks_unlinked serves two purposes:
    1. finding blocks to SetBlockIndexCandidates once their parents are
       received
    2. Setting m_chain_tx_count once the parents are received
    
    In case that a parent of a pruned block is received,
    2. is currently not being done, because pruning removed the entries.
    This can result in CheckBlockIndex failures, and an inconsistent state
    (because in case of a restart, m_chain_tx would be set).
    This is unlikely to happen during normal node operation unless
    there are huge reorgs, but it can happen, for example, in getblockfrompeer scenarios.
    
    Fix this by keeping pruned blocks in m_blocks_unlinked and also removing
    the BLOCK_HAVE_DATA requirement during startup.
    7c7ccc9cc0
  5. test: add functional test for parents of pruned blocks
    This test would lead to a CheckBlockIndex assertion fail without
    the changes of the previous commit.
    5d5af40fcb
  6. validation: don't change nSequenceId of a block that is a chain candidate
    nSequenceId is one of the sort keys of setBlockIndexCandidates, so changing it
    for a block that currently is in one of these sets corrupts their ordering,
    after which lookups for other entries can fail.
    
    This is also necessary for the block_index_tree fuzz test extension in the
    following commit, which would otherwise trip on this.
    80477ba978
  7. fuzz: allow to prune out-of-chain blocks in block_index_tree
    After the issues with m_blocks_unlinked and
    setBlockIndexCandidates have been resolved in previous commits,
    the previous restriction to the fuzz test does no longer apply,
    and we can also prune out-of-chain blocks.
    40a31d97ab
  8. mzumsande force-pushed on Sep 2, 2026
  9. DrahtBot added the label CI failed on Sep 2, 2026
  10. DrahtBot removed the label CI failed on Sep 2, 2026
  11. l0rinc commented at 5:29 PM on September 2, 2026: contributor

    Concept ACK, thanks for fixing it. I will try to review it a bit later.

  12. mzumsande marked this as ready for review on Sep 2, 2026

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-09-06 07:50 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me