@@ -57,8 +57,8 @@ static constexpr auto HEADERS_DOWNLOAD_TIMEOUT_PER_HEADER = 1ms;
57
57
* behind headers chain.
58
58
*/
59
59
static constexpr int32_t MAX_OUTBOUND_PEERS_TO_PROTECT_FROM_DISCONNECT = 4 ;
60
- /* * Timeout for (unprotected) outbound peers to sync to our chainwork, in seconds */
61
- static constexpr int64_t CHAIN_SYNC_TIMEOUT = 20 * 60 ; // 20 minutes
60
+ /* * Timeout for (unprotected) outbound peers to sync to our chainwork */
61
+ static constexpr auto CHAIN_SYNC_TIMEOUT{20min};
62
62
/* * How frequently to check for stale tips */
63
63
static constexpr auto STALE_CHECK_INTERVAL{10min};
64
64
/* * How frequently to check for extra outbound peers and disconnect */
@@ -329,7 +329,7 @@ class PeerManagerImpl final : public PeerManager
329
329
EXCLUSIVE_LOCKS_REQUIRED(cs_main);
330
330
331
331
/* * Consider evicting an outbound peer based on the amount of time they've been behind our tip */
332
- void ConsiderEviction (CNode& pto, int64_t time_in_seconds) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
332
+ void ConsiderEviction (CNode& pto, std::chrono::seconds time_in_seconds) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
333
333
334
334
/* * If we have extra outbound peers, try to disconnect the one with the oldest block announcement */
335
335
void EvictExtraOutboundPeers (std::chrono::seconds now) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
@@ -742,7 +742,7 @@ struct CNodeState {
742
742
*/
743
743
struct ChainSyncTimeoutState {
744
744
// ! A timeout used for checking whether our peer has sufficiently synced
745
- int64_t m_timeout{0 };
745
+ std::chrono::seconds m_timeout{0s };
746
746
// ! A header with the work we require on our peer's chain
747
747
const CBlockIndex* m_work_header{nullptr };
748
748
// ! After timeout is reached, set to true after sending getheaders
@@ -949,10 +949,10 @@ bool PeerManagerImpl::TipMayBeStale()
949
949
{
950
950
AssertLockHeld (cs_main);
951
951
const Consensus::Params& consensusParams = m_chainparams.GetConsensus ();
952
- if (count_seconds ( m_last_tip_update) == 0 ) {
952
+ if (m_last_tip_update. load ( ) == 0s ) {
953
953
m_last_tip_update = GetTime<std::chrono::seconds>();
954
954
}
955
- return count_seconds ( m_last_tip_update) < GetTime () - consensusParams.nPowTargetSpacing * 3 && mapBlocksInFlight.empty ();
955
+ return m_last_tip_update. load ( ) < GetTime<std::chrono::seconds> () - std::chrono::seconds{ consensusParams.nPowTargetSpacing * 3 } && mapBlocksInFlight.empty ();
956
956
}
957
957
958
958
bool PeerManagerImpl::CanDirectFetch ()
@@ -4180,7 +4180,7 @@ bool PeerManagerImpl::ProcessMessages(CNode* pfrom, std::atomic<bool>& interrupt
4180
4180
return fMoreWork ;
4181
4181
}
4182
4182
4183
- void PeerManagerImpl::ConsiderEviction (CNode& pto, int64_t time_in_seconds)
4183
+ void PeerManagerImpl::ConsiderEviction (CNode& pto, std::chrono::seconds time_in_seconds)
4184
4184
{
4185
4185
AssertLockHeld (cs_main);
4186
4186
@@ -4195,20 +4195,20 @@ void PeerManagerImpl::ConsiderEviction(CNode& pto, int64_t time_in_seconds)
4195
4195
// unless it's invalid, in which case we should find that out and
4196
4196
// disconnect from them elsewhere).
4197
4197
if (state.pindexBestKnownBlock != nullptr && state.pindexBestKnownBlock ->nChainWork >= m_chainman.ActiveChain ().Tip ()->nChainWork ) {
4198
- if (state.m_chain_sync .m_timeout != 0 ) {
4199
- state.m_chain_sync .m_timeout = 0 ;
4198
+ if (state.m_chain_sync .m_timeout != 0s ) {
4199
+ state.m_chain_sync .m_timeout = 0s ;
4200
4200
state.m_chain_sync .m_work_header = nullptr ;
4201
4201
state.m_chain_sync .m_sent_getheaders = false ;
4202
4202
}
4203
- } else if (state.m_chain_sync .m_timeout == 0 || (state.m_chain_sync .m_work_header != nullptr && state.pindexBestKnownBlock != nullptr && state.pindexBestKnownBlock ->nChainWork >= state.m_chain_sync .m_work_header ->nChainWork )) {
4203
+ } else if (state.m_chain_sync .m_timeout == 0s || (state.m_chain_sync .m_work_header != nullptr && state.pindexBestKnownBlock != nullptr && state.pindexBestKnownBlock ->nChainWork >= state.m_chain_sync .m_work_header ->nChainWork )) {
4204
4204
// Our best block known by this peer is behind our tip, and we're either noticing
4205
4205
// that for the first time, OR this peer was able to catch up to some earlier point
4206
4206
// where we checked against our tip.
4207
4207
// Either way, set a new timeout based on current tip.
4208
4208
state.m_chain_sync .m_timeout = time_in_seconds + CHAIN_SYNC_TIMEOUT;
4209
4209
state.m_chain_sync .m_work_header = m_chainman.ActiveChain ().Tip ();
4210
4210
state.m_chain_sync .m_sent_getheaders = false ;
4211
- } else if (state.m_chain_sync .m_timeout > 0 && time_in_seconds > state.m_chain_sync .m_timeout ) {
4211
+ } else if (state.m_chain_sync .m_timeout > 0s && time_in_seconds > state.m_chain_sync .m_timeout ) {
4212
4212
// No evidence yet that our peer has synced to a chain with work equal to that
4213
4213
// of our tip, when we first detected it was behind. Send a single getheaders
4214
4214
// message to give the peer a chance to update us.
@@ -4221,7 +4221,7 @@ void PeerManagerImpl::ConsiderEviction(CNode& pto, int64_t time_in_seconds)
4221
4221
LogPrint (BCLog::NET, " sending getheaders to outbound peer=%d to verify chain work (current best known block:%s, benchmark blockhash: %s)\n " , pto.GetId (), state.pindexBestKnownBlock != nullptr ? state.pindexBestKnownBlock ->GetBlockHash ().ToString () : " <none>" , state.m_chain_sync .m_work_header ->GetBlockHash ().ToString ());
4222
4222
m_connman.PushMessage (&pto, msgMaker.Make (NetMsgType::GETHEADERS, m_chainman.ActiveChain ().GetLocator (state.m_chain_sync .m_work_header ->pprev ), uint256 ()));
4223
4223
state.m_chain_sync .m_sent_getheaders = true ;
4224
- constexpr int64_t HEADERS_RESPONSE_TIME = 120 ; // 2 minutes
4224
+ constexpr auto HEADERS_RESPONSE_TIME{2min};
4225
4225
// Bump the timeout to allow a response, which could clear the timeout
4226
4226
// (if the response shows the peer has synced), reset the timeout (if
4227
4227
// the peer syncs to the required work but not to our tip), or result
@@ -4348,7 +4348,8 @@ void PeerManagerImpl::CheckForStaleTipAndEvictPeers()
4348
4348
// Check whether our tip is stale, and if so, allow using an extra
4349
4349
// outbound peer
4350
4350
if (!fImporting && !fReindex && m_connman.GetNetworkActive () && m_connman.GetUseAddrmanOutgoing () && TipMayBeStale ()) {
4351
- LogPrintf (" Potential stale tip detected, will try using extra outbound peer (last tip update: %d seconds ago)\n " , count_seconds (now) - count_seconds (m_last_tip_update));
4351
+ LogPrintf (" Potential stale tip detected, will try using extra outbound peer (last tip update: %d seconds ago)\n " ,
4352
+ count_seconds (now - m_last_tip_update.load ()));
4352
4353
m_connman.SetTryNewOutboundPeer (true );
4353
4354
} else if (m_connman.GetTryNewOutboundPeer ()) {
4354
4355
m_connman.SetTryNewOutboundPeer (false );
@@ -4969,7 +4970,7 @@ bool PeerManagerImpl::SendMessages(CNode* pto)
4969
4970
4970
4971
// Check that outbound peers have reasonable chains
4971
4972
// GetTime() is used by this anti-DoS logic so we can test this using mocktime
4972
- ConsiderEviction (*pto, GetTime ());
4973
+ ConsiderEviction (*pto, GetTime<std::chrono::seconds> ());
4973
4974
4974
4975
//
4975
4976
// Message: getdata (blocks)
0 commit comments