@@ -228,10 +228,9 @@ bool CoinStatsIndex::WriteBlock(const CBlock& block, const CBlockIndex* pindex)
228
228
m_muhash.Finalize (out);
229
229
value.second .muhash = out;
230
230
231
- CDBBatch batch (*m_db);
232
- batch.Write (DBHeightKey (pindex->nHeight ), value);
233
- batch.Write (DB_MUHASH, m_muhash);
234
- return m_db->WriteBatch (batch);
231
+ // Intentionally do not update DB_MUHASH here so it stays in sync with
232
+ // DB_BEST_BLOCK, and the index is not corrupted if there is an unclean shutdown.
233
+ return m_db->Write (DBHeightKey (pindex->nHeight ), value);
235
234
}
236
235
237
236
static bool CopyHeightIndexToHashIndex (CDBIterator& db_it, CDBBatch& batch,
@@ -388,6 +387,14 @@ bool CoinStatsIndex::Init()
388
387
return true ;
389
388
}
390
389
390
+ bool CoinStatsIndex::CommitInternal (CDBBatch& batch)
391
+ {
392
+ // DB_MUHASH should always be committed in a batch together with DB_BEST_BLOCK
393
+ // to prevent an inconsistent state of the DB.
394
+ batch.Write (DB_MUHASH, m_muhash);
395
+ return BaseIndex::CommitInternal (batch);
396
+ }
397
+
391
398
// Reverse a single block as part of a reorg
392
399
bool CoinStatsIndex::ReverseBlock (const CBlock& block, const CBlockIndex* pindex)
393
400
{
@@ -489,5 +496,5 @@ bool CoinStatsIndex::ReverseBlock(const CBlock& block, const CBlockIndex* pindex
489
496
Assert (m_total_unspendables_scripts == read_out.second .total_unspendables_scripts );
490
497
Assert (m_total_unspendables_unclaimed_rewards == read_out.second .total_unspendables_unclaimed_rewards );
491
498
492
- return m_db-> Write (DB_MUHASH, m_muhash) ;
499
+ return true ;
493
500
}
0 commit comments