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