LCOV - code coverage report
Current view: top level - src/test - test_bitcoin.cpp (source / functions) Hit Total Coverage
Test: total_coverage.info Lines: 69 75 92.0 %
Date: 2015-10-12 22:39:14 Functions: 9 12 75.0 %
Legend: Lines: hit not hit

          Line data    Source code
       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.
       4             : 
       5             : #define BOOST_TEST_MODULE Bitcoin Test Suite
       6             : 
       7             : #include "test_bitcoin.h"
       8             : 
       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
      24             : 
      25             : #include <boost/filesystem.hpp>
      26             : #include <boost/test/unit_test.hpp>
      27             : #include <boost/thread.hpp>
      28             : 
      29           1 : CClientUIInterface uiInterface; // Declared but not defined in ui_interface.h
      30             : CWallet* pwalletMain;
      31             : 
      32             : extern bool fPrintToConsole;
      33             : extern void noui_connect();
      34             : 
      35         175 : BasicTestingSetup::BasicTestingSetup(CBaseChainParams::Network network)
      36             : {
      37         175 :         ECC_Start();
      38         175 :         SetupEnvironment();
      39         175 :         fPrintToDebugLog = false; // don't want to write to debug.log file
      40         175 :         fCheckBlockIndex = true;
      41         175 :         SelectParams(network);
      42         175 :         noui_connect();
      43         175 : }
      44             : 
      45         150 : BasicTestingSetup::~BasicTestingSetup()
      46             : {
      47         175 :         ECC_Stop();
      48         175 : }
      49             : 
      50          50 : TestingSetup::TestingSetup(CBaseChainParams::Network network) : BasicTestingSetup(network)
      51             : {
      52             : #ifdef ENABLE_WALLET
      53          25 :         bitdb.MakeMock();
      54             : #endif
      55          25 :         ClearDatadirCache();
      56         150 :         pathTemp = GetTempPath() / strprintf("test_bitcoin_%lu_%i", (unsigned long)GetTime(), (int)(GetRand(100000)));
      57          25 :         boost::filesystem::create_directories(pathTemp);
      58          75 :         mapArgs["-datadir"] = pathTemp.string();
      59          25 :         pblocktree = new CBlockTreeDB(1 << 20, true);
      60          25 :         pcoinsdbview = new CCoinsViewDB(1 << 23, true);
      61          25 :         pcoinsTip = new CCoinsViewCache(pcoinsdbview);
      62          25 :         InitBlockIndex();
      63             : #ifdef ENABLE_WALLET
      64             :         bool fFirstRun;
      65          75 :         pwalletMain = new CWallet("wallet.dat");
      66          25 :         pwalletMain->LoadWallet(fFirstRun);
      67          25 :         RegisterValidationInterface(pwalletMain);
      68             : #endif
      69          25 :         nScriptCheckThreads = 3;
      70          75 :         for (int i=0; i < nScriptCheckThreads-1; i++)
      71          50 :             threadGroup.create_thread(&ThreadScriptCheck);
      72          25 :         RegisterNodeSignals(GetNodeSignals());
      73          25 : }
      74             : 
      75          50 : TestingSetup::~TestingSetup()
      76             : {
      77          25 :         UnregisterNodeSignals(GetNodeSignals());
      78          25 :         threadGroup.interrupt_all();
      79          25 :         threadGroup.join_all();
      80             : #ifdef ENABLE_WALLET
      81          25 :         UnregisterValidationInterface(pwalletMain);
      82          25 :         delete pwalletMain;
      83          25 :         pwalletMain = NULL;
      84             : #endif
      85          25 :         UnloadBlockIndex();
      86          25 :         delete pcoinsTip;
      87          25 :         delete pcoinsdbview;
      88          50 :         delete pblocktree;
      89             : #ifdef ENABLE_WALLET
      90          25 :         bitdb.Flush(true);
      91          25 :         bitdb.Reset();
      92             : #endif
      93          25 :         boost::filesystem::remove_all(pathTemp);
      94          25 : }
      95             : 
      96           2 : TestChain100Setup::TestChain100Setup() : TestingSetup(CBaseChainParams::REGTEST)
      97             : {
      98             :     // Generate a 100-block chain:
      99           1 :     coinbaseKey.MakeNewKey(true);
     100           4 :     CScript scriptPubKey = CScript() <<  ToByteVector(coinbaseKey.GetPubKey()) << OP_CHECKSIG;
     101         101 :     for (int i = 0; i < COINBASE_MATURITY; i++)
     102             :     {
     103             :         std::vector<CMutableTransaction> noTxns;
     104         100 :         CBlock b = CreateAndProcessBlock(noTxns, scriptPubKey);
     105         100 :         coinbaseTxns.push_back(b.vtx[0]);
     106         100 :     }
     107           1 : }
     108             : 
     109             : //
     110             : // Create a new block with just given transactions, coinbase paying to
     111             : // scriptPubKey, and try to add it to the current chain.
     112             : //
     113             : CBlock
     114         104 : TestChain100Setup::CreateAndProcessBlock(const std::vector<CMutableTransaction>& txns, const CScript& scriptPubKey)
     115             : {
     116         104 :     CBlockTemplate *pblocktemplate = CreateNewBlock(scriptPubKey);
     117         104 :     CBlock& block = pblocktemplate->block;
     118             : 
     119             :     // Replace mempool-selected txns with just coinbase plus passed-in txns:
     120         208 :     block.vtx.resize(1);
     121         555 :     BOOST_FOREACH(const CMutableTransaction& tx, txns)
     122          14 :         block.vtx.push_back(tx);
     123             :     // IncrementExtraNonce creates a valid coinbase and merkleRoot
     124         104 :     unsigned int extraNonce = 0;
     125         104 :     IncrementExtraNonce(&block, chainActive.Tip(), extraNonce);
     126             : 
     127         318 :     while (!CheckProofOfWork(block.GetHash(), block.nBits, Params(CBaseChainParams::REGTEST).GetConsensus())) ++block.nNonce;
     128             : 
     129             :     CValidationState state;
     130         104 :     ProcessNewBlock(state, NULL, &block, true, NULL);
     131             : 
     132         104 :     CBlock result = block;
     133         104 :     delete pblocktemplate;
     134         104 :     return result;
     135             : }
     136             : 
     137           2 : TestChain100Setup::~TestChain100Setup()
     138             : {
     139           1 : }
     140             : 
     141           0 : void Shutdown(void* parg)
     142             : {
     143           0 :   exit(0);
     144             : }
     145             : 
     146           0 : void StartShutdown()
     147             : {
     148           0 :   exit(0);
     149             : }
     150             : 
     151           0 : bool ShutdownRequested()
     152             : {
     153           0 :   return false;
     154           3 : }

Generated by: LCOV version 1.11