LCOV - code coverage report
Current view: top level - src/test/util - setup_common.h (source / functions) Hit Total Coverage
Test: total_coverage.info Lines: 22 24 91.7 %
Date: 2020-09-26 01:30:44 Functions: 15 27 55.6 %

          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 <node/context.h>
      12             : #include <pubkey.h>
      13             : #include <random.h>
      14             : #include <txmempool.h>
      15             : #include <util/check.h>
      16             : #include <util/string.h>
      17             : 
      18             : #include <type_traits>
      19             : 
      20             : #include <boost/thread/thread.hpp>
      21             : 
      22             : /** This is connected to the logger. Can be used to redirect logs to any other log */
      23             : extern const std::function<void(const std::string&)> G_TEST_LOG_FUN;
      24             : 
      25             : // Enable BOOST_CHECK_EQUAL for enum class types
      26             : template <typename T>
      27           0 : std::ostream& operator<<(typename std::enable_if<std::is_enum<T>::value, std::ostream>::type& stream, const T& e)
      28             : {
      29           0 :     return stream << static_cast<typename std::underlying_type<T>::type>(e);
      30             : }
      31             : 
      32             : /**
      33             :  * This global and the helpers that use it are not thread-safe.
      34             :  *
      35             :  * If thread-safety is needed, the global could be made thread_local (given
      36             :  * that thread_local is supported on all architectures we support) or a
      37             :  * per-thread instance could be used in the multi-threaded test.
      38             :  */
      39             : extern FastRandomContext g_insecure_rand_ctx;
      40             : 
      41             : /**
      42             :  * Flag to make GetRand in random.h return the same number
      43             :  */
      44             : extern bool g_mock_deterministic_tests;
      45             : 
      46             : enum class SeedRand {
      47             :     ZEROS, //!< Seed with a compile time constant of zeros
      48             :     SEED,  //!< Call the Seed() helper
      49             : };
      50             : 
      51             : /** Seed the given random ctx or use the seed passed in via an environment var */
      52             : void Seed(FastRandomContext& ctx);
      53             : 
      54         539 : static inline void SeedInsecureRand(SeedRand seed = SeedRand::SEED)
      55             : {
      56         539 :     if (seed == SeedRand::ZEROS) {
      57          14 :         g_insecure_rand_ctx = FastRandomContext(/* deterministic */ true);
      58          14 :     } else {
      59         525 :         Seed(g_insecure_rand_ctx);
      60             :     }
      61         539 : }
      62             : 
      63    16656389 : static inline uint32_t InsecureRand32() { return g_insecure_rand_ctx.rand32(); }
      64      598747 : static inline uint256 InsecureRand256() { return g_insecure_rand_ctx.rand256(); }
      65     2821955 : static inline uint64_t InsecureRandBits(int bits) { return g_insecure_rand_ctx.randbits(bits); }
      66     5749031 : static inline uint64_t InsecureRandRange(uint64_t range) { return g_insecure_rand_ctx.randrange(range); }
      67      541458 : static inline bool InsecureRandBool() { return g_insecure_rand_ctx.randbool(); }
      68             : 
      69             : static constexpr CAmount CENT{1000000};
      70             : 
      71             : /** Basic testing setup.
      72             :  * This just configures logging, data dir and chain parameters.
      73             :  */
      74             : struct BasicTestingSetup {
      75             :     ECCVerifyHandle globalVerifyHandle;
      76             :     NodeContext m_node;
      77             : 
      78             :     explicit BasicTestingSetup(const std::string& chainName = CBaseChainParams::MAIN, const std::vector<const char*>& extra_args = {});
      79             :     ~BasicTestingSetup();
      80             : 
      81             : private:
      82             :     const fs::path m_path_root;
      83             : };
      84             : 
      85             : /** Testing setup that configures a complete environment.
      86             :  * Included are coins database, script check threads setup.
      87             :  */
      88             : struct TestingSetup : public BasicTestingSetup {
      89             :     boost::thread_group threadGroup;
      90             : 
      91             :     explicit TestingSetup(const std::string& chainName = CBaseChainParams::MAIN, const std::vector<const char*>& extra_args = {});
      92             :     ~TestingSetup();
      93             : };
      94             : 
      95             : /** Identical to TestingSetup, but chain set to regtest */
      96          33 : struct RegTestingSetup : public TestingSetup {
      97          33 :     RegTestingSetup()
      98          33 :         : TestingSetup{CBaseChainParams::REGTEST} {}
      99             : };
     100             : 
     101             : class CBlock;
     102             : struct CMutableTransaction;
     103             : class CScript;
     104             : 
     105             : /**
     106             :  * Testing fixture that pre-creates a 100-block REGTEST-mode block chain
     107             :  */
     108             : struct TestChain100Setup : public RegTestingSetup {
     109             :     TestChain100Setup();
     110             : 
     111             :     /**
     112             :      * Create a new block with just given transactions, coinbase paying to
     113             :      * scriptPubKey, and try to add it to the current chain.
     114             :      */
     115             :     CBlock CreateAndProcessBlock(const std::vector<CMutableTransaction>& txns,
     116             :                                  const CScript& scriptPubKey);
     117             : 
     118             :     ~TestChain100Setup();
     119             : 
     120             :     std::vector<CTransactionRef> m_coinbase_txns; // For convenience, coinbase transactions
     121             :     CKey coinbaseKey; // private/public key needed to spend coinbase transactions
     122             : };
     123             : 
     124             : class CTxMemPoolEntry;
     125             : 
     126             : struct TestMemPoolEntryHelper
     127             : {
     128             :     // Default values
     129             :     CAmount nFee;
     130             :     int64_t nTime;
     131             :     unsigned int nHeight;
     132             :     bool spendsCoinbase;
     133             :     unsigned int sigOpCost;
     134             :     LockPoints lp;
     135             : 
     136          22 :     TestMemPoolEntryHelper() :
     137          11 :         nFee(0), nTime(0), nHeight(1),
     138          33 :         spendsCoinbase(false), sigOpCost(4) { }
     139             : 
     140             :     CTxMemPoolEntry FromTx(const CMutableTransaction& tx);
     141             :     CTxMemPoolEntry FromTx(const CTransactionRef& tx);
     142             : 
     143             :     // Change the default value
     144       26791 :     TestMemPoolEntryHelper &Fee(CAmount _fee) { nFee = _fee; return *this; }
     145       26748 :     TestMemPoolEntryHelper &Time(int64_t _time) { nTime = _time; return *this; }
     146       24600 :     TestMemPoolEntryHelper &Height(unsigned int _height) { nHeight = _height; return *this; }
     147        2143 :     TestMemPoolEntryHelper &SpendsCoinbase(bool _flag) { spendsCoinbase = _flag; return *this; }
     148        1001 :     TestMemPoolEntryHelper &SigOpsCost(unsigned int _sigopsCost) { sigOpCost = _sigopsCost; return *this; }
     149             : };
     150             : 
     151             : CBlock getBlock13b8a();
     152             : 
     153             : // define an implicit conversion here so that uint256 may be used directly in BOOST_CHECK_*
     154             : std::ostream& operator<<(std::ostream& os, const uint256& num);
     155             : 
     156             : #endif

Generated by: LCOV version 1.15