Line data Source code
1 : // Copyright (c) 2023 The Bitcoin Core developers 2 : // Distributed under the MIT software license, see the accompanying 3 : // file COPYING or http://www.opensource.org/licenses/mit-license.php. 4 : 5 : #include <test/util/random.h> 6 : 7 : #include <logging.h> 8 : #include <random.h> 9 : #include <uint256.h> 10 : 11 : #include <cstdlib> 12 : #include <string> 13 : 14 2 : FastRandomContext g_insecure_rand_ctx; 15 : 16 : /** Return the unsigned from the environment var if available, otherwise 0 */ 17 1 : static uint256 GetUintFromEnv(const std::string& env_name) 18 : { 19 1 : const char* num = std::getenv(env_name.c_str()); 20 1 : if (!num) return {}; 21 0 : return uint256S(num); 22 1 : } 23 : 24 1 : void Seed(FastRandomContext& ctx) 25 : { 26 : // Should be enough to get the seed once for the process 27 : static uint256 seed{}; 28 1 : static const std::string RANDOM_CTX_SEED{"RANDOM_CTX_SEED"}; 29 1 : if (seed.IsNull()) seed = GetUintFromEnv(RANDOM_CTX_SEED); 30 1 : if (seed.IsNull()) seed = GetRandHash(); 31 1 : LogPrintf("%s: Setting random seed for current tests to %s=%s\n", __func__, RANDOM_CTX_SEED, seed.GetHex()); 32 1 : ctx = FastRandomContext(seed); 33 1 : }