Skip to content

Commit ee3a079

Browse files
committed
indexes, refactor: Remove CBlockIndex* uses in index Rewind methods
Replace Rewind method with CustomRewind and pass block hashes and heights instead of CBlockIndex* pointers This commit does not change behavior in any way.
1 parent dc971be commit ee3a079

File tree

6 files changed

+19
-15
lines changed

6 files changed

+19
-15
lines changed

src/index/base.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,10 @@ bool BaseIndex::Rewind(const CBlockIndex* current_tip, const CBlockIndex* new_ti
230230
assert(current_tip == m_best_block_index);
231231
assert(current_tip->GetAncestor(new_tip->nHeight) == new_tip);
232232

233+
if (!CustomRewind({current_tip->GetBlockHash(), current_tip->nHeight}, {new_tip->GetBlockHash(), new_tip->nHeight})) {
234+
return false;
235+
}
236+
233237
// In the case of a reorg, ensure persisted block locator is not stale.
234238
// Pruning has a minimum of 288 blocks-to-keep and getting the index
235239
// out of sync may be possible but a users fault.

src/index/base.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ class BaseIndex : public CValidationInterface
8383
/// getting corrupted.
8484
bool Commit();
8585

86+
/// Loop over disconnected blocks and call CustomRewind.
87+
bool Rewind(const CBlockIndex* current_tip, const CBlockIndex* new_tip);
88+
8689
virtual bool AllowPrune() const = 0;
8790

8891
protected:
@@ -105,7 +108,7 @@ class BaseIndex : public CValidationInterface
105108

106109
/// Rewind index to an earlier chain tip during a chain reorg. The tip must
107110
/// be an ancestor of the current best block.
108-
virtual bool Rewind(const CBlockIndex* current_tip, const CBlockIndex* new_tip);
111+
[[nodiscard]] virtual bool CustomRewind(const interfaces::BlockKey& current_tip, const interfaces::BlockKey& new_tip) { return true; }
109112

110113
virtual DB& GetDB() const = 0;
111114

src/index/blockfilterindex.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -287,17 +287,15 @@ static bool CopyHeightIndexToHashIndex(CDBIterator& db_it, CDBBatch& batch,
287287
return true;
288288
}
289289

290-
bool BlockFilterIndex::Rewind(const CBlockIndex* current_tip, const CBlockIndex* new_tip)
290+
bool BlockFilterIndex::CustomRewind(const interfaces::BlockKey& current_tip, const interfaces::BlockKey& new_tip)
291291
{
292-
assert(current_tip->GetAncestor(new_tip->nHeight) == new_tip);
293-
294292
CDBBatch batch(*m_db);
295293
std::unique_ptr<CDBIterator> db_it(m_db->NewIterator());
296294

297295
// During a reorg, we need to copy all filters for blocks that are getting disconnected from the
298296
// height index to the hash index so we can still find them when the height index entries are
299297
// overwritten.
300-
if (!CopyHeightIndexToHashIndex(*db_it, batch, m_name, new_tip->nHeight, current_tip->nHeight)) {
298+
if (!CopyHeightIndexToHashIndex(*db_it, batch, m_name, new_tip.height, current_tip.height)) {
301299
return false;
302300
}
303301

@@ -307,7 +305,7 @@ bool BlockFilterIndex::Rewind(const CBlockIndex* current_tip, const CBlockIndex*
307305
batch.Write(DB_FILTER_POS, m_next_filter_pos);
308306
if (!m_db->WriteBatch(batch)) return false;
309307

310-
return BaseIndex::Rewind(current_tip, new_tip);
308+
return true;
311309
}
312310

313311
static bool LookupOne(const CDBWrapper& db, const CBlockIndex* block_index, DBVal& result)

src/index/blockfilterindex.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class BlockFilterIndex final : public BaseIndex
4747

4848
bool CustomAppend(const interfaces::BlockInfo& block) override;
4949

50-
bool Rewind(const CBlockIndex* current_tip, const CBlockIndex* new_tip) override;
50+
bool CustomRewind(const interfaces::BlockKey& current_tip, const interfaces::BlockKey& new_tip) override;
5151

5252
BaseIndex::DB& GetDB() const override { return *m_db; }
5353

src/index/coinstatsindex.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -265,25 +265,24 @@ static bool CopyHeightIndexToHashIndex(CDBIterator& db_it, CDBBatch& batch,
265265
return true;
266266
}
267267

268-
bool CoinStatsIndex::Rewind(const CBlockIndex* current_tip, const CBlockIndex* new_tip)
268+
bool CoinStatsIndex::CustomRewind(const interfaces::BlockKey& current_tip, const interfaces::BlockKey& new_tip)
269269
{
270-
assert(current_tip->GetAncestor(new_tip->nHeight) == new_tip);
271-
272270
CDBBatch batch(*m_db);
273271
std::unique_ptr<CDBIterator> db_it(m_db->NewIterator());
274272

275273
// During a reorg, we need to copy all hash digests for blocks that are
276274
// getting disconnected from the height index to the hash index so we can
277275
// still find them when the height index entries are overwritten.
278-
if (!CopyHeightIndexToHashIndex(*db_it, batch, m_name, new_tip->nHeight, current_tip->nHeight)) {
276+
if (!CopyHeightIndexToHashIndex(*db_it, batch, m_name, new_tip.height, current_tip.height)) {
279277
return false;
280278
}
281279

282280
if (!m_db->WriteBatch(batch)) return false;
283281

284282
{
285283
LOCK(cs_main);
286-
const CBlockIndex* iter_tip{m_chainstate->m_blockman.LookupBlockIndex(current_tip->GetBlockHash())};
284+
const CBlockIndex* iter_tip{m_chainstate->m_blockman.LookupBlockIndex(current_tip.hash)};
285+
const CBlockIndex* new_tip_index{m_chainstate->m_blockman.LookupBlockIndex(new_tip.hash)};
287286
const auto& consensus_params{Params().GetConsensus()};
288287

289288
do {
@@ -297,10 +296,10 @@ bool CoinStatsIndex::Rewind(const CBlockIndex* current_tip, const CBlockIndex* n
297296
ReverseBlock(block, iter_tip);
298297

299298
iter_tip = iter_tip->GetAncestor(iter_tip->nHeight - 1);
300-
} while (new_tip != iter_tip);
299+
} while (new_tip_index != iter_tip);
301300
}
302301

303-
return BaseIndex::Rewind(current_tip, new_tip);
302+
return true;
304303
}
305304

306305
static bool LookUpOne(const CDBWrapper& db, const interfaces::BlockKey& block, DBVal& result)

src/index/coinstatsindex.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class CoinStatsIndex final : public BaseIndex
4545

4646
bool CustomAppend(const interfaces::BlockInfo& block) override;
4747

48-
bool Rewind(const CBlockIndex* current_tip, const CBlockIndex* new_tip) override;
48+
bool CustomRewind(const interfaces::BlockKey& current_tip, const interfaces::BlockKey& new_tip) override;
4949

5050
BaseIndex::DB& GetDB() const override { return *m_db; }
5151

0 commit comments

Comments
 (0)