@@ -50,12 +50,12 @@ static constexpr int64_t HEADERS_DOWNLOAD_TIMEOUT_PER_HEADER = 1000; // 1ms/head
5050static constexpr int32_t MAX_OUTBOUND_PEERS_TO_PROTECT_FROM_DISCONNECT = 4 ;
5151/* * Timeout for (unprotected) outbound peers to sync to our chainwork, in seconds */
5252static constexpr int64_t CHAIN_SYNC_TIMEOUT = 20 * 60 ; // 20 minutes
53- /* * How frequently to check for stale tips, in seconds */
54- static constexpr int64_t STALE_CHECK_INTERVAL = 10 * 60 ; // 10 minutes
55- /* * How frequently to check for extra outbound peers and disconnect, in seconds */
56- static constexpr int64_t EXTRA_PEER_CHECK_INTERVAL = 45 ;
57- /* * Minimum time an outbound-peer-eviction candidate must be connected for, in order to evict, in seconds */
58- static constexpr int64_t MINIMUM_CONNECT_TIME = 30 ;
53+ /* * How frequently to check for stale tips */
54+ static constexpr std::chrono::minutes STALE_CHECK_INTERVAL{ 10 };
55+ /* * How frequently to check for extra outbound peers and disconnect */
56+ static constexpr std::chrono::seconds EXTRA_PEER_CHECK_INTERVAL{ 45 } ;
57+ /* * Minimum time an outbound-peer-eviction candidate must be connected for, in order to evict */
58+ static constexpr std::chrono::seconds MINIMUM_CONNECT_TIME{ 30 } ;
5959/* * SHA256("main address relay")[0:8] */
6060static constexpr uint64_t RANDOMIZER_ID_ADDRESS_RELAY = 0x3cac0035b5866b90ULL ;
6161// / Age after which a stale block will no longer be served if requested as
@@ -177,7 +177,7 @@ namespace {
177177 int g_outbound_peers_with_protect_from_disconnect GUARDED_BY (cs_main) = 0;
178178
179179 /* * When our tip was last updated. */
180- std::atomic<int64_t > g_last_tip_update ( 0 ) ;
180+ std::atomic<std::chrono::seconds > g_last_tip_update{std::chrono::seconds{ 0 }} ;
181181
182182 /* * Relay map */
183183 typedef std::map<uint256, CTransactionRef> MapRelay;
@@ -578,10 +578,10 @@ static void MaybeSetPeerAsAnnouncingHeaderAndIDs(NodeId nodeid, CConnman* connma
578578static bool TipMayBeStale (const Consensus::Params &consensusParams) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
579579{
580580 AssertLockHeld (cs_main);
581- if (g_last_tip_update == 0 ) {
582- g_last_tip_update = GetTime ();
581+ if (g_last_tip_update. load (). count () == 0 ) {
582+ g_last_tip_update = GetTime<std::chrono::seconds> ();
583583 }
584- return g_last_tip_update < GetTime () - consensusParams.nPowTargetSpacing * 3 && mapBlocksInFlight.empty ();
584+ return g_last_tip_update. load () < GetTime<std::chrono::seconds> () - std::chrono::seconds{ consensusParams.nPowTargetSpacing } * 3 && mapBlocksInFlight.empty ();
585585}
586586
587587static bool CanDirectFetch (const Consensus::Params &consensusParams) EXCLUSIVE_LOCKS_REQUIRED(cs_main)
@@ -758,6 +758,7 @@ void UpdateLastBlockAnnounceTime(NodeId node, int64_t time_in_seconds)
758758 LOCK (cs_main);
759759 CNodeState *state = State (node);
760760 if (state) state->m_last_block_announcement = time_in_seconds;
761+ if (state) state->fHaveWitness = true ;
761762}
762763
763764// Returns true for outbound peers, excluding manual connections, feelers, and
@@ -1127,7 +1128,7 @@ PeerLogicValidation::PeerLogicValidation(CConnman* connmanIn, BanMan* banman, CS
11271128 // combine them in one function and schedule at the quicker (peer-eviction)
11281129 // timer.
11291130 static_assert (EXTRA_PEER_CHECK_INTERVAL < STALE_CHECK_INTERVAL, " peer eviction timer should be less than stale tip check timer" );
1130- scheduler.scheduleEvery ([this , consensusParams] { this ->CheckForStaleTipAndEvictPeers (consensusParams); }, std::chrono::seconds{ EXTRA_PEER_CHECK_INTERVAL} );
1131+ scheduler.scheduleEvery ([this , consensusParams] { this ->CheckForStaleTipAndEvictPeers (consensusParams); }, EXTRA_PEER_CHECK_INTERVAL);
11311132}
11321133
11331134/* *
@@ -1165,7 +1166,7 @@ void PeerLogicValidation::BlockConnected(const std::shared_ptr<const CBlock>& pb
11651166 LogPrint (BCLog::MEMPOOL, " Erased %d orphan tx included or conflicted by block\n " , nErased);
11661167 }
11671168
1168- g_last_tip_update = GetTime ();
1169+ g_last_tip_update = GetTime<std::chrono::seconds> ();
11691170 }
11701171 {
11711172 LOCK (g_cs_recent_confirmed_transactions);
@@ -3430,7 +3431,7 @@ void PeerLogicValidation::ConsiderEviction(CNode *pto, int64_t time_in_seconds)
34303431 }
34313432}
34323433
3433- void PeerLogicValidation::EvictExtraOutboundPeers (int64_t time_in_seconds )
3434+ void PeerLogicValidation::EvictExtraOutboundPeers (std::chrono::seconds time )
34343435{
34353436 // Check whether we have too many outbound peers
34363437 int extra_peers = connman->GetExtraOutboundCount ();
@@ -3467,8 +3468,8 @@ void PeerLogicValidation::EvictExtraOutboundPeers(int64_t time_in_seconds)
34673468 // it time for new information to have arrived.
34683469 // Also don't disconnect any peer we're trying to download a
34693470 // block from.
3470- CNodeState & state = *State (pnode->GetId ());
3471- if (time_in_seconds - pnode->nTimeConnected > MINIMUM_CONNECT_TIME && state.nBlocksInFlight == 0 ) {
3471+ CNodeState& state = *State (pnode->GetId ());
3472+ if (time - std::chrono::seconds{ pnode->nTimeConnected } > MINIMUM_CONNECT_TIME && state.nBlocksInFlight == 0 ) {
34723473 LogPrint (BCLog::NET, " disconnecting extra outbound peer=%d (last block announcement received at time %d)\n " , pnode->GetId (), oldest_block_announcement);
34733474 pnode->fDisconnect = true ;
34743475 return true ;
@@ -3495,20 +3496,20 @@ void PeerLogicValidation::CheckForStaleTipAndEvictPeers(const Consensus::Params
34953496
34963497 if (connman == nullptr ) return ;
34973498
3498- int64_t time_in_seconds = GetTime ();
3499+ const auto time = GetTime<std::chrono::seconds> ();
34993500
3500- EvictExtraOutboundPeers (time_in_seconds );
3501+ EvictExtraOutboundPeers (time );
35013502
3502- if (time_in_seconds > m_stale_tip_check_time) {
3503+ if (time > m_stale_tip_check_time) {
35033504 // Check whether our tip is stale, and if so, allow using an extra
35043505 // outbound peer
35053506 if (!fImporting && !fReindex && connman->GetNetworkActive () && connman->GetUseAddrmanOutgoing () && TipMayBeStale (consensusParams)) {
3506- LogPrintf (" Potential stale tip detected, will try using extra outbound peer (last tip update: %d seconds ago)\n " , time_in_seconds - g_last_tip_update);
3507+ LogPrintf (" Potential stale tip detected, will try using extra outbound peer (last tip update: %d seconds ago)\n " , count_seconds (time - g_last_tip_update. load ()) );
35073508 connman->SetTryNewOutboundPeer (true );
35083509 } else if (connman->GetTryNewOutboundPeer ()) {
35093510 connman->SetTryNewOutboundPeer (false );
35103511 }
3511- m_stale_tip_check_time = time_in_seconds + STALE_CHECK_INTERVAL;
3512+ m_stale_tip_check_time = time + STALE_CHECK_INTERVAL;
35123513 }
35133514}
35143515
0 commit comments