Skip to content

Commit 15cd1a3

Browse files
authored
Merge pull request #314 from Asherda/inv-buffer
Backport fix for INV buffer
2 parents 5bfb23f + 15b359e commit 15cd1a3

File tree

1 file changed

+14
-27
lines changed

1 file changed

+14
-27
lines changed

src/main.cpp

+14-27
Original file line numberDiff line numberDiff line change
@@ -3135,7 +3135,7 @@ namespace Consensus {
31353135
{
31363136
outOverflow = true;
31373137
}
3138-
3138+
31393139
if (outOverflow || ReserveValueIn < reserveValueOut)
31403140
{
31413141
fprintf(stderr,"overflow or spentheight.%d reservevaluein: %s\nis less than out: %s\n", nSpendHeight,
@@ -8806,7 +8806,7 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
88068806

88078807
LOCK(cs_main);
88088808

8809-
std::vector<CInv> vToFetch;
8809+
const uint256* best_block{nullptr};
88108810

88118811
for (unsigned int nInv = 0; nInv < vInv.size(); nInv++)
88128812
{
@@ -8820,29 +8820,14 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
88208820
if (inv.type == MSG_BLOCK) {
88218821
UpdateBlockAvailability(pfrom->GetId(), inv.hash);
88228822
if (!fAlreadyHave && !fImporting && !fReindex && !mapBlocksInFlight.count(inv.hash)) {
8823-
// First request the headers preceding the announced block. In the normal fully-synced
8824-
// case where a new block is announced that succeeds the current tip (no reorganization),
8825-
// there are no such headers.
8826-
// Secondly, and only when we are close to being synced, we request the announced block directly,
8827-
// to avoid an extra round-trip. Note that we must *first* ask for the headers, so by the
8828-
// time the block arrives, the header chain leading up to it is already validated. Not
8829-
// doing this will result in the received block being rejected as an orphan in case it is
8830-
// not a direct successor.
8831-
pfrom->PushMessage("getheaders", chainActive.GetLocator(pindexBestHeader), inv.hash);
8832-
CNodeState *nodestate = State(pfrom->GetId());
8833-
8834-
if (chainActive.Tip()->GetBlockTime() > GetAdjustedTime() - (ConnectedChains.ThisChain().blockTime * 20) &&
8835-
nodestate->nBlocksInFlight < MAX_BLOCKS_IN_TRANSIT_PER_PEER) {
8836-
vToFetch.push_back(inv);
8837-
// Mark block as in flight already, even though the actual "getdata" message only goes out
8838-
// later (within the same cs_main lock, though).
8839-
MarkBlockAsInFlight(pfrom->GetId(), inv.hash, chainparams.GetConsensus());
8840-
}
8841-
LogPrint("net", "getheaders (%d) %s to peer=%d\n", pindexBestHeader->GetHeight(), inv.hash.ToString(), pfrom->id);
8823+
// Headers-first is the primary method of announcement on
8824+
// the network. If a node fell back to sending blocks by inv,
8825+
// it's probably for a re-org. The final block hash
8826+
// provided should be the highest, so send a getheaders and
8827+
// then fetch the blocks we need to catch up.
8828+
best_block = &inv.hash;
88428829
}
8843-
}
8844-
else
8845-
{
8830+
} else {
88468831
pfrom->AddKnownWTxId(WTxId(inv.hash));
88478832
if (fBlocksOnly)
88488833
LogPrint("net", "transaction (%s) inv sent in violation of protocol peer=%d\n", inv.hash.ToString(), pfrom->id);
@@ -8856,8 +8841,10 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
88568841
}
88578842
}
88588843

8859-
if (!vToFetch.empty())
8860-
pfrom->PushMessage("getdata", vToFetch);
8844+
if (best_block != nullptr) {
8845+
pfrom->PushMessage("getheaders", chainActive.GetLocator(pindexBestHeader), *best_block);
8846+
LogPrint("net", "getheaders (%d) %s to peer=%d\n", pindexBestHeader->GetHeight(), best_block->ToString(), pfrom->id);
8847+
}
88618848
}
88628849

88638850

@@ -9125,7 +9112,7 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
91259112
LogPrint("mempool", "%s from peer=%d %s was not accepted into the memory pool: %s\n", tx.GetHash().ToString(),
91269113
pfrom->id, pfrom->cleanSubVer,
91279114
state.GetRejectReason());
9128-
9115+
91299116
bool sendReject = true;
91309117
if (state.GetRejectReason() == "bad-txns-inputs-spent" && nDoS <= 1)
91319118
{

0 commit comments

Comments
 (0)