{
 "number": 36702,
 "repo": "bitcoin/bitcoin",
 "url": "https://github.com/bitcoin/bitcoin/pull/36702",
 "title": "validation: don't treat snapshot block as linked on startup",
 "author": "shuv-amp",
 "author_association": "CONTRIBUTOR",
 "created_at": "2026-10-30T11:23:40Z",
 "updated_at": "2026-10-31T09:41:21Z",
 "description_hash": "e5fba1d71a004d46",
 "age_days": 2,
 "draft": false,
 "labels": [
  "Validation"
 ],
 "milestone": null,
 "base": "master",
 "head_sha": "80f6ddd6cbba7c8104dc0b9dabb451af13cc4a7d",
 "head_ref": "fix-snapshot-block-unlinked-on-startup",
 "head_repo": "shuv-amp/bitcoin",
 "head_history": [],
 "additions": 15,
 "deletions": 0,
 "changed_files": 3,
 "commit_count": 1,
 "size_bucket": "S",
 "mergeable_state": "clean",
 "bot": {
  "drahtbot": {
   "present": true,
   "reviews": {},
   "conflicts": []
  }
 },
 "acks_parsed": {},
 "acks_tally": {
  "ack": 0,
  "stale_ack": 0,
  "concept_ack": 0,
  "approach_ack": 0,
  "nack": 0,
  "concept_nack": 0,
  "approach_nack": 0
 },
 "reviews": {
  "approved": 0,
  "changes_requested": 0,
  "distinct_reviewers": [
   "ValeraFinebits"
  ]
 },
 "signals": {
  "needs_rebase": false,
  "ci_failed": false,
  "mergeable_state": "clean",
  "last_author_activity": "2026-10-30T10:51:08Z",
  "last_reviewer_activity": "2026-10-31T09:41:21Z",
  "last_reviewer": "ValeraFinebits",
  "author_silent_days": 2,
  "waiting_on_author_days": 1,
  "days_since_update": 1
 },
 "refs": {
  "mentioned": [
   35307,
   36149,
   36452
  ],
  "depends_on": [],
  "fixes": [],
  "linked_issues": [],
  "references": [
   {
    "number": 35307,
    "type": "pull",
    "state": "closed",
    "merged": false,
    "merged_at": null,
    "title": "blockstorage: keep snapshot base in normal blockfile range"
   },
   {
    "number": 36452,
    "type": "pull",
    "state": "open",
    "merged": false,
    "merged_at": null,
    "title": "validation: fix false corruption errors after pruning or AssumeUTXO"
   },
   {
    "number": 36149,
    "type": "pull",
    "state": "open",
    "merged": false,
    "merged_at": null,
    "title": "validation: Leave pruned blocks in `m_blocks_unlinked`, fix another `nSequenceId` issue"
   }
  ],
  "conflicts": []
 },
 "stack": {
  "shares_commits_with": [],
  "based_on": [],
  "base_for": []
 },
 "review_paths": [],
 "body": "Background sync requests blocks up to and including the snapshot block from several peers at once, so the snapshot block can be stored before one of its ancestors. `ReceivedBlockTransactions()` then keeps it in `m_blocks_unlinked`, but that isn't persisted. On restart `LoadBlockIndex()` gives it the hardcoded `m_chain_tx_count`, `PopulateBlockIndexCandidates()` makes it a background candidate, and `FindMostWorkChain()` walks back to its parent, which has no chain tx count:\n\n```\nAssertion failed: (pindexTest->HaveNumChainTxs() || pindexTest->nHeight == 0), function FindMostWorkChain, file validation.cpp, line 3217.\n```\n\nIt needs a restart in that window, so it should be rare, but after that the node aborts on every start. The only ways out I found are `-reindex-chainstate` or `-reindex`, and both delete the snapshot chainstate.\n\nFix this by adding the snapshot block to `m_blocks_unlinked` on startup if an ancestor is missing, and not making it a background candidate until then. Both are needed, without the first one background validation stalls. A node that is already stuck starts with the fix and finishes background validation without a reindex.\n\nThe new step in `feature_assumeutxo.py` restarts the node after the snapshot block is submitted out of order, and fails on master. I also reproduced it over p2p with a test peer that withholds the snapshot block's parent.\n\nThese are the two changes from #35307 (closed, the motivation there was `submitblock` before `loadtxoutset`) without its blockfile and VerifyDB changes. cyb3ralbert hit the same assertion while testing #36452 (https://github.com/bitcoin/bitcoin/pull/36452#issuecomment-6464524112), which is still needed when the snapshot tip is at the snapshot block. #36149 drops the `BLOCK_HAVE_DATA` check in the branch right below, so whichever is merged second should match it.",
 "commits": [
  {
   "sha": "80f6ddd6cbba7c8104dc0b9dabb451af13cc4a7d",
   "date": "2026-10-30T10:51:08Z",
   "message": "validation: don't treat snapshot block as linked on startup\n\nIf the snapshot block is received during background sync before some\nof its ancestors, ReceivedBlockTransactions() adds it to\nm_blocks_unlinked and it isn't a background candidate until the\nmissing blocks arrive.\n\nLoadBlockIndex() doesn't restore this on restart. The snapshot block\ngets its m_chain_tx_count from the assumeutxo data and is never added\nto m_blocks_unlinked, so PopulateBlockIndexCandidates() makes it a\nbackground candidate. FindMostWorkChain() then walks back to its\nparent, which has m_chain_tx_count 0, and hits the HaveNumChainTxs()\nassert. This happens on every restart.\n\nAdd the block to m_blocks_unlinked on startup if an ancestor is\nmissing, and skip it as a background candidate until then. Without\nthe m_blocks_unlinked entry the block isn't picked up when the\nmissing blocks arrive, and background validation stalls.\n\nExtend feature_assumeutxo.py to restart the node in this state."
  }
 ],
 "timeline": [
  {
   "t": "2026-10-31T09:41:21Z",
   "kind": "comment",
   "who": "ValeraFinebits",
   "assoc": "NONE",
   "text": "Tested `feature_assumeutxo.py` at `80f6ddd` on Windows x64: passes with the fix. Reverting only the two C++ changes while keeping the new test reproduces the startup assertion in `FindMostWorkChain()`.",
   "id": "c:6541811854",
   "url": "https://github.com/bitcoin/bitcoin/pull/36702#issuecomment-6541811854",
   "hash": "570796b72f0ab78a",
   "edited": null
  }
 ],
 "labels_log": [
  {
   "t": "2026-10-30T11:23:44Z",
   "action": "labeled",
   "label": "Validation",
   "who": "DrahtBot"
  }
 ],
 "state_log": [],
 "text_chars": 3086,
 "text_tokens_estimate": 771,
 "changed_paths": [
  "src/node/blockstorage.cpp",
  "src/validation.cpp",
  "test/functional/feature_assumeutxo.py"
 ],
 "files": [
  {
   "path": "src/node/blockstorage.cpp",
   "add": 4,
   "del": 0
  },
  {
   "path": "src/validation.cpp",
   "add": 4,
   "del": 0
  },
  {
   "path": "test/functional/feature_assumeutxo.py",
   "add": 7,
   "del": 0
  }
 ],
 "test_lines": 7,
 "git": {
  "head": "80f6ddd6cbba7c8104dc0b9dabb451af13cc4a7d",
  "head_matches_backup": true,
  "base": "6ed5aad06111177c39f9b9f07a52634476380ae1",
  "patch_id": "f64189ae1c64168c",
  "commits": [
   {
    "sha": "80f6ddd6cb",
    "subject": "validation: don't treat snapshot block as linked on startup",
    "files": 3,
    "add": 15,
    "del": 0
   }
  ],
  "patch_truncated": false
 },
 "patch_id": "f64189ae1c64168c",
 "input_hash": "a0e79a8f64b41760",
 "extracted_at": "2026-11-02T01:09:49+00:00"
}