39
39
#include < validation.h>
40
40
41
41
#include < algorithm>
42
+ #include < atomic>
43
+ #include < chrono>
42
44
#include < memory>
43
45
#include < optional>
44
46
#include < typeinfo>
@@ -57,10 +59,10 @@ static constexpr auto HEADERS_DOWNLOAD_TIMEOUT_PER_HEADER = 1ms;
57
59
static constexpr int32_t MAX_OUTBOUND_PEERS_TO_PROTECT_FROM_DISCONNECT = 4 ;
58
60
/* * Timeout for (unprotected) outbound peers to sync to our chainwork, in seconds */
59
61
static constexpr int64_t CHAIN_SYNC_TIMEOUT = 20 * 60 ; // 20 minutes
60
- /* * How frequently to check for stale tips, in seconds */
61
- static constexpr int64_t STALE_CHECK_INTERVAL = 10 * 60 ; // 10 minutes
62
- /* * How frequently to check for extra outbound peers and disconnect, in seconds */
63
- static constexpr int64_t EXTRA_PEER_CHECK_INTERVAL = 45 ;
62
+ /* * How frequently to check for stale tips */
63
+ static constexpr auto STALE_CHECK_INTERVAL{10min};
64
+ /* * How frequently to check for extra outbound peers and disconnect */
65
+ static constexpr auto EXTRA_PEER_CHECK_INTERVAL{45s} ;
64
66
/* * Minimum time an outbound-peer-eviction candidate must be connected for, in order to evict */
65
67
static constexpr std::chrono::seconds MINIMUM_CONNECT_TIME{30 };
66
68
/* * SHA256("main address relay")[0:8] */
@@ -422,7 +424,7 @@ class PeerManagerImpl final : public PeerManager
422
424
std::atomic<int > m_best_height{-1 };
423
425
424
426
/* * Next time to check for stale tip */
425
- int64_t m_stale_tip_check_time{0 };
427
+ std::chrono::seconds m_stale_tip_check_time{0s };
426
428
427
429
/* * Whether this node is running in blocks only mode */
428
430
const bool m_ignore_incoming_txs;
@@ -541,7 +543,7 @@ class PeerManagerImpl final : public PeerManager
541
543
std::map<uint256, std::pair<NodeId, std::list<QueuedBlock>::iterator> > mapBlocksInFlight GUARDED_BY (cs_main);
542
544
543
545
/* * When our tip was last updated. */
544
- std::atomic<int64_t > m_last_tip_update{0 };
546
+ std::atomic<std::chrono::seconds > m_last_tip_update{0s };
545
547
546
548
/* * Determine whether or not a peer can request a transaction, and return it (or nullptr if not found or not allowed). */
547
549
CTransactionRef FindTxForGetData (const CNode& peer, const GenTxid& gtxid, const std::chrono::seconds mempool_req, const std::chrono::seconds now) LOCKS_EXCLUDED(cs_main);
@@ -947,10 +949,10 @@ bool PeerManagerImpl::TipMayBeStale()
947
949
{
948
950
AssertLockHeld (cs_main);
949
951
const Consensus::Params& consensusParams = m_chainparams.GetConsensus ();
950
- if (m_last_tip_update == 0 ) {
951
- m_last_tip_update = GetTime ();
952
+ if (count_seconds ( m_last_tip_update) == 0 ) {
953
+ m_last_tip_update = GetTime<std::chrono::seconds> ();
952
954
}
953
- return m_last_tip_update < GetTime () - consensusParams.nPowTargetSpacing * 3 && mapBlocksInFlight.empty ();
955
+ return count_seconds ( m_last_tip_update) < GetTime () - consensusParams.nPowTargetSpacing * 3 && mapBlocksInFlight.empty ();
954
956
}
955
957
956
958
bool PeerManagerImpl::CanDirectFetch ()
@@ -1505,7 +1507,7 @@ void PeerManagerImpl::StartScheduledTasks(CScheduler& scheduler)
1505
1507
void PeerManagerImpl::BlockConnected (const std::shared_ptr<const CBlock>& pblock, const CBlockIndex* pindex)
1506
1508
{
1507
1509
m_orphanage.EraseForBlock (*pblock);
1508
- m_last_tip_update = GetTime ();
1510
+ m_last_tip_update = GetTime<std::chrono::seconds> ();
1509
1511
1510
1512
{
1511
1513
LOCK (m_recent_confirmed_transactions_mutex);
@@ -4335,20 +4337,20 @@ void PeerManagerImpl::CheckForStaleTipAndEvictPeers()
4335
4337
{
4336
4338
LOCK (cs_main);
4337
4339
4338
- int64_t time_in_seconds = GetTime () ;
4340
+ auto now{ GetTime<std::chrono::seconds>()} ;
4339
4341
4340
- EvictExtraOutboundPeers (std::chrono::seconds{time_in_seconds} );
4342
+ EvictExtraOutboundPeers (now );
4341
4343
4342
- if (time_in_seconds > m_stale_tip_check_time) {
4344
+ if (now > m_stale_tip_check_time) {
4343
4345
// Check whether our tip is stale, and if so, allow using an extra
4344
4346
// outbound peer
4345
4347
if (!fImporting && !fReindex && m_connman.GetNetworkActive () && m_connman.GetUseAddrmanOutgoing () && TipMayBeStale ()) {
4346
- LogPrintf (" Potential stale tip detected, will try using extra outbound peer (last tip update: %d seconds ago)\n " , time_in_seconds - m_last_tip_update);
4348
+ 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) );
4347
4349
m_connman.SetTryNewOutboundPeer (true );
4348
4350
} else if (m_connman.GetTryNewOutboundPeer ()) {
4349
4351
m_connman.SetTryNewOutboundPeer (false );
4350
4352
}
4351
- m_stale_tip_check_time = time_in_seconds + STALE_CHECK_INTERVAL;
4353
+ m_stale_tip_check_time = now + STALE_CHECK_INTERVAL;
4352
4354
}
4353
4355
4354
4356
if (!m_initial_sync_finished && CanDirectFetch ()) {
0 commit comments