Skip to content

Commit 3392137

Browse files
committed
Merge #21015: Make all of net_processing (and some of net) use std::chrono types
0eaea66 Make tx relay data structure use std::chrono types (Pieter Wuille) 55e8288 Make all Poisson delays use std::chrono types (Pieter Wuille) c733ac4 Convert block/header sync timeouts to std::chrono types (Pieter Wuille) 4d98b40 Change all ping times to std::chrono types (Pieter Wuille) Pull request description: (Picking up #20044. Rebased against master.) This changes various uses of integers to represent timestamps and durations to `std::chrono` duration types with type-safe conversions, getting rid of various `.count()`, constructors, and conversion factors. ACKs for top commit: jnewbery: utACK 0eaea66 vasild: ACK 0eaea66 MarcoFalke: re-ACK 0eaea66, only changes: minor rename, using C++11 member initializer, using 2min chrono literal, rebase 🤚 ajtowns: utACK 0eaea66 Tree-SHA512: 2dbd8d53bf82e98f9b4611e61dc14c448e8957d1a02575b837fadfd59f80e98614d0ccf890fc351f960ade76a6fb8051b282e252e81675a8ee753dba8b1d7f57
2 parents 92b7efc + 0eaea66 commit 3392137

File tree

13 files changed

+141
-141
lines changed

13 files changed

+141
-141
lines changed

src/net.cpp

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -603,8 +603,8 @@ void CNode::copyStats(CNodeStats &stats, const std::vector<bool> &m_asmap)
603603
stats.minFeeFilter = 0;
604604
}
605605

606-
stats.m_ping_usec = m_last_ping_time;
607-
stats.m_min_ping_usec = m_min_ping_time;
606+
X(m_last_ping_time);
607+
X(m_min_ping_time);
608608

609609
// Leave string empty if addrLocal invalid (not filled in yet)
610610
CService addrLocalUnlocked = GetAddrLocal();
@@ -1761,12 +1761,11 @@ void CConnman::ThreadOpenConnections(const std::vector<std::string> connect)
17611761
}
17621762

17631763
// Initiate network connections
1764-
auto start = GetTime<std::chrono::seconds>();
1764+
auto start = GetTime<std::chrono::microseconds>();
17651765

17661766
// Minimum time before next feeler connection (in microseconds).
1767-
1768-
int64_t nNextFeeler = PoissonNextSend(count_microseconds(start), FEELER_INTERVAL);
1769-
int64_t nNextExtraBlockRelay = PoissonNextSend(count_microseconds(start), EXTRA_BLOCK_RELAY_ONLY_PEER_INTERVAL);
1767+
auto next_feeler = PoissonNextSend(start, FEELER_INTERVAL);
1768+
auto next_extra_block_relay = PoissonNextSend(start, EXTRA_BLOCK_RELAY_ONLY_PEER_INTERVAL);
17701769
const bool dnsseed = gArgs.GetBoolArg("-dnsseed", DEFAULT_DNSSEED);
17711770
bool add_fixed_seeds = gArgs.GetBoolArg("-fixedseeds", DEFAULT_FIXEDSEEDS);
17721771

@@ -1849,7 +1848,7 @@ void CConnman::ThreadOpenConnections(const std::vector<std::string> connect)
18491848
}
18501849

18511850
ConnectionType conn_type = ConnectionType::OUTBOUND_FULL_RELAY;
1852-
int64_t nTime = GetTimeMicros();
1851+
auto now = GetTime<std::chrono::microseconds>();
18531852
bool anchor = false;
18541853
bool fFeeler = false;
18551854

@@ -1861,7 +1860,7 @@ void CConnman::ThreadOpenConnections(const std::vector<std::string> connect)
18611860
// GetTryNewOutboundPeer() gets set when a stale tip is detected, so we
18621861
// try opening an additional OUTBOUND_FULL_RELAY connection. If none of
18631862
// these conditions are met, check to see if it's time to try an extra
1864-
// block-relay-only peer (to confirm our tip is current, see below) or the nNextFeeler
1863+
// block-relay-only peer (to confirm our tip is current, see below) or the next_feeler
18651864
// timer to decide if we should open a FEELER.
18661865

18671866
if (!m_anchors.empty() && (nOutboundBlockRelay < m_max_outbound_block_relay)) {
@@ -1873,7 +1872,7 @@ void CConnman::ThreadOpenConnections(const std::vector<std::string> connect)
18731872
conn_type = ConnectionType::BLOCK_RELAY;
18741873
} else if (GetTryNewOutboundPeer()) {
18751874
// OUTBOUND_FULL_RELAY
1876-
} else if (nTime > nNextExtraBlockRelay && m_start_extra_block_relay_peers) {
1875+
} else if (now > next_extra_block_relay && m_start_extra_block_relay_peers) {
18771876
// Periodically connect to a peer (using regular outbound selection
18781877
// methodology from addrman) and stay connected long enough to sync
18791878
// headers, but not much else.
@@ -1895,10 +1894,10 @@ void CConnman::ThreadOpenConnections(const std::vector<std::string> connect)
18951894
// Because we can promote these connections to block-relay-only
18961895
// connections, they do not get their own ConnectionType enum
18971896
// (similar to how we deal with extra outbound peers).
1898-
nNextExtraBlockRelay = PoissonNextSend(nTime, EXTRA_BLOCK_RELAY_ONLY_PEER_INTERVAL);
1897+
next_extra_block_relay = PoissonNextSend(now, EXTRA_BLOCK_RELAY_ONLY_PEER_INTERVAL);
18991898
conn_type = ConnectionType::BLOCK_RELAY;
1900-
} else if (nTime > nNextFeeler) {
1901-
nNextFeeler = PoissonNextSend(nTime, FEELER_INTERVAL);
1899+
} else if (now > next_feeler) {
1900+
next_feeler = PoissonNextSend(now, FEELER_INTERVAL);
19021901
conn_type = ConnectionType::FEELER;
19031902
fFeeler = true;
19041903
} else {
@@ -2983,20 +2982,21 @@ bool CConnman::ForNode(NodeId id, std::function<bool(CNode* pnode)> func)
29832982
return found != nullptr && NodeFullyConnected(found) && func(found);
29842983
}
29852984

2986-
int64_t CConnman::PoissonNextSendInbound(int64_t now, int average_interval_seconds)
2985+
std::chrono::microseconds CConnman::PoissonNextSendInbound(std::chrono::microseconds now, std::chrono::seconds average_interval)
29872986
{
2988-
if (m_next_send_inv_to_incoming < now) {
2987+
if (m_next_send_inv_to_incoming.load() < now) {
29892988
// If this function were called from multiple threads simultaneously
29902989
// it would possible that both update the next send variable, and return a different result to their caller.
29912990
// This is not possible in practice as only the net processing thread invokes this function.
2992-
m_next_send_inv_to_incoming = PoissonNextSend(now, average_interval_seconds);
2991+
m_next_send_inv_to_incoming = PoissonNextSend(now, average_interval);
29932992
}
29942993
return m_next_send_inv_to_incoming;
29952994
}
29962995

2997-
int64_t PoissonNextSend(int64_t now, int average_interval_seconds)
2996+
std::chrono::microseconds PoissonNextSend(std::chrono::microseconds now, std::chrono::seconds average_interval)
29982997
{
2999-
return now + (int64_t)(log1p(GetRand(1ULL << 48) * -0.0000000000000035527136788 /* -1/2^48 */) * average_interval_seconds * -1000000.0 + 0.5);
2998+
double unscaled = -log1p(GetRand(1ULL << 48) * -0.0000000000000035527136788 /* -1/2^48 */);
2999+
return now + std::chrono::duration_cast<std::chrono::microseconds>(unscaled * average_interval + 0.5us);
30003000
}
30013001

30023002
CSipHasher CConnman::GetDeterministicRandomizer(uint64_t id) const

src/net.h

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@ static const bool DEFAULT_WHITELISTFORCERELAY = false;
4949

5050
/** Time after which to disconnect, after waiting for a ping response (or inactivity). */
5151
static const int TIMEOUT_INTERVAL = 20 * 60;
52-
/** Run the feeler connection loop once every 2 minutes or 120 seconds. **/
53-
static const int FEELER_INTERVAL = 120;
52+
/** Run the feeler connection loop once every 2 minutes. **/
53+
static constexpr auto FEELER_INTERVAL = 2min;
5454
/** Run the extra block-relay-only connection loop once every 5 minutes. **/
55-
static const int EXTRA_BLOCK_RELAY_ONLY_PEER_INTERVAL = 300;
55+
static constexpr auto EXTRA_BLOCK_RELAY_ONLY_PEER_INTERVAL = 5min;
5656
/** The maximum number of addresses from our addrman to return in response to a getaddr message. */
5757
static constexpr size_t MAX_ADDR_TO_SEND = 1000;
5858
/** Maximum length of incoming protocol messages (no message over 4 MB is currently acceptable). */
@@ -261,8 +261,8 @@ class CNodeStats
261261
uint64_t nRecvBytes;
262262
mapMsgCmdSize mapRecvBytesPerMsgCmd;
263263
NetPermissionFlags m_permissionFlags;
264-
int64_t m_ping_usec;
265-
int64_t m_min_ping_usec;
264+
std::chrono::microseconds m_last_ping_time;
265+
std::chrono::microseconds m_min_ping_time;
266266
CAmount minFeeFilter;
267267
// Our address, as reported by the peer
268268
std::string addrLocal;
@@ -573,7 +573,7 @@ class CNode
573573
/** Minimum fee rate with which to filter inv's to this node */
574574
std::atomic<CAmount> minFeeFilter{0};
575575
CAmount lastSentFeeFilter{0};
576-
int64_t nextSendTimeFeeFilter{0};
576+
std::chrono::microseconds m_next_send_feefilter{0};
577577
};
578578

579579
// m_tx_relay == nullptr if we're not relaying transactions with this peer
@@ -593,11 +593,11 @@ class CNode
593593
std::atomic<int64_t> nLastTXTime{0};
594594

595595
/** Last measured round-trip time. Used only for RPC/GUI stats/debugging.*/
596-
std::atomic<int64_t> m_last_ping_time{0};
596+
std::atomic<std::chrono::microseconds> m_last_ping_time{0us};
597597

598598
/** Lowest measured round-trip time. Used as an inbound peer eviction
599599
* criterium in CConnman::AttemptToEvictConnection. */
600-
std::atomic<int64_t> m_min_ping_time{std::numeric_limits<int64_t>::max()};
600+
std::atomic<std::chrono::microseconds> m_min_ping_time{std::chrono::microseconds::max()};
601601

602602
CNode(NodeId id, ServiceFlags nLocalServicesIn, SOCKET hSocketIn, const CAddress& addrIn, uint64_t nKeyedNetGroupIn, uint64_t nLocalHostNonceIn, const CAddress& addrBindIn, const std::string& addrNameIn, ConnectionType conn_type_in, bool inbound_onion);
603603
~CNode();
@@ -719,8 +719,8 @@ class CNode
719719

720720
/** A ping-pong round trip has completed successfully. Update latest and minimum ping times. */
721721
void PongReceived(std::chrono::microseconds ping_time) {
722-
m_last_ping_time = count_microseconds(ping_time);
723-
m_min_ping_time = std::min(m_min_ping_time.load(), count_microseconds(ping_time));
722+
m_last_ping_time = ping_time;
723+
m_min_ping_time = std::min(m_min_ping_time.load(), ping_time);
724724
}
725725

726726
private:
@@ -1021,7 +1021,7 @@ class CConnman
10211021
Works assuming that a single interval is used.
10221022
Variable intervals will result in privacy decrease.
10231023
*/
1024-
int64_t PoissonNextSendInbound(int64_t now, int average_interval_seconds);
1024+
std::chrono::microseconds PoissonNextSendInbound(std::chrono::microseconds now, std::chrono::seconds average_interval);
10251025

10261026
void SetAsmap(std::vector<bool> asmap) { addrman.m_asmap = std::move(asmap); }
10271027

@@ -1256,7 +1256,7 @@ class CConnman
12561256
*/
12571257
std::atomic_bool m_start_extra_block_relay_peers{false};
12581258

1259-
std::atomic<int64_t> m_next_send_inv_to_incoming{0};
1259+
std::atomic<std::chrono::microseconds> m_next_send_inv_to_incoming{0us};
12601260

12611261
/**
12621262
* A vector of -bind=<address>:<port>=onion arguments each of which is
@@ -1269,13 +1269,7 @@ class CConnman
12691269
};
12701270

12711271
/** Return a timestamp in the future (in microseconds) for exponentially distributed events. */
1272-
int64_t PoissonNextSend(int64_t now, int average_interval_seconds);
1273-
1274-
/** Wrapper to return mockable type */
1275-
inline std::chrono::microseconds PoissonNextSend(std::chrono::microseconds now, std::chrono::seconds average_interval)
1276-
{
1277-
return std::chrono::microseconds{PoissonNextSend(now.count(), average_interval.count())};
1278-
}
1272+
std::chrono::microseconds PoissonNextSend(std::chrono::microseconds now, std::chrono::seconds average_interval);
12791273

12801274
/** Dump binary message to file, with timestamp */
12811275
void CaptureMessage(const CAddress& addr, const std::string& msg_type, const Span<const unsigned char>& data, bool is_incoming);
@@ -1284,7 +1278,7 @@ struct NodeEvictionCandidate
12841278
{
12851279
NodeId id;
12861280
int64_t nTimeConnected;
1287-
int64_t m_min_ping_time;
1281+
std::chrono::microseconds m_min_ping_time;
12881282
int64_t nLastBlockTime;
12891283
int64_t nLastTXTime;
12901284
bool fRelevantServices;

0 commit comments

Comments
 (0)