Skip to content

Commit 1111e2f

Browse files
committed
Use steady clock in SeedStrengthen and FindBestImplementation
1 parent 74981aa commit 1111e2f

File tree

4 files changed

+14
-12
lines changed

4 files changed

+14
-12
lines changed

ci/test/06_script_b.sh

+2
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,14 @@ if [ "${RUN_TIDY}" = "true" ]; then
5050
" src/node/chainstate.cpp"\
5151
" src/node/chainstatemanager_args.cpp"\
5252
" src/node/mempool_args.cpp"\
53+
" src/node/minisketchwrapper.cpp"\
5354
" src/node/utxo_snapshot.cpp"\
5455
" src/node/validation_cache_args.cpp"\
5556
" src/policy/feerate.cpp"\
5657
" src/policy/packages.cpp"\
5758
" src/policy/settings.cpp"\
5859
" src/primitives/transaction.cpp"\
60+
" src/random.cpp"\
5961
" src/rpc/fees.cpp"\
6062
" src/rpc/signmessage.cpp"\
6163
" src/test/fuzz/txorphan.cpp"\

src/node/minisketchwrapper.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -23,25 +23,25 @@ static constexpr uint32_t BITS = 32;
2323

2424
uint32_t FindBestImplementation()
2525
{
26-
std::optional<std::pair<int64_t, uint32_t>> best;
26+
std::optional<std::pair<SteadyClock::duration, uint32_t>> best;
2727

2828
uint32_t max_impl = Minisketch::MaxImplementation();
2929
for (uint32_t impl = 0; impl <= max_impl; ++impl) {
30-
std::vector<int64_t> benches;
30+
std::vector<SteadyClock::duration> benches;
3131
uint64_t offset = 0;
3232
/* Run a little benchmark with capacity 32, adding 184 entries, and decoding 11 of them once. */
3333
for (int b = 0; b < 11; ++b) {
3434
if (!Minisketch::ImplementationSupported(BITS, impl)) break;
3535
Minisketch sketch(BITS, impl, 32);
36-
auto start = GetTimeMicros();
36+
auto start = SteadyClock::now();
3737
for (uint64_t e = 0; e < 100; ++e) {
3838
sketch.Add(e*1337 + b*13337 + offset);
3939
}
4040
for (uint64_t e = 0; e < 84; ++e) {
4141
sketch.Add(e*1337 + b*13337 + offset);
4242
}
4343
offset += (*sketch.Decode(32))[0];
44-
auto stop = GetTimeMicros();
44+
auto stop = SteadyClock::now();
4545
benches.push_back(stop - start);
4646
}
4747
/* Remember which implementation has the best median benchmark time. */

src/random.cpp

+7-7
Original file line numberDiff line numberDiff line change
@@ -221,14 +221,14 @@ static void SeedHardwareSlow(CSHA512& hasher) noexcept {
221221
}
222222

223223
/** Use repeated SHA512 to strengthen the randomness in seed32, and feed into hasher. */
224-
static void Strengthen(const unsigned char (&seed)[32], int microseconds, CSHA512& hasher) noexcept
224+
static void Strengthen(const unsigned char (&seed)[32], SteadyClock::duration dur, CSHA512& hasher) noexcept
225225
{
226226
CSHA512 inner_hasher;
227227
inner_hasher.Write(seed, sizeof(seed));
228228

229229
// Hash loop
230230
unsigned char buffer[64];
231-
int64_t stop = GetTimeMicros() + microseconds;
231+
const auto stop{SteadyClock::now() + dur};
232232
do {
233233
for (int i = 0; i < 1000; ++i) {
234234
inner_hasher.Finalize(buffer);
@@ -238,7 +238,7 @@ static void Strengthen(const unsigned char (&seed)[32], int microseconds, CSHA51
238238
// Benchmark operation and feed it into outer hasher.
239239
int64_t perf = GetPerformanceCounter();
240240
hasher.Write((const unsigned char*)&perf, sizeof(perf));
241-
} while (GetTimeMicros() < stop);
241+
} while (SteadyClock::now() < stop);
242242

243243
// Produce output from inner state and feed it to outer hasher.
244244
inner_hasher.Finalize(buffer);
@@ -492,13 +492,13 @@ static void SeedSlow(CSHA512& hasher, RNGState& rng) noexcept
492492
}
493493

494494
/** Extract entropy from rng, strengthen it, and feed it into hasher. */
495-
static void SeedStrengthen(CSHA512& hasher, RNGState& rng, int microseconds) noexcept
495+
static void SeedStrengthen(CSHA512& hasher, RNGState& rng, SteadyClock::duration dur) noexcept
496496
{
497497
// Generate 32 bytes of entropy from the RNG, and a copy of the entropy already in hasher.
498498
unsigned char strengthen_seed[32];
499499
rng.MixExtract(strengthen_seed, sizeof(strengthen_seed), CSHA512(hasher), false);
500500
// Strengthen the seed, and feed it into hasher.
501-
Strengthen(strengthen_seed, microseconds, hasher);
501+
Strengthen(strengthen_seed, dur, hasher);
502502
}
503503

504504
static void SeedPeriodic(CSHA512& hasher, RNGState& rng) noexcept
@@ -518,7 +518,7 @@ static void SeedPeriodic(CSHA512& hasher, RNGState& rng) noexcept
518518
LogPrint(BCLog::RAND, "Feeding %i bytes of dynamic environment data into RNG\n", hasher.Size() - old_size);
519519

520520
// Strengthen for 10 ms
521-
SeedStrengthen(hasher, rng, 10000);
521+
SeedStrengthen(hasher, rng, 10ms);
522522
}
523523

524524
static void SeedStartup(CSHA512& hasher, RNGState& rng) noexcept
@@ -538,7 +538,7 @@ static void SeedStartup(CSHA512& hasher, RNGState& rng) noexcept
538538
LogPrint(BCLog::RAND, "Feeding %i bytes of environment data into RNG\n", hasher.Size() - old_size);
539539

540540
// Strengthen for 100 ms
541-
SeedStrengthen(hasher, rng, 100000);
541+
SeedStrengthen(hasher, rng, 100ms);
542542
}
543543

544544
enum class RNGLevel {

src/util/time.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
#include <compat/compat.h>
1010

11-
#include <chrono>
11+
#include <chrono> // IWYU pragma: export
1212
#include <cstdint>
1313
#include <string>
1414

0 commit comments

Comments
 (0)