File tree Expand file tree Collapse file tree 6 files changed +31
-24
lines changed Expand file tree Collapse file tree 6 files changed +31
-24
lines changed Original file line number Diff line number Diff line change @@ -51,6 +51,7 @@ if [ "${RUN_TIDY}" = "true" ]; then
51
51
" src/rpc/fees.cpp" \
52
52
" src/rpc/signmessage.cpp" \
53
53
" src/test/fuzz/txorphan.cpp" \
54
+ " src/threadinterrupt.cpp" \
54
55
" src/util/bip32.cpp" \
55
56
" src/util/bytevectorhash.cpp" \
56
57
" src/util/error.cpp" \
Original file line number Diff line number Diff line change @@ -85,8 +85,8 @@ static constexpr int DNSSEEDS_DELAY_PEER_THRESHOLD = 1000; // "many" vs "few" pe
85
85
/* * The default timeframe for -maxuploadtarget. 1 day. */
86
86
static constexpr std::chrono::seconds MAX_UPLOAD_TIMEFRAME{60 * 60 * 24 };
87
87
88
- // We add a random period time (0 to 1 seconds) to feeler connections to prevent synchronization.
89
- # define FEELER_SLEEP_WINDOW 1
88
+ // A random time period (0 to 1 seconds) is added to feeler connections to prevent synchronization.
89
+ static constexpr auto FEELER_SLEEP_WINDOW{1s};
90
90
91
91
/* * Used to pass flags to the Bind() function */
92
92
enum BindFlags {
@@ -1568,6 +1568,7 @@ int CConnman::GetExtraBlockRelayCount() const
1568
1568
void CConnman::ThreadOpenConnections (const std::vector<std::string> connect)
1569
1569
{
1570
1570
SetSyscallSandboxPolicy (SyscallSandboxPolicy::NET_OPEN_CONNECTION);
1571
+ FastRandomContext rng;
1571
1572
// Connect to specific addresses
1572
1573
if (!connect.empty ())
1573
1574
{
@@ -1821,12 +1822,11 @@ void CConnman::ThreadOpenConnections(const std::vector<std::string> connect)
1821
1822
}
1822
1823
1823
1824
if (addrConnect.IsValid ()) {
1824
-
1825
1825
if (fFeeler ) {
1826
1826
// Add small amount of random noise before connection to avoid synchronization.
1827
- int randsleep = GetRand<int >(FEELER_SLEEP_WINDOW * 1000 );
1828
- if (!interruptNet.sleep_for (std::chrono::milliseconds (randsleep)))
1827
+ if (!interruptNet.sleep_for (rng.rand_uniform_duration <CThreadInterrupt::Clock>(FEELER_SLEEP_WINDOW))) {
1829
1828
return ;
1829
+ }
1830
1830
LogPrint (BCLog::NET, " Making feeler connection to %s\n " , addrConnect.ToString ());
1831
1831
}
1832
1832
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 ;
Original file line number Diff line number Diff line change @@ -28,18 +28,8 @@ void CThreadInterrupt::operator()()
28
28
cond.notify_all ();
29
29
}
30
30
31
- bool CThreadInterrupt::sleep_for (std::chrono::milliseconds rel_time)
31
+ bool CThreadInterrupt::sleep_for (Clock::duration rel_time)
32
32
{
33
33
WAIT_LOCK (mut, lock);
34
34
return !cond.wait_for (lock, rel_time, [this ]() { return flag.load (std::memory_order_acquire); });
35
35
}
36
-
37
- bool CThreadInterrupt::sleep_for (std::chrono::seconds rel_time)
38
- {
39
- return sleep_for (std::chrono::duration_cast<std::chrono::milliseconds>(rel_time));
40
- }
41
-
42
- bool CThreadInterrupt::sleep_for (std::chrono::minutes rel_time)
43
- {
44
- return sleep_for (std::chrono::duration_cast<std::chrono::milliseconds>(rel_time));
45
- }
Original file line number Diff line number Diff line change 19
19
class CThreadInterrupt
20
20
{
21
21
public:
22
+ using Clock = std::chrono::steady_clock;
22
23
CThreadInterrupt ();
23
24
explicit operator bool () const ;
24
25
void operator ()() EXCLUSIVE_LOCKS_REQUIRED(!mut);
25
26
void reset ();
26
- bool sleep_for (std::chrono::milliseconds rel_time) EXCLUSIVE_LOCKS_REQUIRED(!mut);
27
- bool sleep_for (std::chrono::seconds rel_time) EXCLUSIVE_LOCKS_REQUIRED(!mut);
28
- bool sleep_for (std::chrono::minutes rel_time) EXCLUSIVE_LOCKS_REQUIRED(!mut);
27
+ bool sleep_for (Clock::duration rel_time) EXCLUSIVE_LOCKS_REQUIRED(!mut);
29
28
30
29
private:
31
30
std::condition_variable cond;
You can’t perform that action at this time.
0 commit comments