File tree Expand file tree Collapse file tree 3 files changed +30
-0
lines changed Expand file tree Collapse file tree 3 files changed +30
-0
lines changed Original file line number Diff line number Diff line change @@ -203,6 +203,23 @@ void GetRandBytes(unsigned char* buf, int num)
203
203
}
204
204
}
205
205
206
+ static void AddDataToRng (void * data, size_t len);
207
+
208
+ void RandAddSeedSleep ()
209
+ {
210
+ int64_t nPerfCounter1 = GetPerformanceCounter ();
211
+ std::this_thread::sleep_for (std::chrono::milliseconds (1 ));
212
+ int64_t nPerfCounter2 = GetPerformanceCounter ();
213
+
214
+ // Combine with and update state
215
+ AddDataToRng (&nPerfCounter1, sizeof (nPerfCounter1));
216
+ AddDataToRng (&nPerfCounter2, sizeof (nPerfCounter2));
217
+
218
+ memory_cleanse (&nPerfCounter1, sizeof (nPerfCounter1));
219
+ memory_cleanse (&nPerfCounter2, sizeof (nPerfCounter2));
220
+ }
221
+
222
+
206
223
static std::mutex cs_rng_state;
207
224
static unsigned char rng_state[32 ] = {0 };
208
225
static uint64_t rng_counter = 0 ;
Original file line number Diff line number Diff line change @@ -23,6 +23,13 @@ uint64_t GetRand(uint64_t nMax);
23
23
int GetRandInt (int nMax);
24
24
uint256 GetRandHash ();
25
25
26
+ /* *
27
+ * Add a little bit of randomness to the output of GetStrongRangBytes.
28
+ * This sleeps for a millisecond, so should only be called when there is
29
+ * no other work to be done.
30
+ */
31
+ void RandAddSeedSleep ();
32
+
26
33
/* *
27
34
* Function to gather random data from multiple sources, failing whenever any
28
35
* of those source fail to provide a result.
Original file line number Diff line number Diff line change 4
4
5
5
#include " scheduler.h"
6
6
7
+ #include " random.h"
7
8
#include " reverselock.h"
8
9
9
10
#include < assert.h>
@@ -39,6 +40,11 @@ void CScheduler::serviceQueue()
39
40
// is called.
40
41
while (!shouldStop ()) {
41
42
try {
43
+ if (!shouldStop () && taskQueue.empty ()) {
44
+ reverse_lock<boost::unique_lock<boost::mutex> > rlock (lock);
45
+ // Use this chance to get a tiny bit more entropy
46
+ RandAddSeedSleep ();
47
+ }
42
48
while (!shouldStop () && taskQueue.empty ()) {
43
49
// Wait until there is something to do.
44
50
newTaskScheduled.wait (lock);
You can’t perform that action at this time.
0 commit comments