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.
54 static void AddNode(CNode
& node
);
55 static void ClearNodes();
58 class PeerLogicValidation
;
59 struct TestingSetup
: public BasicTestingSetup
{
61 boost::thread_group threadGroup
;
64 std::unique_ptr
<PeerLogicValidation
> peerLogic
;
66 explicit TestingSetup(const std::string
& chainName
= CBaseChainParams::MAIN
);
71 struct CMutableTransaction
;
75 // Testing fixture that pre-creates a
76 // 100-block REGTEST-mode block chain
78 struct TestChain100Setup
: public TestingSetup
{
81 // Create a new block with just given transactions, coinbase paying to
82 // scriptPubKey, and try to add it to the current chain.
83 CBlock
CreateAndProcessBlock(const std::vector
<CMutableTransaction
>& txns
,
84 const CScript
& scriptPubKey
);
88 std::vector
<CTransaction
> coinbaseTxns
; // For convenience, coinbase transactions
89 CKey coinbaseKey
; // private/public key needed to spend coinbase transactions
92 class CTxMemPoolEntry
;
94 struct TestMemPoolEntryHelper
101 unsigned int sigOpCost
;
104 TestMemPoolEntryHelper() :
105 nFee(0), nTime(0), nHeight(1),
106 spendsCoinbase(false), sigOpCost(4) { }
108 CTxMemPoolEntry
FromTx(const CMutableTransaction
&tx
);
109 CTxMemPoolEntry
FromTx(const CTransaction
&tx
);
111 // Change the default value
112 TestMemPoolEntryHelper
&Fee(CAmount _fee
) { nFee
= _fee
; return *this; }
113 TestMemPoolEntryHelper
&Time(int64_t _time
) { nTime
= _time
; return *this; }
114 TestMemPoolEntryHelper
&Height(unsigned int _height
) { nHeight
= _height
; return *this; }
115 TestMemPoolEntryHelper
&SpendsCoinbase(bool _flag
) { spendsCoinbase
= _flag
; return *this; }
116 TestMemPoolEntryHelper
&SigOpsCost(unsigned int _sigopsCost
) { sigOpCost
= _sigopsCost
; return *this; }
119 CBlock
getBlock13b8a();