Skip to content

Commit 888cce5

Browse files
committed
Add perf counter data to GetStrongRandBytes state in scheduler
1 parent 399fb8f commit 888cce5

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

src/random.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,23 @@ void GetRandBytes(unsigned char* buf, int num)
203203
}
204204
}
205205

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+
206223
static std::mutex cs_rng_state;
207224
static unsigned char rng_state[32] = {0};
208225
static uint64_t rng_counter = 0;

src/random.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ uint64_t GetRand(uint64_t nMax);
2323
int GetRandInt(int nMax);
2424
uint256 GetRandHash();
2525

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+
2633
/**
2734
* Function to gather random data from multiple sources, failing whenever any
2835
* of those source fail to provide a result.

src/scheduler.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include "scheduler.h"
66

7+
#include "random.h"
78
#include "reverselock.h"
89

910
#include <assert.h>
@@ -39,6 +40,11 @@ void CScheduler::serviceQueue()
3940
// is called.
4041
while (!shouldStop()) {
4142
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+
}
4248
while (!shouldStop() && taskQueue.empty()) {
4349
// Wait until there is something to do.
4450
newTaskScheduled.wait(lock);

0 commit comments

Comments
 (0)