Changeset View
Changeset View
Standalone View
Standalone View
src/test/setup_common.cpp
// Copyright (c) 2011-2019 The Bitcoin Core developers | // Copyright (c) 2011-2019 The Bitcoin Core developers | ||||
// Distributed under the MIT software license, see the accompanying | // Distributed under the MIT software license, see the accompanying | ||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
#include <test/setup_common.h> | #include <test/setup_common.h> | ||||
#include <banman.h> | #include <banman.h> | ||||
#include <chain.h> | #include <chain.h> | ||||
#include <chainparams.h> | #include <chainparams.h> | ||||
#include <config.h> | #include <config.h> | ||||
#include <consensus/consensus.h> | #include <consensus/consensus.h> | ||||
#include <consensus/validation.h> | #include <consensus/validation.h> | ||||
#include <crypto/sha256.h> | #include <crypto/sha256.h> | ||||
#include <fs.h> | #include <fs.h> | ||||
#include <init.h> | |||||
#include <key.h> | #include <key.h> | ||||
#include <logging.h> | #include <logging.h> | ||||
#include <miner.h> | #include <miner.h> | ||||
#include <net_processing.h> | #include <net_processing.h> | ||||
#include <noui.h> | #include <noui.h> | ||||
#include <pow.h> | #include <pow.h> | ||||
#include <pubkey.h> | #include <pubkey.h> | ||||
#include <random.h> | #include <random.h> | ||||
Show All 23 Lines | 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()), | strprintf("%lu_%i", static_cast<unsigned long>(GetTime()), | ||||
int(InsecureRandRange(1 << 30)))) { | int(InsecureRandRange(1 << 30)))) { | ||||
fs::create_directories(m_path_root); | |||||
gArgs.ForceSetArg("-datadir", m_path_root.string()); | |||||
ClearDatadirCache(); | |||||
SelectParams(chainName); | |||||
gArgs.ForceSetArg("-printtoconsole", "0"); | |||||
InitLogging(); | |||||
LogInstance().StartLogging(); | |||||
SHA256AutoDetect(); | SHA256AutoDetect(); | ||||
ECC_Start(); | ECC_Start(); | ||||
SetupEnvironment(); | SetupEnvironment(); | ||||
SetupNetworking(); | SetupNetworking(); | ||||
InitSignatureCache(); | InitSignatureCache(); | ||||
InitScriptExecutionCache(); | InitScriptExecutionCache(); | ||||
fCheckBlockIndex = true; | fCheckBlockIndex = true; | ||||
SelectParams(chainName); | |||||
static bool noui_connected = false; | static bool noui_connected = false; | ||||
if (!noui_connected) { | if (!noui_connected) { | ||||
noui_connect(); | noui_connect(); | ||||
noui_connected = true; | noui_connected = true; | ||||
} | } | ||||
} | } | ||||
BasicTestingSetup::~BasicTestingSetup() { | BasicTestingSetup::~BasicTestingSetup() { | ||||
LogInstance().DisconnectTestLogger(); | |||||
fs::remove_all(m_path_root); | fs::remove_all(m_path_root); | ||||
ECC_Stop(); | ECC_Stop(); | ||||
} | } | ||||
fs::path BasicTestingSetup::SetDataDir(const std::string &name) { | |||||
fs::path ret = m_path_root / name; | |||||
fs::create_directories(ret); | |||||
gArgs.ForceSetArg("-datadir", ret.string()); | |||||
return ret; | |||||
} | |||||
TestingSetup::TestingSetup(const std::string &chainName) | TestingSetup::TestingSetup(const std::string &chainName) | ||||
: BasicTestingSetup(chainName) { | : BasicTestingSetup(chainName) { | ||||
SetDataDir("tempdir"); | |||||
const Config &config = GetConfig(); | const Config &config = GetConfig(); | ||||
const CChainParams &chainparams = config.GetChainParams(); | const CChainParams &chainparams = config.GetChainParams(); | ||||
// Ideally we'd move all the RPC tests to the functional testing framework | // Ideally we'd move all the RPC tests to the functional testing framework | ||||
// instead of unit tests, but for now we need these here. | // instead of unit tests, but for now we need these here. | ||||
RPCServer rpcServer; | RPCServer rpcServer; | ||||
RegisterAllRPCCommands(config, rpcServer, tableRPC); | RegisterAllRPCCommands(config, rpcServer, tableRPC); | ||||
/** | /** | ||||
* RPC does not come out of the warmup state on its own. Normally, this is | * RPC does not come out of the warmup state on its own. Normally, this is | ||||
* handled in bitcoind's init path, but unit tests do not trigger this | * handled in bitcoind's init path, but unit tests do not trigger this | ||||
* codepath, so we call it explicitly as part of setup. | * codepath, so we call it explicitly as part of setup. | ||||
*/ | */ | ||||
std::string rpcWarmupStatus; | std::string rpcWarmupStatus; | ||||
if (RPCIsInWarmup(&rpcWarmupStatus)) { | if (RPCIsInWarmup(&rpcWarmupStatus)) { | ||||
SetRPCWarmupFinished(); | SetRPCWarmupFinished(); | ||||
} | } | ||||
ClearDatadirCache(); | |||||
// We have to run a scheduler thread to prevent ActivateBestChain | // We have to run a scheduler thread to prevent ActivateBestChain | ||||
// from blocking due to queue overrun. | // from blocking due to queue overrun. | ||||
threadGroup.create_thread(std::bind(&CScheduler::serviceQueue, &scheduler)); | threadGroup.create_thread(std::bind(&CScheduler::serviceQueue, &scheduler)); | ||||
GetMainSignals().RegisterBackgroundSignalScheduler(scheduler); | GetMainSignals().RegisterBackgroundSignalScheduler(scheduler); | ||||
g_mempool.setSanityCheck(1.0); | g_mempool.setSanityCheck(1.0); | ||||
pblocktree.reset(new CBlockTreeDB(1 << 20, true)); | pblocktree.reset(new CBlockTreeDB(1 << 20, true)); | ||||
pcoinsdbview.reset(new CCoinsViewDB(1 << 23, true)); | pcoinsdbview.reset(new CCoinsViewDB(1 << 23, true)); | ||||
▲ Show 20 Lines • Show All 211 Lines • Show Last 20 Lines |