Skip to content

Commit fa74e72

Browse files
author
MacroFake
committed
refactor: Make FEELER_SLEEP_WINDOW type safe (std::chrono)
1 parent fa3b3cb commit fa74e72

File tree

3 files changed

+27
-10
lines changed

3 files changed

+27
-10
lines changed

src/net.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ static constexpr int DNSSEEDS_DELAY_PEER_THRESHOLD = 1000; // "many" vs "few" pe
8686
/** The default timeframe for -maxuploadtarget. 1 day. */
8787
static constexpr std::chrono::seconds MAX_UPLOAD_TIMEFRAME{60 * 60 * 24};
8888

89-
// We add a random period time (0 to 1 seconds) to feeler connections to prevent synchronization.
90-
#define FEELER_SLEEP_WINDOW 1
89+
// A random time period (0 to 1 seconds) is added to feeler connections to prevent synchronization.
90+
static constexpr auto FEELER_SLEEP_WINDOW{1s};
9191

9292
/** Used to pass flags to the Bind() function */
9393
enum BindFlags {
@@ -1574,6 +1574,7 @@ int CConnman::GetExtraBlockRelayCount() const
15741574
void CConnman::ThreadOpenConnections(const std::vector<std::string> connect)
15751575
{
15761576
SetSyscallSandboxPolicy(SyscallSandboxPolicy::NET_OPEN_CONNECTION);
1577+
FastRandomContext rng;
15771578
// Connect to specific addresses
15781579
if (!connect.empty())
15791580
{
@@ -1827,12 +1828,11 @@ void CConnman::ThreadOpenConnections(const std::vector<std::string> connect)
18271828
}
18281829

18291830
if (addrConnect.IsValid()) {
1830-
18311831
if (fFeeler) {
18321832
// Add small amount of random noise before connection to avoid synchronization.
1833-
int randsleep = GetRand<int>(FEELER_SLEEP_WINDOW * 1000);
1834-
if (!interruptNet.sleep_for(std::chrono::milliseconds(randsleep)))
1833+
if (!interruptNet.sleep_for(rng.rand_uniform_duration<CThreadInterrupt::Clock>(FEELER_SLEEP_WINDOW))) {
18351834
return;
1835+
}
18361836
LogPrint(BCLog::NET, "Making feeler connection to %s\n", addrConnect.ToString());
18371837
}
18381838

src/random.h

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <span.h>
1212
#include <uint256.h>
1313

14+
#include <cassert>
1415
#include <chrono>
1516
#include <cstdint>
1617
#include <limits>
@@ -236,13 +237,19 @@ class FastRandomContext
236237
template <typename Tp>
237238
Tp rand_uniform_delay(const Tp& time, typename Tp::duration range)
238239
{
239-
using Dur = typename Tp::duration;
240-
Dur dur{range.count() > 0 ? /* interval [0..range) */ Dur{randrange(range.count())} :
241-
range.count() < 0 ? /* interval (range..0] */ -Dur{randrange(-range.count())} :
242-
/* interval [0..0] */ Dur{0}};
243-
return time + dur;
240+
return time + rand_uniform_duration<Tp>(range);
244241
}
245242

243+
/** Generate a uniform random duration in the range from 0 (inclusive) to range (exclusive). */
244+
template <typename Chrono>
245+
typename Chrono::duration rand_uniform_duration(typename Chrono::duration range) noexcept
246+
{
247+
using Dur = typename Chrono::duration;
248+
return range.count() > 0 ? /* interval [0..range) */ Dur{randrange(range.count())} :
249+
range.count() < 0 ? /* interval (range..0] */ -Dur{randrange(-range.count())} :
250+
/* interval [0..0] */ Dur{0};
251+
};
252+
246253
// Compatibility with the C++11 UniformRandomBitGenerator concept
247254
typedef uint64_t result_type;
248255
static constexpr uint64_t min() { return 0; }

src/test/random_tests.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,16 @@ BOOST_AUTO_TEST_CASE(fastrandom_tests)
5353
BOOST_CHECK_EQUAL(ctx1.randbits(3), ctx2.randbits(3));
5454
BOOST_CHECK(ctx1.rand256() == ctx2.rand256());
5555
BOOST_CHECK(ctx1.randbytes(50) == ctx2.randbytes(50));
56+
{
57+
struct MicroClock {
58+
using duration = std::chrono::microseconds;
59+
};
60+
FastRandomContext ctx{true};
61+
// Check with clock type
62+
BOOST_CHECK_EQUAL(47222, ctx.rand_uniform_duration<MicroClock>(1s).count());
63+
// Check with time-point type
64+
BOOST_CHECK_EQUAL(2782, ctx.rand_uniform_duration<SteadySeconds>(9h).count());
65+
}
5666

5767
// Check that a nondeterministic ones are not
5868
g_mock_deterministic_tests = false;

0 commit comments

Comments
 (0)