Skip to content

Commit

Permalink
Merge bitcoin#8054: net: Avoid duplicate getheaders requests.
Browse files Browse the repository at this point in the history
f93c2a1 net: Avoid duplicate getheaders requests. (Daniel Kraft)
  • Loading branch information
laanwj committed May 18, 2016
2 parents c74837b + f93c2a1 commit 8e8bebc
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5084,6 +5084,13 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
return true;
}

// If we already know the last header in the message, then it contains
// no new information for us. In this case, we do not request
// more headers later. This prevents multiple chains of redundant
// getheader requests from running in parallel if triggered by incoming
// blocks while the node is still in initial headers sync.
const bool hasNewHeaders = (mapBlockIndex.count(headers.back().GetHash()) == 0);

CBlockIndex *pindexLast = NULL;
BOOST_FOREACH(const CBlockHeader& header, headers) {
CValidationState state;
Expand All @@ -5104,7 +5111,7 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
if (pindexLast)
UpdateBlockAvailability(pfrom->GetId(), pindexLast->GetBlockHash());

if (nCount == MAX_HEADERS_RESULTS && pindexLast) {
if (nCount == MAX_HEADERS_RESULTS && pindexLast && hasNewHeaders) {
// Headers message had its maximum size; the peer may have more headers.
// TODO: optimize: if pindexLast is an ancestor of chainActive.Tip or pindexBestHeader, continue
// from there instead.
Expand Down

0 comments on commit 8e8bebc

Please sign in to comment.