Remove redundant nullptr checks before deallocation
[bitcoinplatinum.git] / src / test / test_bitcoin.cpp
blob94ec7c03f595b7b84ae497f992b43be4f8ab2576
1 // Copyright (c) 2011-2016 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.
5 #include "test_bitcoin.h"
7 #include "chainparams.h"
8 #include "consensus/consensus.h"
9 #include "consensus/validation.h"
10 #include "crypto/sha256.h"
11 #include "fs.h"
12 #include "key.h"
13 #include "validation.h"
14 #include "miner.h"
15 #include "net_processing.h"
16 #include "pubkey.h"
17 #include "random.h"
18 #include "txdb.h"
19 #include "txmempool.h"
20 #include "ui_interface.h"
21 #include "rpc/server.h"
22 #include "rpc/register.h"
23 #include "script/sigcache.h"
25 #include "test/testutil.h"
27 #include <memory>
29 uint256 insecure_rand_seed = GetRandHash();
30 FastRandomContext insecure_rand_ctx(insecure_rand_seed);
32 extern bool fPrintToConsole;
33 extern void noui_connect();
35 BasicTestingSetup::BasicTestingSetup(const std::string& chainName)
37 SHA256AutoDetect();
38 RandomInit();
39 ECC_Start();
40 SetupEnvironment();
41 SetupNetworking();
42 InitSignatureCache();
43 InitScriptExecutionCache();
44 fPrintToDebugLog = false; // don't want to write to debug.log file
45 fCheckBlockIndex = true;
46 SelectParams(chainName);
47 noui_connect();
50 BasicTestingSetup::~BasicTestingSetup()
52 ECC_Stop();
53 g_connman.reset();
56 TestingSetup::TestingSetup(const std::string& chainName) : BasicTestingSetup(chainName)
58 const CChainParams& chainparams = Params();
59 // Ideally we'd move all the RPC tests to the functional testing framework
60 // instead of unit tests, but for now we need these here.
62 RegisterAllCoreRPCCommands(tableRPC);
63 ClearDatadirCache();
64 pathTemp = GetTempPath() / strprintf("test_bitcoin_%lu_%i", (unsigned long)GetTime(), (int)(InsecureRandRange(100000)));
65 fs::create_directories(pathTemp);
66 gArgs.ForceSetArg("-datadir", pathTemp.string());
68 // Note that because we don't bother running a scheduler thread here,
69 // callbacks via CValidationInterface are unreliable, but that's OK,
70 // our unit tests aren't testing multiple parts of the code at once.
71 GetMainSignals().RegisterBackgroundSignalScheduler(scheduler);
73 mempool.setSanityCheck(1.0);
74 pblocktree = new CBlockTreeDB(1 << 20, true);
75 pcoinsdbview = new CCoinsViewDB(1 << 23, true);
76 pcoinsTip = new CCoinsViewCache(pcoinsdbview);
77 if (!LoadGenesisBlock(chainparams)) {
78 throw std::runtime_error("LoadGenesisBlock failed.");
81 CValidationState state;
82 if (!ActivateBestChain(state, chainparams)) {
83 throw std::runtime_error("ActivateBestChain failed.");
86 nScriptCheckThreads = 3;
87 for (int i=0; i < nScriptCheckThreads-1; i++)
88 threadGroup.create_thread(&ThreadScriptCheck);
89 g_connman = std::unique_ptr<CConnman>(new CConnman(0x1337, 0x1337)); // Deterministic randomness for tests.
90 connman = g_connman.get();
91 RegisterNodeSignals(GetNodeSignals());
94 TestingSetup::~TestingSetup()
96 UnregisterNodeSignals(GetNodeSignals());
97 threadGroup.interrupt_all();
98 threadGroup.join_all();
99 GetMainSignals().FlushBackgroundCallbacks();
100 GetMainSignals().UnregisterBackgroundSignalScheduler();
101 UnloadBlockIndex();
102 delete pcoinsTip;
103 delete pcoinsdbview;
104 delete pblocktree;
105 fs::remove_all(pathTemp);
108 TestChain100Setup::TestChain100Setup() : TestingSetup(CBaseChainParams::REGTEST)
110 // Generate a 100-block chain:
111 coinbaseKey.MakeNewKey(true);
112 CScript scriptPubKey = CScript() << ToByteVector(coinbaseKey.GetPubKey()) << OP_CHECKSIG;
113 for (int i = 0; i < COINBASE_MATURITY; i++)
115 std::vector<CMutableTransaction> noTxns;
116 CBlock b = CreateAndProcessBlock(noTxns, scriptPubKey);
117 coinbaseTxns.push_back(*b.vtx[0]);
122 // Create a new block with just given transactions, coinbase paying to
123 // scriptPubKey, and try to add it to the current chain.
125 CBlock
126 TestChain100Setup::CreateAndProcessBlock(const std::vector<CMutableTransaction>& txns, const CScript& scriptPubKey)
128 const CChainParams& chainparams = Params();
129 std::unique_ptr<CBlockTemplate> pblocktemplate = BlockAssembler(chainparams).CreateNewBlock(scriptPubKey);
130 CBlock& block = pblocktemplate->block;
132 // Replace mempool-selected txns with just coinbase plus passed-in txns:
133 block.vtx.resize(1);
134 for (const CMutableTransaction& tx : txns)
135 block.vtx.push_back(MakeTransactionRef(tx));
136 // IncrementExtraNonce creates a valid coinbase and merkleRoot
137 unsigned int extraNonce = 0;
138 IncrementExtraNonce(&block, chainActive.Tip(), extraNonce);
140 while (!CheckProofOfWork(block.GetHash(), block.nBits, chainparams.GetConsensus())) ++block.nNonce;
142 std::shared_ptr<const CBlock> shared_pblock = std::make_shared<const CBlock>(block);
143 ProcessNewBlock(chainparams, shared_pblock, true, nullptr);
145 CBlock result = block;
146 return result;
149 TestChain100Setup::~TestChain100Setup()
154 CTxMemPoolEntry TestMemPoolEntryHelper::FromTx(const CMutableTransaction &tx) {
155 CTransaction txn(tx);
156 return FromTx(txn);
159 CTxMemPoolEntry TestMemPoolEntryHelper::FromTx(const CTransaction &txn) {
160 return CTxMemPoolEntry(MakeTransactionRef(txn), nFee, nTime, nHeight,
161 spendsCoinbase, sigOpCost, lp);