Skip to content

Commit bef4e40

Browse files
committed
indexes, refactor: Remove CBlockIndex* uses in index Init methods
Replace overriden index Init() methods that use the best block CBlockIndex* pointer with pure CustomInit() callbacks that are passed the block hash and height. This gets rid of more CBlockIndex* pointer uses so indexes can work outside the bitcoin-node process. It also simplifies the initialization call sequence so index implementations are not responsible for initializing the base class. There is a slight change in behavior here since now the best block pointer is loaded and checked before the custom index init functions are called instead of while they are called.
1 parent addb4f2 commit bef4e40

File tree

6 files changed

+15
-15
lines changed

6 files changed

+15
-15
lines changed

src/index/base.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,10 @@ bool BaseIndex::Start()
359359
// Need to register this ValidationInterface before running Init(), so that
360360
// callbacks are not missed if Init sets m_synced to true.
361361
RegisterValidationInterface(this);
362-
if (!Init()) {
362+
if (!Init()) return false;
363+
364+
const CBlockIndex* index = m_best_block_index.load();
365+
if (!CustomInit(index ? std::make_optional(interfaces::BlockKey{index->GetBlockHash(), index->nHeight}) : std::nullopt)) {
363366
return false;
364367
}
365368

src/index/base.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ class BaseIndex : public CValidationInterface
6363
std::thread m_thread_sync;
6464
CThreadInterrupt m_interrupt;
6565

66+
/// Read best block locator and check that data needed to sync has not been pruned.
67+
bool Init();
68+
6669
/// Sync the index with the block index starting from the current best block.
6770
/// Intended to be run in its own thread, m_thread_sync, and can be
6871
/// interrupted with m_interrupt. Once the index gets in sync, the m_synced
@@ -90,10 +93,8 @@ class BaseIndex : public CValidationInterface
9093

9194
void ChainStateFlushed(const CBlockLocator& locator) override;
9295

93-
const CBlockIndex* CurrentIndex() { return m_best_block_index.load(); };
94-
9596
/// Initialize internal state from the database and block index.
96-
[[nodiscard]] virtual bool Init();
97+
[[nodiscard]] virtual bool CustomInit(const std::optional<interfaces::BlockKey>& block) { return true; }
9798

9899
/// Write update index entries for a newly connected block.
99100
virtual bool WriteBlock(const CBlock& block, const CBlockIndex* pindex) { return true; }

src/index/blockfilterindex.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ BlockFilterIndex::BlockFilterIndex(std::unique_ptr<interfaces::Chain> chain, Blo
109109
m_filter_fileseq = std::make_unique<FlatFileSeq>(std::move(path), "fltr", FLTR_FILE_CHUNK_SIZE);
110110
}
111111

112-
bool BlockFilterIndex::Init()
112+
bool BlockFilterIndex::CustomInit(const std::optional<interfaces::BlockKey>& block)
113113
{
114114
if (!m_db->Read(DB_FILTER_POS, m_next_filter_pos)) {
115115
// Check that the cause of the read failure is that the key does not exist. Any other errors
@@ -124,7 +124,7 @@ bool BlockFilterIndex::Init()
124124
m_next_filter_pos.nFile = 0;
125125
m_next_filter_pos.nPos = 0;
126126
}
127-
return BaseIndex::Init();
127+
return true;
128128
}
129129

130130
bool BlockFilterIndex::CommitInternal(CDBBatch& batch)

src/index/blockfilterindex.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class BlockFilterIndex final : public BaseIndex
4141
bool AllowPrune() const override { return true; }
4242

4343
protected:
44-
bool Init() override;
44+
bool CustomInit(const std::optional<interfaces::BlockKey>& block) override;
4545

4646
bool CommitInternal(CDBBatch& batch) override;
4747

src/index/coinstatsindex.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ std::optional<CCoinsStats> CoinStatsIndex::LookUpStats(const CBlockIndex* block_
345345
return stats;
346346
}
347347

348-
bool CoinStatsIndex::Init()
348+
bool CoinStatsIndex::CustomInit(const std::optional<interfaces::BlockKey>& block)
349349
{
350350
if (!m_db->Read(DB_MUHASH, m_muhash)) {
351351
// Check that the cause of the read failure is that the key does not
@@ -357,13 +357,9 @@ bool CoinStatsIndex::Init()
357357
}
358358
}
359359

360-
if (!BaseIndex::Init()) return false;
361-
362-
const CBlockIndex* pindex{CurrentIndex()};
363-
364-
if (pindex) {
360+
if (block) {
365361
DBVal entry;
366-
if (!LookUpOne(*m_db, {pindex->GetBlockHash(), pindex->nHeight}, entry)) {
362+
if (!LookUpOne(*m_db, *block, entry)) {
367363
return error("%s: Cannot read current %s state; index may be corrupted",
368364
__func__, GetName());
369365
}

src/index/coinstatsindex.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class CoinStatsIndex final : public BaseIndex
3939
bool AllowPrune() const override { return true; }
4040

4141
protected:
42-
bool Init() override;
42+
bool CustomInit(const std::optional<interfaces::BlockKey>& block) override;
4343

4444
bool CommitInternal(CDBBatch& batch) override;
4545

0 commit comments

Comments
 (0)