Skip to content

Commit 92082ea

Browse files
committed
Change time variable type to std::chrono::seconds in src/net_processing.cpp
- This commit is a followup to commit: 60b579 - This changes the remaining time variable in net_processing.cpp from int64_t to std::chrono
1 parent 8c0bd87 commit 92082ea

File tree

1 file changed

+17
-15
lines changed

1 file changed

+17
-15
lines changed

src/net_processing.cpp

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
#include <validation.h>
4040

4141
#include <algorithm>
42+
#include <atomic>
43+
#include <chrono>
4244
#include <memory>
4345
#include <optional>
4446
#include <typeinfo>
@@ -57,10 +59,10 @@ static constexpr auto HEADERS_DOWNLOAD_TIMEOUT_PER_HEADER = 1ms;
5759
static constexpr int32_t MAX_OUTBOUND_PEERS_TO_PROTECT_FROM_DISCONNECT = 4;
5860
/** Timeout for (unprotected) outbound peers to sync to our chainwork, in seconds */
5961
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};
6466
/** Minimum time an outbound-peer-eviction candidate must be connected for, in order to evict */
6567
static constexpr std::chrono::seconds MINIMUM_CONNECT_TIME{30};
6668
/** SHA256("main address relay")[0:8] */
@@ -422,7 +424,7 @@ class PeerManagerImpl final : public PeerManager
422424
std::atomic<int> m_best_height{-1};
423425

424426
/** 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};
426428

427429
/** Whether this node is running in blocks only mode */
428430
const bool m_ignore_incoming_txs;
@@ -541,7 +543,7 @@ class PeerManagerImpl final : public PeerManager
541543
std::map<uint256, std::pair<NodeId, std::list<QueuedBlock>::iterator> > mapBlocksInFlight GUARDED_BY(cs_main);
542544

543545
/** 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};
545547

546548
/** Determine whether or not a peer can request a transaction, and return it (or nullptr if not found or not allowed). */
547549
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()
947949
{
948950
AssertLockHeld(cs_main);
949951
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>();
952954
}
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();
954956
}
955957

956958
bool PeerManagerImpl::CanDirectFetch()
@@ -1505,7 +1507,7 @@ void PeerManagerImpl::StartScheduledTasks(CScheduler& scheduler)
15051507
void PeerManagerImpl::BlockConnected(const std::shared_ptr<const CBlock>& pblock, const CBlockIndex* pindex)
15061508
{
15071509
m_orphanage.EraseForBlock(*pblock);
1508-
m_last_tip_update = GetTime();
1510+
m_last_tip_update = GetTime<std::chrono::seconds>();
15091511

15101512
{
15111513
LOCK(m_recent_confirmed_transactions_mutex);
@@ -4335,20 +4337,20 @@ void PeerManagerImpl::CheckForStaleTipAndEvictPeers()
43354337
{
43364338
LOCK(cs_main);
43374339

4338-
int64_t time_in_seconds = GetTime();
4340+
auto now{GetTime<std::chrono::seconds>()};
43394341

4340-
EvictExtraOutboundPeers(std::chrono::seconds{time_in_seconds});
4342+
EvictExtraOutboundPeers(now);
43414343

4342-
if (time_in_seconds > m_stale_tip_check_time) {
4344+
if (now > m_stale_tip_check_time) {
43434345
// Check whether our tip is stale, and if so, allow using an extra
43444346
// outbound peer
43454347
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));
43474349
m_connman.SetTryNewOutboundPeer(true);
43484350
} else if (m_connman.GetTryNewOutboundPeer()) {
43494351
m_connman.SetTryNewOutboundPeer(false);
43504352
}
4351-
m_stale_tip_check_time = time_in_seconds + STALE_CHECK_INTERVAL;
4353+
m_stale_tip_check_time = now + STALE_CHECK_INTERVAL;
43524354
}
43534355

43544356
if (!m_initial_sync_finished && CanDirectFetch()) {

0 commit comments

Comments
 (0)