Document assumptions that are being made to avoid NULL pointer dereferences
[bitcoinplatinum.git] / src / test / test_bitcoin.h
blob2ddac2f0761e28232d9d7b8317c792920c95d9ca
1 // Copyright (c) 2015-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 #ifndef BITCOIN_TEST_TEST_BITCOIN_H
6 #define BITCOIN_TEST_TEST_BITCOIN_H
8 #include "chainparamsbase.h"
9 #include "fs.h"
10 #include "key.h"
11 #include "pubkey.h"
12 #include "random.h"
13 #include "scheduler.h"
14 #include "txdb.h"
15 #include "txmempool.h"
17 #include <boost/thread.hpp>
19 extern uint256 insecure_rand_seed;
20 extern FastRandomContext insecure_rand_ctx;
22 static inline void SeedInsecureRand(bool fDeterministic = false)
24 if (fDeterministic) {
25 insecure_rand_seed = uint256();
26 } else {
27 insecure_rand_seed = GetRandHash();
29 insecure_rand_ctx = FastRandomContext(insecure_rand_seed);
32 static inline uint32_t InsecureRand32() { return insecure_rand_ctx.rand32(); }
33 static inline uint256 InsecureRand256() { return insecure_rand_ctx.rand256(); }
34 static inline uint64_t InsecureRandBits(int bits) { return insecure_rand_ctx.randbits(bits); }
35 static inline uint64_t InsecureRandRange(uint64_t range) { return insecure_rand_ctx.randrange(range); }
36 static inline bool InsecureRandBool() { return insecure_rand_ctx.randbool(); }
38 /** Basic testing setup.
39 * This just configures logging and chain parameters.
41 struct BasicTestingSetup {
42 ECCVerifyHandle globalVerifyHandle;
44 explicit BasicTestingSetup(const std::string& chainName = CBaseChainParams::MAIN);
45 ~BasicTestingSetup();
48 /** Testing setup that configures a complete environment.
49 * Included are data directory, coins database, script check threads setup.
51 class CConnman;
52 struct TestingSetup: public BasicTestingSetup {
53 CCoinsViewDB *pcoinsdbview;
54 fs::path pathTemp;
55 boost::thread_group threadGroup;
56 CConnman* connman;
57 CScheduler scheduler;
59 explicit TestingSetup(const std::string& chainName = CBaseChainParams::MAIN);
60 ~TestingSetup();
63 class CBlock;
64 struct CMutableTransaction;
65 class CScript;
68 // Testing fixture that pre-creates a
69 // 100-block REGTEST-mode block chain
71 struct TestChain100Setup : public TestingSetup {
72 TestChain100Setup();
74 // Create a new block with just given transactions, coinbase paying to
75 // scriptPubKey, and try to add it to the current chain.
76 CBlock CreateAndProcessBlock(const std::vector<CMutableTransaction>& txns,
77 const CScript& scriptPubKey);
79 ~TestChain100Setup();
81 std::vector<CTransaction> coinbaseTxns; // For convenience, coinbase transactions
82 CKey coinbaseKey; // private/public key needed to spend coinbase transactions
85 class CTxMemPoolEntry;
87 struct TestMemPoolEntryHelper
89 // Default values
90 CAmount nFee;
91 int64_t nTime;
92 unsigned int nHeight;
93 bool spendsCoinbase;
94 unsigned int sigOpCost;
95 LockPoints lp;
97 TestMemPoolEntryHelper() :
98 nFee(0), nTime(0), nHeight(1),
99 spendsCoinbase(false), sigOpCost(4) { }
101 CTxMemPoolEntry FromTx(const CMutableTransaction &tx);
102 CTxMemPoolEntry FromTx(const CTransaction &tx);
104 // Change the default value
105 TestMemPoolEntryHelper &Fee(CAmount _fee) { nFee = _fee; return *this; }
106 TestMemPoolEntryHelper &Time(int64_t _time) { nTime = _time; return *this; }
107 TestMemPoolEntryHelper &Height(unsigned int _height) { nHeight = _height; return *this; }
108 TestMemPoolEntryHelper &SpendsCoinbase(bool _flag) { spendsCoinbase = _flag; return *this; }
109 TestMemPoolEntryHelper &SigOpsCost(unsigned int _sigopsCost) { sigOpCost = _sigopsCost; return *this; }
111 #endif