net: correctly ban before the handshake is complete
[bitcoinplatinum.git] / src / test / test_bitcoin.cpp
blob4785415e3cc5dd61c48671e576b6c2ed4c0cebd5
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 #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 "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 #include <boost/filesystem.hpp>
30 #include <boost/test/unit_test.hpp>
31 #include <boost/thread.hpp>
33 std::unique_ptr<CConnman> g_connman;
34 FastRandomContext insecure_rand_ctx(true);
36 extern bool fPrintToConsole;
37 extern void noui_connect();
39 BasicTestingSetup::BasicTestingSetup(const std::string& chainName)
41 ECC_Start();
42 SetupEnvironment();
43 SetupNetworking();
44 InitSignatureCache();
45 fPrintToDebugLog = false; // don't want to write to debug.log file
46 fCheckBlockIndex = true;
47 SelectParams(chainName);
48 noui_connect();
51 BasicTestingSetup::~BasicTestingSetup()
53 ECC_Stop();
54 g_connman.reset();
57 TestingSetup::TestingSetup(const std::string& chainName) : BasicTestingSetup(chainName)
59 const CChainParams& chainparams = Params();
60 // Ideally we'd move all the RPC tests to the functional testing framework
61 // instead of unit tests, but for now we need these here.
63 RegisterAllCoreRPCCommands(tableRPC);
64 ClearDatadirCache();
65 pathTemp = GetTempPath() / strprintf("test_bitcoin_%lu_%i", (unsigned long)GetTime(), (int)(GetRand(100000)));
66 boost::filesystem::create_directories(pathTemp);
67 ForceSetArg("-datadir", pathTemp.string());
68 mempool.setSanityCheck(1.0);
69 pblocktree = new CBlockTreeDB(1 << 20, true);
70 pcoinsdbview = new CCoinsViewDB(1 << 23, true);
71 pcoinsTip = new CCoinsViewCache(pcoinsdbview);
72 InitBlockIndex(chainparams);
74 CValidationState state;
75 bool ok = ActivateBestChain(state, chainparams);
76 BOOST_CHECK(ok);
78 nScriptCheckThreads = 3;
79 for (int i=0; i < nScriptCheckThreads-1; i++)
80 threadGroup.create_thread(&ThreadScriptCheck);
81 g_connman = std::unique_ptr<CConnman>(new CConnman(0x1337, 0x1337)); // Deterministic randomness for tests.
82 connman = g_connman.get();
83 RegisterNodeSignals(GetNodeSignals());
86 TestingSetup::~TestingSetup()
88 UnregisterNodeSignals(GetNodeSignals());
89 threadGroup.interrupt_all();
90 threadGroup.join_all();
91 UnloadBlockIndex();
92 delete pcoinsTip;
93 delete pcoinsdbview;
94 delete pblocktree;
95 boost::filesystem::remove_all(pathTemp);
98 TestChain100Setup::TestChain100Setup() : TestingSetup(CBaseChainParams::REGTEST)
100 // Generate a 100-block chain:
101 coinbaseKey.MakeNewKey(true);
102 CScript scriptPubKey = CScript() << ToByteVector(coinbaseKey.GetPubKey()) << OP_CHECKSIG;
103 for (int i = 0; i < COINBASE_MATURITY; i++)
105 std::vector<CMutableTransaction> noTxns;
106 CBlock b = CreateAndProcessBlock(noTxns, scriptPubKey);
107 coinbaseTxns.push_back(*b.vtx[0]);
112 // Create a new block with just given transactions, coinbase paying to
113 // scriptPubKey, and try to add it to the current chain.
115 CBlock
116 TestChain100Setup::CreateAndProcessBlock(const std::vector<CMutableTransaction>& txns, const CScript& scriptPubKey)
118 const CChainParams& chainparams = Params();
119 std::unique_ptr<CBlockTemplate> pblocktemplate = BlockAssembler(chainparams).CreateNewBlock(scriptPubKey);
120 CBlock& block = pblocktemplate->block;
122 // Replace mempool-selected txns with just coinbase plus passed-in txns:
123 block.vtx.resize(1);
124 BOOST_FOREACH(const CMutableTransaction& tx, txns)
125 block.vtx.push_back(MakeTransactionRef(tx));
126 // IncrementExtraNonce creates a valid coinbase and merkleRoot
127 unsigned int extraNonce = 0;
128 IncrementExtraNonce(&block, chainActive.Tip(), extraNonce);
130 while (!CheckProofOfWork(block.GetHash(), block.nBits, chainparams.GetConsensus())) ++block.nNonce;
132 std::shared_ptr<const CBlock> shared_pblock = std::make_shared<const CBlock>(block);
133 ProcessNewBlock(chainparams, shared_pblock, true, NULL);
135 CBlock result = block;
136 return result;
139 TestChain100Setup::~TestChain100Setup()
144 CTxMemPoolEntry TestMemPoolEntryHelper::FromTx(const CMutableTransaction &tx, CTxMemPool *pool) {
145 CTransaction txn(tx);
146 return FromTx(txn, pool);
149 CTxMemPoolEntry TestMemPoolEntryHelper::FromTx(const CTransaction &txn, CTxMemPool *pool) {
150 // Hack to assume either it's completely dependent on other mempool txs or not at all
151 CAmount inChainValue = pool && pool->HasNoInputsOf(txn) ? txn.GetValueOut() : 0;
153 return CTxMemPoolEntry(MakeTransactionRef(txn), nFee, nTime, dPriority, nHeight,
154 inChainValue, spendsCoinbase, sigOpCost, lp);
157 void Shutdown(void* parg)
159 exit(0);
162 void StartShutdown()
164 exit(0);
167 bool ShutdownRequested()
169 return false;