Skip to content

Commit 7467366

Browse files
committed
[net processing] Only send a getheaders for one block in an INV
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.
1 parent 8da1e43 commit 7467366

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

src/net_processing.cpp

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2420,6 +2420,7 @@ bool ProcessMessage(CNode* pfrom, const std::string& msg_type, CDataStream& vRec
24202420

24212421
uint32_t nFetchFlags = GetFetchFlags(pfrom);
24222422
const auto current_time = GetTime<std::chrono::microseconds>();
2423+
uint256* best_block{nullptr};
24232424

24242425
for (CInv &inv : vInv)
24252426
{
@@ -2436,17 +2437,14 @@ bool ProcessMessage(CNode* pfrom, const std::string& msg_type, CDataStream& vRec
24362437
if (inv.type == MSG_BLOCK) {
24372438
UpdateBlockAvailability(pfrom->GetId(), inv.hash);
24382439
if (!fAlreadyHave && !fImporting && !fReindex && !mapBlocksInFlight.count(inv.hash)) {
2439-
// We used to request the full block here, but since headers-announcements are now the
2440-
// primary method of announcement on the network, and since, in the case that a node
2441-
// fell back to inv we probably have a reorg which we should get the headers for first,
2442-
// we now only provide a getheaders response here. When we receive the headers, we will
2443-
// then ask for the blocks we need.
2444-
connman->PushMessage(pfrom, msgMaker.Make(NetMsgType::GETHEADERS, ::ChainActive().GetLocator(pindexBestHeader), inv.hash));
2445-
LogPrint(BCLog::NET, "getheaders (%d) %s to peer=%d\n", pindexBestHeader->nHeight, inv.hash.ToString(), pfrom->GetId());
2440+
// Headers-first is the primary method of announcement on
2441+
// the network. If a node fell back to sending blocks by inv,
2442+
// it's probably for a re-org. The final block hash
2443+
// provided should be the highest, so send a getheaders and
2444+
// then fetch the blocks we need to catch up.
2445+
best_block = &inv.hash;
24462446
}
2447-
}
2448-
else
2449-
{
2447+
} else {
24502448
pfrom->AddInventoryKnown(inv);
24512449
if (fBlocksOnly) {
24522450
LogPrint(BCLog::NET, "transaction (%s) inv sent in violation of protocol, disconnecting peer=%d\n", inv.hash.ToString(), pfrom->GetId());
@@ -2457,6 +2455,12 @@ bool ProcessMessage(CNode* pfrom, const std::string& msg_type, CDataStream& vRec
24572455
}
24582456
}
24592457
}
2458+
2459+
if (best_block != nullptr) {
2460+
connman->PushMessage(pfrom, msgMaker.Make(NetMsgType::GETHEADERS, ::ChainActive().GetLocator(pindexBestHeader), *best_block));
2461+
LogPrint(BCLog::NET, "getheaders (%d) %s to peer=%d\n", pindexBestHeader->nHeight, best_block->ToString(), pfrom->GetId());
2462+
}
2463+
24602464
return true;
24612465
}
24622466

0 commit comments

Comments
 (0)