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"
12 #include "validation.h"
14 #include "net_processing.h"
18 #include "txmempool.h"
19 #include "ui_interface.h"
20 #include "rpc/server.h"
21 #include "rpc/register.h"
22 #include "script/sigcache.h"
24 #include "test/testutil.h"
28 #include <boost/thread.hpp>
30 uint256 insecure_rand_seed
= GetRandHash();
31 FastRandomContext
insecure_rand_ctx(insecure_rand_seed
);
33 extern bool fPrintToConsole
;
34 extern void noui_connect();
36 BasicTestingSetup::BasicTestingSetup(const std::string
& chainName
)
43 fPrintToDebugLog
= false; // don't want to write to debug.log file
44 fCheckBlockIndex
= true;
45 SelectParams(chainName
);
49 BasicTestingSetup::~BasicTestingSetup()
55 TestingSetup::TestingSetup(const std::string
& chainName
) : BasicTestingSetup(chainName
)
57 const CChainParams
& chainparams
= Params();
58 // Ideally we'd move all the RPC tests to the functional testing framework
59 // instead of unit tests, but for now we need these here.
61 RegisterAllCoreRPCCommands(tableRPC
);
63 pathTemp
= GetTempPath() / strprintf("test_bitcoin_%lu_%i", (unsigned long)GetTime(), (int)(GetRand(100000)));
64 fs::create_directories(pathTemp
);
65 ForceSetArg("-datadir", pathTemp
.string());
66 mempool
.setSanityCheck(1.0);
67 pblocktree
= new CBlockTreeDB(1 << 20, true);
68 pcoinsdbview
= new CCoinsViewDB(1 << 23, true);
69 pcoinsTip
= new CCoinsViewCache(pcoinsdbview
);
70 if (!InitBlockIndex(chainparams
)) {
71 throw std::runtime_error("InitBlockIndex failed.");
74 CValidationState state
;
75 if (!ActivateBestChain(state
, chainparams
)) {
76 throw std::runtime_error("ActivateBestChain failed.");
79 nScriptCheckThreads
= 3;
80 for (int i
=0; i
< nScriptCheckThreads
-1; i
++)
81 threadGroup
.create_thread(&ThreadScriptCheck
);
82 g_connman
= std::unique_ptr
<CConnman
>(new CConnman(0x1337, 0x1337)); // Deterministic randomness for tests.
83 connman
= g_connman
.get();
84 RegisterNodeSignals(GetNodeSignals());
87 TestingSetup::~TestingSetup()
89 UnregisterNodeSignals(GetNodeSignals());
90 threadGroup
.interrupt_all();
91 threadGroup
.join_all();
96 fs::remove_all(pathTemp
);
99 TestChain100Setup::TestChain100Setup() : TestingSetup(CBaseChainParams::REGTEST
)
101 // Generate a 100-block chain:
102 coinbaseKey
.MakeNewKey(true);
103 CScript scriptPubKey
= CScript() << ToByteVector(coinbaseKey
.GetPubKey()) << OP_CHECKSIG
;
104 for (int i
= 0; i
< COINBASE_MATURITY
; i
++)
106 std::vector
<CMutableTransaction
> noTxns
;
107 CBlock b
= CreateAndProcessBlock(noTxns
, scriptPubKey
);
108 coinbaseTxns
.push_back(*b
.vtx
[0]);
113 // Create a new block with just given transactions, coinbase paying to
114 // scriptPubKey, and try to add it to the current chain.
117 TestChain100Setup::CreateAndProcessBlock(const std::vector
<CMutableTransaction
>& txns
, const CScript
& scriptPubKey
)
119 const CChainParams
& chainparams
= Params();
120 std::unique_ptr
<CBlockTemplate
> pblocktemplate
= BlockAssembler(chainparams
).CreateNewBlock(scriptPubKey
);
121 CBlock
& block
= pblocktemplate
->block
;
123 // Replace mempool-selected txns with just coinbase plus passed-in txns:
125 BOOST_FOREACH(const CMutableTransaction
& tx
, txns
)
126 block
.vtx
.push_back(MakeTransactionRef(tx
));
127 // IncrementExtraNonce creates a valid coinbase and merkleRoot
128 unsigned int extraNonce
= 0;
129 IncrementExtraNonce(&block
, chainActive
.Tip(), extraNonce
);
131 while (!CheckProofOfWork(block
.GetHash(), block
.nBits
, chainparams
.GetConsensus())) ++block
.nNonce
;
133 std::shared_ptr
<const CBlock
> shared_pblock
= std::make_shared
<const CBlock
>(block
);
134 ProcessNewBlock(chainparams
, shared_pblock
, true, NULL
);
136 CBlock result
= block
;
140 TestChain100Setup::~TestChain100Setup()
145 CTxMemPoolEntry
TestMemPoolEntryHelper::FromTx(const CMutableTransaction
&tx
) {
146 CTransaction
txn(tx
);
150 CTxMemPoolEntry
TestMemPoolEntryHelper::FromTx(const CTransaction
&txn
) {
151 return CTxMemPoolEntry(MakeTransactionRef(txn
), nFee
, nTime
, nHeight
,
152 spendsCoinbase
, sigOpCost
, lp
);