Skip to content

Commit 7f7fede

Browse files
committed
Merge pull request #5157
b4ee0bd Introduce preferred download peers (Pieter Wuille)
2 parents c969096 + b4ee0bd commit 7f7fede

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

src/main.cpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ namespace {
128128
};
129129
map<uint256, pair<NodeId, list<QueuedBlock>::iterator> > mapBlocksInFlight;
130130

131+
// Number of preferrable block download peers.
132+
int nPreferredDownload = 0;
131133
} // anon namespace
132134

133135
//////////////////////////////////////////////////////////////////////////////
@@ -230,6 +232,8 @@ struct CNodeState {
230232
int64_t nStallingSince;
231233
list<QueuedBlock> vBlocksInFlight;
232234
int nBlocksInFlight;
235+
// Whether we consider this a preferred download peer.
236+
bool fPreferredDownload;
233237

234238
CNodeState() {
235239
nMisbehavior = 0;
@@ -240,6 +244,7 @@ struct CNodeState {
240244
fSyncStarted = false;
241245
nStallingSince = 0;
242246
nBlocksInFlight = 0;
247+
fPreferredDownload = false;
243248
}
244249
};
245250

@@ -260,6 +265,16 @@ int GetHeight()
260265
return chainActive.Height();
261266
}
262267

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+
263278
void InitializeNode(NodeId nodeid, const CNode *pnode) {
264279
LOCK(cs_main);
265280
CNodeState &state = mapNodeState.insert(std::make_pair(nodeid, CNodeState())).first->second;
@@ -276,6 +291,7 @@ void FinalizeNode(NodeId nodeid) {
276291
BOOST_FOREACH(const QueuedBlock& entry, state->vBlocksInFlight)
277292
mapBlocksInFlight.erase(entry.hash);
278293
EraseOrphansFor(nodeid);
294+
nPreferredDownload -= state->fPreferredDownload;
279295

280296
mapNodeState.erase(nodeid);
281297
}
@@ -3478,6 +3494,8 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
34783494

34793495
pfrom->fClient = !(pfrom->nServices & NODE_NETWORK);
34803496

3497+
// Potentially mark this peer as a preferred download peer.
3498+
UpdatePreferredDownload(pfrom, State(pfrom->GetId()));
34813499

34823500
// Change version
34833501
pfrom->PushMessage("verack");
@@ -4422,7 +4440,7 @@ bool SendMessages(CNode* pto, bool fSendTrickle)
44224440
// Start block sync
44234441
if (pindexBestHeader == NULL)
44244442
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.
44264444
if (!state.fSyncStarted && !pto->fClient && fFetch && !fImporting && !fReindex) {
44274445
// Only actively request headers from a single peer, unless we're close to today.
44284446
if (nSyncStarted == 0 || pindexBestHeader->GetBlockTime() > GetAdjustedTime() - 24 * 60 * 60) {

0 commit comments

Comments
 (0)