index, refactor: Append blocks to coinstatsindex without db read

This commit is contained in:
Fabian Jahr 2025-08-16 00:20:29 +02:00
parent 431a076ae6
commit b2e8b64ddc
No known key found for this signature in database
GPG Key ID: F13D1E9D890798CD
2 changed files with 9 additions and 7 deletions

View File

@ -137,16 +137,12 @@ bool CoinStatsIndex::CustomAppend(const interfaces::BlockInfo& block)
// Ignore genesis block
if (block.height > 0) {
std::pair<uint256, DBVal> 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<uint256, DBVal> 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<interfaces::BlockRef>& 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;
}

View File

@ -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; }