Skip to content

Commit ec11695

Browse files
laanwjPastaPastaPasta
authored andcommitted
Merge bitcoin#18962: net processing: Only send a getheaders for one block in an INV
7467366 [net processing] Only send a getheaders for one block in an INV (John Newbery) Pull request description: Headers-first is the primary method of announcement on the network. If a node fell back sending blocks by inv, it's probably for a re-org. The final block hash provided should be the highest, so send a getheaders and then fetch the blocks we need to catch up. Sending many GETHEADERS messages to the peer would cause them to send a large number of potentially large HEADERS messages with redundant data, which is a waste of bandwidth. ACKs for top commit: sipa: utACK 7467366 mzumsande: utACK 7467366 as per ajtowns' reasoning. naumenkogs: utACK 7467366 ajtowns: ACK 7467366 jonatack: ACK 7467366 Tree-SHA512: 59e243b80d3f0873709dfacb2e4ffba34689aad7de31ec7f69a64e0e3a0756235a0150e4082ff5de823949ba4411ee1aed2344b4749b62e0eb1ea906e41f5ea9
1 parent 5035a1c commit ec11695

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

src/net_processing.cpp

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3221,6 +3221,7 @@ void PeerManagerImpl::ProcessMessage(
32213221
LOCK(cs_main);
32223222

32233223
const auto current_time = GetTime<std::chrono::microseconds>();
3224+
uint256* best_block{nullptr};
32243225

32253226
for (CInv &inv : vInv)
32263227
{
@@ -3256,18 +3257,14 @@ void PeerManagerImpl::ProcessMessage(
32563257
state->fSyncStarted = true;
32573258
state->nHeadersSyncTimeout = GetTimeMicros() + HEADERS_DOWNLOAD_TIMEOUT_BASE + HEADERS_DOWNLOAD_TIMEOUT_PER_HEADER * (GetAdjustedTime() - pindexBestHeader->GetBlockTime())/(m_chainparams.GetConsensus().nPowTargetSpacing);
32583259
nSyncStarted++;
3259-
// We used to request the full block here, but since headers-announcements are now the
3260-
// primary method of announcement on the network, and since, in the case that a node
3261-
// fell back to inv we probably have a reorg which we should get the headers for first,
3262-
// we now only provide a getheaders response here. When we receive the headers, we will
3263-
// then ask for the blocks we need.
3264-
std::string msg_type = (pfrom.nServices & NODE_HEADERS_COMPRESSED) ? NetMsgType::GETHEADERS2 : NetMsgType::GETHEADERS;
3265-
m_connman.PushMessage(&pfrom, msgMaker.Make(msg_type, m_chainman.ActiveChain().GetLocator(pindexBestHeader), inv.hash));
3266-
LogPrint(BCLog::NET, "%s (%d) %s to peer=%d\n", msg_type, pindexBestHeader->nHeight, inv.hash.ToString(), pfrom.GetId());
3260+
// Headers-first is the primary method of announcement on
3261+
// the network. If a node fell back to sending blocks by inv,
3262+
// it's probably for a re-org. The final block hash
3263+
// provided should be the highest, so send a getheaders and
3264+
// then fetch the blocks we need to catch up.
3265+
best_block = &inv.hash;
32673266
}
3268-
}
3269-
else
3270-
{
3267+
} else {
32713268
static std::set<int> allowWhileInIBDObjs = {
32723269
MSG_SPORK
32733270
};
@@ -3296,6 +3293,12 @@ void PeerManagerImpl::ProcessMessage(
32963293
}
32973294
}
32983295
}
3296+
if (best_block != nullptr) {
3297+
std::string msg_type = (pfrom.nServices & NODE_HEADERS_COMPRESSED) ? NetMsgType::GETHEADERS2 : NetMsgType::GETHEADERS;
3298+
m_connman.PushMessage(&pfrom, msgMaker.Make(msg_type, m_chainman.ActiveChain().GetLocator(pindexBestHeader), *best_block));
3299+
LogPrint(BCLog::NET, "%s (%d) %s to peer=%d\n", msg_type, pindexBestHeader->nHeight, best_block->ToString(), pfrom.GetId());
3300+
}
3301+
32993302
return;
33003303
}
33013304

0 commit comments

Comments
 (0)