@@ -43,10 +43,10 @@ static constexpr int64_t ORPHAN_TX_EXPIRE_INTERVAL = 5 * 60;
4343static constexpr std::chrono::seconds RELAY_TX_CACHE_TIME = std::chrono::minutes{15 };
4444/* * How long a transaction has to be in the mempool before it can unconditionally be relayed (even when not in mapRelay). */
4545static constexpr std::chrono::seconds UNCONDITIONAL_RELAY_DELAY = std::chrono::minutes{2 };
46- /* * Headers download timeout expressed in microseconds
46+ /* * Headers download timeout.
4747 * Timeout = base + per_header * (expected number of headers) */
48- static constexpr int64_t HEADERS_DOWNLOAD_TIMEOUT_BASE = 15 * 60 * 1000000 ; // 15 minutes
49- static constexpr int64_t HEADERS_DOWNLOAD_TIMEOUT_PER_HEADER = 1000 ; // 1ms/header
48+ static constexpr auto HEADERS_DOWNLOAD_TIMEOUT_BASE = 15min;
49+ static constexpr auto HEADERS_DOWNLOAD_TIMEOUT_PER_HEADER = 1ms;
5050/* * Protect at least this many outbound peers from disconnection due to slow/
5151 * behind headers chain.
5252 */
@@ -93,8 +93,8 @@ static constexpr std::chrono::microseconds GETDATA_TX_INTERVAL{std::chrono::seco
9393static const unsigned int MAX_GETDATA_SZ = 1000 ;
9494/* * Number of blocks that can be requested at any given time from a single peer. */
9595static const int MAX_BLOCKS_IN_TRANSIT_PER_PEER = 16 ;
96- /* * Timeout in seconds during which a peer must stall block download progress before being disconnected. */
97- static const unsigned int BLOCK_STALLING_TIMEOUT = 2 ;
96+ /* * Time during which a peer must stall block download progress before being disconnected. */
97+ static constexpr auto BLOCK_STALLING_TIMEOUT = 2s ;
9898/* * Number of headers sent in one getheaders result. We rely on the assumption that if a peer sends
9999 * less than this number, we reached its tip. Changing this value is a protocol upgrade. */
100100static const unsigned int MAX_HEADERS_RESULTS = 2000 ;
@@ -108,10 +108,10 @@ static const int MAX_BLOCKTXN_DEPTH = 10;
108108 * degree of disordering of blocks on disk (which make reindexing and pruning harder). We'll probably
109109 * want to make this a per-peer adaptive value at some point. */
110110static const unsigned int BLOCK_DOWNLOAD_WINDOW = 1024 ;
111- /* * Block download timeout base, expressed in millionths of the block interval (i.e. 10 min) */
112- static const int64_t BLOCK_DOWNLOAD_TIMEOUT_BASE = 1000000 ;
111+ /* * Block download timeout base, expressed in multiples of the block interval (i.e. 10 min) */
112+ static constexpr double BLOCK_DOWNLOAD_TIMEOUT_BASE = 1 ;
113113/* * Additional block download timeout per parallel downloading peer (i.e. 5 min) */
114- static const int64_t BLOCK_DOWNLOAD_TIMEOUT_PER_PEER = 500000 ;
114+ static constexpr double BLOCK_DOWNLOAD_TIMEOUT_PER_PEER = 0.5 ;
115115/* * Maximum number of headers to announce when relaying blocks with headers message.*/
116116static const unsigned int MAX_BLOCKS_TO_ANNOUNCE = 8 ;
117117/* * Maximum number of unconnecting headers announcements before DoS score */
@@ -533,12 +533,12 @@ struct CNodeState {
533533 // ! Whether we've started headers synchronization with this peer.
534534 bool fSyncStarted ;
535535 // ! When to potentially disconnect peer for stalling headers download
536- int64_t nHeadersSyncTimeout ;
536+ std::chrono::microseconds m_headers_sync_timeout{0us} ;
537537 // ! Since when we're stalling block download progress (in microseconds), or 0.
538- int64_t nStallingSince ;
538+ std::chrono::microseconds m_stalling_since{0us} ;
539539 std::list<QueuedBlock> vBlocksInFlight;
540540 // ! When the first entry in vBlocksInFlight started downloading. Don't care when vBlocksInFlight is empty.
541- int64_t nDownloadingSince ;
541+ std::chrono::microseconds m_downloading_since{0us} ;
542542 int nBlocksInFlight;
543543 int nBlocksInFlightValidHeaders;
544544 // ! Whether we consider this a preferred download peer.
@@ -621,9 +621,6 @@ struct CNodeState {
621621 pindexBestHeaderSent = nullptr ;
622622 nUnconnectingHeaders = 0 ;
623623 fSyncStarted = false ;
624- nHeadersSyncTimeout = 0 ;
625- nStallingSince = 0 ;
626- nDownloadingSince = 0 ;
627624 nBlocksInFlight = 0 ;
628625 nBlocksInFlightValidHeaders = 0 ;
629626 fPreferredDownload = false ;
@@ -672,11 +669,11 @@ bool PeerManagerImpl::MarkBlockAsReceived(const uint256& hash)
672669 }
673670 if (state->vBlocksInFlight .begin () == itInFlight->second .second ) {
674671 // First block on the queue was received, update the start download time for the next one
675- state->nDownloadingSince = std::max (state->nDownloadingSince , count_microseconds ( GetTime<std::chrono::microseconds>() ));
672+ state->m_downloading_since = std::max (state->m_downloading_since , GetTime<std::chrono::microseconds>());
676673 }
677674 state->vBlocksInFlight .erase (itInFlight->second .second );
678675 state->nBlocksInFlight --;
679- state->nStallingSince = 0 ;
676+ state->m_stalling_since = 0us ;
680677 mapBlocksInFlight.erase (itInFlight);
681678 return true ;
682679 }
@@ -706,7 +703,7 @@ bool PeerManagerImpl::MarkBlockAsInFlight(NodeId nodeid, const uint256& hash, co
706703 state->nBlocksInFlightValidHeaders += it->fValidatedHeaders ;
707704 if (state->nBlocksInFlight == 1 ) {
708705 // We're starting a block download (batch) from this peer.
709- state->nDownloadingSince = GetTime<std::chrono::microseconds>(). count ();
706+ state->m_downloading_since = GetTime<std::chrono::microseconds>();
710707 }
711708 if (state->nBlocksInFlightValidHeaders == 1 && pindex != nullptr ) {
712709 nPeersWithValidatedDownloads++;
@@ -4485,7 +4482,13 @@ bool PeerManagerImpl::SendMessages(CNode* pto)
44854482 // Only actively request headers from a single peer, unless we're close to today.
44864483 if ((nSyncStarted == 0 && fFetch ) || pindexBestHeader->GetBlockTime () > GetAdjustedTime () - 24 * 60 * 60 ) {
44874484 state.fSyncStarted = true ;
4488- state.nHeadersSyncTimeout = count_microseconds (current_time) + HEADERS_DOWNLOAD_TIMEOUT_BASE + HEADERS_DOWNLOAD_TIMEOUT_PER_HEADER * (GetAdjustedTime () - pindexBestHeader->GetBlockTime ())/(consensusParams.nPowTargetSpacing );
4485+ state.m_headers_sync_timeout = current_time + HEADERS_DOWNLOAD_TIMEOUT_BASE +
4486+ (
4487+ // Convert HEADERS_DOWNLOAD_TIMEOUT_PER_HEADER to microseconds before scaling
4488+ // to maintain precision
4489+ std::chrono::microseconds{HEADERS_DOWNLOAD_TIMEOUT_PER_HEADER} *
4490+ (GetAdjustedTime () - pindexBestHeader->GetBlockTime ()) / consensusParams.nPowTargetSpacing
4491+ );
44894492 nSyncStarted++;
44904493 const CBlockIndex *pindexStart = pindexBestHeader;
44914494 /* If possible, start at the block preceding the currently
@@ -4795,33 +4798,33 @@ bool PeerManagerImpl::SendMessages(CNode* pto)
47954798
47964799 // Detect whether we're stalling
47974800 current_time = GetTime<std::chrono::microseconds>();
4798- if (state.nStallingSince && state.nStallingSince < count_microseconds ( current_time) - 1000000 * BLOCK_STALLING_TIMEOUT) {
4801+ if (state.m_stalling_since . count () && state.m_stalling_since < current_time - BLOCK_STALLING_TIMEOUT) {
47994802 // Stalling only triggers when the block download window cannot move. During normal steady state,
48004803 // the download window should be much larger than the to-be-downloaded set of blocks, so disconnection
48014804 // should only happen during initial block download.
48024805 LogPrintf (" Peer=%d is stalling block download, disconnecting\n " , pto->GetId ());
48034806 pto->fDisconnect = true ;
48044807 return true ;
48054808 }
4806- // In case there is a block that has been in flight from this peer for 2 + 0.5 * N times the block interval
4809+ // In case there is a block that has been in flight from this peer for block_interval * (1 + 0.5 * N)
48074810 // (with N the number of peers from which we're downloading validated blocks), disconnect due to timeout.
48084811 // We compensate for other peers to prevent killing off peers due to our own downstream link
48094812 // being saturated. We only count validated in-flight blocks so peers can't advertise non-existing block hashes
48104813 // to unreasonably increase our timeout.
48114814 if (state.vBlocksInFlight .size () > 0 ) {
48124815 QueuedBlock &queuedBlock = state.vBlocksInFlight .front ();
48134816 int nOtherPeersWithValidatedDownloads = nPeersWithValidatedDownloads - (state.nBlocksInFlightValidHeaders > 0 );
4814- if (count_microseconds ( current_time) > state.nDownloadingSince + consensusParams.nPowTargetSpacing * (BLOCK_DOWNLOAD_TIMEOUT_BASE + BLOCK_DOWNLOAD_TIMEOUT_PER_PEER * nOtherPeersWithValidatedDownloads)) {
4817+ if (current_time > state.m_downloading_since + std::chrono::seconds{ consensusParams.nPowTargetSpacing } * (BLOCK_DOWNLOAD_TIMEOUT_BASE + BLOCK_DOWNLOAD_TIMEOUT_PER_PEER * nOtherPeersWithValidatedDownloads)) {
48154818 LogPrintf (" Timeout downloading block %s from peer=%d, disconnecting\n " , queuedBlock.hash .ToString (), pto->GetId ());
48164819 pto->fDisconnect = true ;
48174820 return true ;
48184821 }
48194822 }
48204823 // Check for headers sync timeouts
4821- if (state.fSyncStarted && state.nHeadersSyncTimeout < std::numeric_limits< int64_t > ::max ()) {
4824+ if (state.fSyncStarted && state.m_headers_sync_timeout < std::chrono::microseconds ::max ()) {
48224825 // Detect whether this is a stalling initial-headers-sync peer
48234826 if (pindexBestHeader->GetBlockTime () <= GetAdjustedTime () - 24 * 60 * 60 ) {
4824- if (count_microseconds ( current_time) > state.nHeadersSyncTimeout && nSyncStarted == 1 && (nPreferredDownload - state.fPreferredDownload >= 1 )) {
4827+ if (current_time > state.m_headers_sync_timeout && nSyncStarted == 1 && (nPreferredDownload - state.fPreferredDownload >= 1 )) {
48254828 // Disconnect a peer (without the noban permission) if it is our only sync peer,
48264829 // and we have others we could be using instead.
48274830 // Note: If all our peers are inbound, then we won't
@@ -4840,13 +4843,13 @@ bool PeerManagerImpl::SendMessages(CNode* pto)
48404843 // this peer (eventually).
48414844 state.fSyncStarted = false ;
48424845 nSyncStarted--;
4843- state.nHeadersSyncTimeout = 0 ;
4846+ state.m_headers_sync_timeout = 0us ;
48444847 }
48454848 }
48464849 } else {
48474850 // After we've caught up once, reset the timeout so we can't trigger
48484851 // disconnect later.
4849- state.nHeadersSyncTimeout = std::numeric_limits< int64_t > ::max ();
4852+ state.m_headers_sync_timeout = std::chrono::microseconds ::max ();
48504853 }
48514854 }
48524855
@@ -4870,8 +4873,8 @@ bool PeerManagerImpl::SendMessages(CNode* pto)
48704873 pindex->nHeight , pto->GetId ());
48714874 }
48724875 if (state.nBlocksInFlight == 0 && staller != -1 ) {
4873- if (State (staller)->nStallingSince == 0 ) {
4874- State (staller)->nStallingSince = count_microseconds ( current_time) ;
4876+ if (State (staller)->m_stalling_since == 0us ) {
4877+ State (staller)->m_stalling_since = current_time;
48754878 LogPrint (BCLog::NET, " Stall started peer=%d\n " , staller);
48764879 }
48774880 }
0 commit comments