Skip to content

Commit 9f7809f

Browse files
committed
Merge pull request #5976
8ba7f84 Reduce download timeouts as blocks arrive (Suhas Daftuar)
2 parents 847be04 + 8ba7f84 commit 9f7809f

File tree

1 file changed

+29
-5
lines changed

1 file changed

+29
-5
lines changed

src/main.cpp

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,8 @@ namespace {
154154
uint256 hash;
155155
CBlockIndex *pindex; //! Optional.
156156
int64_t nTime; //! Time of "getdata" request in microseconds.
157-
int nValidatedQueuedBefore; //! Number of blocks queued with validated headers (globally) at the time this one is requested.
158157
bool fValidatedHeaders; //! Whether this block has validated headers at the time of request.
158+
int64_t nTimeDisconnect; //! The timeout for this block request (for disconnecting a slow peer)
159159
};
160160
map<uint256, pair<NodeId, list<QueuedBlock>::iterator> > mapBlocksInFlight;
161161

@@ -216,6 +216,7 @@ struct CNodeState {
216216
int64_t nStallingSince;
217217
list<QueuedBlock> vBlocksInFlight;
218218
int nBlocksInFlight;
219+
int nBlocksInFlightValidHeaders;
219220
//! Whether we consider this a preferred download peer.
220221
bool fPreferredDownload;
221222

@@ -229,6 +230,7 @@ struct CNodeState {
229230
fSyncStarted = false;
230231
nStallingSince = 0;
231232
nBlocksInFlight = 0;
233+
nBlocksInFlightValidHeaders = 0;
232234
fPreferredDownload = false;
233235
}
234236
};
@@ -260,6 +262,12 @@ void UpdatePreferredDownload(CNode* node, CNodeState* state)
260262
nPreferredDownload += state->fPreferredDownload;
261263
}
262264

265+
// Returns time at which to timeout block request (nTime in microseconds)
266+
int64_t GetBlockTimeout(int64_t nTime, int nValidatedQueuedBefore)
267+
{
268+
return nTime + 500000 * Params().GetConsensus().nPowTargetSpacing * (4 + nValidatedQueuedBefore);
269+
}
270+
263271
void InitializeNode(NodeId nodeid, const CNode *pnode) {
264272
LOCK(cs_main);
265273
CNodeState &state = mapNodeState.insert(std::make_pair(nodeid, CNodeState())).first->second;
@@ -292,6 +300,7 @@ void MarkBlockAsReceived(const uint256& hash) {
292300
if (itInFlight != mapBlocksInFlight.end()) {
293301
CNodeState *state = State(itInFlight->second.first);
294302
nQueuedValidatedHeaders -= itInFlight->second.second->fValidatedHeaders;
303+
state->nBlocksInFlightValidHeaders -= itInFlight->second.second->fValidatedHeaders;
295304
state->vBlocksInFlight.erase(itInFlight->second.second);
296305
state->nBlocksInFlight--;
297306
state->nStallingSince = 0;
@@ -307,10 +316,12 @@ void MarkBlockAsInFlight(NodeId nodeid, const uint256& hash, CBlockIndex *pindex
307316
// Make sure it's not listed somewhere already.
308317
MarkBlockAsReceived(hash);
309318

310-
QueuedBlock newentry = {hash, pindex, GetTimeMicros(), nQueuedValidatedHeaders, pindex != NULL};
319+
int64_t nNow = GetTimeMicros();
320+
QueuedBlock newentry = {hash, pindex, nNow, pindex != NULL, GetBlockTimeout(nNow, nQueuedValidatedHeaders)};
311321
nQueuedValidatedHeaders += newentry.fValidatedHeaders;
312322
list<QueuedBlock>::iterator it = state->vBlocksInFlight.insert(state->vBlocksInFlight.end(), newentry);
313323
state->nBlocksInFlight++;
324+
state->nBlocksInFlightValidHeaders += newentry.fValidatedHeaders;
314325
mapBlocksInFlight[hash] = std::make_pair(nodeid, it);
315326
}
316327

@@ -5015,9 +5026,22 @@ bool SendMessages(CNode* pto, bool fSendTrickle)
50155026
// timeout. We compensate for in-flight blocks to prevent killing off peers due to our own downstream link
50165027
// being saturated. We only count validated in-flight blocks so peers can't advertise non-existing block hashes
50175028
// to unreasonably increase our timeout.
5018-
if (!pto->fDisconnect && state.vBlocksInFlight.size() > 0 && state.vBlocksInFlight.front().nTime < nNow - 500000 * consensusParams.nPowTargetSpacing * (4 + state.vBlocksInFlight.front().nValidatedQueuedBefore)) {
5019-
LogPrintf("Timeout downloading block %s from peer=%d, disconnecting\n", state.vBlocksInFlight.front().hash.ToString(), pto->id);
5020-
pto->fDisconnect = true;
5029+
// We also compare the block download timeout originally calculated against the time at which we'd disconnect
5030+
// if we assumed the block were being requested now (ignoring blocks we've requested from this peer, since we're
5031+
// only looking at this peer's oldest request). This way a large queue in the past doesn't result in a
5032+
// permanently large window for this block to be delivered (ie if the number of blocks in flight is decreasing
5033+
// more quickly than once every 5 minutes, then we'll shorten the download window for this block).
5034+
if (!pto->fDisconnect && state.vBlocksInFlight.size() > 0) {
5035+
QueuedBlock &queuedBlock = state.vBlocksInFlight.front();
5036+
int64_t nTimeoutIfRequestedNow = GetBlockTimeout(nNow, nQueuedValidatedHeaders - state.nBlocksInFlightValidHeaders);
5037+
if (queuedBlock.nTimeDisconnect > nTimeoutIfRequestedNow) {
5038+
LogPrint("net", "Reducing block download timeout for peer=%d block=%s, orig=%d new=%d\n", pto->id, queuedBlock.hash.ToString(), queuedBlock.nTimeDisconnect, nTimeoutIfRequestedNow);
5039+
queuedBlock.nTimeDisconnect = nTimeoutIfRequestedNow;
5040+
}
5041+
if (queuedBlock.nTimeDisconnect < nNow) {
5042+
LogPrintf("Timeout downloading block %s from peer=%d, disconnecting\n", queuedBlock.hash.ToString(), pto->id);
5043+
pto->fDisconnect = true;
5044+
}
50215045
}
50225046

50235047
//

0 commit comments

Comments
 (0)