From 948238a683b6c99f4e91114aa75680c6c2d73714 Mon Sep 17 00:00:00 2001 From: Ryan Ofsky Date: Wed, 14 Aug 2024 09:39:31 -0400 Subject: [PATCH] test: Remove FastRandomContext global Drop g_insecure_rand_ctx --- src/bench/sign_transaction.cpp | 7 ++++--- src/test/fuzz/fuzz.cpp | 1 - src/test/util/random.cpp | 2 -- src/test/util/random.h | 33 --------------------------------- src/test/util/setup_common.h | 2 +- 5 files changed, 5 insertions(+), 40 deletions(-) diff --git a/src/bench/sign_transaction.cpp b/src/bench/sign_transaction.cpp index 6f28f581af..9599284f99 100644 --- a/src/bench/sign_transaction.cpp +++ b/src/bench/sign_transaction.cpp @@ -69,12 +69,13 @@ static void SignTransactionSchnorr(benchmark::Bench& bench) { SignTransactionSin static void SignSchnorrTapTweakBenchmark(benchmark::Bench& bench, bool use_null_merkle_root) { + FastRandomContext rng; ECC_Context ecc_context{}; auto key = GenerateRandomKey(); - auto msg = InsecureRand256(); - auto merkle_root = use_null_merkle_root ? uint256() : InsecureRand256(); - auto aux = InsecureRand256(); + auto msg = rng.rand256(); + auto merkle_root = use_null_merkle_root ? uint256() : rng.rand256(); + auto aux = rng.rand256(); std::vector sig(64); bench.minEpochIterations(100).run([&] { diff --git a/src/test/fuzz/fuzz.cpp b/src/test/fuzz/fuzz.cpp index 61f1d313bf..fdad0a287a 100644 --- a/src/test/fuzz/fuzz.cpp +++ b/src/test/fuzz/fuzz.cpp @@ -107,7 +107,6 @@ void initialize() // - GetStrongRandBytes(), which is used for the creation of private key material. // - Creating a BasicTestingSetup or derived class will switch to a random seed. SeedRandomStateForTest(SeedRand::ZEROS); - g_insecure_rand_ctx.Reseed(GetRandHash()); // Terminate immediately if a fuzzing harness ever tries to create a socket. // Individual tests can override this by pointing CreateSock to a mocked alternative. diff --git a/src/test/util/random.cpp b/src/test/util/random.cpp index da2f51ffbc..75b2a25857 100644 --- a/src/test/util/random.cpp +++ b/src/test/util/random.cpp @@ -11,8 +11,6 @@ #include #include -FastRandomContext g_insecure_rand_ctx; - extern void MakeRandDeterministicDANGEROUS(const uint256& seed) noexcept; void SeedRandomStateForTest(SeedRand seedtype) diff --git a/src/test/util/random.h b/src/test/util/random.h index 10215ccbe5..c458534d48 100644 --- a/src/test/util/random.h +++ b/src/test/util/random.h @@ -11,14 +11,6 @@ #include -/** - * This global and the helpers that use it are not thread-safe. - * - * If thread-safety is needed, a per-thread instance could be - * used in the multi-threaded test. - */ -extern FastRandomContext g_insecure_rand_ctx; - enum class SeedRand { ZEROS, //!< Seed with a compile time constant of zeros SEED, //!< Use (and report) random seed from environment, or a (truly) random one. @@ -27,31 +19,6 @@ enum class SeedRand { /** Seed the global RNG state for testing and log the seed value. This affects all randomness, except GetStrongRandBytes(). */ void SeedRandomStateForTest(SeedRand seed); -static inline uint32_t InsecureRand32() -{ - return g_insecure_rand_ctx.rand32(); -} - -static inline uint256 InsecureRand256() -{ - return g_insecure_rand_ctx.rand256(); -} - -static inline uint64_t InsecureRandBits(int bits) -{ - return g_insecure_rand_ctx.randbits(bits); -} - -static inline uint64_t InsecureRandRange(uint64_t range) -{ - return g_insecure_rand_ctx.randrange(range); -} - -static inline bool InsecureRandBool() -{ - return g_insecure_rand_ctx.randbool(); -} - template inline CAmount RandMoney(Rng&& rng) { diff --git a/src/test/util/setup_common.h b/src/test/util/setup_common.h index 677355c9f5..d995549ca6 100644 --- a/src/test/util/setup_common.h +++ b/src/test/util/setup_common.h @@ -66,7 +66,7 @@ struct BasicTestingSetup { util::SignalInterrupt m_interrupt; node::NodeContext m_node; // keep as first member to be destructed last - FastRandomContext& m_rng{g_insecure_rand_ctx}; // Alias (reference) for the global, to allow easy removal of the global in the future. + FastRandomContext m_rng; /** Seed the global RNG state and m_rng for testing and log the seed value. This affects all randomness, except GetStrongRandBytes(). */ void SeedRandomForTest(SeedRand seed = SeedRand::SEED) {