{
 "number": 35524,
 "input_hash": "f8382c48d76bf744",
 "model": "ledger",
 "prompt_hash": null,
 "created": "2026-10-01T01:22:02+00:00",
 "result": {
  "summary": "Avoids rewriting the genesis block to disk during block index recovery if a valid genesis block is already present at block position {0, 0}. Additionally, updates `AllocateFileRange` on Windows and in the zero-fill fallback so existing file contents are not overwritten or truncated when preallocating space within the current file boundaries.",
  "problem": "On platforms without native `posix_fallocate` (such as *BSD and illumos distributions like OmniOS), running `-reindex` or recovering from a missing block index overwrites valid block data in `blk00000.dat`. When `ChainstateManager::LoadGenesisBlock()` unconditionally rewrites the genesis block, `AllocateFileRange` zeroes out subsequent block data, causing `-reindex` to quietly finish with the chain height stuck at 0.",
  "card": "Prevents silent block data destruction during `-reindex` and index recovery on platforms without native `posix_fallocate` (*BSD, OmniOS). `ChainstateManager::LoadGenesisBlock()` now checks for an existing genesis block in block file 0 before writing a new one, avoiding calls to `AllocateFileRange` that previously zeroed out subsequent blocks. In addition, the Windows and fallback implementations of `AllocateFileRange` are fixed to only extend files rather than clobbering existing bytes. This resolves issue #33128.",
  "evidence": [
   "Linked issue #33128 ('*BSD, OmniOS: -reindex is broken') reports that running `-reindex` with deleted block index files silently destroyed subsequent blocks, leaving the node at height 0.",
   "Commit message: 'The log reports reindexing finished, but the blocks after genesis are gone and the chain height comes back as 0. Now, we check whether a valid genesis block is already present before writing one, and reuse it when possible.'"
  ],
  "scope_notes": "",
  "changed_since_previous": "Added changes to `ChainstateManager::LoadGenesisBlock()` in `src/validation.cpp` to inspect `blk00000.dat` for an existing genesis block at offset {0, 0} and reuse it instead of unconditionally invoking `WriteBlock()`.",
  "thread": {
   "last_event_at": "2026-09-30T19:13:15Z",
   "events": 12,
   "head_sha": "fd2d21053f014e76f44fb72ff081240cd3a2b519",
   "schema_floor": 2,
   "schema": 2
  },
  "waiting_on": [
   {
    "at": "2026-09-30",
    "id": "c:5917957172",
    "on": "reviewer",
    "url": "https://github.com/bitcoin/bitcoin/pull/35524#issuecomment-5917957172",
    "what": "sedited to respond to author's finding that reading genesis alone does not resolve the bug"
   }
  ],
  "risk": {
   "level": "medium",
   "touches": [
    "validation"
   ],
   "why": "Mistakes in genesis block detection or disk allocation handling could prevent node startup, corrupt block files, or misinitialize the block index."
  },
  "discussion": {
   "author_status": "last statement processed 2026-09-30",
   "open_concerns": [],
   "resolved_concerns": [
    "thomasbuilds: widen offset to off_t before adding length to match other platforms (fixed by winterrdog 2026-06-16)",
    "sedited: check whether genesis can be read from disk in LoadGenesisBlock instead of patching low-level file handling code (answered by winterrdog 2026-09-30)"
   ]
  },
  "reviewability": {
   "state": "Ready",
   "label": "Needs rebase",
   "reason": "Conflicts with master; review the logic now, the rebase is mechanical."
  },
  "agreement": {
   "state": "Crickets",
   "summary": "Crickets: no substantive comment either way",
   "reason": "Updated sedited's suggestion c:5742883004 to answered after winterrdog tested the approach and reported that it does not fix the issue without AllocateFileRange changes.",
   "evidence": [],
   "objections": [
    {
     "reviewer": "thomasbuilds",
     "association": "CONTRIBUTOR",
     "type": "suggestion",
     "kind": "safety",
     "harm": "",
     "request": "widen offset to off_t before adding length to match other platforms",
     "question": "",
     "text": "widen offset to off_t before adding length to match other platforms",
     "clears_with": "answer",
     "blocking": false,
     "status": "fixed",
     "status_note": "author applied the widening cast",
     "cleared": true,
     "author_replied": true,
     "others_replied": false,
     "fix_pushed": true,
     "replies": [
      "rc:3423008427"
     ],
     "evidence": "2026-06-14: 'nit: the Windows, macOS, and posix computations above all widen `offset` before adding `length`'",
     "url": "https://github.com/bitcoin/bitcoin/pull/35524#discussion_r3409196404",
     "id": "rc:3409196404",
     "at": "2026-06-14",
     "status_at": "2026-06-16",
     "status_by": "winterrdog",
     "resolution_evidence": "2026-06-16: winterrdog: 'i think it's a good point for safety... applied the change'",
     "pin": null,
     "from_schema": 2
    },
    {
     "reviewer": "sedited",
     "association": "MEMBER",
     "type": "suggestion",
     "kind": "approach",
     "harm": "",
     "request": "check whether genesis can be read from disk in LoadGenesisBlock instead of patching low-level file handling code",
     "question": "",
     "text": "check whether genesis can be read from disk in LoadGenesisBlock instead of patching low-level file handling code",
     "clears_with": "answer",
     "blocking": false,
     "status": "answered",
     "status_note": "author tested the approach and found it does not fix the bug without the AllocateFileRange guard",
     "cleared": true,
     "author_replied": true,
     "others_replied": false,
     "fix_pushed": false,
     "replies": [
      "c:5757418198",
      "rc:4076176279",
      "c:5917957172"
     ],
     "evidence": "2026-09-19: 'I wonder if instead of trying to fix this salvaging scenario by doing a bunch of patches in our low-level file handling code, the easier fix might be to check if we can read the genesis block from dis'",
     "url": "https://github.com/bitcoin/bitcoin/pull/35524#issuecomment-5742883004",
     "id": "c:5742883004",
     "at": "2026-09-19",
     "status_at": "2026-09-30",
     "status_by": "winterrdog",
     "resolution_evidence": "2026-09-30: winterrdog: 'tested both fixes independently and it turns out the `ReadBlock()` check in `ChainstateManager::LoadGenesisBlock()` alone does not fix the reported bug'",
     "pin": null,
     "from_schema": 2
    }
   ],
   "support": [],
   "participants": [
    {
     "login": "thomasbuilds",
     "stance": "neutral",
     "note": "raised nits on indentation and widening offset before addition",
     "association": "CONTRIBUTOR",
     "comments": 2,
     "first": "2026-06-13",
     "last": "2026-06-14"
    },
    {
     "login": "sedited",
     "stance": "neutral",
     "note": "suggested reading genesis from disk in LoadGenesisBlock rather than patching low-level file code",
     "association": "MEMBER",
     "comments": 1,
     "first": "2026-09-19",
     "last": "2026-09-19"
    }
   ],
   "derivation": "no substantive comment either way",
   "model_state": null,
   "corrections": [],
   "notes": "Updated sedited's suggestion c:5742883004 to answered after winterrdog tested the approach and reported that it does not fix the issue without AllocateFileRange changes."
  },
  "dependencies": {
   "depends_on": [],
   "enables": []
  },
  "categories": [
   {
    "name": "mempool",
    "member": false,
    "evidence": "Changes only file allocation utilities and genesis block loading during reindex; touches no mempool or policy code.",
    "band": "Unranked",
    "reason_tag": "bug fix",
    "score": 0.0,
    "factors": {
     "bug_severity": 2,
     "leverage": 0,
     "performance": 0,
     "security_stability": 2,
     "user_value": 1
    },
    "rationale": "PR does not alter mempool or policy behavior."
   },
   {
    "name": "utils",
    "member": true,
    "evidence": "Has the Utils/log/libs label and changes src/util/fs_helpers.cpp (+25/-15).",
    "band": "P2",
    "reason_tag": "bug fix",
    "score": 0.65,
    "factors": {
     "bug_severity": 2,
     "leverage": 0,
     "performance": 0,
     "security_stability": 2,
     "user_value": 1
    },
    "rationale": "Fixes AllocateFileRange so preallocating within existing file boundaries does not overwrite or truncate existing data on Windows and platforms using the zero-fill fallback. As described, the zero-fill fallback was zeroing out existing data when preallocating within current boundaries, violating basic file utility safety."
   },
   {
    "name": "validation",
    "member": true,
    "evidence": "Modifies ChainstateManager::LoadGenesisBlock in src/validation.cpp to manage block index recovery and prevent blk00000.dat corruption.",
    "band": "P2",
    "reason_tag": "bug fix",
    "score": 0.72,
    "factors": {
     "bug_severity": 2,
     "leverage": 0,
     "performance": 0,
     "security_stability": 2,
     "user_value": 1
    },
    "rationale": "Fixes silent block data destruction during -reindex on platforms without native posix_fallocate (like *BSD and OmniOS). Issue #33128 reported that recovering from a missing block index caused subsequent block data in blk00000.dat to be zeroed out, leaving nodes stuck at height 0; this PR preserves chain integrity by checking for an existing genesis block before writing."
   }
  ],
  "confidence": "high",
  "uncertainties": [],
  "needs": []
 },
 "cost_usd": 0.0,
 "source": {
  "record": "2026-10-01T01:22:02+00:00",
  "code": "2026-09-28T01:22:12+00:00",
  "judgments": 3
 }
}