Skip to content

Commit 3ec8f9f

Browse files
author
MarcoFalke
committed
Merge bitcoin/bitcoin#23801: Refactor: Change time variable type from int64_t to std::chrono::seconds in net_processing.cpp
92082ea Change time variable type to std::chrono::seconds in src/net_processing.cpp (Shashwat) Pull request description: - This is a follow-up to PR #23758 - This changes the remaining time variable in `net_processing.cpp` from **int64_t** to **std::chrono::seconds** ACKs for top commit: naumenkogs: ACK 92082ea hebasto: re-ACK 92082ea Tree-SHA512: 559e351d9046d4ba2b842ae38da13b4befc7feee71f0762f97907812471e2840b0d43c90c92222d15619fe40cc21f11d40900500ca07b470f7ac8b0046cc1d68
2 parents 23afc5f + 92082ea commit 3ec8f9f

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()
@@ -1506,7 +1508,7 @@ void PeerManagerImpl::StartScheduledTasks(CScheduler& scheduler)
15061508
void PeerManagerImpl::BlockConnected(const std::shared_ptr<const CBlock>& pblock, const CBlockIndex* pindex)
15071509
{
15081510
m_orphanage.EraseForBlock(*pblock);
1509-
m_last_tip_update = GetTime();
1511+
m_last_tip_update = GetTime<std::chrono::seconds>();
15101512

15111513
{
15121514
LOCK(m_recent_confirmed_transactions_mutex);
@@ -4338,20 +4340,20 @@ void PeerManagerImpl::CheckForStaleTipAndEvictPeers()
43384340
{
43394341
LOCK(cs_main);
43404342

4341-
int64_t time_in_seconds = GetTime();
4343+
auto now{GetTime<std::chrono::seconds>()};
43424344

4343-
EvictExtraOutboundPeers(std::chrono::seconds{time_in_seconds});
4345+
EvictExtraOutboundPeers(now);
43444346

4345-
if (time_in_seconds > m_stale_tip_check_time) {
4347+
if (now > m_stale_tip_check_time) {
43464348
// Check whether our tip is stale, and if so, allow using an extra
43474349
// outbound peer
43484350
if (!fImporting && !fReindex && m_connman.GetNetworkActive() && m_connman.GetUseAddrmanOutgoing() && TipMayBeStale()) {
4349-
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);
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));
43504352
m_connman.SetTryNewOutboundPeer(true);
43514353
} else if (m_connman.GetTryNewOutboundPeer()) {
43524354
m_connman.SetTryNewOutboundPeer(false);
43534355
}
4354-
m_stale_tip_check_time = time_in_seconds + STALE_CHECK_INTERVAL;
4356+
m_stale_tip_check_time = now + STALE_CHECK_INTERVAL;
43554357
}
43564358

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

0 commit comments

Comments
 (0)