diff --git a/src/index/base.cpp b/src/index/base.cpp index 26b3653f7bb..1b861df4bfa 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 35863764594..82317c1ec28 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& 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 15a7cfeaea6..f44b5ac6e84 100644 --- a/src/index/blockfilterindex.cpp +++ b/src/index/blockfilterindex.cpp @@ -109,7 +109,7 @@ BlockFilterIndex::BlockFilterIndex(std::unique_ptr chain, Blo m_filter_fileseq = std::make_unique(std::move(path), "fltr", FLTR_FILE_CHUNK_SIZE); } -bool BlockFilterIndex::Init() +bool BlockFilterIndex::CustomInit(const std::optional& 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 71e150ba751..ac622b9d6bd 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& block) override; bool CommitInternal(CDBBatch& batch) override; diff --git a/src/index/coinstatsindex.cpp b/src/index/coinstatsindex.cpp index 2920c217f09..742882c3ef9 100644 --- a/src/index/coinstatsindex.cpp +++ b/src/index/coinstatsindex.cpp @@ -345,7 +345,7 @@ std::optional CoinStatsIndex::LookUpStats(const CBlockIndex* block_ return stats; } -bool CoinStatsIndex::Init() +bool CoinStatsIndex::CustomInit(const std::optional& 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 06846d07ab4..4d900653306 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& block) override; bool CommitInternal(CDBBatch& batch) override;