Branch data Line data Source code
# 1 : : // Copyright (c) 2015-2020 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.
# 4 : :
# 5 : : #ifndef BITCOIN_TEST_UTIL_SETUP_COMMON_H
# 6 : : #define BITCOIN_TEST_UTIL_SETUP_COMMON_H
# 7 : :
# 8 : : #include <chainparamsbase.h>
# 9 : : #include <fs.h>
# 10 : : #include <key.h>
# 11 : : #include <util/system.h>
# 12 : : #include <node/context.h>
# 13 : : #include <pubkey.h>
# 14 : : #include <random.h>
# 15 : : #include <stdexcept>
# 16 : : #include <txmempool.h>
# 17 : : #include <util/check.h>
# 18 : : #include <util/string.h>
# 19 : : #include <util/vector.h>
# 20 : :
# 21 : : #include <type_traits>
# 22 : : #include <vector>
# 23 : :
# 24 : : /** This is connected to the logger. Can be used to redirect logs to any other log */
# 25 : : extern const std::function<void(const std::string&)> G_TEST_LOG_FUN;
# 26 : :
# 27 : : // Enable BOOST_CHECK_EQUAL for enum class types
# 28 : : template <typename T>
# 29 : : std::ostream& operator<<(typename std::enable_if<std::is_enum<T>::value, std::ostream>::type& stream, const T& e)
# 30 : 0 : {
# 31 : 0 : return stream << static_cast<typename std::underlying_type<T>::type>(e);
# 32 : 0 : }
# 33 : :
# 34 : : /**
# 35 : : * This global and the helpers that use it are not thread-safe.
# 36 : : *
# 37 : : * If thread-safety is needed, the global could be made thread_local (given
# 38 : : * that thread_local is supported on all architectures we support) or a
# 39 : : * per-thread instance could be used in the multi-threaded test.
# 40 : : */
# 41 : : extern FastRandomContext g_insecure_rand_ctx;
# 42 : :
# 43 : : /**
# 44 : : * Flag to make GetRand in random.h return the same number
# 45 : : */
# 46 : : extern bool g_mock_deterministic_tests;
# 47 : :
# 48 : : enum class SeedRand {
# 49 : : ZEROS, //!< Seed with a compile time constant of zeros
# 50 : : SEED, //!< Call the Seed() helper
# 51 : : };
# 52 : :
# 53 : : /** Seed the given random ctx or use the seed passed in via an environment var */
# 54 : : void Seed(FastRandomContext& ctx);
# 55 : :
# 56 : : static inline void SeedInsecureRand(SeedRand seed = SeedRand::SEED)
# 57 : 1002 : {
# 58 [ + - ][ + - ]: 1002 : if (seed == SeedRand::ZEROS) {
# [ + - ][ - + ]
# [ + - ][ + - ]
# [ - + ]
# 59 : 26 : g_insecure_rand_ctx = FastRandomContext(/* deterministic */ true);
# 60 : 976 : } else {
# 61 : 976 : Seed(g_insecure_rand_ctx);
# 62 : 976 : }
# 63 : 1002 : }
# 64 : :
# 65 : 33074776 : static inline uint32_t InsecureRand32() { return g_insecure_rand_ctx.rand32(); }
# 66 : 2351730 : static inline uint256 InsecureRand256() { return g_insecure_rand_ctx.rand256(); }
# 67 : 6666746 : static inline uint64_t InsecureRandBits(int bits) { return g_insecure_rand_ctx.randbits(bits); }
# 68 : 11481986 : static inline uint64_t InsecureRandRange(uint64_t range) { return g_insecure_rand_ctx.randrange(range); }
# 69 : 1096302 : static inline bool InsecureRandBool() { return g_insecure_rand_ctx.randbool(); }
# 70 : :
# 71 : : static constexpr CAmount CENT{1000000};
# 72 : :
# 73 : : /** Basic testing setup.
# 74 : : * This just configures logging, data dir and chain parameters.
# 75 : : */
# 76 : : struct BasicTestingSetup {
# 77 : : ECCVerifyHandle globalVerifyHandle;
# 78 : : NodeContext m_node;
# 79 : :
# 80 : : explicit BasicTestingSetup(const std::string& chainName = CBaseChainParams::MAIN, const std::vector<const char*>& extra_args = {});
# 81 : : ~BasicTestingSetup();
# 82 : :
# 83 : : const fs::path m_path_root;
# 84 : : ArgsManager m_args;
# 85 : : };
# 86 : :
# 87 : : /** Testing setup that performs all steps up until right before
# 88 : : * ChainstateManager gets initialized. Meant for testing ChainstateManager
# 89 : : * initialization behaviour.
# 90 : : */
# 91 : : struct ChainTestingSetup : public BasicTestingSetup {
# 92 : :
# 93 : : explicit ChainTestingSetup(const std::string& chainName = CBaseChainParams::MAIN, const std::vector<const char*>& extra_args = {});
# 94 : : ~ChainTestingSetup();
# 95 : : };
# 96 : :
# 97 : : /** Testing setup that configures a complete environment.
# 98 : : */
# 99 : : struct TestingSetup : public ChainTestingSetup {
# 100 : : explicit TestingSetup(const std::string& chainName = CBaseChainParams::MAIN, const std::vector<const char*>& extra_args = {});
# 101 : : };
# 102 : :
# 103 : : /** Identical to TestingSetup, but chain set to regtest */
# 104 : : struct RegTestingSetup : public TestingSetup {
# 105 : : RegTestingSetup()
# 106 : 55 : : TestingSetup{CBaseChainParams::REGTEST} {}
# 107 : : };
# 108 : :
# 109 : : class CBlock;
# 110 : : struct CMutableTransaction;
# 111 : : class CScript;
# 112 : :
# 113 : : /**
# 114 : : * Testing fixture that pre-creates a 100-block REGTEST-mode block chain
# 115 : : */
# 116 : : struct TestChain100Setup : public RegTestingSetup {
# 117 : : TestChain100Setup();
# 118 : :
# 119 : : /**
# 120 : : * Create a new block with just given transactions, coinbase paying to
# 121 : : * scriptPubKey, and try to add it to the current chain.
# 122 : : */
# 123 : : CBlock CreateAndProcessBlock(const std::vector<CMutableTransaction>& txns,
# 124 : : const CScript& scriptPubKey);
# 125 : :
# 126 : : //! Mine a series of new blocks on the active chain.
# 127 : : void mineBlocks(int num_blocks);
# 128 : :
# 129 : : /**
# 130 : : * Create a transaction and submit to the mempool.
# 131 : : *
# 132 : : * @param input_transaction The transaction to spend
# 133 : : * @param input_vout The vout to spend from the input_transaction
# 134 : : * @param input_height The height of the block that included the input_transaction
# 135 : : * @param input_signing_key The key to spend the input_transaction
# 136 : : * @param output_destination Where to send the output
# 137 : : * @param output_amount How much to send
# 138 : : * @param submit Whether or not to submit to mempool
# 139 : : */
# 140 : : CMutableTransaction CreateValidMempoolTransaction(CTransactionRef input_transaction,
# 141 : : int input_vout,
# 142 : : int input_height,
# 143 : : CKey input_signing_key,
# 144 : : CScript output_destination,
# 145 : : CAmount output_amount = CAmount(1 * COIN),
# 146 : : bool submit = true);
# 147 : :
# 148 : : ~TestChain100Setup();
# 149 : :
# 150 : : std::vector<CTransactionRef> m_coinbase_txns; // For convenience, coinbase transactions
# 151 : : CKey coinbaseKey; // private/public key needed to spend coinbase transactions
# 152 : : };
# 153 : :
# 154 : : /**
# 155 : : * Make a test setup that has disk access to the debug.log file disabled. Can
# 156 : : * be used in "hot loops", for example fuzzing or benchmarking.
# 157 : : */
# 158 : : template <class T = const BasicTestingSetup>
# 159 : : std::unique_ptr<T> MakeNoLogFileContext(const std::string& chain_name = CBaseChainParams::REGTEST, const std::vector<const char*>& extra_args = {})
# 160 : : {
# 161 : : const std::vector<const char*> arguments = Cat(
# 162 : : {
# 163 : : "-nodebuglogfile",
# 164 : : "-nodebug",
# 165 : : },
# 166 : : extra_args);
# 167 : :
# 168 : : return std::make_unique<T>(chain_name, arguments);
# 169 : : }
# 170 : :
# 171 : : class CTxMemPoolEntry;
# 172 : :
# 173 : : struct TestMemPoolEntryHelper
# 174 : : {
# 175 : : // Default values
# 176 : : CAmount nFee;
# 177 : : int64_t nTime;
# 178 : : unsigned int nHeight;
# 179 : : bool spendsCoinbase;
# 180 : : unsigned int sigOpCost;
# 181 : : LockPoints lp;
# 182 : :
# 183 : : TestMemPoolEntryHelper() :
# 184 : : nFee(0), nTime(0), nHeight(1),
# 185 : 22 : spendsCoinbase(false), sigOpCost(4) { }
# 186 : :
# 187 : : CTxMemPoolEntry FromTx(const CMutableTransaction& tx) const;
# 188 : : CTxMemPoolEntry FromTx(const CTransactionRef& tx) const;
# 189 : :
# 190 : : // Change the default value
# 191 : 53582 : TestMemPoolEntryHelper &Fee(CAmount _fee) { nFee = _fee; return *this; }
# 192 : 53496 : TestMemPoolEntryHelper &Time(int64_t _time) { nTime = _time; return *this; }
# 193 : 49200 : TestMemPoolEntryHelper &Height(unsigned int _height) { nHeight = _height; return *this; }
# 194 : 4286 : TestMemPoolEntryHelper &SpendsCoinbase(bool _flag) { spendsCoinbase = _flag; return *this; }
# 195 : 2002 : TestMemPoolEntryHelper &SigOpsCost(unsigned int _sigopsCost) { sigOpCost = _sigopsCost; return *this; }
# 196 : : };
# 197 : :
# 198 : : CBlock getBlock13b8a();
# 199 : :
# 200 : : // define an implicit conversion here so that uint256 may be used directly in BOOST_CHECK_*
# 201 : : std::ostream& operator<<(std::ostream& os, const uint256& num);
# 202 : :
# 203 : : /**
# 204 : : * BOOST_CHECK_EXCEPTION predicates to check the specific validation error.
# 205 : : * Use as
# 206 : : * BOOST_CHECK_EXCEPTION(code that throws, exception type, HasReason("foo"));
# 207 : : */
# 208 : : class HasReason
# 209 : : {
# 210 : : public:
# 211 : 42 : explicit HasReason(const std::string& reason) : m_reason(reason) {}
# 212 : : bool operator()(const std::exception& e) const
# 213 : 42 : {
# 214 : 42 : return std::string(e.what()).find(m_reason) != std::string::npos;
# 215 : 42 : };
# 216 : :
# 217 : : private:
# 218 : : const std::string m_reason;
# 219 : : };
# 220 : :
# 221 : : #endif
|