aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPieter Wuille <pieter.wuille@gmail.com>2014-10-28 09:33:55 -0700
committerPieter Wuille <pieter.wuille@gmail.com>2014-10-29 08:08:13 -0700
commitb4ee0bddad94cb580c1b56c592021bb102ed7b1a (patch)
treec9eb02d200b3e78eeb1b0bf29e6fa7738a9fed7f /src
parent723c7526368badda15df8ac1ffc047a0ab2e384a (diff)
Introduce preferred download peers
Diffstat (limited to 'src')
-rw-r--r--src/main.cpp20
1 files changed, 19 insertions, 1 deletions
diff --git a/src/main.cpp b/src/main.cpp
index 008a059103..36af1c2827 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -128,6 +128,8 @@ namespace {
};
map<uint256, pair<NodeId, list<QueuedBlock>::iterator> > mapBlocksInFlight;
+ // Number of preferrable block download peers.
+ int nPreferredDownload = 0;
} // anon namespace
//////////////////////////////////////////////////////////////////////////////
@@ -225,6 +227,8 @@ struct CNodeState {
int64_t nStallingSince;
list<QueuedBlock> vBlocksInFlight;
int nBlocksInFlight;
+ // Whether we consider this a preferred download peer.
+ bool fPreferredDownload;
CNodeState() {
nMisbehavior = 0;
@@ -235,6 +239,7 @@ struct CNodeState {
fSyncStarted = false;
nStallingSince = 0;
nBlocksInFlight = 0;
+ fPreferredDownload = false;
}
};
@@ -255,6 +260,16 @@ int GetHeight()
return chainActive.Height();
}
+void UpdatePreferredDownload(CNode* node, CNodeState* state)
+{
+ nPreferredDownload -= state->fPreferredDownload;
+
+ // Whether this node should be marked as a preferred download node.
+ state->fPreferredDownload = (!node->fInbound || node->fWhitelisted) && !node->fOneShot && !node->fClient;
+
+ nPreferredDownload += state->fPreferredDownload;
+}
+
void InitializeNode(NodeId nodeid, const CNode *pnode) {
LOCK(cs_main);
CNodeState &state = mapNodeState.insert(std::make_pair(nodeid, CNodeState())).first->second;
@@ -271,6 +286,7 @@ void FinalizeNode(NodeId nodeid) {
BOOST_FOREACH(const QueuedBlock& entry, state->vBlocksInFlight)
mapBlocksInFlight.erase(entry.hash);
EraseOrphansFor(nodeid);
+ nPreferredDownload -= state->fPreferredDownload;
mapNodeState.erase(nodeid);
}
@@ -3471,6 +3487,8 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv,
pfrom->fClient = !(pfrom->nServices & NODE_NETWORK);
+ // Potentially mark this peer as a preferred download peer.
+ UpdatePreferredDownload(pfrom, State(pfrom->GetId()));
// Change version
pfrom->PushMessage("verack");
@@ -4415,7 +4433,7 @@ bool SendMessages(CNode* pto, bool fSendTrickle)
// Start block sync
if (pindexBestHeader == NULL)
pindexBestHeader = chainActive.Tip();
- bool fFetch = !pto->fInbound || (pindexBestHeader && (state.pindexLastCommonBlock ? state.pindexLastCommonBlock->nHeight : 0) + 144 > pindexBestHeader->nHeight);
+ 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.
if (!state.fSyncStarted && !pto->fClient && fFetch && !fImporting && !fReindex) {
// Only actively request headers from a single peer, unless we're close to today.
if (nSyncStarted == 0 || pindexBestHeader->GetBlockTime() > GetAdjustedTime() - 24 * 60 * 60) {