Skip to content

Commit 33f853d

Browse files
committed
Test that GetPerformanceCounter() increments
1 parent f544094 commit 33f853d

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

src/random.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <stdlib.h>
1818
#include <limits>
1919
#include <chrono>
20+
#include <thread>
2021

2122
#ifndef WIN32
2223
#include <sys/time.h>
@@ -262,6 +263,8 @@ FastRandomContext::FastRandomContext(const uint256& seed) : requires_seed(false)
262263

263264
bool Random_SanityCheck()
264265
{
266+
uint64_t start = GetPerformanceCounter();
267+
265268
/* This does not measure the quality of randomness, but it does test that
266269
* OSRandom() overwrites all 32 bytes of the output given a maximum
267270
* number of tries.
@@ -288,7 +291,14 @@ bool Random_SanityCheck()
288291

289292
tries += 1;
290293
} while (num_overwritten < NUM_OS_RANDOM_BYTES && tries < MAX_TRIES);
291-
return (num_overwritten == NUM_OS_RANDOM_BYTES); /* If this failed, bailed out after too many tries */
294+
if (num_overwritten != NUM_OS_RANDOM_BYTES) return false; /* If this failed, bailed out after too many tries */
295+
296+
// Check that GetPerformanceCounter increases at least during a GetOSRand() call + 1ms sleep.
297+
std::this_thread::sleep_for(std::chrono::milliseconds(1));
298+
uint64_t stop = GetPerformanceCounter();
299+
if (stop == start) return false;
300+
301+
return true;
292302
}
293303

294304
FastRandomContext::FastRandomContext(bool fDeterministic) : requires_seed(!fDeterministic), bytebuf_size(0), bitbuf_size(0)

0 commit comments

Comments
 (0)