Line data Source code
1 : // Copyright (c) 2015-2022 The PIVX Core developers 2 : // Distributed under the MIT software license, see the accompanying 3 : // file COPYING or http://www.opensource.org/licenses/mit-license.php. 4 : 5 : #ifndef PIVX_TEST_TEST_PIVX_H 6 : #define PIVX_TEST_TEST_PIVX_H 7 : 8 : #include "fs.h" 9 : #include "scheduler.h" 10 : #include "txdb.h" 11 : 12 : #include <stdexcept> 13 : 14 : #include <boost/thread.hpp> 15 : 16 : extern FastRandomContext g_insecure_rand_ctx; 17 : 18 : /** 19 : * Flag to make GetRand in random.h return the same number 20 : */ 21 : extern bool g_mock_deterministic_tests; 22 : 23 : enum class SeedRand { 24 : ZEROS, //!< Seed with a compile time constant of zeros 25 : SEED, //!< Call the Seed() helper 26 : }; 27 : 28 : /** Seed the given random ctx or use the seed passed in via an environment var */ 29 : void Seed(FastRandomContext& ctx); 30 : 31 494 : static inline void SeedInsecureRand(SeedRand seed = SeedRand::SEED) 32 : { 33 494 : if (seed == SeedRand::ZEROS) { 34 15 : g_insecure_rand_ctx = FastRandomContext(/* deterministic */ true); 35 : } else { 36 479 : Seed(g_insecure_rand_ctx); 37 : } 38 15 : } 39 : 40 2 : static inline uint16_t InsecureRand16() { return g_insecure_rand_ctx.rand16(); } 41 16887910 : static inline uint32_t InsecureRand32() { return g_insecure_rand_ctx.rand32(); } 42 271655 : static inline uint256 InsecureRand256() { return g_insecure_rand_ctx.rand256(); } 43 479870 : static inline uint64_t InsecureRandBits(int bits) { return g_insecure_rand_ctx.randbits(bits); } 44 6607148 : static inline uint64_t InsecureRandRange(uint64_t range) { return g_insecure_rand_ctx.randrange(range); } 45 413582 : static inline bool InsecureRandBool() { return g_insecure_rand_ctx.randbool(); } 46 9 : static inline std::vector<unsigned char> InsecureRandBytes(size_t len) { return g_insecure_rand_ctx.randbytes(len); } 47 : 48 : /** Basic testing setup. 49 : * This just configures logging and chain parameters. 50 : */ 51 : struct BasicTestingSetup { 52 : ECCVerifyHandle globalVerifyHandle; 53 : 54 : explicit BasicTestingSetup(const std::string& chainName = CBaseChainParams::MAIN); 55 : ~BasicTestingSetup(); 56 : 57 : fs::path SetDataDir(const std::string& name); 58 : 59 : private: 60 : const fs::path m_path_root; 61 : }; 62 : 63 : /** Testing setup that configures a complete environment. 64 : * Included are data directory, coins database, script check threads 65 : * and wallet (if enabled) setup. 66 : */ 67 : class CConnman; 68 : class PeerLogicValidation; 69 : class EvoNotificationInterface; 70 : struct TestingSetup: public BasicTestingSetup 71 : { 72 : boost::thread_group threadGroup; 73 : CConnman* connman; 74 : EvoNotificationInterface* pEvoNotificationInterface; 75 : CScheduler scheduler; 76 : std::unique_ptr<PeerLogicValidation> peerLogic; 77 : 78 : explicit TestingSetup(const std::string& chainName = CBaseChainParams::MAIN); 79 : ~TestingSetup(); 80 : }; 81 : 82 1 : struct TestnetSetup : public TestingSetup 83 : { 84 1 : TestnetSetup() : TestingSetup(CBaseChainParams::TESTNET) {} 85 : }; 86 : 87 3 : struct RegTestingSetup : public TestingSetup 88 : { 89 3 : RegTestingSetup() : TestingSetup(CBaseChainParams::REGTEST) {} 90 : }; 91 : 92 : // Helper to finalize a modified PoW block. 93 : std::shared_ptr<CBlock> FinalizeBlock(std::shared_ptr<CBlock> pblock); 94 : 95 : class CBlock; 96 : struct CMutableTransaction; 97 : class CScript; 98 : 99 : // Test chain only available on regtest 100 : struct TestChainSetup : public TestingSetup 101 : { 102 : explicit TestChainSetup(int blockCount); 103 : ~TestChainSetup(); 104 : 105 : // Create a new block with coinbase paying to scriptPubKey, and try to add it to the current chain. 106 : // Include given transactions, and, if fNoMempoolTx=true, remove transactions coming from the mempool. 107 : CBlock CreateAndProcessBlock(const std::vector<CMutableTransaction>& txns, const CScript& scriptPubKey, bool fNoMempoolTx = true); 108 : CBlock CreateAndProcessBlock(const std::vector<CMutableTransaction>& txns, const CKey& scriptKey); 109 : CBlock CreateBlock(const std::vector<CMutableTransaction>& txns, 110 : const CScript& scriptPubKey, 111 : bool fNoMempoolTx = true, 112 : bool fTestBlockValidity = true, 113 : bool fIncludeQfc = true, 114 : CBlockIndex* customPrevBlock = nullptr); 115 : CBlock CreateBlock(const std::vector<CMutableTransaction>& txns, const CKey& scriptKey, bool fTestBlockValidity = true); 116 : 117 : std::vector<CTransaction> coinbaseTxns; // For convenience, coinbase transactions 118 : CKey coinbaseKey; // private/public key needed to spend coinbase transactions 119 : }; 120 : 121 : // Testing fixture that pre-creates a 100-block REGTEST-mode blockchain 122 7 : struct TestChain100Setup : public TestChainSetup 123 : { 124 7 : TestChain100Setup() : TestChainSetup(100) {} 125 : }; 126 : 127 : // Testing fixture that pre-creates a 400-block REGTEST-mode blockchain 128 : // all 400 blocks are PoW. PoS starts at height 500 129 2 : struct TestChain400Setup : public TestChainSetup 130 : { 131 2 : TestChain400Setup() : TestChainSetup(400) {} 132 : }; 133 : 134 : class CTxMemPoolEntry; 135 : 136 : struct TestMemPoolEntryHelper 137 : { 138 : // Default values 139 : CAmount nFee; 140 : int64_t nTime; 141 : unsigned int nHeight; 142 : bool spendsCoinbaseOrCoinstake; 143 : unsigned int sigOpCount; 144 : 145 7 : TestMemPoolEntryHelper() : 146 : nFee(0), nTime(0), nHeight(1), 147 7 : spendsCoinbaseOrCoinstake(false), sigOpCount(1) { } 148 : 149 : CTxMemPoolEntry FromTx(const CMutableTransaction& tx); 150 : CTxMemPoolEntry FromTx(const CTransaction& tx); 151 : 152 : // Change the default value 153 19860 : TestMemPoolEntryHelper &Fee(CAmount _fee) { nFee = _fee; return *this; } 154 16605 : TestMemPoolEntryHelper &Time(int64_t _time) { nTime = _time; return *this; } 155 16601 : TestMemPoolEntryHelper &Height(unsigned int _height) { nHeight = _height; return *this; } 156 2230 : TestMemPoolEntryHelper &SpendsCoinbaseOrCoinstake(bool _flag) { spendsCoinbaseOrCoinstake = _flag; return *this; } 157 1001 : TestMemPoolEntryHelper &SigOps(unsigned int _sigops) { sigOpCount = _sigops; return *this; } 158 : }; 159 : 160 : // define an implicit conversion here so that uint256 may be used directly in BOOST_CHECK_* 161 : std::ostream& operator<<(std::ostream& os, const uint256& num); 162 : 163 : /** 164 : * BOOST_CHECK_EXCEPTION predicates to check the specific validation error. 165 : * Use as 166 : * BOOST_CHECK_EXCEPTION(code that throws, exception type, HasReason("foo")); 167 : */ 168 25 : class HasReason 169 : { 170 : public: 171 14 : explicit HasReason(const std::string& reason) : m_reason(reason) {} 172 14 : bool operator()(const std::exception& e) const 173 : { 174 14 : return std::string(e.what()).find(m_reason) != std::string::npos; 175 : }; 176 : 177 : private: 178 : const std::string m_reason; 179 : }; 180 : 181 : #endif // PIVX_TEST_TEST_PIVX_H