SaveBlockToDisk
/ FindBlockPos
are used for two purposes, depending on whether they are called during reindexing (dbp
set, fKnown = true
) or in the “normal” case when adding new blocks (dbp == nullptr
, fKnown = false
).
The actual tasks are quite different
- In normal mode, preparations for saving a new block are made, which is then saved: find the correct position on disk (maybe skipping to a new blk file), check for available disk space, update the blockfile info db, save the block.
- during reindex, most of this is not necessary (the block is already on disk after all), only the blockfile info needs to rebuilt because reindex wiped the leveldb it’s saved in.
Using one function with many conditional statements for this leads to code that is hard to read / understand and bug-prone:
- many code paths in
FindBlockPos
are conditional onfKnown
or!fKnown
- It’s not really clear what actually needs to be done during reindex (we don’t need to “save a block to disk” or “find a block pos” as the function names suggest)
- logic that should be applied to only one of the two modes is sometimes applied to both (see first commit, or #27039)
#24858 and #27039 were recent bugs directly related to the differences between reindexing and normal mode, and in both cases the simple fix took a long time to be reviewed and merged.
This PR proposes to clean this code up by splitting out the reindex logic into a separate function (UpdateBlockInfo
) which will be called directly from validation. As a result, SaveBlockToDisk
and FindBlockPos
only need to cover the non-reindex logic.