diff --git a/src/index/base.cpp b/src/index/base.cpp index 6d4248047c2..a94b4ca7225 100644 --- a/src/index/base.cpp +++ b/src/index/base.cpp @@ -109,7 +109,7 @@ bool BaseIndex::Init() // m_chainstate member gives indexing code access to node internals. It is // removed in followup https://github.com/bitcoin/bitcoin/pull/24230 m_chainstate = WITH_LOCK(::cs_main, - return &m_chain->context()->chainman->GetChainstateForIndexing()); + return &m_chain->context()->chainman->ValidatedChainstate()); // Register to validation interface before setting the 'm_synced' flag, so that // callbacks are not missed once m_synced is true. m_chain->context()->validation_signals->RegisterValidationInterface(this); diff --git a/src/init.cpp b/src/init.cpp index 593467e3439..9c710d69152 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -2229,7 +2229,7 @@ bool StartIndexBackgroundSync(NodeContext& node) std::optional indexes_start_block; std::string older_index_name; ChainstateManager& chainman = *Assert(node.chainman); - const Chainstate& chainstate = WITH_LOCK(::cs_main, return chainman.GetChainstateForIndexing()); + const Chainstate& chainstate = WITH_LOCK(::cs_main, return chainman.ValidatedChainstate()); const CChain& index_chain = chainstate.m_chain; for (auto index : node.indexes) { diff --git a/src/validation.cpp b/src/validation.cpp index fabff80c587..da60efbc3cf 100644 --- a/src/validation.cpp +++ b/src/validation.cpp @@ -6437,14 +6437,6 @@ bool ChainstateManager::ValidatedSnapshotCleanup(Chainstate& validated_cs, Chain return true; } -Chainstate& ChainstateManager::GetChainstateForIndexing() -{ - // We can't always return `m_ibd_chainstate` because after background validation - // has completed, `m_snapshot_chainstate == m_active_chainstate`, but it can be - // indexed. - return (this->GetAll().size() > 1) ? *m_ibd_chainstate : *m_active_chainstate; -} - std::pair ChainstateManager::GetPruneRange(const Chainstate& chainstate, int last_height_can_prune) { if (chainstate.m_chain.Height() <= 0) { diff --git a/src/validation.h b/src/validation.h index cafe24934be..464dcbd03a7 100644 --- a/src/validation.h +++ b/src/validation.h @@ -1153,6 +1153,17 @@ public: return cs && cs->m_target_blockhash && !cs->m_target_utxohash ? cs : nullptr; } + //! Return fully validated chainstate that should be used for indexing, to + //! support indexes that need to index blocks in order and can't start from + //! the snapshot block. + Chainstate& ValidatedChainstate() const EXCLUSIVE_LOCKS_REQUIRED(GetMutex()) + { + for (auto* cs : {&CurrentChainstate(), HistoricalChainstate()}) { + if (cs && cs->m_assumeutxo == Assumeutxo::VALIDATED) return *cs; + } + abort(); + } + //! Alternatives to CurrentChainstate() used by older code to query latest //! chainstate information without locking cs_main. Newer code should avoid //! querying ChainstateManager and use Chainstate objects directly, or @@ -1344,16 +1355,6 @@ public: //! @sa node/chainstate:LoadChainstate() bool ValidatedSnapshotCleanup(Chainstate& validated_cs, Chainstate& unvalidated_cs) EXCLUSIVE_LOCKS_REQUIRED(::cs_main); - //! @returns the chainstate that indexes should consult when ensuring that an - //! index is synced with a chain where we can expect block index entries to have - //! BLOCK_HAVE_DATA beneath the tip. - //! - //! In other words, give us the chainstate for which we can reasonably expect - //! that all blocks beneath the tip have been indexed. In practice this means - //! when using an assumed-valid chainstate based upon a snapshot, return only the - //! fully validated chain. - Chainstate& GetChainstateForIndexing() EXCLUSIVE_LOCKS_REQUIRED(::cs_main); - //! Return the [start, end] (inclusive) of block heights we can prune. //! //! start > end is possible, meaning no blocks can be pruned.