Skip to content

Commit e3bf6aa

Browse files
committed
Detect when GETBLOCKTXN requests are ignored.
1 parent 5688f8a commit e3bf6aa

File tree

1 file changed

+27
-3
lines changed

1 file changed

+27
-3
lines changed

src/net_processing.cpp

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,10 @@ struct CNodeState {
632632
//! When the first entry in vBlocksInFlight started downloading. Don't care when vBlocksInFlight is empty.
633633
std::chrono::microseconds m_downloading_since{0us};
634634
int nBlocksInFlight{0};
635+
//! How many TXs are currently in flight
636+
unsigned int nTxInFlight{0};
637+
//! How many TXs were in flight when we sent GETBLOCKTXN
638+
int nBlockAfterTXs{0};
635639
//! How many bytes of useful TX data received (specifically orphans)
636640
int nMempoolBytes{0};
637641
//! Whether we consider this a preferred download peer.
@@ -3236,6 +3240,8 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
32363240
pfrom.nMempoolBytes += nodestate->nMempoolBytes;
32373241
nodestate->nMempoolBytes = 0;
32383242
}
3243+
if (nodestate->nTxInFlight) nodestate->nTxInFlight--;
3244+
if (nodestate->nBlockAfterTXs > 1) nodestate->nBlockAfterTXs--;
32393245

32403246
const uint256& hash = nodestate->m_wtxid_relay ? wtxid : txid;
32413247
pfrom.AddKnownTx(hash); // REBTODO - check what this does
@@ -3584,6 +3590,8 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
35843590
LogPrint(BCLog::BLOCK, "send getblocktxn %s indexes=%d/%d peer=%d\n", strBlkHeight(pindex), req.indexes.size(), cmpctblock.BlockTxCount(), pfrom.GetId());
35853591
req.blockhash = pindex->GetBlockHash();
35863592
m_connman.PushMessage(&pfrom, msgMaker.Make(NetMsgType::GETBLOCKTXN, req));
3593+
// If we get more TXs than currently in flight then we know the request has been ignored.
3594+
nodestate->nBlockAfterTXs = nodestate->nTxInFlight + 2; // Add 2 so that one more TX is requested.
35873595
}
35883596
} else {
35893597
// This block is either already in flight from a different
@@ -3668,9 +3676,14 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
36683676
{
36693677
// Ignore blocktxn received while importing
36703678
if (fImporting || fReindex) {
3671-
LogPrint(BCLog::NET, "Unexpected blocktxn message received from peer %d\n", pfrom.GetId());
3679+
LogPrint(BCLog::NET, "Unexpected blocktxn message received from peer=%d\n", pfrom.GetId());
36723680
return;
36733681
}
3682+
{
3683+
LOCK(cs_main);
3684+
CNodeState *nodestate = State(pfrom.GetId());
3685+
nodestate->nBlockAfterTXs = 0;
3686+
}
36743687

36753688
BlockTransactions resp;
36763689
int nSize = vRecv.size();
@@ -4066,6 +4079,9 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
40664079
if (inv.IsGenTxMsg()) {
40674080
// If we receive a NOTFOUND message for a tx we requested, mark the announcement for it as
40684081
// completed in TxRequestTracker.
4082+
CNodeState *nodestate = State(pfrom.GetId());
4083+
if (nodestate->nTxInFlight) nodestate->nTxInFlight--;
4084+
if (nodestate->nBlockAfterTXs > 1) nodestate->nBlockAfterTXs--;
40694085
m_txrequest.ReceivedResponse(pfrom.GetId(), inv.hash);
40704086
}
40714087
}
@@ -4911,7 +4927,12 @@ bool PeerManagerImpl::SendMessages(CNode* pto)
49114927
// Stalling only triggers when the block download window cannot move. During normal steady state,
49124928
// the download window should be much larger than the to-be-downloaded set of blocks, so disconnection
49134929
// should only happen during initial block download.
4914-
LogPrintf("Peer=%d is stalling block download, disconnecting\n", pto->GetId());
4930+
LogPrintf("block download stalled. disconnecting peer=%d\n", pto->GetId());
4931+
pto->fDisconnect = true;
4932+
return true;
4933+
}
4934+
if (state.nBlockAfterTXs == 1) {
4935+
LogPrintf("getblocktxn ignored. disconnecting peer=%d\n", pto->GetId());
49154936
pto->fDisconnect = true;
49164937
return true;
49174938
}
@@ -5003,6 +5024,7 @@ bool PeerManagerImpl::SendMessages(CNode* pto)
50035024
gtxid.GetHash().ToString(), pto->GetId());
50045025
vGetData.emplace_back(gtxid.IsWtxid() ? MSG_WTX : (MSG_TX | GetFetchFlags(*pto)), gtxid.GetHash());
50055026
if (vGetData.size() >= MAX_GETDATA_SZ) {
5027+
state.nTxInFlight += vGetData.size();
50065028
m_connman.PushMessage(pto, msgMaker.Make(NetMsgType::GETDATA, vGetData));
50075029
vGetData.clear();
50085030
}
@@ -5015,8 +5037,10 @@ bool PeerManagerImpl::SendMessages(CNode* pto)
50155037
}
50165038

50175039

5018-
if (!vGetData.empty())
5040+
if (!vGetData.empty()) {
5041+
state.nTxInFlight += vGetData.size();
50195042
m_connman.PushMessage(pto, msgMaker.Make(NetMsgType::GETDATA, vGetData));
5043+
}
50205044

50215045
MaybeSendFeefilter(*pto, current_time);
50225046
} // release cs_main

0 commit comments

Comments
 (0)