refactor: Add ChainstateManager::ValidatedChainstate() method

ValidatedChainstate() accessor replaces GetChainstateForIndexing() with no
change in behavior.
This commit is contained in:
Ryan Ofsky 2024-05-30 12:03:47 -04:00
parent a229cb9477
commit 352ad27fc1
4 changed files with 13 additions and 20 deletions

View File

@ -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);

View File

@ -2229,7 +2229,7 @@ bool StartIndexBackgroundSync(NodeContext& node)
std::optional<const CBlockIndex*> 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) {

View File

@ -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<int, int> ChainstateManager::GetPruneRange(const Chainstate& chainstate, int last_height_can_prune)
{
if (chainstate.m_chain.Height() <= 0) {

View File

@ -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.