mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-01-20 15:19:07 +01:00
p2p: During block download, adjust pindexLastCommonBlock better
Simplify and improve the logic for calculating pindexLastCommonBlock, in order to calculate
nWindowEnd better.
The previous logic would not take into account when the chain tip had moved forward, so that
FindNextBlocks could iterate over many blocks already downloaded and
connected, which could result in blocks not being requested for download that should have been
requested, and peers being wrongly marked as staller.
It also removes extra logic from commit 49d569cb1f
for the situation right after a snapshot was loaded:
After snapshot loading, our tip becomes the snapshot block.
For peers that have the most-work chain, which inlcludes the snapshot,
our tip is an ancestor of the peer's best block, hence the general
advancement logic will move pindexLastCommonBlock
from any pre-snapshot position to the snapshot height automatically.
Co-authored-by: stringintech <stringintech@gmail.com>
Co-authored-by: Pieter Wuille <pieter@wuille.net>
This commit is contained in:
@@ -1397,17 +1397,16 @@ void PeerManagerImpl::FindNextBlocksToDownload(const Peer& peer, unsigned int co
|
||||
return;
|
||||
}
|
||||
|
||||
// Bootstrap quickly by guessing a parent of our best tip is the forking point.
|
||||
// Guessing wrong in either direction is not a problem.
|
||||
// Also reset pindexLastCommonBlock after a snapshot was loaded, so that blocks after the snapshot will be prioritised for download.
|
||||
// Determine the forking point between the peer's chain and our chain:
|
||||
// pindexLastCommonBlock is required to be an ancestor of pindexBestKnownBlock, and will be used as a starting point.
|
||||
// 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
|
||||
// an ancestor with more work than the fork point.
|
||||
auto fork_point = LastCommonAncestor(state->pindexBestKnownBlock, m_chainman.ActiveTip());
|
||||
if (state->pindexLastCommonBlock == nullptr ||
|
||||
(snap_base && state->pindexLastCommonBlock->nHeight < snap_base->nHeight)) {
|
||||
state->pindexLastCommonBlock = m_chainman.ActiveChain()[std::min(state->pindexBestKnownBlock->nHeight, m_chainman.ActiveChain().Height())];
|
||||
fork_point->nChainWork > state->pindexLastCommonBlock->nChainWork ||
|
||||
state->pindexBestKnownBlock->GetAncestor(state->pindexLastCommonBlock->nHeight) != state->pindexLastCommonBlock) {
|
||||
state->pindexLastCommonBlock = fork_point;
|
||||
}
|
||||
|
||||
// If the peer reorganized, our previous pindexLastCommonBlock may not be an ancestor
|
||||
// of its current tip anymore. Go back enough to fix that.
|
||||
state->pindexLastCommonBlock = LastCommonAncestor(state->pindexLastCommonBlock, state->pindexBestKnownBlock);
|
||||
if (state->pindexLastCommonBlock == state->pindexBestKnownBlock)
|
||||
return;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user