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"
13 #include "scheduler.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)
25 insecure_rand_seed
= uint256();
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
);
48 /** Testing setup that configures a complete environment.
49 * Included are data directory, coins database, script check threads setup.
52 class PeerLogicValidation
;
53 struct TestingSetup
: public BasicTestingSetup
{
54 CCoinsViewDB
*pcoinsdbview
;
56 boost::thread_group threadGroup
;
59 std::unique_ptr
<PeerLogicValidation
> peerLogic
;
61 explicit TestingSetup(const std::string
& chainName
= CBaseChainParams::MAIN
);
66 struct CMutableTransaction
;
70 // Testing fixture that pre-creates a
71 // 100-block REGTEST-mode block chain
73 struct TestChain100Setup
: public TestingSetup
{
76 // Create a new block with just given transactions, coinbase paying to
77 // scriptPubKey, and try to add it to the current chain.
78 CBlock
CreateAndProcessBlock(const std::vector
<CMutableTransaction
>& txns
,
79 const CScript
& scriptPubKey
);
83 std::vector
<CTransaction
> coinbaseTxns
; // For convenience, coinbase transactions
84 CKey coinbaseKey
; // private/public key needed to spend coinbase transactions
87 class CTxMemPoolEntry
;
89 struct TestMemPoolEntryHelper
96 unsigned int sigOpCost
;
99 TestMemPoolEntryHelper() :
100 nFee(0), nTime(0), nHeight(1),
101 spendsCoinbase(false), sigOpCost(4) { }
103 CTxMemPoolEntry
FromTx(const CMutableTransaction
&tx
);
104 CTxMemPoolEntry
FromTx(const CTransaction
&tx
);
106 // Change the default value
107 TestMemPoolEntryHelper
&Fee(CAmount _fee
) { nFee
= _fee
; return *this; }
108 TestMemPoolEntryHelper
&Time(int64_t _time
) { nTime
= _time
; return *this; }
109 TestMemPoolEntryHelper
&Height(unsigned int _height
) { nHeight
= _height
; return *this; }
110 TestMemPoolEntryHelper
&SpendsCoinbase(bool _flag
) { spendsCoinbase
= _flag
; return *this; }
111 TestMemPoolEntryHelper
&SigOpsCost(unsigned int _sigopsCost
) { sigOpCost
= _sigopsCost
; return *this; }
114 CBlock
getBlock13b8a();