Currently, if the reindex process is interrupted it will start over on next run at blk00000.dat. Even after reindexing is finished when the node is in ActivateBestChain() an interruption may STILL require a full reindex process because DB_REINDEX_FLAG is written false, but not flushed.
Mentioned in #30424 but I couldn't find any specific follow-up:
There is no reindex progess (it should pick up the previous work and try to make progess)
The solution in this PR is simply to write a new field DB_REINDEX_LASTFILE after every block is reindexed, and flush the DB_REINDEX_FLAG setting when the process is complete. The complication is that blocks may be out of order on disk and so as we reindex we store orphan blocks temporarily in memory until they are reconnected with their parent in later files. To ensure that data is recovered, the orphan map is serialized and also saved to the database as DB_REINDEX_ORPHAN_BLOCKS.
I ran a full chain reindex on a Debian desktop CPUx8 32GB up to block 944994, the results were not super hot: master: 3.5 hours branch: 3.75 hours
This is a bit of a drag so I'm opening as a draft for now to get feedback and run more tests.
One idea I had is to either throttle the write to every 100 block files, or only trigger the write at all inside if (chainman.m_interrupt) which would be okay for user interruptions but not hardware or power failure.