Skip to content

Commit 6fff333

Browse files
committed
Merge bitcoin#17507: random: mark RandAddPeriodic and SeedPeriodic as noexcept
55b2cb1 random: mark RandAddPeriodic and SeedPeriodic as noexcept (fanquake) 461e547 doc: correct random.h docs after bitcoin#17270 (fanquake) Pull request description: The usage of `MilliSleep()` in SeedPeriodic (previously SeedSleep) was [removed](bitcoin@d61f2bb) in bitcoin#17270, meaning it, and its users can now be marked `noexcept`. This also corrects the docs in random.h for some of the changes in bitcoin#17270. ACKs for top commit: practicalswift: ACK 55b2cb1 laanwj: ACK 55b2cb1 sipa: ACK 55b2cb1 Tree-SHA512: 672d369796e7c4f9b4d98dc545e5454999fa1bef373871994a26041d6163c58909e2255e4f820d3ef011679aa3392754eb57477306a89f5fd3d57e2bd7f0811a
2 parents 5aee0e2 + 55b2cb1 commit 6fff333

File tree

2 files changed

+12
-22
lines changed

2 files changed

+12
-22
lines changed

Diff for: src/random.cpp

+4-14
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ static uint64_t GetRdSeed() noexcept
179179
/* Access to other hardware random number generators could be added here later,
180180
* assuming it is sufficiently fast (in the order of a few hundred CPU cycles).
181181
* Slower sources should probably be invoked separately, and/or only from
182-
* RandAddSeedSleep (which is called during idle background operation).
182+
* RandAddPeriodic (which is called once a minute).
183183
*/
184184
static void InitHardwareRand() {}
185185
static void ReportHardwareRand() {}
@@ -416,17 +416,7 @@ RNGState& GetRNGState() noexcept
416416

417417
/* A note on the use of noexcept in the seeding functions below:
418418
*
419-
* None of the RNG code should ever throw any exception, with the sole exception
420-
* of MilliSleep in SeedSleep, which can (and does) support interruptions which
421-
* cause a boost::thread_interrupted to be thrown.
422-
*
423-
* This means that SeedSleep, and all functions that invoke it are throwing.
424-
* However, we know that GetRandBytes() and GetStrongRandBytes() never trigger
425-
* this sleeping logic, so they are noexcept. The same is true for all the
426-
* GetRand*() functions that use GetRandBytes() indirectly.
427-
*
428-
* TODO: After moving away from interruptible boost-based thread management,
429-
* everything can become noexcept here.
419+
* None of the RNG code should ever throw any exception.
430420
*/
431421

432422
static void SeedTimestamp(CSHA512& hasher) noexcept
@@ -498,7 +488,7 @@ static void SeedStrengthen(CSHA512& hasher, RNGState& rng, int microseconds) noe
498488
Strengthen(strengthen_seed, microseconds, hasher);
499489
}
500490

501-
static void SeedPeriodic(CSHA512& hasher, RNGState& rng)
491+
static void SeedPeriodic(CSHA512& hasher, RNGState& rng) noexcept
502492
{
503493
// Everything that the 'fast' seeder includes
504494
SeedFast(hasher);
@@ -575,7 +565,7 @@ static void ProcRand(unsigned char* out, int num, RNGLevel level)
575565

576566
void GetRandBytes(unsigned char* buf, int num) noexcept { ProcRand(buf, num, RNGLevel::FAST); }
577567
void GetStrongRandBytes(unsigned char* buf, int num) noexcept { ProcRand(buf, num, RNGLevel::SLOW); }
578-
void RandAddPeriodic() { ProcRand(nullptr, 0, RNGLevel::PERIODIC); }
568+
void RandAddPeriodic() noexcept { ProcRand(nullptr, 0, RNGLevel::PERIODIC); }
579569

580570
void RandAddEvent(const uint32_t event_info) {
581571
LOCK(events_mutex);

Diff for: src/random.h

+8-8
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,17 @@
4040
* These entropy sources are slower, but designed to make sure the RNG state contains
4141
* fresh data that is unpredictable to attackers.
4242
*
43-
* - RandAddSeedSleep() seeds everything that fast seeding includes, but additionally:
44-
* - A high-precision timestamp before and after sleeping 1ms.
45-
* - (On Windows) Once every 10 minutes, performance monitoring data from the OS.
46-
- - Once every minute, strengthen the entropy for 10 ms using repeated SHA512.
47-
* These just exploit the fact the system is idle to improve the quality of the RNG
48-
* slightly.
43+
* - RandAddPeriodic() seeds everything that fast seeding includes, but additionally:
44+
* - A high-precision timestamp
45+
* - Dynamic environment data (performance monitoring, ...)
46+
* - Strengthen the entropy for 10 ms using repeated SHA512.
47+
* This is run once every minute.
4948
*
5049
* On first use of the RNG (regardless of what function is called first), all entropy
5150
* sources used in the 'slow' seeder are included, but also:
5251
* - 256 bits from the hardware RNG (rdseed or rdrand) when available.
53-
* - (On Windows) Performance monitoring data from the OS.
52+
* - Dynamic environment data (performance monitoring, ...)
53+
* - Static environment data
5454
* - Strengthen the entropy for 100 ms using repeated SHA512.
5555
*
5656
* When mixing in new entropy, H = SHA512(entropy || old_rng_state) is computed, and
@@ -87,7 +87,7 @@ void GetStrongRandBytes(unsigned char* buf, int num) noexcept;
8787
*
8888
* Thread-safe.
8989
*/
90-
void RandAddPeriodic();
90+
void RandAddPeriodic() noexcept;
9191

9292
/**
9393
* Gathers entropy from the low bits of the time at which events occur. Should

0 commit comments

Comments
 (0)