File tree Expand file tree Collapse file tree 3 files changed +27
-10
lines changed Expand file tree Collapse file tree 3 files changed +27
-10
lines changed Original file line number Diff line number Diff line change @@ -86,8 +86,8 @@ static constexpr int DNSSEEDS_DELAY_PEER_THRESHOLD = 1000; // "many" vs "few" pe
86
86
/* * The default timeframe for -maxuploadtarget. 1 day. */
87
87
static constexpr std::chrono::seconds MAX_UPLOAD_TIMEFRAME{60 * 60 * 24 };
88
88
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};
91
91
92
92
/* * Used to pass flags to the Bind() function */
93
93
enum BindFlags {
@@ -1574,6 +1574,7 @@ int CConnman::GetExtraBlockRelayCount() const
1574
1574
void CConnman::ThreadOpenConnections (const std::vector<std::string> connect)
1575
1575
{
1576
1576
SetSyscallSandboxPolicy (SyscallSandboxPolicy::NET_OPEN_CONNECTION);
1577
+ FastRandomContext rng;
1577
1578
// Connect to specific addresses
1578
1579
if (!connect.empty ())
1579
1580
{
@@ -1827,12 +1828,11 @@ void CConnman::ThreadOpenConnections(const std::vector<std::string> connect)
1827
1828
}
1828
1829
1829
1830
if (addrConnect.IsValid ()) {
1830
-
1831
1831
if (fFeeler ) {
1832
1832
// 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))) {
1835
1834
return ;
1835
+ }
1836
1836
LogPrint (BCLog::NET, " Making feeler connection to %s\n " , addrConnect.ToString ());
1837
1837
}
1838
1838
Original file line number Diff line number Diff line change 11
11
#include < span.h>
12
12
#include < uint256.h>
13
13
14
+ #include < cassert>
14
15
#include < chrono>
15
16
#include < cstdint>
16
17
#include < limits>
@@ -236,13 +237,19 @@ class FastRandomContext
236
237
template <typename Tp>
237
238
Tp rand_uniform_delay (const Tp& time, typename Tp::duration range)
238
239
{
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);
244
241
}
245
242
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
+
246
253
// Compatibility with the C++11 UniformRandomBitGenerator concept
247
254
typedef uint64_t result_type;
248
255
static constexpr uint64_t min () { return 0 ; }
Original file line number Diff line number Diff line change @@ -53,6 +53,16 @@ BOOST_AUTO_TEST_CASE(fastrandom_tests)
53
53
BOOST_CHECK_EQUAL (ctx1.randbits (3 ), ctx2.randbits (3 ));
54
54
BOOST_CHECK (ctx1.rand256 () == ctx2.rand256 ());
55
55
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
+ }
56
66
57
67
// Check that a nondeterministic ones are not
58
68
g_mock_deterministic_tests = false ;
You can’t perform that action at this time.
0 commit comments