2 minor findings · 2 suggestions
Findings
[minor] · Fresh initialization reports two spurious genesis read errors
The new probe emits two error-level diagnostics for the expected absence of blk00000.dat, even though fresh non-reindex initialization then creates genesis successfully.
Location: src/validation.cpp:5042 (head).
Details and suggested fix
On a fresh start, CompleteChainstateInitialization() reaches this probe with an empty block index and no blk00000.dat. ReadRawBlock() requests a read-only open, so FlatFileSeq::Open() cannot use its file-creation fallback and logs Unable to open file. ReadRawBlock() then logs OpenBlockFile failed before WriteBlock() creates genesis normally. Base went directly to the creating writer. Node state is unaffected, but these diagnostics report a normal first-start condition as an error. Skip the probe when fs::exists(m_blockman.GetBlockPosFilename({0, 0})) is false. That preserves genesis reuse and diagnostics for unreadable existing files.
[minor] · Genesis reuse does not check the stored transaction commitment
A parseable genesis coinbase corruption can survive the new probe and cause fatal activation during empty-index, empty-coins recovery, instead of the previous canonical rewrite.
Location: src/validation.cpp:5042 (head).
Details and suggested fix
On a start without -reindex, ReadBlock(..., genesis_hash) verifies header identity but does not recompute the transaction Merkle root. A same-length change to the genesis coinbase's scriptSig text therefore passes. This branch discards existing_block and records the canonical in-memory genesis at {0, 8} as BLOCK_HAVE_DATA and BLOCK_VALID_TRANSACTIONS. If the coins view is empty, activation rereads those bytes and CheckBlock() returns BLOCK_MUTATED, causing the fatal corruption error before the genesis connection exemption. This includes deleting both blocks/index and chainstate, or deleting the index and using -reindex-chainstate. Base rewrote canonical genesis on this path. Check BlockMerkleRoot(existing_block) against the configured genesis root before recording reuse, and repair a mismatching payload in place without calling Allocate(), which would reintroduce the overwrite hazard. No invalid chain is accepted, and no production occurrence of this local-corruption pattern was identified.
Optional improvements
[suggestion] · Recovery test misses the no-rewrite guarantee
The recovery test would still pass after restoring the unconditional genesis write whenever allocation preserves its existing block data.
Location: src/validation.cpp:5042 (head).
Details and suggested fix
feature_reindex_init.py stops a height-200 fixture, deletes blocks/index, checks the initialization error, then reindexes and checks height 200. An identical genesis rewrite preserves those outcomes with successful posix_fallocate(). The fixture also fits within the first 16 MiB chunk, so macOS and Windows truncation need not remove any blocks. The NetBSD cross job runs neither test class. The connect_block fuzz setup appears to catch missing UpdateBlockInfo() indirectly, but does not assert that reuse avoided a rewrite. Wrap only assert_start_raises_init_error() in node.assert_debug_log(expected_msgs=[], unexpected_msgs=["Pre-allocating up to position"]), with validation debug logging enabled. FlatFileSeq::Allocate() emits that message before choosing an allocation backend, so this detects restoration of the old rewrite portably. Keep the recovered-height assertion.
[suggestion] · Comment omits the truncation hazard on macOS and Windows
The same empty-index genesis rewrite can also truncate a large blk00000.dat to 16 MiB on macOS and Windows, which the new comment does not explain.
Location: src/validation.cpp:5035 (head).
Details and suggested fix
With empty blockfile metadata, the old WriteBlock() path requests the first default 16 MiB allocation from offset zero. The Windows SetEndOfFile() and macOS ftruncate() branches set the file length to offset + length, so a larger blk00000.dat loses its data beyond that boundary. The reuse path avoids this as well as zero-filling. Document the underlying AllocateFileRange() precondition that its allocation range must not contain live data, and explain that violating it can zero-fill or truncate the file depending on the platform.