aboutsummaryrefslogtreecommitdiff
path: root/src/index
diff options
context:
space:
mode:
authorRyan Ofsky <ryan@ofsky.org>2022-01-17 18:36:40 -0500
committerRyan Ofsky <ryan@ofsky.org>2022-07-18 13:39:55 -0500
commitbef4e405f3de2718dfee279a9abff4daf016da26 (patch)
tree52a53b50ad0da1ae8dcf59dc1a0ef174df1381e8 /src/index
parentaddb4f2af183a25ce4a6b6485b5b49575a2ba31b (diff)
downloadbitcoin-bef4e405f3de2718dfee279a9abff4daf016da26.tar.xz
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.
Diffstat (limited to 'src/index')
-rw-r--r--src/index/base.cpp5
-rw-r--r--src/index/base.h7
-rw-r--r--src/index/blockfilterindex.cpp4
-rw-r--r--src/index/blockfilterindex.h2
-rw-r--r--src/index/coinstatsindex.cpp10
-rw-r--r--src/index/coinstatsindex.h2
6 files changed, 15 insertions, 15 deletions
diff --git a/src/index/base.cpp b/src/index/base.cpp
index 26b3653f7b..1b861df4bf 100644
--- a/src/index/base.cpp
+++ b/src/index/base.cpp
@@ -359,7 +359,10 @@ bool BaseIndex::Start()
// Need to register this ValidationInterface before running Init(), so that
// callbacks are not missed if Init sets m_synced to true.
RegisterValidationInterface(this);
- if (!Init()) {
+ if (!Init()) return false;
+
+ const CBlockIndex* index = m_best_block_index.load();
+ if (!CustomInit(index ? std::make_optional(interfaces::BlockKey{index->GetBlockHash(), index->nHeight}) : std::nullopt)) {
return false;
}
diff --git a/src/index/base.h b/src/index/base.h
index 3586376459..82317c1ec2 100644
--- a/src/index/base.h
+++ b/src/index/base.h
@@ -63,6 +63,9 @@ private:
std::thread m_thread_sync;
CThreadInterrupt m_interrupt;
+ /// Read best block locator and check that data needed to sync has not been pruned.
+ bool Init();
+
/// Sync the index with the block index starting from the current best block.
/// Intended to be run in its own thread, m_thread_sync, and can be
/// interrupted with m_interrupt. Once the index gets in sync, the m_synced
@@ -90,10 +93,8 @@ protected:
void ChainStateFlushed(const CBlockLocator& locator) override;
- const CBlockIndex* CurrentIndex() { return m_best_block_index.load(); };
-
/// Initialize internal state from the database and block index.
- [[nodiscard]] virtual bool Init();
+ [[nodiscard]] virtual bool CustomInit(const std::optional<interfaces::BlockKey>& block) { return true; }
/// Write update index entries for a newly connected block.
virtual bool WriteBlock(const CBlock& block, const CBlockIndex* pindex) { return true; }
diff --git a/src/index/blockfilterindex.cpp b/src/index/blockfilterindex.cpp
index 15a7cfeaea..f44b5ac6e8 100644
--- a/src/index/blockfilterindex.cpp
+++ b/src/index/blockfilterindex.cpp
@@ -109,7 +109,7 @@ BlockFilterIndex::BlockFilterIndex(std::unique_ptr<interfaces::Chain> chain, Blo
m_filter_fileseq = std::make_unique<FlatFileSeq>(std::move(path), "fltr", FLTR_FILE_CHUNK_SIZE);
}
-bool BlockFilterIndex::Init()
+bool BlockFilterIndex::CustomInit(const std::optional<interfaces::BlockKey>& block)
{
if (!m_db->Read(DB_FILTER_POS, m_next_filter_pos)) {
// 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()
m_next_filter_pos.nFile = 0;
m_next_filter_pos.nPos = 0;
}
- return BaseIndex::Init();
+ return true;
}
bool BlockFilterIndex::CommitInternal(CDBBatch& batch)
diff --git a/src/index/blockfilterindex.h b/src/index/blockfilterindex.h
index 71e150ba75..ac622b9d6b 100644
--- a/src/index/blockfilterindex.h
+++ b/src/index/blockfilterindex.h
@@ -41,7 +41,7 @@ private:
bool AllowPrune() const override { return true; }
protected:
- bool Init() override;
+ bool CustomInit(const std::optional<interfaces::BlockKey>& block) override;
bool CommitInternal(CDBBatch& batch) override;
diff --git a/src/index/coinstatsindex.cpp b/src/index/coinstatsindex.cpp
index 2920c217f0..742882c3ef 100644
--- a/src/index/coinstatsindex.cpp
+++ b/src/index/coinstatsindex.cpp
@@ -345,7 +345,7 @@ std::optional<CCoinsStats> CoinStatsIndex::LookUpStats(const CBlockIndex* block_
return stats;
}
-bool CoinStatsIndex::Init()
+bool CoinStatsIndex::CustomInit(const std::optional<interfaces::BlockKey>& block)
{
if (!m_db->Read(DB_MUHASH, m_muhash)) {
// Check that the cause of the read failure is that the key does not
@@ -357,13 +357,9 @@ bool CoinStatsIndex::Init()
}
}
- if (!BaseIndex::Init()) return false;
-
- const CBlockIndex* pindex{CurrentIndex()};
-
- if (pindex) {
+ if (block) {
DBVal entry;
- if (!LookUpOne(*m_db, {pindex->GetBlockHash(), pindex->nHeight}, entry)) {
+ if (!LookUpOne(*m_db, *block, entry)) {
return error("%s: Cannot read current %s state; index may be corrupted",
__func__, GetName());
}
diff --git a/src/index/coinstatsindex.h b/src/index/coinstatsindex.h
index 06846d07ab..4d90065330 100644
--- a/src/index/coinstatsindex.h
+++ b/src/index/coinstatsindex.h
@@ -39,7 +39,7 @@ private:
bool AllowPrune() const override { return true; }
protected:
- bool Init() override;
+ bool CustomInit(const std::optional<interfaces::BlockKey>& block) override;
bool CommitInternal(CDBBatch& batch) override;