Skip to content

Commit 91b7ab6

Browse files
committed
refactor: index, simplify CopyHeightIndexToHashIndex to process single block
The function previously handled a range of heights but now copies only a single entry. This change simplifies the code to match its current usage.
1 parent 6f1392c commit 91b7ab6

File tree

2 files changed

+35
-46
lines changed

2 files changed

+35
-46
lines changed

src/index/blockfilterindex.cpp

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -289,30 +289,25 @@ bool BlockFilterIndex::Write(const BlockFilter& filter, uint32_t block_height, c
289289
}
290290

291291
[[nodiscard]] static bool CopyHeightIndexToHashIndex(CDBIterator& db_it, CDBBatch& batch,
292-
const std::string& index_name,
293-
int start_height, int stop_height)
292+
const std::string& index_name, int height)
294293
{
295-
DBHeightKey key(start_height);
294+
DBHeightKey key(height);
296295
db_it.Seek(key);
297296

298-
for (int height = start_height; height <= stop_height; ++height) {
299-
if (!db_it.GetKey(key) || key.height != height) {
300-
LogError("%s: unexpected key in %s: expected (%c, %d)\n",
301-
__func__, index_name, DB_BLOCK_HEIGHT, height);
302-
return false;
303-
}
304-
305-
std::pair<uint256, DBVal> value;
306-
if (!db_it.GetValue(value)) {
307-
LogError("%s: unable to read value in %s at key (%c, %d)\n",
308-
__func__, index_name, DB_BLOCK_HEIGHT, height);
309-
return false;
310-
}
311-
312-
batch.Write(DBHashKey(value.first), std::move(value.second));
297+
if (!db_it.GetKey(key) || key.height != height) {
298+
LogError("%s: unexpected key in %s: expected (%c, %d)\n",
299+
__func__, index_name, DB_BLOCK_HEIGHT, height);
300+
return false;
301+
}
313302

314-
db_it.Next();
303+
std::pair<uint256, DBVal> value;
304+
if (!db_it.GetValue(value)) {
305+
LogError("%s: unable to read value in %s at key (%c, %d)\n",
306+
__func__, index_name, DB_BLOCK_HEIGHT, height);
307+
return false;
315308
}
309+
310+
batch.Write(DBHashKey(value.first), std::move(value.second));
316311
return true;
317312
}
318313

@@ -321,10 +316,10 @@ bool BlockFilterIndex::CustomRemove(const interfaces::BlockInfo& block)
321316
CDBBatch batch(*m_db);
322317
std::unique_ptr<CDBIterator> db_it(m_db->NewIterator());
323318

324-
// During a reorg, we need to copy all filters for blocks that are getting disconnected from the
325-
// height index to the hash index so we can still find them when the height index entries are
326-
// overwritten.
327-
if (!CopyHeightIndexToHashIndex(*db_it, batch, m_name, block.height - 1, block.height)) {
319+
// During a reorg, we need to copy block filter that is getting disconnected from the
320+
// height index to the hash index so we can still find it when the height index entry
321+
// is overwritten.
322+
if (!CopyHeightIndexToHashIndex(*db_it, batch, m_name, block.height)) {
328323
return false;
329324
}
330325

src/index/coinstatsindex.cpp

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -238,30 +238,25 @@ bool CoinStatsIndex::CustomAppend(const interfaces::BlockInfo& block)
238238
}
239239

240240
[[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)
243242
{
244-
DBHeightKey key{start_height};
243+
DBHeightKey key{height};
245244
db_it.Seek(key);
246245

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+
}
262251

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;
264257
}
258+
259+
batch.Write(DBHashKey(value.first), std::move(value.second));
265260
return true;
266261
}
267262

@@ -270,10 +265,9 @@ bool CoinStatsIndex::CustomRemove(const interfaces::BlockInfo& block)
270265
CDBBatch batch(*m_db);
271266
std::unique_ptr<CDBIterator> db_it(m_db->NewIterator());
272267

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)) {
277271
return false;
278272
}
279273

0 commit comments

Comments
 (0)