Fixes #24076
Coinstatsindex currently writes the MuHash (DB_MUHASH
) to disk in CoinStatsIndex::WriteBlock()
and CoinStatsIndex::ReverseBlock()
, but the best synced block is written in BaseIndex::Commit()
. These are called at different points in time, both during the ThreadSync phase, and also after the initial sync is finished and validation callbacks (BlockConnected()
vs ChainStateFlushed()
) perform the syncing.
As a result, the index DB is temporarily in an inconsistent state, and if bitcoind is terminated uncleanly (so that there is no time to call Commit()
by receiving an interrupt or by flushing the chainstate) this leads to problems:
On the next startup, Init()
will read the best block and a MuHash that corresponds to a different (higher) block. Indexing will be picked up at the the best block processing some blocks again, but since MuHash is a rolling hash, it will process some utxos twice and the muhashes for all future blocks will be wrong, as was observed in #24076.
Fix this by always committing DB_MUHASH
together with DB_BEST_BLOCK
.
Note that the block data for the index is still written at different times, but this does not corrupt the index - at worst, these entries will be processed another time and overwritten after an unclean shutdown and restart.