aboutsummaryrefslogtreecommitdiff
path: root/src/random.h
diff options
context:
space:
mode:
authorJohn Newbery <john@johnnewbery.com>2021-12-14 10:15:10 +0000
committerMartin Zumsande <mzumsande@gmail.com>2022-01-13 15:54:59 +0100
commit9e64d69bf74c8a381fb59841519cc3736bce14d4 (patch)
tree9b2bffa0f8dfd562a90234023783c5d14c1c65e4 /src/random.h
parent801aaac2b39564aa14009785146ba26d2506fb53 (diff)
downloadbitcoin-9e64d69bf74c8a381fb59841519cc3736bce14d4.tar.xz
[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.
Diffstat (limited to 'src/random.h')
-rw-r--r--src/random.h14
1 files changed, 13 insertions, 1 deletions
diff --git a/src/random.h b/src/random.h
index 0c6dc24983..be73e44a87 100644
--- a/src/random.h
+++ b/src/random.h
@@ -10,7 +10,7 @@
#include <crypto/common.h>
#include <uint256.h>
-#include <chrono> // For std::chrono::microseconds
+#include <chrono>
#include <cstdint>
#include <limits>
@@ -82,6 +82,18 @@ D GetRandomDuration(typename std::common_type<D>::type max) noexcept
};
constexpr auto GetRandMicros = GetRandomDuration<std::chrono::microseconds>;
constexpr auto GetRandMillis = GetRandomDuration<std::chrono::milliseconds>;
+
+/**
+ * Return a timestamp in the future sampled from an exponential distribution
+ * (https://en.wikipedia.org/wiki/Exponential_distribution). This distribution
+ * is memoryless and should be used for repeated network events (e.g. sending a
+ * certain type of message) to minimize leaking information to observers.
+ *
+ * The probability of an event occuring before time x is 1 - e^-(x/a) where a
+ * is the average interval between events.
+ * */
+std::chrono::microseconds PoissonNextSend(std::chrono::microseconds now, std::chrono::seconds average_interval);
+
int GetRandInt(int nMax) noexcept;
uint256 GetRandHash() noexcept;