@@ -238,30 +238,25 @@ bool CoinStatsIndex::CustomAppend(const interfaces::BlockInfo& block)
238
238
}
239
239
240
240
[[nodiscard]] static bool CopyHeightIndexToHashIndex (CDBIterator& db_it, CDBBatch& batch,
241
- const std::string& index_name,
242
- int start_height, int stop_height)
241
+ const std::string& index_name, int height)
243
242
{
244
- DBHeightKey key{start_height };
243
+ DBHeightKey key{height };
245
244
db_it.Seek (key);
246
245
247
- for (int height = start_height; height <= stop_height; ++height) {
248
- if (!db_it.GetKey (key) || key.height != height) {
249
- LogError (" %s: unexpected key in %s: expected (%c, %d)\n " ,
250
- __func__, index_name, DB_BLOCK_HEIGHT, height);
251
- return false ;
252
- }
253
-
254
- std::pair<uint256, DBVal> value;
255
- if (!db_it.GetValue (value)) {
256
- LogError (" %s: unable to read value in %s at key (%c, %d)\n " ,
257
- __func__, index_name, DB_BLOCK_HEIGHT, height);
258
- return false ;
259
- }
260
-
261
- batch.Write (DBHashKey (value.first ), std::move (value.second ));
246
+ if (!db_it.GetKey (key) || key.height != height) {
247
+ LogError (" %s: unexpected key in %s: expected (%c, %d)\n " ,
248
+ __func__, index_name, DB_BLOCK_HEIGHT, height);
249
+ return false ;
250
+ }
262
251
263
- db_it.Next ();
252
+ std::pair<uint256, DBVal> value;
253
+ if (!db_it.GetValue (value)) {
254
+ LogError (" %s: unable to read value in %s at key (%c, %d)\n " ,
255
+ __func__, index_name, DB_BLOCK_HEIGHT, height);
256
+ return false ;
264
257
}
258
+
259
+ batch.Write (DBHashKey (value.first ), std::move (value.second ));
265
260
return true ;
266
261
}
267
262
@@ -270,10 +265,9 @@ bool CoinStatsIndex::CustomRemove(const interfaces::BlockInfo& block)
270
265
CDBBatch batch (*m_db);
271
266
std::unique_ptr<CDBIterator> db_it (m_db->NewIterator ());
272
267
273
- // During a reorg, we need to copy all hash digests for blocks that are
274
- // getting disconnected from the height index to the hash index so we can
275
- // still find them when the height index entries are overwritten.
276
- if (!CopyHeightIndexToHashIndex (*db_it, batch, m_name, block.height - 1 , block.height )) {
268
+ // During a reorg, copy the block's hash digest from the height index to the hash index,
269
+ // ensuring it's still accessible after the height index entry is overwritten.
270
+ if (!CopyHeightIndexToHashIndex (*db_it, batch, m_name, block.height )) {
277
271
return false ;
278
272
}
279
273
0 commit comments