@@ -43,10 +43,10 @@ static constexpr int64_t ORPHAN_TX_EXPIRE_INTERVAL = 5 * 60;
43
43
static constexpr std::chrono::seconds RELAY_TX_CACHE_TIME = std::chrono::minutes{15 };
44
44
/* * How long a transaction has to be in the mempool before it can unconditionally be relayed (even when not in mapRelay). */
45
45
static constexpr std::chrono::seconds UNCONDITIONAL_RELAY_DELAY = std::chrono::minutes{2 };
46
- /* * Headers download timeout expressed in microseconds
46
+ /* * Headers download timeout.
47
47
* 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;
50
50
/* * Protect at least this many outbound peers from disconnection due to slow/
51
51
* behind headers chain.
52
52
*/
@@ -93,8 +93,8 @@ static constexpr std::chrono::microseconds GETDATA_TX_INTERVAL{std::chrono::seco
93
93
static const unsigned int MAX_GETDATA_SZ = 1000 ;
94
94
/* * Number of blocks that can be requested at any given time from a single peer. */
95
95
static 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 ;
98
98
/* * Number of headers sent in one getheaders result. We rely on the assumption that if a peer sends
99
99
* less than this number, we reached its tip. Changing this value is a protocol upgrade. */
100
100
static const unsigned int MAX_HEADERS_RESULTS = 2000 ;
@@ -108,10 +108,10 @@ static const int MAX_BLOCKTXN_DEPTH = 10;
108
108
* degree of disordering of blocks on disk (which make reindexing and pruning harder). We'll probably
109
109
* want to make this a per-peer adaptive value at some point. */
110
110
static 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 ;
113
113
/* * 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 ;
115
115
/* * Maximum number of headers to announce when relaying blocks with headers message.*/
116
116
static const unsigned int MAX_BLOCKS_TO_ANNOUNCE = 8 ;
117
117
/* * Maximum number of unconnecting headers announcements before DoS score */
@@ -533,12 +533,12 @@ struct CNodeState {
533
533
// ! Whether we've started headers synchronization with this peer.
534
534
bool fSyncStarted ;
535
535
// ! When to potentially disconnect peer for stalling headers download
536
- int64_t nHeadersSyncTimeout ;
536
+ std::chrono::microseconds m_headers_sync_timeout{0us} ;
537
537
// ! Since when we're stalling block download progress (in microseconds), or 0.
538
- int64_t nStallingSince ;
538
+ std::chrono::microseconds m_stalling_since{0us} ;
539
539
std::list<QueuedBlock> vBlocksInFlight;
540
540
// ! 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} ;
542
542
int nBlocksInFlight;
543
543
int nBlocksInFlightValidHeaders;
544
544
// ! Whether we consider this a preferred download peer.
@@ -621,9 +621,6 @@ struct CNodeState {
621
621
pindexBestHeaderSent = nullptr ;
622
622
nUnconnectingHeaders = 0 ;
623
623
fSyncStarted = false ;
624
- nHeadersSyncTimeout = 0 ;
625
- nStallingSince = 0 ;
626
- nDownloadingSince = 0 ;
627
624
nBlocksInFlight = 0 ;
628
625
nBlocksInFlightValidHeaders = 0 ;
629
626
fPreferredDownload = false ;
@@ -672,11 +669,11 @@ bool PeerManagerImpl::MarkBlockAsReceived(const uint256& hash)
672
669
}
673
670
if (state->vBlocksInFlight .begin () == itInFlight->second .second ) {
674
671
// 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>());
676
673
}
677
674
state->vBlocksInFlight .erase (itInFlight->second .second );
678
675
state->nBlocksInFlight --;
679
- state->nStallingSince = 0 ;
676
+ state->m_stalling_since = 0us ;
680
677
mapBlocksInFlight.erase (itInFlight);
681
678
return true ;
682
679
}
@@ -706,7 +703,7 @@ bool PeerManagerImpl::MarkBlockAsInFlight(NodeId nodeid, const uint256& hash, co
706
703
state->nBlocksInFlightValidHeaders += it->fValidatedHeaders ;
707
704
if (state->nBlocksInFlight == 1 ) {
708
705
// 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>();
710
707
}
711
708
if (state->nBlocksInFlightValidHeaders == 1 && pindex != nullptr ) {
712
709
nPeersWithValidatedDownloads++;
@@ -4485,7 +4482,13 @@ bool PeerManagerImpl::SendMessages(CNode* pto)
4485
4482
// Only actively request headers from a single peer, unless we're close to today.
4486
4483
if ((nSyncStarted == 0 && fFetch ) || pindexBestHeader->GetBlockTime () > GetAdjustedTime () - 24 * 60 * 60 ) {
4487
4484
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
+ );
4489
4492
nSyncStarted++;
4490
4493
const CBlockIndex *pindexStart = pindexBestHeader;
4491
4494
/* If possible, start at the block preceding the currently
@@ -4795,33 +4798,33 @@ bool PeerManagerImpl::SendMessages(CNode* pto)
4795
4798
4796
4799
// Detect whether we're stalling
4797
4800
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) {
4799
4802
// Stalling only triggers when the block download window cannot move. During normal steady state,
4800
4803
// the download window should be much larger than the to-be-downloaded set of blocks, so disconnection
4801
4804
// should only happen during initial block download.
4802
4805
LogPrintf (" Peer=%d is stalling block download, disconnecting\n " , pto->GetId ());
4803
4806
pto->fDisconnect = true ;
4804
4807
return true ;
4805
4808
}
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)
4807
4810
// (with N the number of peers from which we're downloading validated blocks), disconnect due to timeout.
4808
4811
// We compensate for other peers to prevent killing off peers due to our own downstream link
4809
4812
// being saturated. We only count validated in-flight blocks so peers can't advertise non-existing block hashes
4810
4813
// to unreasonably increase our timeout.
4811
4814
if (state.vBlocksInFlight .size () > 0 ) {
4812
4815
QueuedBlock &queuedBlock = state.vBlocksInFlight .front ();
4813
4816
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)) {
4815
4818
LogPrintf (" Timeout downloading block %s from peer=%d, disconnecting\n " , queuedBlock.hash .ToString (), pto->GetId ());
4816
4819
pto->fDisconnect = true ;
4817
4820
return true ;
4818
4821
}
4819
4822
}
4820
4823
// 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 ()) {
4822
4825
// Detect whether this is a stalling initial-headers-sync peer
4823
4826
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 )) {
4825
4828
// Disconnect a peer (without the noban permission) if it is our only sync peer,
4826
4829
// and we have others we could be using instead.
4827
4830
// Note: If all our peers are inbound, then we won't
@@ -4840,13 +4843,13 @@ bool PeerManagerImpl::SendMessages(CNode* pto)
4840
4843
// this peer (eventually).
4841
4844
state.fSyncStarted = false ;
4842
4845
nSyncStarted--;
4843
- state.nHeadersSyncTimeout = 0 ;
4846
+ state.m_headers_sync_timeout = 0us ;
4844
4847
}
4845
4848
}
4846
4849
} else {
4847
4850
// After we've caught up once, reset the timeout so we can't trigger
4848
4851
// disconnect later.
4849
- state.nHeadersSyncTimeout = std::numeric_limits< int64_t > ::max ();
4852
+ state.m_headers_sync_timeout = std::chrono::microseconds ::max ();
4850
4853
}
4851
4854
}
4852
4855
@@ -4870,8 +4873,8 @@ bool PeerManagerImpl::SendMessages(CNode* pto)
4870
4873
pindex->nHeight , pto->GetId ());
4871
4874
}
4872
4875
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;
4875
4878
LogPrint (BCLog::NET, " Stall started peer=%d\n " , staller);
4876
4879
}
4877
4880
}
0 commit comments