refactor: Add Chainstate::m_target_blockhash member

Make Chainstate objects aware of what block they are targeting. This makes
Chainstate objects more self contained, so it's possible for validation code to
look at one Chainstate object and know what blocks to connect to it without
needing to consider global validation state or look at other Chainstate
objects.

The motivation for this change is to make validation and networking code more
readable, so understanding it just requires knowing about chains and blocks,
not reasoning about assumeutxo download states. This change also enables
simplifications to the ChainstateManager interface in subsequent commits, and
could make it easier to implement new features like creating new Chainstate
objects to generate UTXO snapshots or index UTXO data.

Note that behavior of the MaybeCompleteSnapshotValidation function is not
changing here but some checks that were previously impossible to trigger like
the BASE_BLOCKHASH_MISMATCH case have been turned into asserts.
This commit is contained in:
Ryan Ofsky
2024-05-29 20:27:27 -04:00
parent de00e87548
commit 6082c84713
6 changed files with 125 additions and 76 deletions

View File

@@ -5921,18 +5921,19 @@ bool PeerManagerImpl::SendMessages(CNode* pto)
return std::max(0, MAX_BLOCKS_IN_TRANSIT_PER_PEER - static_cast<int>(state.vBlocksInFlight.size()));
};
// If a snapshot chainstate is in use, we want to find its next blocks
// before the background chainstate to prioritize getting to network tip.
// If there are multiple chainstates, download blocks for the
// current chainstate first, to prioritize getting to network tip
// before downloading historical blocks.
FindNextBlocksToDownload(*peer, get_inflight_budget(), vToDownload, staller);
if (m_chainman.BackgroundSyncInProgress() && !IsLimitedPeer(*peer)) {
// If the background tip is not an ancestor of the snapshot block,
auto historical_blocks{m_chainman.GetHistoricalBlockRange()};
if (historical_blocks && !IsLimitedPeer(*peer)) {
// If the first needed historical block is not an ancestor of the last,
// we need to start requesting blocks from their last common ancestor.
const CBlockIndex *from_tip = LastCommonAncestor(m_chainman.GetBackgroundSyncTip(), m_chainman.GetSnapshotBaseBlock());
const CBlockIndex* from_tip = LastCommonAncestor(historical_blocks->first, historical_blocks->second);
TryDownloadingHistoricalBlocks(
*peer,
get_inflight_budget(),
vToDownload, from_tip,
Assert(m_chainman.GetSnapshotBaseBlock()));
vToDownload, from_tip, historical_blocks->second);
}
for (const CBlockIndex *pindex : vToDownload) {
uint32_t nFetchFlags = GetFetchFlags(*peer);