@@ -1682,7 +1682,24 @@ bool static ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStr
1682
1682
1683
1683
if (inv.type == MSG_BLOCK) {
1684
1684
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++;
1686
1703
// We used to request the full block here, but since headers-announcements are now the
1687
1704
// primary method of announcement on the network, and since, in the case that a node
1688
1705
// 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
3099
3116
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.
3100
3117
if (!state.fSyncStarted && !pto->fClient && !fImporting && !fReindex ) {
3101
3118
// 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 ) {
3103
3120
state.fSyncStarted = true ;
3104
3121
state.nHeadersSyncTimeout = GetTimeMicros () + HEADERS_DOWNLOAD_TIMEOUT_BASE + HEADERS_DOWNLOAD_TIMEOUT_PER_HEADER * (GetAdjustedTime () - pindexBestHeader->GetBlockTime ())/(consensusParams.nPowTargetSpacing );
3105
3122
nSyncStarted++;
@@ -3418,7 +3435,7 @@ bool SendMessages(CNode* pto, CConnman& connman, const std::atomic<bool>& interr
3418
3435
// Check for headers sync timeouts
3419
3436
if (state.fSyncStarted && state.nHeadersSyncTimeout < std::numeric_limits<int64_t >::max ()) {
3420
3437
// 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 ) {
3422
3439
if (nNow > state.nHeadersSyncTimeout && nSyncStarted == 1 && (nPreferredDownload - state.fPreferredDownload >= 1 )) {
3423
3440
// Disconnect a (non-whitelisted) peer if it is our only sync peer,
3424
3441
// and we have others we could be using instead.
0 commit comments