Skip to content

Commit 55e8288

Browse files
sipadhruv
authored andcommitted
Make all Poisson delays use std::chrono types
1 parent c733ac4 commit 55e8288

File tree

6 files changed

+47
-63
lines changed

6 files changed

+47
-63
lines changed

src/net.cpp

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -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: 7 additions & 13 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). */
@@ -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
@@ -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);

src/net_processing.cpp

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -119,17 +119,21 @@ static const int MAX_UNCONNECTING_HEADERS = 10;
119119
/** Minimum blocks required to signal NODE_NETWORK_LIMITED */
120120
static const unsigned int NODE_NETWORK_LIMITED_MIN_BLOCKS = 288;
121121
/** Average delay between local address broadcasts */
122-
static constexpr std::chrono::hours AVG_LOCAL_ADDRESS_BROADCAST_INTERVAL{24};
122+
static constexpr auto AVG_LOCAL_ADDRESS_BROADCAST_INTERVAL = 24h;
123123
/** Average delay between peer address broadcasts */
124-
static constexpr std::chrono::seconds AVG_ADDRESS_BROADCAST_INTERVAL{30};
125-
/** Average delay between trickled inventory transmissions in seconds.
126-
* Blocks and peers with noban permission bypass this, outbound peers get half this delay. */
127-
static const unsigned int INVENTORY_BROADCAST_INTERVAL = 5;
124+
static constexpr auto AVG_ADDRESS_BROADCAST_INTERVAL = 30s;
125+
/** Average delay between trickled inventory transmissions for inbound peers.
126+
* Blocks and peers with noban permission bypass this. */
127+
static constexpr auto INBOUND_INVENTORY_BROADCAST_INTERVAL = 5s;
128+
/** Average delay between trickled inventory transmissions for outbound peers.
129+
* Use a smaller delay as there is less privacy concern for them.
130+
* Blocks and peers with noban permission bypass this. */
131+
static constexpr auto OUTBOUND_INVENTORY_BROADCAST_INTERVAL = 2s;
128132
/** Maximum rate of inventory items to send per second.
129133
* Limits the impact of low-fee transaction floods. */
130134
static constexpr unsigned int INVENTORY_BROADCAST_PER_SECOND = 7;
131135
/** Maximum number of inventory items to send per transmission. */
132-
static constexpr unsigned int INVENTORY_BROADCAST_MAX = INVENTORY_BROADCAST_PER_SECOND * INVENTORY_BROADCAST_INTERVAL;
136+
static constexpr unsigned int INVENTORY_BROADCAST_MAX = INVENTORY_BROADCAST_PER_SECOND * count_seconds(INBOUND_INVENTORY_BROADCAST_INTERVAL);
133137
/** The number of most recently announced transactions a peer can request. */
134138
static constexpr unsigned int INVENTORY_MAX_RECENT_RELAY = 3500;
135139
/** Verify that INVENTORY_MAX_RECENT_RELAY is enough to cache everything typically
@@ -138,9 +142,9 @@ static constexpr unsigned int INVENTORY_MAX_RECENT_RELAY = 3500;
138142
* peers, and random variations in the broadcast mechanism. */
139143
static_assert(INVENTORY_MAX_RECENT_RELAY >= INVENTORY_BROADCAST_PER_SECOND * UNCONDITIONAL_RELAY_DELAY / std::chrono::seconds{1}, "INVENTORY_RELAY_MAX too low");
140144
/** Average delay between feefilter broadcasts in seconds. */
141-
static constexpr unsigned int AVG_FEEFILTER_BROADCAST_INTERVAL = 10 * 60;
145+
static constexpr auto AVG_FEEFILTER_BROADCAST_INTERVAL = 10min;
142146
/** Maximum feefilter broadcast delay after significant change. */
143-
static constexpr unsigned int MAX_FEEFILTER_CHANGE_DELAY = 5 * 60;
147+
static constexpr auto MAX_FEEFILTER_CHANGE_DELAY = 5min;
144148
/** Maximum number of compact filters that may be requested with one getcfilters. See BIP 157. */
145149
static constexpr uint32_t MAX_GETCFILTERS_SIZE = 1000;
146150
/** Maximum number of cf hashes that may be requested with one getcfheaders. See BIP 157. */
@@ -4669,10 +4673,9 @@ bool PeerManagerImpl::SendMessages(CNode* pto)
46694673
if (pto->m_tx_relay->nNextInvSend < current_time) {
46704674
fSendTrickle = true;
46714675
if (pto->IsInboundConn()) {
4672-
pto->m_tx_relay->nNextInvSend = std::chrono::microseconds{m_connman.PoissonNextSendInbound(count_microseconds(current_time), INVENTORY_BROADCAST_INTERVAL)};
4676+
pto->m_tx_relay->nNextInvSend = m_connman.PoissonNextSendInbound(current_time, INBOUND_INVENTORY_BROADCAST_INTERVAL);
46734677
} else {
4674-
// Use half the delay for outbound peers, as there is less privacy concern for them.
4675-
pto->m_tx_relay->nNextInvSend = PoissonNextSend(current_time, std::chrono::seconds{INVENTORY_BROADCAST_INTERVAL >> 1});
4678+
pto->m_tx_relay->nNextInvSend = PoissonNextSend(current_time, OUTBOUND_INVENTORY_BROADCAST_INTERVAL);
46764679
}
46774680
}
46784681

@@ -4927,24 +4930,24 @@ bool PeerManagerImpl::SendMessages(CNode* pto)
49274930
if (pto->m_tx_relay->lastSentFeeFilter == MAX_FILTER) {
49284931
// Send the current filter if we sent MAX_FILTER previously
49294932
// and made it out of IBD.
4930-
pto->m_tx_relay->nextSendTimeFeeFilter = count_microseconds(current_time) - 1;
4933+
pto->m_tx_relay->m_next_send_feefilter = 0us;
49314934
}
49324935
}
4933-
if (count_microseconds(current_time) > pto->m_tx_relay->nextSendTimeFeeFilter) {
4936+
if (current_time > pto->m_tx_relay->m_next_send_feefilter) {
49344937
CAmount filterToSend = g_filter_rounder.round(currentFilter);
49354938
// We always have a fee filter of at least minRelayTxFee
49364939
filterToSend = std::max(filterToSend, ::minRelayTxFee.GetFeePerK());
49374940
if (filterToSend != pto->m_tx_relay->lastSentFeeFilter) {
49384941
m_connman.PushMessage(pto, msgMaker.Make(NetMsgType::FEEFILTER, filterToSend));
49394942
pto->m_tx_relay->lastSentFeeFilter = filterToSend;
49404943
}
4941-
pto->m_tx_relay->nextSendTimeFeeFilter = PoissonNextSend(count_microseconds(current_time), AVG_FEEFILTER_BROADCAST_INTERVAL);
4944+
pto->m_tx_relay->m_next_send_feefilter = PoissonNextSend(current_time, AVG_FEEFILTER_BROADCAST_INTERVAL);
49424945
}
49434946
// If the fee filter has changed substantially and it's still more than MAX_FEEFILTER_CHANGE_DELAY
49444947
// until scheduled broadcast, then move the broadcast to within MAX_FEEFILTER_CHANGE_DELAY.
4945-
else if (count_microseconds(current_time) + MAX_FEEFILTER_CHANGE_DELAY * 1000000 < pto->m_tx_relay->nextSendTimeFeeFilter &&
4948+
else if (current_time + MAX_FEEFILTER_CHANGE_DELAY < pto->m_tx_relay->m_next_send_feefilter &&
49464949
(currentFilter < 3 * pto->m_tx_relay->lastSentFeeFilter / 4 || currentFilter > 4 * pto->m_tx_relay->lastSentFeeFilter / 3)) {
4947-
pto->m_tx_relay->nextSendTimeFeeFilter = count_microseconds(current_time) + GetRandInt(MAX_FEEFILTER_CHANGE_DELAY) * 1000000;
4950+
pto->m_tx_relay->m_next_send_feefilter = current_time + GetRandomDuration<std::chrono::microseconds>(MAX_FEEFILTER_CHANGE_DELAY);
49484951
}
49494952
}
49504953
} // release cs_main

src/test/fuzz/connman.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,9 @@ FUZZ_TARGET_INIT(connman, initialize_connman)
104104
},
105105
[&] {
106106
// Limit now to int32_t to avoid signed integer overflow
107-
(void)connman.PoissonNextSendInbound(fuzzed_data_provider.ConsumeIntegral<int32_t>(), fuzzed_data_provider.ConsumeIntegral<int>());
107+
(void)connman.PoissonNextSendInbound(
108+
std::chrono::microseconds{fuzzed_data_provider.ConsumeIntegral<int32_t>()},
109+
std::chrono::seconds{fuzzed_data_provider.ConsumeIntegral<int>()});
108110
},
109111
[&] {
110112
CSerializedNetMsg serialized_net_msg;

src/test/net_tests.cpp

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -803,21 +803,6 @@ BOOST_AUTO_TEST_CASE(LocalAddress_BasicLifecycle)
803803
BOOST_CHECK_EQUAL(IsLocal(addr), false);
804804
}
805805

806-
BOOST_AUTO_TEST_CASE(PoissonNextSend)
807-
{
808-
g_mock_deterministic_tests = true;
809-
810-
int64_t now = 5000;
811-
int average_interval_seconds = 600;
812-
813-
auto poisson = ::PoissonNextSend(now, average_interval_seconds);
814-
std::chrono::microseconds poisson_chrono = ::PoissonNextSend(std::chrono::microseconds{now}, std::chrono::seconds{average_interval_seconds});
815-
816-
BOOST_CHECK_EQUAL(poisson, poisson_chrono.count());
817-
818-
g_mock_deterministic_tests = false;
819-
}
820-
821806
std::vector<NodeEvictionCandidate> GetRandomNodeEvictionCandidates(const int n_candidates, FastRandomContext& random_context)
822807
{
823808
std::vector<NodeEvictionCandidate> candidates;

src/util/time.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ void UninterruptibleSleep(const std::chrono::microseconds& n);
2626
* This helper is used to convert durations before passing them over an
2727
* interface that doesn't support std::chrono (e.g. RPC, debug log, or the GUI)
2828
*/
29-
inline int64_t count_seconds(std::chrono::seconds t) { return t.count(); }
30-
inline int64_t count_milliseconds(std::chrono::milliseconds t) { return t.count(); }
31-
inline int64_t count_microseconds(std::chrono::microseconds t) { return t.count(); }
29+
constexpr int64_t count_seconds(std::chrono::seconds t) { return t.count(); }
30+
constexpr int64_t count_milliseconds(std::chrono::milliseconds t) { return t.count(); }
31+
constexpr int64_t count_microseconds(std::chrono::microseconds t) { return t.count(); }
3232

3333
using SecondsDouble = std::chrono::duration<double, std::chrono::seconds::period>;
3434

0 commit comments

Comments
 (0)