Skip to content

Commit

Permalink
test: Remove FastRandomContext global
Browse files Browse the repository at this point in the history
Drop g_insecure_rand_ctx
  • Loading branch information
ryanofsky authored and MarcoFalke committed Aug 26, 2024
1 parent fa0fe08 commit 948238a
Show file tree
Hide file tree
Showing 5 changed files with 5 additions and 40 deletions.
7 changes: 4 additions & 3 deletions src/bench/sign_transaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<unsigned char> sig(64);

bench.minEpochIterations(100).run([&] {
Expand Down
1 change: 0 additions & 1 deletion src/test/fuzz/fuzz.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 0 additions & 2 deletions src/test/util/random.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
#include <cstdlib>
#include <string>

FastRandomContext g_insecure_rand_ctx;

extern void MakeRandDeterministicDANGEROUS(const uint256& seed) noexcept;

void SeedRandomStateForTest(SeedRand seedtype)
Expand Down
33 changes: 0 additions & 33 deletions src/test/util/random.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,6 @@

#include <cstdint>

/**
* 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.
Expand All @@ -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 <RandomNumberGenerator Rng>
inline CAmount RandMoney(Rng&& rng)
{
Expand Down
2 changes: 1 addition & 1 deletion src/test/util/setup_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down

0 comments on commit 948238a

Please sign in to comment.