@@ -128,6 +128,8 @@ namespace {
128
128
};
129
129
map<uint256, pair<NodeId, list<QueuedBlock>::iterator> > mapBlocksInFlight;
130
130
131
+ // Number of preferrable block download peers.
132
+ int nPreferredDownload = 0 ;
131
133
} // anon namespace
132
134
133
135
// ////////////////////////////////////////////////////////////////////////////
@@ -230,6 +232,8 @@ struct CNodeState {
230
232
int64_t nStallingSince;
231
233
list<QueuedBlock> vBlocksInFlight;
232
234
int nBlocksInFlight;
235
+ // Whether we consider this a preferred download peer.
236
+ bool fPreferredDownload ;
233
237
234
238
CNodeState () {
235
239
nMisbehavior = 0 ;
@@ -240,6 +244,7 @@ struct CNodeState {
240
244
fSyncStarted = false ;
241
245
nStallingSince = 0 ;
242
246
nBlocksInFlight = 0 ;
247
+ fPreferredDownload = false ;
243
248
}
244
249
};
245
250
@@ -260,6 +265,16 @@ int GetHeight()
260
265
return chainActive.Height ();
261
266
}
262
267
268
+ void UpdatePreferredDownload (CNode* node, CNodeState* state)
269
+ {
270
+ nPreferredDownload -= state->fPreferredDownload ;
271
+
272
+ // Whether this node should be marked as a preferred download node.
273
+ state->fPreferredDownload = (!node->fInbound || node->fWhitelisted ) && !node->fOneShot && !node->fClient ;
274
+
275
+ nPreferredDownload += state->fPreferredDownload ;
276
+ }
277
+
263
278
void InitializeNode (NodeId nodeid, const CNode *pnode) {
264
279
LOCK (cs_main);
265
280
CNodeState &state = mapNodeState.insert (std::make_pair (nodeid, CNodeState ())).first ->second ;
@@ -276,6 +291,7 @@ void FinalizeNode(NodeId nodeid) {
276
291
BOOST_FOREACH (const QueuedBlock& entry, state->vBlocksInFlight )
277
292
mapBlocksInFlight.erase (entry.hash );
278
293
EraseOrphansFor (nodeid);
294
+ nPreferredDownload -= state->fPreferredDownload ;
279
295
280
296
mapNodeState.erase (nodeid);
281
297
}
@@ -3478,6 +3494,8 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
3478
3494
3479
3495
pfrom->fClient = !(pfrom->nServices & NODE_NETWORK);
3480
3496
3497
+ // Potentially mark this peer as a preferred download peer.
3498
+ UpdatePreferredDownload (pfrom, State (pfrom->GetId ()));
3481
3499
3482
3500
// Change version
3483
3501
pfrom->PushMessage (" verack" );
@@ -4422,7 +4440,7 @@ bool SendMessages(CNode* pto, bool fSendTrickle)
4422
4440
// Start block sync
4423
4441
if (pindexBestHeader == NULL )
4424
4442
pindexBestHeader = chainActive.Tip ();
4425
- bool fFetch = !pto-> fInbound || (pindexBestHeader && (state. pindexLastCommonBlock ? state. pindexLastCommonBlock -> nHeight : 0 ) + 144 > pindexBestHeader-> nHeight );
4443
+ bool fFetch = state. fPreferredDownload || (nPreferredDownload == 0 && !pto-> fClient && !pto-> fOneShot ); // Download if this is a nice peer, or we have no nice peers and this one might do.
4426
4444
if (!state.fSyncStarted && !pto->fClient && fFetch && !fImporting && !fReindex ) {
4427
4445
// Only actively request headers from a single peer, unless we're close to today.
4428
4446
if (nSyncStarted == 0 || pindexBestHeader->GetBlockTime () > GetAdjustedTime () - 24 * 60 * 60 ) {
0 commit comments