mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-02-22 06:52:36 +01:00
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.
This commit is contained in:
parent
addb4f2af1
commit
bef4e405f3
6 changed files with 15 additions and 15 deletions
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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; }
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue