@@ -1682,7 +1682,24 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
16821682
16831683 if (inv.type == MSG_BLOCK) {
16841684 UpdateBlockAvailability (pfrom->GetId (), inv.hash );
1685- if (!fAlreadyHave && !fImporting && !fReindex && !mapBlocksInFlight.count (inv.hash )) {
1685+
1686+ if (fAlreadyHave || fImporting || fReindex || mapBlocksInFlight.count (inv.hash )) {
1687+ continue ;
1688+ }
1689+
1690+ CNodeState *state = State (pfrom->id );
1691+ if (!state) {
1692+ continue ;
1693+ }
1694+
1695+ // Download if this is a nice peer, or we have no nice peers and this one might do.
1696+ bool fFetch = state->fPreferredDownload || (nPreferredDownload == 0 && !pfrom->fOneShot );
1697+ // Only actively request headers from a single peer, unless we're close to end of initial download.
1698+ if ((nSyncStarted == 0 && fFetch ) || pindexBestHeader->GetBlockTime () > GetAdjustedTime () - nMaxTipAge) {
1699+ // Make sure to mark this peer as the one we are currently syncing with etc.
1700+ state->fSyncStarted = true ;
1701+ state->nHeadersSyncTimeout = GetTimeMicros () + HEADERS_DOWNLOAD_TIMEOUT_BASE + HEADERS_DOWNLOAD_TIMEOUT_PER_HEADER * (GetAdjustedTime () - pindexBestHeader->GetBlockTime ())/(chainparams.GetConsensus ().nPowTargetSpacing );
1702+ nSyncStarted++;
16861703 // We used to request the full block here, but since headers-announcements are now the
16871704 // primary method of announcement on the network, and since, in the case that a node
16881705 // fell back to inv we probably have a reorg which we should get the headers for first,
@@ -3099,7 +3116,7 @@ bool SendMessages(CNode* pto, CConnman& connman, const std::atomic<bool>& interr
30993116 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.
31003117 if (!state.fSyncStarted && !pto->fClient && !fImporting && !fReindex ) {
31013118 // Only actively request headers from a single peer, unless we're close to end of initial download.
3102- if ((nSyncStarted == 0 && fFetch ) || pindexBestHeader->GetBlockTime () > GetAdjustedTime () - 6 * 60 * 60 ) { // NOTE: was "close to today" and 24h in Bitcoin
3119+ if ((nSyncStarted == 0 && fFetch ) || pindexBestHeader->GetBlockTime () > GetAdjustedTime () - nMaxTipAge ) {
31033120 state.fSyncStarted = true ;
31043121 state.nHeadersSyncTimeout = GetTimeMicros () + HEADERS_DOWNLOAD_TIMEOUT_BASE + HEADERS_DOWNLOAD_TIMEOUT_PER_HEADER * (GetAdjustedTime () - pindexBestHeader->GetBlockTime ())/(consensusParams.nPowTargetSpacing );
31053122 nSyncStarted++;
@@ -3418,7 +3435,7 @@ bool SendMessages(CNode* pto, CConnman& connman, const std::atomic<bool>& interr
34183435 // Check for headers sync timeouts
34193436 if (state.fSyncStarted && state.nHeadersSyncTimeout < std::numeric_limits<int64_t >::max ()) {
34203437 // Detect whether this is a stalling initial-headers-sync peer
3421- if (pindexBestHeader->GetBlockTime () <= GetAdjustedTime () - 6 * 60 * 60 ) { // was 24*60*60 in bitcoin
3438+ if (pindexBestHeader->GetBlockTime () <= GetAdjustedTime () - nMaxTipAge ) {
34223439 if (nNow > state.nHeadersSyncTimeout && nSyncStarted == 1 && (nPreferredDownload - state.fPreferredDownload >= 1 )) {
34233440 // Disconnect a (non-whitelisted) peer if it is our only sync peer,
34243441 // and we have others we could be using instead.
0 commit comments