diff --git a/src/test/bloom_tests.cpp b/src/test/bloom_tests.cpp --- a/src/test/bloom_tests.cpp +++ b/src/test/bloom_tests.cpp @@ -1096,7 +1096,7 @@ } BOOST_AUTO_TEST_CASE(rolling_bloom) { - SeedInsecureRand(/* deterministic */ true); + SeedInsecureRand(SeedRand::ZEROS); g_mock_deterministic_tests = true; // last-100-entry, 1% false positive: diff --git a/src/test/coins_tests.cpp b/src/test/coins_tests.cpp --- a/src/test/coins_tests.cpp +++ b/src/test/coins_tests.cpp @@ -293,7 +293,7 @@ // duplicate coinbase tx has the expected effect (the other duplicate is // overwritten at all cache levels) BOOST_AUTO_TEST_CASE(updatecoins_simulation_test) { - SeedInsecureRand(/* deterministic */ true); + SeedInsecureRand(SeedRand::ZEROS); g_mock_deterministic_tests = true; bool spent_a_duplicate_coinbase = false; diff --git a/src/test/cuckoocache_tests.cpp b/src/test/cuckoocache_tests.cpp --- a/src/test/cuckoocache_tests.cpp +++ b/src/test/cuckoocache_tests.cpp @@ -107,7 +107,7 @@ * There are no repeats in the first 400000 insecure_GetRandHash calls */ BOOST_AUTO_TEST_CASE(test_cuckoocache_no_fakes) { - SeedInsecureRand(true); + SeedInsecureRand(SeedRand::ZEROS); CuckooCacheSet cc{}; size_t megabytes = 4; cc.setup_bytes(megabytes << 20); @@ -134,7 +134,7 @@ */ template static double test_cache(size_t megabytes, double load) { - SeedInsecureRand(true); + SeedInsecureRand(SeedRand::ZEROS); std::vector hashes; Cache set{}; size_t bytes = megabytes * (1 << 20); @@ -210,7 +210,7 @@ */ template static void test_cache_erase(size_t megabytes) { double load = 1; - SeedInsecureRand(true); + SeedInsecureRand(SeedRand::ZEROS); std::vector hashes; Cache set{}; size_t bytes = megabytes * (1 << 20); @@ -278,7 +278,7 @@ template static void test_cache_erase_parallel(size_t megabytes) { double load = 1; - SeedInsecureRand(true); + SeedInsecureRand(SeedRand::ZEROS); std::vector hashes; Cache set{}; size_t bytes = megabytes * (1 << 20); @@ -388,7 +388,7 @@ // iterations with non-deterministic values, so it isn't "overfit" to the // specific entropy in FastRandomContext(true) and implementation of the // cache. - SeedInsecureRand(true); + SeedInsecureRand(SeedRand::ZEROS); // block_activity models a chunk of network activity. n_insert elements are // added to the cache. The first and last n/4 are stored for removal later @@ -483,7 +483,7 @@ } BOOST_AUTO_TEST_CASE(cuckoocache_map) { - SeedInsecureRand(true); + SeedInsecureRand(SeedRand::ZEROS); // 4k cache. CuckooCacheMap cm{}; diff --git a/src/test/pmt_tests.cpp b/src/test/pmt_tests.cpp --- a/src/test/pmt_tests.cpp +++ b/src/test/pmt_tests.cpp @@ -29,7 +29,6 @@ BOOST_FIXTURE_TEST_SUITE(pmt_tests, BasicTestingSetup) BOOST_AUTO_TEST_CASE(pmt_test1) { - SeedInsecureRand(false); static const unsigned int nTxCounts[] = {1, 4, 7, 17, 56, 100, 127, 256, 312, 513, 1000, 4095}; diff --git a/src/test/sighash_tests.cpp b/src/test/sighash_tests.cpp --- a/src/test/sighash_tests.cpp +++ b/src/test/sighash_tests.cpp @@ -122,8 +122,6 @@ BOOST_FIXTURE_TEST_SUITE(sighash_tests, BasicTestingSetup) BOOST_AUTO_TEST_CASE(sighash_test) { - SeedInsecureRand(false); - #if defined(PRINT_SIGHASH_JSON) std::cout << "[\n"; std::cout << "\t[\"raw_transaction, script, input_index, hashType, " diff --git a/src/test/util/setup_common.h b/src/test/util/setup_common.h --- a/src/test/util/setup_common.h +++ b/src/test/util/setup_common.h @@ -51,8 +51,23 @@ */ extern bool g_mock_deterministic_tests; -static inline void SeedInsecureRand(bool deterministic = false) { - g_insecure_rand_ctx = FastRandomContext(deterministic); +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(/* deterministic */ true); + } else { + Seed(g_insecure_rand_ctx); + } } static inline uint32_t InsecureRand32() { diff --git a/src/test/util/setup_common.cpp b/src/test/util/setup_common.cpp --- a/src/test/util/setup_common.cpp +++ b/src/test/util/setup_common.cpp @@ -38,6 +38,38 @@ const std::function G_TRANSLATION_FUN = nullptr; FastRandomContext g_insecure_rand_ctx; +/** + * Random context to get unique temp data dirs. Separate from + * g_insecure_rand_ctx, which can be seeded from a const env var + */ +static FastRandomContext g_insecure_rand_ctx_temp_path; + +/** + * 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); +} std::ostream &operator<<(std::ostream &os, const uint256 &num) { os << num.ToString(); @@ -50,14 +82,14 @@ } BasicTestingSetup::BasicTestingSetup(const std::string &chainName) - : m_path_root(fs::temp_directory_path() / "test_common_" PACKAGE_NAME / - strprintf("%lu_%i", static_cast(GetTime()), - int(InsecureRandRange(1 << 30)))) { + : m_path_root{fs::temp_directory_path() / "test_common_" PACKAGE_NAME / + std::to_string(g_insecure_rand_ctx_temp_path.rand32())} { SetMockTime(0); fs::create_directories(m_path_root); gArgs.ForceSetArg("-datadir", m_path_root.string()); ClearDatadirCache(); SelectParams(chainName); + SeedInsecureRand(); gArgs.ForceSetArg("-printtoconsole", "0"); InitLogging(); LogInstance().StartLogging(); diff --git a/src/test/util_tests.cpp b/src/test/util_tests.cpp --- a/src/test/util_tests.cpp +++ b/src/test/util_tests.cpp @@ -1386,7 +1386,7 @@ } BOOST_AUTO_TEST_CASE(util_seed_insecure_rand) { - SeedInsecureRand(true); + SeedInsecureRand(SeedRand::ZEROS); for (int mod = 2; mod < 11; mod++) { int mask = 1; // Really rough binomial confidence approximation.