1 major finding · 3 suggestions
Findings
[major] · Genesis reuse leaves the next block write at offset zero
With a missing block index and empty or wiped coins, reusing genesis leaves file accounting at zero, so the next accepted block overwrites genesis even with working posix_fallocate().
Location: src/validation.cpp:5040 (head).
Details and suggested fix
Deleting both blocks/index and chainstate while retaining the block files reaches this branch with file 0's nSize == 0. A missing index plus -reindex-chainstate also reaches it because that option wipes coins without selecting block-file reindexing. The successful read skips the accounting formerly performed by WriteBlock(), and ReceivedBlockTransactions() only records genesis at {0,8} with BLOCK_HAVE_DATA. The next ordinary WriteBlock() consequently writes its storage header at offset 0 and its payload at offset 8, overlapping genesis's indexed position. This overwrite is independent of the allocation backend; a zero-filling allocator can additionally erase later records. Once flushed, the bad positions survive restart. Checked reads of genesis fail, including during a later chainstate rebuild, while getblock(genesis_hash, 0) can return the replacement block's bytes. Call m_blockman.UpdateBlockInfo(genesis_block, 0, blockPos) in the reuse branch before ReceivedBlockTransactions(), as AcceptBlock() does for known disk positions. That records the 293-byte end without allocating and places the next block's payload at offset 301.
Optional improvements
[suggestion] · Fresh initialization reports two errors for the expected genesis probe miss
A normal first start with no blk00000.dat now emits two error-level diagnostics before successfully creating genesis.
Location: src/validation.cpp:5040 (head).
Details and suggested fix
The probe reaches ReadRawBlock(), which opens blk00000.dat read-only. On a fresh datadir, FlatFileSeq::Open() therefore logs Unable to open file, and ReadRawBlock() logs OpenBlockFile failed ... while reading raw block. The fallback then writes genesis normally, leaving two error messages for successful initialization. The logging guidance reserves LogError for severe problems requiring shutdown. Skip the probe when !fs::exists(m_blockman.GetBlockPosFilename({0, 0})); this avoids the expected missing-file diagnostics while preserving reuse and diagnostics for an existing file that cannot be read.
[suggestion] · Recovery coverage does not distinguish genesis reuse from rewrite
Where posix_fallocate() succeeds, the recovery test's height assertion also permits rewriting genesis, and it never checks the next write after reuse.
Location: src/validation.cpp:5040 (head).
Details and suggested fix
feature_reindex_init.py starts with a height-200 fixture, deletes only blocks/index, expects initialization failure, then checks the height after reindexing. The base rewrite can satisfy those assertions when allocation preserves existing bytes. Wrap the failing start in node.assert_debug_log([], unexpected_msgs=["Pre-allocating up to position"]) with validation debug logging enabled to distinguish reuse from allocation. Also cover accounting with retained block files, an empty index and empty coins, followed by one accepted block. Compare getblock(genesis_hash, 0) with the saved genesis hex; merely requiring the RPC to succeed misses an overlapping write because it reads raw bytes without checking their hash.
[suggestion] · Genesis reuse checks the header but not its transaction payload
With a missing index, deserializable coinbase corruption under an intact genesis header passes the probe and later makes activation fail, whereas the base rewrote the canonical record.
Location: src/validation.cpp:5040 (head).
Details and suggested fix
ReadBlock() checks proof of work and the expected header hash, but it does not recompute the transaction merkle root. This branch discards existing_block and records the hardcoded genesis as stored at {0,8}, so an intact header with a locally corrupted, still-deserializable coinbase is reused. With an empty coins view, activation rereads that record and ConnectBlock() detects the merkle mismatch, producing Corrupt block found indicating potential hardware failure. No invalid chain state is accepted. Require BlockMerkleRoot(existing_block) == genesis_block.hashMerkleRoot before reuse, and restore a canonical copy on mismatch. That repair must avoid allocating over retained later blocks rather than blindly taking the allocating WriteBlock() fallback.