aboutsummaryrefslogtreecommitdiff
path: root/src/index
diff options
context:
space:
mode:
authorMartin Zumsande <mzumsande@gmail.com>2022-01-24 14:34:28 +0100
committerMartin Zumsande <mzumsande@gmail.com>2022-02-21 11:37:19 -0500
commiteb6cc05da32c5bde122725a0bc907d3767a791cd (patch)
tree5a9b70c648c3c1232b2feb1e2306345106ebba34 /src/index
parent48a90c61e2269e6de78416001dd28bf3e12d9ebe (diff)
index: Commit DB_MUHASH and DB_BEST_BLOCK to disk together
If these are written to disk at different times, unclean shutdowns can lead to index corruption.
Diffstat (limited to 'src/index')
-rw-r--r--src/index/coinstatsindex.cpp17
-rw-r--r--src/index/coinstatsindex.h2
2 files changed, 14 insertions, 5 deletions
diff --git a/src/index/coinstatsindex.cpp b/src/index/coinstatsindex.cpp
index a1c8a5937c..386eb67ce9 100644
--- a/src/index/coinstatsindex.cpp
+++ b/src/index/coinstatsindex.cpp
@@ -228,10 +228,9 @@ bool CoinStatsIndex::WriteBlock(const CBlock& block, const CBlockIndex* pindex)
m_muhash.Finalize(out);
value.second.muhash = out;
- CDBBatch batch(*m_db);
- batch.Write(DBHeightKey(pindex->nHeight), value);
- batch.Write(DB_MUHASH, m_muhash);
- return m_db->WriteBatch(batch);
+ // Intentionally do not update DB_MUHASH here so it stays in sync with
+ // DB_BEST_BLOCK, and the index is not corrupted if there is an unclean shutdown.
+ return m_db->Write(DBHeightKey(pindex->nHeight), value);
}
static bool CopyHeightIndexToHashIndex(CDBIterator& db_it, CDBBatch& batch,
@@ -388,6 +387,14 @@ bool CoinStatsIndex::Init()
return true;
}
+bool CoinStatsIndex::CommitInternal(CDBBatch& batch)
+{
+ // DB_MUHASH should always be committed in a batch together with DB_BEST_BLOCK
+ // to prevent an inconsistent state of the DB.
+ batch.Write(DB_MUHASH, m_muhash);
+ return BaseIndex::CommitInternal(batch);
+}
+
// Reverse a single block as part of a reorg
bool CoinStatsIndex::ReverseBlock(const CBlock& block, const CBlockIndex* pindex)
{
@@ -489,5 +496,5 @@ bool CoinStatsIndex::ReverseBlock(const CBlock& block, const CBlockIndex* pindex
Assert(m_total_unspendables_scripts == read_out.second.total_unspendables_scripts);
Assert(m_total_unspendables_unclaimed_rewards == read_out.second.total_unspendables_unclaimed_rewards);
- return m_db->Write(DB_MUHASH, m_muhash);
+ return true;
}
diff --git a/src/index/coinstatsindex.h b/src/index/coinstatsindex.h
index d2a6c9c964..24190ac137 100644
--- a/src/index/coinstatsindex.h
+++ b/src/index/coinstatsindex.h
@@ -39,6 +39,8 @@ private:
protected:
bool Init() override;
+ bool CommitInternal(CDBBatch& batch) override;
+
bool WriteBlock(const CBlock& block, const CBlockIndex* pindex) override;
bool Rewind(const CBlockIndex* current_tip, const CBlockIndex* new_tip) override;