Logic for calculating which blocks to download from a given peer, given our current tip.
| 1404 | |
| 1405 | // Logic for calculating which blocks to download from a given peer, given our current tip. |
| 1406 | void PeerManagerImpl::FindNextBlocksToDownload(const Peer& peer, unsigned int count, std::vector<const CBlockIndex*>& vBlocks, NodeId& nodeStaller) |
| 1407 | { |
| 1408 | if (count == 0) |
| 1409 | return; |
| 1410 | |
| 1411 | vBlocks.reserve(vBlocks.size() + count); |
| 1412 | CNodeState *state = State(peer.m_id); |
| 1413 | assert(state != nullptr); |
| 1414 | |
| 1415 | // Make sure pindexBestKnownBlock is up to date, we'll need it. |
| 1416 | ProcessBlockAvailability(peer.m_id); |
| 1417 | |
| 1418 | if (state->pindexBestKnownBlock == nullptr || state->pindexBestKnownBlock->nChainWork < m_chainman.ActiveChain().Tip()->nChainWork || state->pindexBestKnownBlock->nChainWork < m_chainman.MinimumChainWork()) { |
| 1419 | // This peer has nothing interesting. |
| 1420 | return; |
| 1421 | } |
| 1422 | |
| 1423 | // When syncing with AssumeUtxo and the snapshot has not yet been validated, |
| 1424 | // abort downloading blocks from peers that don't have the snapshot block in their best chain. |
| 1425 | // We can't reorg to this chain due to missing undo data until validation completes, |
| 1426 | // so downloading blocks from it would be futile. |
| 1427 | const CBlockIndex* snap_base{m_chainman.CurrentChainstate().SnapshotBase()}; |
| 1428 | if (snap_base && m_chainman.CurrentChainstate().m_assumeutxo == Assumeutxo::UNVALIDATED && |
| 1429 | state->pindexBestKnownBlock->GetAncestor(snap_base->nHeight) != snap_base) { |
| 1430 | LogDebug(BCLog::NET, "Not downloading blocks from peer=%d, which doesn't have the snapshot block in its best chain.\n", peer.m_id); |
| 1431 | return; |
| 1432 | } |
| 1433 | |
| 1434 | // Determine the forking point between the peer's chain and our chain: |
| 1435 | // pindexLastCommonBlock is required to be an ancestor of pindexBestKnownBlock, and will be used as a starting point. |
| 1436 | // It is being set to the fork point between the peer's best known block and the current tip, unless it is already set to |
| 1437 | // an ancestor with more work than the fork point. |
| 1438 | auto fork_point = LastCommonAncestor(state->pindexBestKnownBlock, m_chainman.ActiveTip()); |
| 1439 | if (state->pindexLastCommonBlock == nullptr || |
| 1440 | fork_point->nChainWork > state->pindexLastCommonBlock->nChainWork || |
| 1441 | state->pindexBestKnownBlock->GetAncestor(state->pindexLastCommonBlock->nHeight) != state->pindexLastCommonBlock) { |
| 1442 | state->pindexLastCommonBlock = fork_point; |
| 1443 | } |
| 1444 | if (state->pindexLastCommonBlock == state->pindexBestKnownBlock) |
| 1445 | return; |
| 1446 | |
| 1447 | const CBlockIndex *pindexWalk = state->pindexLastCommonBlock; |
| 1448 | // Never fetch further than the best block we know the peer has, or more than BLOCK_DOWNLOAD_WINDOW + 1 beyond the last |
| 1449 | // linked block we have in common with this peer. The +1 is so we can detect stalling, namely if we would be able to |
| 1450 | // download that next block if the window were 1 larger. |
| 1451 | int nWindowEnd = state->pindexLastCommonBlock->nHeight + BLOCK_DOWNLOAD_WINDOW; |
| 1452 | |
| 1453 | FindNextBlocks(vBlocks, peer, state, pindexWalk, count, nWindowEnd, &m_chainman.ActiveChain(), &nodeStaller); |
| 1454 | } |
| 1455 | |
| 1456 | void PeerManagerImpl::TryDownloadingHistoricalBlocks(const Peer& peer, unsigned int count, std::vector<const CBlockIndex*>& vBlocks, const CBlockIndex *from_tip, const CBlockIndex* target_block) |
| 1457 | { |
nothing calls this directly
no test coverage detected