From b2e8b64ddc351124ac1390ee906a8fcd2781ca50 Mon Sep 17 00:00:00 2001 From: Fabian Jahr Date: Sat, 16 Aug 2025 00:20:29 +0200 Subject: [PATCH] index, refactor: Append blocks to coinstatsindex without db read --- src/index/coinstatsindex.cpp | 14 +++++++------- src/index/coinstatsindex.h | 2 ++ 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/index/coinstatsindex.cpp b/src/index/coinstatsindex.cpp index bb4be90bf40..e4daf9ef425 100644 --- a/src/index/coinstatsindex.cpp +++ b/src/index/coinstatsindex.cpp @@ -137,16 +137,12 @@ bool CoinStatsIndex::CustomAppend(const interfaces::BlockInfo& block) // Ignore genesis block if (block.height > 0) { - std::pair read_out; - if (!m_db->Read(DBHeightKey(block.height - 1), read_out)) { - return false; - } - uint256 expected_block_hash{*Assert(block.prev_hash)}; - if (read_out.first != expected_block_hash) { + if (m_current_block_hash != expected_block_hash) { LogWarning("previous block header belongs to unexpected block %s; expected %s", - read_out.first.ToString(), expected_block_hash.ToString()); + m_current_block_hash.ToString(), expected_block_hash.ToString()); + std::pair read_out; if (!m_db->Read(DBHashKey(expected_block_hash), read_out)) { LogError("previous block header not found; expected %s", expected_block_hash.ToString()); @@ -240,6 +236,8 @@ bool CoinStatsIndex::CustomAppend(const interfaces::BlockInfo& block) m_muhash.Finalize(out); value.second.muhash = out; + m_current_block_hash = block.hash; + // 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(block.height), value); @@ -373,6 +371,7 @@ bool CoinStatsIndex::CustomInit(const std::optional& block m_total_unspendables_bip30 = entry.total_unspendables_bip30; m_total_unspendables_scripts = entry.total_unspendables_scripts; m_total_unspendables_unclaimed_rewards = entry.total_unspendables_unclaimed_rewards; + m_current_block_hash = block->hash; } return true; @@ -466,6 +465,7 @@ bool CoinStatsIndex::RevertBlock(const interfaces::BlockInfo& block) m_total_unspendables_bip30 = read_out.second.total_unspendables_bip30; m_total_unspendables_scripts = read_out.second.total_unspendables_scripts; m_total_unspendables_unclaimed_rewards = read_out.second.total_unspendables_unclaimed_rewards; + m_current_block_hash = *block.prev_hash; return true; } diff --git a/src/index/coinstatsindex.h b/src/index/coinstatsindex.h index 5dcbc186415..7e48f4c4eef 100644 --- a/src/index/coinstatsindex.h +++ b/src/index/coinstatsindex.h @@ -38,6 +38,8 @@ private: CAmount m_total_unspendables_scripts{0}; CAmount m_total_unspendables_unclaimed_rewards{0}; + uint256 m_current_block_hash{}; + [[nodiscard]] bool RevertBlock(const interfaces::BlockInfo& block); bool AllowPrune() const override { return true; }