Skip to content

Commit c7e6b3b

Browse files
author
MarcoFalke
committed
Merge #17243: p2p: add PoissonNextSend method that returns mockable time
1a8f0d5 [tools] update nNextInvSend to use mockable time (Amiti Uttarwar) 4de6303 [tools] add PoissonNextSend method that returns mockable time (Amiti Uttarwar) Pull request description: Introduce a Poisson helper method that wraps the existing method to return `std::chrono::duration` type, which is mockable. Needed for bitcoin/bitcoin#16698. ACKs for top commit: ajtowns: ACK 1a8f0d5 MarcoFalke: re-ACK 1a8f0d5 naumenkogs: ACK 1a8f0d5, and let's merge it and come back to it later. Tree-SHA512: 7e2325d7c55fc0b4357cb86b83e0c218ba269f678c1786342d8bc380bfd9696373bc24ff124b9ff17a6e761c62b2b44ff5247c3911e2afdc7cc5c20417e8290b
2 parents 54a3374 + 1a8f0d5 commit c7e6b3b

File tree

3 files changed

+27
-9
lines changed

3 files changed

+27
-9
lines changed

src/net.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -808,7 +808,7 @@ class CNode
808808
bool fSendMempool GUARDED_BY(cs_tx_inventory){false};
809809
// Last time a "MEMPOOL" request was serviced.
810810
std::atomic<std::chrono::seconds> m_last_mempool_req{std::chrono::seconds{0}};
811-
int64_t nNextInvSend{0};
811+
std::chrono::microseconds nNextInvSend{0};
812812

813813
CCriticalSection cs_feeFilter;
814814
// Minimum fee rate with which to filter inv's to this node
@@ -991,11 +991,13 @@ class CNode
991991
void MaybeSetAddrName(const std::string& addrNameIn);
992992
};
993993

994-
995-
996-
997-
998994
/** Return a timestamp in the future (in microseconds) for exponentially distributed events. */
999995
int64_t PoissonNextSend(int64_t now, int average_interval_seconds);
1000996

997+
/** Wrapper to return mockable type */
998+
inline std::chrono::microseconds PoissonNextSend(std::chrono::microseconds now, std::chrono::seconds average_interval)
999+
{
1000+
return std::chrono::microseconds{PoissonNextSend(now.count(), average_interval.count())};
1001+
}
1002+
10011003
#endif // BITCOIN_NET_H

src/net_processing.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3575,6 +3575,8 @@ bool PeerLogicValidation::SendMessages(CNode* pto)
35753575

35763576
// Address refresh broadcast
35773577
int64_t nNow = GetTimeMicros();
3578+
auto current_time = GetTime<std::chrono::microseconds>();
3579+
35783580
if (pto->IsAddrRelayPeer() && !::ChainstateActive().IsInitialBlockDownload() && pto->nNextLocalAddrSend < nNow) {
35793581
AdvertiseLocal(pto);
35803582
pto->nNextLocalAddrSend = PoissonNextSend(nNow, AVG_LOCAL_ADDRESS_BROADCAST_INTERVAL);
@@ -3796,13 +3798,13 @@ bool PeerLogicValidation::SendMessages(CNode* pto)
37963798
LOCK(pto->m_tx_relay->cs_tx_inventory);
37973799
// Check whether periodic sends should happen
37983800
bool fSendTrickle = pto->HasPermission(PF_NOBAN);
3799-
if (pto->m_tx_relay->nNextInvSend < nNow) {
3801+
if (pto->m_tx_relay->nNextInvSend < current_time) {
38003802
fSendTrickle = true;
38013803
if (pto->fInbound) {
3802-
pto->m_tx_relay->nNextInvSend = connman->PoissonNextSendInbound(nNow, INVENTORY_BROADCAST_INTERVAL);
3804+
pto->m_tx_relay->nNextInvSend = std::chrono::microseconds{connman->PoissonNextSendInbound(nNow, INVENTORY_BROADCAST_INTERVAL)};
38033805
} else {
38043806
// Use half the delay for outbound peers, as there is less privacy concern for them.
3805-
pto->m_tx_relay->nNextInvSend = PoissonNextSend(nNow, INVENTORY_BROADCAST_INTERVAL >> 1);
3807+
pto->m_tx_relay->nNextInvSend = PoissonNextSend(current_time, std::chrono::seconds{INVENTORY_BROADCAST_INTERVAL >> 1});
38063808
}
38073809
}
38083810

@@ -3917,7 +3919,7 @@ bool PeerLogicValidation::SendMessages(CNode* pto)
39173919
connman->PushMessage(pto, msgMaker.Make(NetMsgType::INV, vInv));
39183920

39193921
// Detect whether we're stalling
3920-
const auto current_time = GetTime<std::chrono::microseconds>();
3922+
current_time = GetTime<std::chrono::microseconds>();
39213923
// nNow is the current system time (GetTimeMicros is not mockable) and
39223924
// should be replaced by the mockable current_time eventually
39233925
nNow = GetTimeMicros();

src/test/net_tests.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,5 +301,19 @@ BOOST_AUTO_TEST_CASE(LocalAddress_BasicLifecycle)
301301
BOOST_CHECK_EQUAL(IsLocal(addr), false);
302302
}
303303

304+
BOOST_AUTO_TEST_CASE(PoissonNextSend)
305+
{
306+
g_mock_deterministic_tests = true;
307+
308+
int64_t now = 5000;
309+
int average_interval_seconds = 600;
310+
311+
auto poisson = ::PoissonNextSend(now, average_interval_seconds);
312+
std::chrono::microseconds poisson_chrono = ::PoissonNextSend(std::chrono::microseconds{now}, std::chrono::seconds{average_interval_seconds});
313+
314+
BOOST_CHECK_EQUAL(poisson, poisson_chrono.count());
315+
316+
g_mock_deterministic_tests = false;
317+
}
304318

305319
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)