Skip to content

Commit

Permalink
Prevent stuck block download in large reorganisations
Browse files Browse the repository at this point in the history
In cases of very large reorganisations (hundreds of blocks), a situation
may appear where an 'inv' is sent as response to a 'getblocks', but the
last block mentioned in the inv is already known to the receiver node.
However, the supplying node uses a request for this last block as a
trigger to send the rest of the inv blocks. If it never comes, the block
chain download is stuck.

This commit makes the receiver node always request the last inv'ed block,
even if it is already known, to prevent this problem.
  • Loading branch information
sipa committed Mar 21, 2012
1 parent ef14236 commit 0aa89c0
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2357,8 +2357,10 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv)
}

CTxDB txdb("r");
BOOST_FOREACH(const CInv& inv, vInv)
for (int nInv = 0; nInv < vInv.size(); nInv++)
{
const CInv &inv = vInv[nInv];

if (fShutdown)
return true;
pfrom->AddInventoryKnown(inv);
Expand All @@ -2367,9 +2369,12 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv)
if (fDebug)
printf(" got inventory: %s %s\n", inv.ToString().c_str(), fAlreadyHave ? "have" : "new");

if (!fAlreadyHave)
// Always request the last block in an inv bundle (even if we already have it), as it is the
// trigger for the other side to send further invs. If we are stuck on a (very long) side chain,
// this is necessary to connect earlier received orphan blocks to the chain again.
if (!fAlreadyHave || (inv.type == MSG_BLOCK && nInv==vInv.size()-1))
pfrom->AskFor(inv);
else if (inv.type == MSG_BLOCK && mapOrphanBlocks.count(inv.hash))
if (inv.type == MSG_BLOCK && mapOrphanBlocks.count(inv.hash))
pfrom->PushGetBlocks(pindexBest, GetOrphanRoot(mapOrphanBlocks[inv.hash]));

// Track requests for our stuff
Expand Down

0 comments on commit 0aa89c0

Please sign in to comment.