Skip to content

Commit 9e64d69

Browse files
jnewberymzumsande
authored andcommitted
[move] Move PoissonNextSend to src/random and update comment
PoissonNextSend is used by net and net_processing and is stateless, so place it in the utility random.cpp translation unit.
1 parent 801aaac commit 9e64d69

File tree

4 files changed

+20
-10
lines changed

4 files changed

+20
-10
lines changed

src/net.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3069,12 +3069,6 @@ std::chrono::microseconds CConnman::PoissonNextSendInbound(std::chrono::microsec
30693069
return m_next_send_inv_to_incoming;
30703070
}
30713071

3072-
std::chrono::microseconds PoissonNextSend(std::chrono::microseconds now, std::chrono::seconds average_interval)
3073-
{
3074-
double unscaled = -log1p(GetRand(1ULL << 48) * -0.0000000000000035527136788 /* -1/2^48 */);
3075-
return now + std::chrono::duration_cast<std::chrono::microseconds>(unscaled * average_interval + 0.5us);
3076-
}
3077-
30783072
CSipHasher CConnman::GetDeterministicRandomizer(uint64_t id) const
30793073
{
30803074
return CSipHasher(nSeed0, nSeed1).Write(id);

src/net.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1270,9 +1270,6 @@ class CConnman
12701270
friend struct ConnmanTestMsg;
12711271
};
12721272

1273-
/** Return a timestamp in the future (in microseconds) for exponentially distributed events. */
1274-
std::chrono::microseconds PoissonNextSend(std::chrono::microseconds now, std::chrono::seconds average_interval);
1275-
12761273
/** Dump binary message to file, with timestamp */
12771274
void CaptureMessage(const CAddress& addr, const std::string& msg_type, const Span<const unsigned char>& data, bool is_incoming);
12781275

src/random.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <sync.h> // for Mutex
2020
#include <util/time.h> // for GetTimeMicros()
2121

22+
#include <cmath>
2223
#include <stdlib.h>
2324
#include <thread>
2425

@@ -714,3 +715,9 @@ void RandomInit()
714715

715716
ReportHardwareRand();
716717
}
718+
719+
std::chrono::microseconds PoissonNextSend(std::chrono::microseconds now, std::chrono::seconds average_interval)
720+
{
721+
double unscaled = -log1p(GetRand(1ULL << 48) * -0.0000000000000035527136788 /* -1/2^48 */);
722+
return now + std::chrono::duration_cast<std::chrono::microseconds>(unscaled * average_interval + 0.5us);
723+
}

src/random.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#include <crypto/common.h>
1111
#include <uint256.h>
1212

13-
#include <chrono> // For std::chrono::microseconds
13+
#include <chrono>
1414
#include <cstdint>
1515
#include <limits>
1616

@@ -82,6 +82,18 @@ D GetRandomDuration(typename std::common_type<D>::type max) noexcept
8282
};
8383
constexpr auto GetRandMicros = GetRandomDuration<std::chrono::microseconds>;
8484
constexpr auto GetRandMillis = GetRandomDuration<std::chrono::milliseconds>;
85+
86+
/**
87+
* Return a timestamp in the future sampled from an exponential distribution
88+
* (https://en.wikipedia.org/wiki/Exponential_distribution). This distribution
89+
* is memoryless and should be used for repeated network events (e.g. sending a
90+
* certain type of message) to minimize leaking information to observers.
91+
*
92+
* The probability of an event occuring before time x is 1 - e^-(x/a) where a
93+
* is the average interval between events.
94+
* */
95+
std::chrono::microseconds PoissonNextSend(std::chrono::microseconds now, std::chrono::seconds average_interval);
96+
8597
int GetRandInt(int nMax) noexcept;
8698
uint256 GetRandHash() noexcept;
8799

0 commit comments

Comments
 (0)