mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-04-29 14:59:39 -04:00
test: Remove FastRandomContext global
Drop g_insecure_rand_ctx
This commit is contained in:
parent
fa0fe08eca
commit
948238a683
5 changed files with 5 additions and 40 deletions
|
@ -69,12 +69,13 @@ static void SignTransactionSchnorr(benchmark::Bench& bench) { SignTransactionSin
|
||||||
|
|
||||||
static void SignSchnorrTapTweakBenchmark(benchmark::Bench& bench, bool use_null_merkle_root)
|
static void SignSchnorrTapTweakBenchmark(benchmark::Bench& bench, bool use_null_merkle_root)
|
||||||
{
|
{
|
||||||
|
FastRandomContext rng;
|
||||||
ECC_Context ecc_context{};
|
ECC_Context ecc_context{};
|
||||||
|
|
||||||
auto key = GenerateRandomKey();
|
auto key = GenerateRandomKey();
|
||||||
auto msg = InsecureRand256();
|
auto msg = rng.rand256();
|
||||||
auto merkle_root = use_null_merkle_root ? uint256() : InsecureRand256();
|
auto merkle_root = use_null_merkle_root ? uint256() : rng.rand256();
|
||||||
auto aux = InsecureRand256();
|
auto aux = rng.rand256();
|
||||||
std::vector<unsigned char> sig(64);
|
std::vector<unsigned char> sig(64);
|
||||||
|
|
||||||
bench.minEpochIterations(100).run([&] {
|
bench.minEpochIterations(100).run([&] {
|
||||||
|
|
|
@ -107,7 +107,6 @@ void initialize()
|
||||||
// - GetStrongRandBytes(), which is used for the creation of private key material.
|
// - GetStrongRandBytes(), which is used for the creation of private key material.
|
||||||
// - Creating a BasicTestingSetup or derived class will switch to a random seed.
|
// - Creating a BasicTestingSetup or derived class will switch to a random seed.
|
||||||
SeedRandomStateForTest(SeedRand::ZEROS);
|
SeedRandomStateForTest(SeedRand::ZEROS);
|
||||||
g_insecure_rand_ctx.Reseed(GetRandHash());
|
|
||||||
|
|
||||||
// Terminate immediately if a fuzzing harness ever tries to create a socket.
|
// Terminate immediately if a fuzzing harness ever tries to create a socket.
|
||||||
// Individual tests can override this by pointing CreateSock to a mocked alternative.
|
// Individual tests can override this by pointing CreateSock to a mocked alternative.
|
||||||
|
|
|
@ -11,8 +11,6 @@
|
||||||
#include <cstdlib>
|
#include <cstdlib>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
FastRandomContext g_insecure_rand_ctx;
|
|
||||||
|
|
||||||
extern void MakeRandDeterministicDANGEROUS(const uint256& seed) noexcept;
|
extern void MakeRandDeterministicDANGEROUS(const uint256& seed) noexcept;
|
||||||
|
|
||||||
void SeedRandomStateForTest(SeedRand seedtype)
|
void SeedRandomStateForTest(SeedRand seedtype)
|
||||||
|
|
|
@ -11,14 +11,6 @@
|
||||||
|
|
||||||
#include <cstdint>
|
#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 {
|
enum class SeedRand {
|
||||||
ZEROS, //!< Seed with a compile time constant of zeros
|
ZEROS, //!< Seed with a compile time constant of zeros
|
||||||
SEED, //!< Use (and report) random seed from environment, or a (truly) random one.
|
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(). */
|
/** Seed the global RNG state for testing and log the seed value. This affects all randomness, except GetStrongRandBytes(). */
|
||||||
void SeedRandomStateForTest(SeedRand seed);
|
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>
|
template <RandomNumberGenerator Rng>
|
||||||
inline CAmount RandMoney(Rng&& rng)
|
inline CAmount RandMoney(Rng&& rng)
|
||||||
{
|
{
|
||||||
|
|
|
@ -66,7 +66,7 @@ struct BasicTestingSetup {
|
||||||
util::SignalInterrupt m_interrupt;
|
util::SignalInterrupt m_interrupt;
|
||||||
node::NodeContext m_node; // keep as first member to be destructed last
|
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(). */
|
/** 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)
|
void SeedRandomForTest(SeedRand seed = SeedRand::SEED)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Add table
Reference in a new issue