diff options
author | jonatack <jon@atack.com> | 2023-01-22 09:57:19 -0800 |
---|---|---|
committer | Jon Atack <jon@atack.com> | 2023-06-14 08:28:33 -0600 |
commit | 1b246fdd145a95f5da479159f5e8eaf5a76bdc3a (patch) | |
tree | a7799cb90d8d0c5ffa96040ed95a97e82eff399a /src/test/util/random.cpp | |
parent | 681ecac5c2d462920cd32636eec15599a9bcf424 (diff) |
test: move remaining random test util code from setup_common to random
and drop the util/random dependency on util/setup_common.
This improves code separation and avoids creating a circular dependency if
setup_common needs to call the util/random functions.
Diffstat (limited to 'src/test/util/random.cpp')
-rw-r--r-- | src/test/util/random.cpp | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/src/test/util/random.cpp b/src/test/util/random.cpp new file mode 100644 index 0000000000..4c87ab8df8 --- /dev/null +++ b/src/test/util/random.cpp @@ -0,0 +1,33 @@ +// 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. + +#include <test/util/random.h> + +#include <logging.h> +#include <random.h> +#include <uint256.h> + +#include <cstdlib> +#include <string> + +FastRandomContext g_insecure_rand_ctx; + +/** Return the unsigned from the environment var if available, otherwise 0 */ +static uint256 GetUintFromEnv(const std::string& env_name) +{ + const char* num = std::getenv(env_name.c_str()); + if (!num) return {}; + return uint256S(num); +} + +void Seed(FastRandomContext& ctx) +{ + // Should be enough to get the seed once for the process + static uint256 seed{}; + static const std::string RANDOM_CTX_SEED{"RANDOM_CTX_SEED"}; + if (seed.IsNull()) seed = GetUintFromEnv(RANDOM_CTX_SEED); + if (seed.IsNull()) seed = GetRandHash(); + LogPrintf("%s: Setting random seed for current tests to %s=%s\n", __func__, RANDOM_CTX_SEED, seed.GetHex()); + ctx = FastRandomContext(seed); +} |