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"
18 #include "ui_interface.h"
21 #include "wallet/db.h"
22 #include "wallet/wallet.h"
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
32 extern bool fPrintToConsole
;
33 extern void noui_connect();
35 BasicTestingSetup::BasicTestingSetup(CBaseChainParams::Network network
)
39 fPrintToDebugLog
= false; // don't want to write to debug.log file
40 fCheckBlockIndex
= true;
41 SelectParams(network
);
45 BasicTestingSetup::~BasicTestingSetup()
50 TestingSetup::TestingSetup(CBaseChainParams::Network network
) : BasicTestingSetup(network
)
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
);
65 pwalletMain
= new CWallet("wallet.dat");
66 pwalletMain
->LoadWallet(fFirstRun
);
67 RegisterValidationInterface(pwalletMain
);
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();
81 UnregisterValidationInterface(pwalletMain
);
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.
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:
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
;
137 TestChain100Setup::~TestChain100Setup()
141 void Shutdown(void* parg
)
151 bool ShutdownRequested()