Changeset View
Changeset View
Standalone View
Standalone View
src/test/test_bitcoin.cpp
Show All 40 Lines | |||||
thread_local FastRandomContext g_insecure_rand_ctx; | thread_local FastRandomContext g_insecure_rand_ctx; | ||||
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; | ||||
} | } | ||||
BasicTestingSetup::BasicTestingSetup(const std::string &chainName) { | BasicTestingSetup::BasicTestingSetup(const std::string &chainName) | ||||
: m_path_root(fs::temp_directory_path() / "test_bitcoin" / | |||||
strprintf("%lu_%i", static_cast<unsigned long>(GetTime()), | |||||
int(InsecureRandRange(1 << 30)))) { | |||||
SHA256AutoDetect(); | SHA256AutoDetect(); | ||||
RandomInit(); | RandomInit(); | ||||
ECC_Start(); | ECC_Start(); | ||||
SetupEnvironment(); | SetupEnvironment(); | ||||
SetupNetworking(); | SetupNetworking(); | ||||
InitSignatureCache(); | InitSignatureCache(); | ||||
InitScriptExecutionCache(); | InitScriptExecutionCache(); | ||||
// Don't want to write to debug.log file. | // Don't want to write to debug.log file. | ||||
GetLogger().m_print_to_file = false; | GetLogger().m_print_to_file = false; | ||||
fCheckBlockIndex = true; | fCheckBlockIndex = true; | ||||
SelectParams(chainName); | SelectParams(chainName); | ||||
noui_connect(); | noui_connect(); | ||||
// Set config parameters to default. | // Set config parameters to default. | ||||
GlobalConfig config; | GlobalConfig config; | ||||
config.SetMaxBlockSize(DEFAULT_MAX_BLOCK_SIZE); | config.SetMaxBlockSize(DEFAULT_MAX_BLOCK_SIZE); | ||||
} | } | ||||
BasicTestingSetup::~BasicTestingSetup() { | BasicTestingSetup::~BasicTestingSetup() { | ||||
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(); | ClearDatadirCache(); | ||||
pathTemp = fs::temp_directory_path() / | |||||
strprintf("test_bitcoin_%lu_%i", (unsigned long)GetTime(), | |||||
(int)(InsecureRandRange(1 << 30))); | |||||
fs::create_directories(pathTemp); | |||||
gArgs.ForceSetArg("-datadir", pathTemp.string()); | |||||
// 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( | threadGroup.create_thread( | ||||
boost::bind(&CScheduler::serviceQueue, &scheduler)); | boost::bind(&CScheduler::serviceQueue, &scheduler)); | ||||
GetMainSignals().RegisterBackgroundSignalScheduler(scheduler); | GetMainSignals().RegisterBackgroundSignalScheduler(scheduler); | ||||
g_mempool.setSanityCheck(1.0); | g_mempool.setSanityCheck(1.0); | ||||
Show All 26 Lines | TestingSetup::~TestingSetup() { | ||||
GetMainSignals().FlushBackgroundCallbacks(); | GetMainSignals().FlushBackgroundCallbacks(); | ||||
GetMainSignals().UnregisterBackgroundSignalScheduler(); | GetMainSignals().UnregisterBackgroundSignalScheduler(); | ||||
g_connman.reset(); | g_connman.reset(); | ||||
peerLogic.reset(); | peerLogic.reset(); | ||||
UnloadBlockIndex(); | UnloadBlockIndex(); | ||||
pcoinsTip.reset(); | pcoinsTip.reset(); | ||||
pcoinsdbview.reset(); | pcoinsdbview.reset(); | ||||
pblocktree.reset(); | pblocktree.reset(); | ||||
fs::remove_all(pathTemp); | |||||
} | } | ||||
TestChain100Setup::TestChain100Setup() | TestChain100Setup::TestChain100Setup() | ||||
: TestingSetup(CBaseChainParams::REGTEST) { | : TestingSetup(CBaseChainParams::REGTEST) { | ||||
// Generate a 100-block chain: | // Generate a 100-block chain: | ||||
coinbaseKey.MakeNewKey(true); | coinbaseKey.MakeNewKey(true); | ||||
CScript scriptPubKey = CScript() << ToByteVector(coinbaseKey.GetPubKey()) | CScript scriptPubKey = CScript() << ToByteVector(coinbaseKey.GetPubKey()) | ||||
<< OP_CHECKSIG; | << OP_CHECKSIG; | ||||
▲ Show 20 Lines • Show All 67 Lines • Show Last 20 Lines |