diff --git a/src/index/blockfilterindex.cpp b/src/index/blockfilterindex.cpp index 460d5d7c41b..de55170daa7 100644 --- a/src/index/blockfilterindex.cpp +++ b/src/index/blockfilterindex.cpp @@ -289,30 +289,25 @@ bool BlockFilterIndex::Write(const BlockFilter& filter, uint32_t block_height, c } [[nodiscard]] static bool CopyHeightIndexToHashIndex(CDBIterator& db_it, CDBBatch& batch, - const std::string& index_name, - int start_height, int stop_height) + const std::string& index_name, int height) { - DBHeightKey key(start_height); + DBHeightKey key(height); db_it.Seek(key); - for (int height = start_height; height <= stop_height; ++height) { - if (!db_it.GetKey(key) || key.height != height) { - LogError("%s: unexpected key in %s: expected (%c, %d)\n", - __func__, index_name, DB_BLOCK_HEIGHT, height); - return false; - } - - std::pair value; - if (!db_it.GetValue(value)) { - LogError("%s: unable to read value in %s at key (%c, %d)\n", - __func__, index_name, DB_BLOCK_HEIGHT, height); - return false; - } - - batch.Write(DBHashKey(value.first), std::move(value.second)); - - db_it.Next(); + if (!db_it.GetKey(key) || key.height != height) { + LogError("%s: unexpected key in %s: expected (%c, %d)\n", + __func__, index_name, DB_BLOCK_HEIGHT, height); + return false; } + + std::pair value; + if (!db_it.GetValue(value)) { + LogError("%s: unable to read value in %s at key (%c, %d)\n", + __func__, index_name, DB_BLOCK_HEIGHT, height); + return false; + } + + batch.Write(DBHashKey(value.first), std::move(value.second)); return true; } @@ -321,10 +316,10 @@ bool BlockFilterIndex::CustomRemove(const interfaces::BlockInfo& block) CDBBatch batch(*m_db); std::unique_ptr db_it(m_db->NewIterator()); - // During a reorg, we need to copy all filters for blocks that are getting disconnected from the - // height index to the hash index so we can still find them when the height index entries are - // overwritten. - if (!CopyHeightIndexToHashIndex(*db_it, batch, m_name, block.height - 1, block.height)) { + // During a reorg, we need to copy block filter that is getting disconnected from the + // height index to the hash index so we can still find it when the height index entry + // is overwritten. + if (!CopyHeightIndexToHashIndex(*db_it, batch, m_name, block.height)) { return false; } diff --git a/src/index/coinstatsindex.cpp b/src/index/coinstatsindex.cpp index 5f82cf93b7e..28e7d66e8f5 100644 --- a/src/index/coinstatsindex.cpp +++ b/src/index/coinstatsindex.cpp @@ -238,30 +238,25 @@ bool CoinStatsIndex::CustomAppend(const interfaces::BlockInfo& block) } [[nodiscard]] static bool CopyHeightIndexToHashIndex(CDBIterator& db_it, CDBBatch& batch, - const std::string& index_name, - int start_height, int stop_height) + const std::string& index_name, int height) { - DBHeightKey key{start_height}; + DBHeightKey key{height}; db_it.Seek(key); - for (int height = start_height; height <= stop_height; ++height) { - if (!db_it.GetKey(key) || key.height != height) { - LogError("%s: unexpected key in %s: expected (%c, %d)\n", - __func__, index_name, DB_BLOCK_HEIGHT, height); - return false; - } - - std::pair value; - if (!db_it.GetValue(value)) { - LogError("%s: unable to read value in %s at key (%c, %d)\n", - __func__, index_name, DB_BLOCK_HEIGHT, height); - return false; - } - - batch.Write(DBHashKey(value.first), std::move(value.second)); - - db_it.Next(); + if (!db_it.GetKey(key) || key.height != height) { + LogError("%s: unexpected key in %s: expected (%c, %d)\n", + __func__, index_name, DB_BLOCK_HEIGHT, height); + return false; } + + std::pair value; + if (!db_it.GetValue(value)) { + LogError("%s: unable to read value in %s at key (%c, %d)\n", + __func__, index_name, DB_BLOCK_HEIGHT, height); + return false; + } + + batch.Write(DBHashKey(value.first), std::move(value.second)); return true; } @@ -270,10 +265,9 @@ bool CoinStatsIndex::CustomRemove(const interfaces::BlockInfo& block) CDBBatch batch(*m_db); std::unique_ptr db_it(m_db->NewIterator()); - // During a reorg, we need to copy all hash digests for blocks that are - // getting disconnected from the height index to the hash index so we can - // still find them when the height index entries are overwritten. - if (!CopyHeightIndexToHashIndex(*db_it, batch, m_name, block.height - 1, block.height)) { + // During a reorg, copy the block's hash digest from the height index to the hash index, + // ensuring it's still accessible after the height index entry is overwritten. + if (!CopyHeightIndexToHashIndex(*db_it, batch, m_name, block.height)) { return false; }