Merge pull request #6599
[bitcoinplatinum.git] / src / test / test_bitcoin.cpp
blob8d81275a6fefa471460110ecde632a6a6f246b2b
1 // Copyright (c) 2011-2013 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 #define BOOST_TEST_MODULE Bitcoin Test Suite
7 #include "test_bitcoin.h"
9 #include "chainparams.h"
10 #include "consensus/consensus.h"
11 #include "consensus/validation.h"
12 #include "key.h"
13 #include "main.h"
14 #include "miner.h"
15 #include "pubkey.h"
16 #include "random.h"
17 #include "txdb.h"
18 #include "ui_interface.h"
19 #include "util.h"
20 #ifdef ENABLE_WALLET
21 #include "wallet/db.h"
22 #include "wallet/wallet.h"
23 #endif
25 #include <boost/filesystem.hpp>
26 #include <boost/test/unit_test.hpp>
27 #include <boost/thread.hpp>
29 CClientUIInterface uiInterface; // Declared but not defined in ui_interface.h
30 CWallet* pwalletMain;
32 extern bool fPrintToConsole;
33 extern void noui_connect();
35 BasicTestingSetup::BasicTestingSetup(CBaseChainParams::Network network)
37 ECC_Start();
38 SetupEnvironment();
39 fPrintToDebugLog = false; // don't want to write to debug.log file
40 fCheckBlockIndex = true;
41 SelectParams(network);
42 noui_connect();
45 BasicTestingSetup::~BasicTestingSetup()
47 ECC_Stop();
50 TestingSetup::TestingSetup(CBaseChainParams::Network network) : BasicTestingSetup(network)
52 #ifdef ENABLE_WALLET
53 bitdb.MakeMock();
54 #endif
55 ClearDatadirCache();
56 pathTemp = GetTempPath() / strprintf("test_bitcoin_%lu_%i", (unsigned long)GetTime(), (int)(GetRand(100000)));
57 boost::filesystem::create_directories(pathTemp);
58 mapArgs["-datadir"] = pathTemp.string();
59 pblocktree = new CBlockTreeDB(1 << 20, true);
60 pcoinsdbview = new CCoinsViewDB(1 << 23, true);
61 pcoinsTip = new CCoinsViewCache(pcoinsdbview);
62 InitBlockIndex();
63 #ifdef ENABLE_WALLET
64 bool fFirstRun;
65 pwalletMain = new CWallet("wallet.dat");
66 pwalletMain->LoadWallet(fFirstRun);
67 RegisterValidationInterface(pwalletMain);
68 #endif
69 nScriptCheckThreads = 3;
70 for (int i=0; i < nScriptCheckThreads-1; i++)
71 threadGroup.create_thread(&ThreadScriptCheck);
72 RegisterNodeSignals(GetNodeSignals());
75 TestingSetup::~TestingSetup()
77 UnregisterNodeSignals(GetNodeSignals());
78 threadGroup.interrupt_all();
79 threadGroup.join_all();
80 #ifdef ENABLE_WALLET
81 UnregisterValidationInterface(pwalletMain);
82 delete pwalletMain;
83 pwalletMain = NULL;
84 #endif
85 UnloadBlockIndex();
86 delete pcoinsTip;
87 delete pcoinsdbview;
88 delete pblocktree;
89 #ifdef ENABLE_WALLET
90 bitdb.Flush(true);
91 bitdb.Reset();
92 #endif
93 boost::filesystem::remove_all(pathTemp);
96 TestChain100Setup::TestChain100Setup() : TestingSetup(CBaseChainParams::REGTEST)
98 // Generate a 100-block chain:
99 coinbaseKey.MakeNewKey(true);
100 CScript scriptPubKey = CScript() << ToByteVector(coinbaseKey.GetPubKey()) << OP_CHECKSIG;
101 for (int i = 0; i < COINBASE_MATURITY; i++)
103 std::vector<CMutableTransaction> noTxns;
104 CBlock b = CreateAndProcessBlock(noTxns, scriptPubKey);
105 coinbaseTxns.push_back(b.vtx[0]);
110 // Create a new block with just given transactions, coinbase paying to
111 // scriptPubKey, and try to add it to the current chain.
113 CBlock
114 TestChain100Setup::CreateAndProcessBlock(const std::vector<CMutableTransaction>& txns, const CScript& scriptPubKey)
116 CBlockTemplate *pblocktemplate = CreateNewBlock(scriptPubKey);
117 CBlock& block = pblocktemplate->block;
119 // Replace mempool-selected txns with just coinbase plus passed-in txns:
120 block.vtx.resize(1);
121 BOOST_FOREACH(const CMutableTransaction& tx, txns)
122 block.vtx.push_back(tx);
123 // IncrementExtraNonce creates a valid coinbase and merkleRoot
124 unsigned int extraNonce = 0;
125 IncrementExtraNonce(&block, chainActive.Tip(), extraNonce);
127 while (!CheckProofOfWork(block.GetHash(), block.nBits, Params(CBaseChainParams::REGTEST).GetConsensus())) ++block.nNonce;
129 CValidationState state;
130 ProcessNewBlock(state, NULL, &block, true, NULL);
132 CBlock result = block;
133 delete pblocktemplate;
134 return result;
137 TestChain100Setup::~TestChain100Setup()
141 void Shutdown(void* parg)
143 exit(0);
146 void StartShutdown()
148 exit(0);
151 bool ShutdownRequested()
153 return false;