Skip to content

Commit 0569b5c

Browse files
committed
Sync chain more readily from inbound peers during IBD
When in IBD, if the honest chain is only known by inbound peers, then we must eventually sync from them in order to learn it. This change allows us to perform initial headers sync and fetch blocks from inbound peers, if we have no blocks in flight. The restriction on having no blocks in flight means that we will naturally throttle our block downloads to any such inbound peers that we may be downloading from, until we leave IBD. This is a tradeoff between preferring outbound peers for most of our block download, versus making sure we always eventually will get blocks we need that are only known by inbound peers even during IBD, as otherwise we may be stuck in IBD indefinitely (which could have cascading failure on the network, if a large fraction of the network managed to get stuck in IBD).
1 parent 225e5b5 commit 0569b5c

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

src/net_processing.cpp

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4681,10 +4681,31 @@ bool PeerManagerImpl::SendMessages(CNode* pto)
46814681
if (m_chainman.m_best_header == nullptr) {
46824682
m_chainman.m_best_header = m_chainman.ActiveChain().Tip();
46834683
}
4684-
bool fFetch = state.fPreferredDownload || (m_num_preferred_download_peers == 0 && !pto->fClient && !pto->IsAddrFetchConn()); // Download if this is a nice peer, or we have no nice peers and this one might do.
4684+
4685+
// Determine whether we might try initial headers sync or parallel
4686+
// block download from this peer -- this mostly affects behavior while
4687+
// in IBD (once out of IBD, we sync from all peers).
4688+
bool sync_blocks_and_headers_from_peer = false;
4689+
if (state.fPreferredDownload) {
4690+
sync_blocks_and_headers_from_peer = true;
4691+
} else if (!pto->fClient && !pto->IsAddrFetchConn()) {
4692+
// Typically this is an inbound peer. If we don't have any outbound
4693+
// peers, or if we aren't downloading any blocks from such peers,
4694+
// then allow block downloads from this peer, too.
4695+
// We prefer downloading blocks from outbound peers to avoid
4696+
// putting undue load on (say) some home user who is just making
4697+
// outbound connections to the network, but if our only source of
4698+
// the latest blocks is from an inbound peer, we have to be sure to
4699+
// eventually download it (and not just wait indefinitely for an
4700+
// outbound peer to have it).
4701+
if (m_num_preferred_download_peers == 0 || mapBlocksInFlight.empty()) {
4702+
sync_blocks_and_headers_from_peer = true;
4703+
}
4704+
}
4705+
46854706
if (!state.fSyncStarted && !pto->fClient && !fImporting && !fReindex) {
46864707
// Only actively request headers from a single peer, unless we're close to today.
4687-
if ((nSyncStarted == 0 && fFetch) || m_chainman.m_best_header->GetBlockTime() > GetAdjustedTime() - 24 * 60 * 60) {
4708+
if ((nSyncStarted == 0 && sync_blocks_and_headers_from_peer) || m_chainman.m_best_header->GetBlockTime() > GetAdjustedTime() - 24 * 60 * 60) {
46884709
state.fSyncStarted = true;
46894710
state.m_headers_sync_timeout = current_time + HEADERS_DOWNLOAD_TIMEOUT_BASE +
46904711
(
@@ -5063,7 +5084,7 @@ bool PeerManagerImpl::SendMessages(CNode* pto)
50635084
// Message: getdata (blocks)
50645085
//
50655086
std::vector<CInv> vGetData;
5066-
if (!pto->fClient && ((fFetch && !pto->m_limited_node) || !m_chainman.ActiveChainstate().IsInitialBlockDownload()) && state.nBlocksInFlight < MAX_BLOCKS_IN_TRANSIT_PER_PEER) {
5087+
if (!pto->fClient && ((sync_blocks_and_headers_from_peer && !pto->m_limited_node) || !m_chainman.ActiveChainstate().IsInitialBlockDownload()) && state.nBlocksInFlight < MAX_BLOCKS_IN_TRANSIT_PER_PEER) {
50675088
std::vector<const CBlockIndex*> vToDownload;
50685089
NodeId staller = -1;
50695090
FindNextBlocksToDownload(pto->GetId(), MAX_BLOCKS_IN_TRANSIT_PER_PEER - state.nBlocksInFlight, vToDownload, staller);

0 commit comments

Comments
 (0)