0
0
Fork 0
mirror of https://github.com/bitcoin/bitcoin.git synced 2025-02-02 09:46:52 -05:00

test: refactor: Move g_insecure_rand_ctx.Reseed out of the helper that calls MakeRandDeterministicDANGEROUS

The global g_insecure_rand_ctx will be removed in the future, so
removing it from this helper is useful.

Also, tying the two concepts of the global internal RNGState and the
global test-only rng context is a bit confusing, because tests can
simply use the m_rng, if it exists. Also, tests may seed more than one
random context, or none at all, or a random context of a different type.

Fix all issues by moving the Reseed call to the two places where it is
used.
This commit is contained in:
MarcoFalke 2024-08-16 11:02:54 +02:00
parent 3dc527f460
commit fa19af555d
No known key found for this signature in database
4 changed files with 12 additions and 5 deletions

View file

@ -106,7 +106,8 @@ void initialize()
// randomness during the fuzz test, except:
// - GetStrongRandBytes(), which is used for the creation of private key material.
// - Creating a BasicTestingSetup or derived class will switch to a random seed.
SeedRandomForTest(SeedRand::ZEROS);
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.

View file

@ -15,7 +15,7 @@ FastRandomContext g_insecure_rand_ctx;
extern void MakeRandDeterministicDANGEROUS(const uint256& seed) noexcept;
void SeedRandomForTest(SeedRand seedtype)
void SeedRandomStateForTest(SeedRand seedtype)
{
static const std::string RANDOM_CTX_SEED{"RANDOM_CTX_SEED"};
@ -34,5 +34,4 @@ void SeedRandomForTest(SeedRand seedtype)
const uint256& seed{seedtype == SeedRand::SEED ? ctx_seed : uint256::ZERO};
LogPrintf("%s: Setting random seed for current tests to %s=%s\n", __func__, RANDOM_CTX_SEED, seed.GetHex());
MakeRandDeterministicDANGEROUS(seed);
g_insecure_rand_ctx.Reseed(GetRandHash());
}

View file

@ -24,8 +24,8 @@ enum class SeedRand {
SEED, //!< Use (and report) random seed from environment, or a (truly) random one.
};
/** Seed the RNG for testing. This affects all randomness, except GetStrongRandBytes(). */
void SeedRandomForTest(SeedRand seed = SeedRand::SEED);
/** 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()
{

View file

@ -65,7 +65,14 @@ struct TestOpts {
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.
/** 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)
{
SeedRandomStateForTest(seed);
m_rng.Reseed(GetRandHash());
}
explicit BasicTestingSetup(const ChainType chainType = ChainType::MAIN, TestOpts = {});
~BasicTestingSetup();