Changeset View
Changeset View
Standalone View
Standalone View
src/test/util/setup_common.cpp
Show All 32 Lines | |||||
#include <validationinterface.h> | #include <validationinterface.h> | ||||
#include <functional> | #include <functional> | ||||
#include <memory> | #include <memory> | ||||
const std::function<std::string(const char *)> G_TRANSLATION_FUN = nullptr; | const std::function<std::string(const char *)> G_TRANSLATION_FUN = nullptr; | ||||
FastRandomContext g_insecure_rand_ctx; | 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) { | std::ostream &operator<<(std::ostream &os, const uint256 &num) { | ||||
os << num.ToString(); | os << num.ToString(); | ||||
return os; | return os; | ||||
} | } | ||||
std::ostream &operator<<(std::ostream &os, const ScriptError &err) { | std::ostream &operator<<(std::ostream &os, const ScriptError &err) { | ||||
os << ScriptErrorString(err); | os << ScriptErrorString(err); | ||||
return os; | return os; | ||||
} | } | ||||
BasicTestingSetup::BasicTestingSetup(const std::string &chainName) | BasicTestingSetup::BasicTestingSetup(const std::string &chainName) | ||||
: m_path_root(fs::temp_directory_path() / "test_common_" PACKAGE_NAME / | : m_path_root{fs::temp_directory_path() / "test_common_" PACKAGE_NAME / | ||||
strprintf("%lu_%i", static_cast<unsigned long>(GetTime()), | std::to_string(g_insecure_rand_ctx_temp_path.rand32())} { | ||||
int(InsecureRandRange(1 << 30)))) { | |||||
SetMockTime(0); | SetMockTime(0); | ||||
fs::create_directories(m_path_root); | fs::create_directories(m_path_root); | ||||
gArgs.ForceSetArg("-datadir", m_path_root.string()); | gArgs.ForceSetArg("-datadir", m_path_root.string()); | ||||
ClearDatadirCache(); | ClearDatadirCache(); | ||||
SelectParams(chainName); | SelectParams(chainName); | ||||
SeedInsecureRand(); | |||||
gArgs.ForceSetArg("-printtoconsole", "0"); | gArgs.ForceSetArg("-printtoconsole", "0"); | ||||
InitLogging(); | InitLogging(); | ||||
LogInstance().StartLogging(); | LogInstance().StartLogging(); | ||||
SHA256AutoDetect(); | SHA256AutoDetect(); | ||||
ECC_Start(); | ECC_Start(); | ||||
SetupEnvironment(); | SetupEnvironment(); | ||||
SetupNetworking(); | SetupNetworking(); | ||||
InitSignatureCache(); | InitSignatureCache(); | ||||
▲ Show 20 Lines • Show All 269 Lines • Show Last 20 Lines |