2023-01-22 09:57:19 -08:00
|
|
|
// Copyright (c) 2023 The Bitcoin Core developers
|
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
|
|
|
#ifndef BITCOIN_TEST_UTIL_RANDOM_H
|
|
|
|
#define BITCOIN_TEST_UTIL_RANDOM_H
|
|
|
|
|
2023-01-20 11:05:09 -08:00
|
|
|
#include <consensus/amount.h>
|
2023-01-22 09:57:19 -08:00
|
|
|
#include <random.h>
|
|
|
|
#include <uint256.h>
|
|
|
|
|
|
|
|
#include <cstdint>
|
|
|
|
|
2023-01-22 09:57:19 -08:00
|
|
|
/**
|
|
|
|
* This global and the helpers that use it are not thread-safe.
|
|
|
|
*
|
2024-05-21 10:29:16 +01:00
|
|
|
* If thread-safety is needed, a per-thread instance could be
|
|
|
|
* used in the multi-threaded test.
|
2023-01-22 09:57:19 -08:00
|
|
|
*/
|
|
|
|
extern FastRandomContext g_insecure_rand_ctx;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Flag to make GetRand in random.h return the same number
|
|
|
|
*/
|
|
|
|
extern bool g_mock_deterministic_tests;
|
|
|
|
|
|
|
|
enum class SeedRand {
|
|
|
|
ZEROS, //!< Seed with a compile time constant of zeros
|
|
|
|
SEED, //!< Call the Seed() helper
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Seed the given random ctx or use the seed passed in via an environment var */
|
|
|
|
void Seed(FastRandomContext& ctx);
|
|
|
|
|
|
|
|
static inline void SeedInsecureRand(SeedRand seed = SeedRand::SEED)
|
|
|
|
{
|
|
|
|
if (seed == SeedRand::ZEROS) {
|
|
|
|
g_insecure_rand_ctx = FastRandomContext(/*fDeterministic=*/true);
|
|
|
|
} else {
|
|
|
|
Seed(g_insecure_rand_ctx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-22 09:57:19 -08:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2023-01-20 11:05:09 -08:00
|
|
|
static inline CAmount InsecureRandMoneyAmount()
|
|
|
|
{
|
|
|
|
return static_cast<CAmount>(InsecureRandRange(MAX_MONEY + 1));
|
|
|
|
}
|
|
|
|
|
2023-01-22 09:57:19 -08:00
|
|
|
#endif // BITCOIN_TEST_UTIL_RANDOM_H
|