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"
14 #include "txmempool.h"
16 #include <boost/thread.hpp>
18 extern uint256 insecure_rand_seed
;
19 extern FastRandomContext insecure_rand_ctx
;
21 static inline void SeedInsecureRand(bool fDeterministic
= false)
24 insecure_rand_seed
= uint256();
26 insecure_rand_seed
= GetRandHash();
28 insecure_rand_ctx
= FastRandomContext(insecure_rand_seed
);
31 static inline uint32_t InsecureRand32() { return insecure_rand_ctx
.rand32(); }
32 static inline uint256
InsecureRand256() { return insecure_rand_ctx
.rand256(); }
33 static inline uint64_t InsecureRandBits(int bits
) { return insecure_rand_ctx
.randbits(bits
); }
34 static inline uint64_t InsecureRandRange(uint64_t range
) { return insecure_rand_ctx
.randrange(range
); }
35 static inline bool InsecureRandBool() { return insecure_rand_ctx
.randbool(); }
37 /** Basic testing setup.
38 * This just configures logging and chain parameters.
40 struct BasicTestingSetup
{
41 ECCVerifyHandle globalVerifyHandle
;
43 BasicTestingSetup(const std::string
& chainName
= CBaseChainParams::MAIN
);
47 /** Testing setup that configures a complete environment.
48 * Included are data directory, coins database, script check threads setup.
51 struct TestingSetup
: public BasicTestingSetup
{
52 CCoinsViewDB
*pcoinsdbview
;
54 boost::thread_group threadGroup
;
57 TestingSetup(const std::string
& chainName
= CBaseChainParams::MAIN
);
62 struct CMutableTransaction
;
66 // Testing fixture that pre-creates a
67 // 100-block REGTEST-mode block chain
69 struct TestChain100Setup
: public TestingSetup
{
72 // Create a new block with just given transactions, coinbase paying to
73 // scriptPubKey, and try to add it to the current chain.
74 CBlock
CreateAndProcessBlock(const std::vector
<CMutableTransaction
>& txns
,
75 const CScript
& scriptPubKey
);
79 std::vector
<CTransaction
> coinbaseTxns
; // For convenience, coinbase transactions
80 CKey coinbaseKey
; // private/public key needed to spend coinbase transactions
83 class CTxMemPoolEntry
;
85 struct TestMemPoolEntryHelper
92 unsigned int sigOpCost
;
95 TestMemPoolEntryHelper() :
96 nFee(0), nTime(0), nHeight(1),
97 spendsCoinbase(false), sigOpCost(4) { }
99 CTxMemPoolEntry
FromTx(const CMutableTransaction
&tx
);
100 CTxMemPoolEntry
FromTx(const CTransaction
&tx
);
102 // Change the default value
103 TestMemPoolEntryHelper
&Fee(CAmount _fee
) { nFee
= _fee
; return *this; }
104 TestMemPoolEntryHelper
&Time(int64_t _time
) { nTime
= _time
; return *this; }
105 TestMemPoolEntryHelper
&Height(unsigned int _height
) { nHeight
= _height
; return *this; }
106 TestMemPoolEntryHelper
&SpendsCoinbase(bool _flag
) { spendsCoinbase
= _flag
; return *this; }
107 TestMemPoolEntryHelper
&SigOpsCost(unsigned int _sigopsCost
) { sigOpCost
= _sigopsCost
; return *this; }