LCOV - code coverage report
Current view: top level - src/test/util - setup_common.h (source / functions) Hit Total Coverage
Test: fuzz_coverage.info Lines: 6 14 42.9 %
Date: 2023-10-05 12:38:51 Functions: 2 37 5.4 %
Branches: 3 6 50.0 %

           Branch data     Line data    Source code
       1                 :            : // Copyright (c) 2015-2022 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 <common/args.h>
       9                 :            : #include <key.h>
      10                 :            : #include <node/caches.h>
      11                 :            : #include <node/context.h> // IWYU pragma: export
      12                 :            : #include <primitives/transaction.h>
      13                 :            : #include <pubkey.h>
      14                 :            : #include <stdexcept>
      15                 :            : #include <util/chaintype.h>
      16                 :            : #include <util/check.h>
      17                 :            : #include <util/fs.h>
      18                 :            : #include <util/string.h>
      19                 :            : #include <util/vector.h>
      20                 :            : 
      21                 :            : #include <functional>
      22                 :            : #include <type_traits>
      23                 :            : #include <vector>
      24                 :            : 
      25                 :            : class CFeeRate;
      26                 :            : class Chainstate;
      27                 :            : class FastRandomContext;
      28                 :            : 
      29                 :            : /** This is connected to the logger. Can be used to redirect logs to any other log */
      30                 :            : extern const std::function<void(const std::string&)> G_TEST_LOG_FUN;
      31                 :            : 
      32                 :            : /** Retrieve the command line arguments. */
      33                 :            : extern const std::function<std::vector<const char*>()> G_TEST_COMMAND_LINE_ARGUMENTS;
      34                 :            : 
      35                 :            : // Enable BOOST_CHECK_EQUAL for enum class types
      36                 :            : namespace std {
      37                 :            : template <typename T>
      38                 :          0 : std::ostream& operator<<(typename std::enable_if<std::is_enum<T>::value, std::ostream>::type& stream, const T& e)
      39                 :            : {
      40                 :          0 :     return stream << static_cast<typename std::underlying_type<T>::type>(e);
      41                 :            : }
      42                 :            : } // namespace std
      43                 :            : 
      44                 :            : static constexpr CAmount CENT{1000000};
      45                 :            : 
      46                 :            : /** Basic testing setup.
      47                 :            :  * This just configures logging, data dir and chain parameters.
      48                 :            :  */
      49                 :            : struct BasicTestingSetup {
      50                 :            :     node::NodeContext m_node; // keep as first member to be destructed last
      51                 :            : 
      52                 :            :     explicit BasicTestingSetup(const ChainType chainType = ChainType::MAIN, const std::vector<const char*>& extra_args = {});
      53                 :            :     ~BasicTestingSetup();
      54                 :            : 
      55                 :            :     const fs::path m_path_root;
      56                 :            :     ArgsManager m_args;
      57                 :            : };
      58                 :            : 
      59                 :            : /** Testing setup that performs all steps up until right before
      60                 :            :  * ChainstateManager gets initialized. Meant for testing ChainstateManager
      61                 :            :  * initialization behaviour.
      62                 :            :  */
      63                 :            : struct ChainTestingSetup : public BasicTestingSetup {
      64                 :            :     node::CacheSizes m_cache_sizes{};
      65                 :            :     bool m_coins_db_in_memory{true};
      66                 :            :     bool m_block_tree_db_in_memory{true};
      67                 :            : 
      68                 :            :     explicit ChainTestingSetup(const ChainType chainType = ChainType::MAIN, const std::vector<const char*>& extra_args = {});
      69                 :            :     ~ChainTestingSetup();
      70                 :            : 
      71                 :            :     // Supplies a chainstate, if one is needed
      72                 :            :     void LoadVerifyActivateChainstate();
      73                 :            : };
      74                 :            : 
      75                 :            : /** Testing setup that configures a complete environment.
      76                 :            :  */
      77                 :            : struct TestingSetup : public ChainTestingSetup {
      78                 :            :     explicit TestingSetup(
      79                 :            :         const ChainType chainType = ChainType::MAIN,
      80                 :            :         const std::vector<const char*>& extra_args = {},
      81                 :            :         const bool coins_db_in_memory = true,
      82                 :            :         const bool block_tree_db_in_memory = true);
      83                 :            : };
      84                 :            : 
      85                 :            : /** Identical to TestingSetup, but chain set to regtest */
      86                 :            : struct RegTestingSetup : public TestingSetup {
      87                 :          0 :     RegTestingSetup()
      88                 :          0 :         : TestingSetup{ChainType::REGTEST} {}
      89                 :            : };
      90                 :            : 
      91                 :            : class CBlock;
      92                 :            : struct CMutableTransaction;
      93                 :            : class CScript;
      94                 :            : 
      95                 :            : /**
      96                 :            :  * Testing fixture that pre-creates a 100-block REGTEST-mode block chain
      97                 :            :  */
      98                 :            : struct TestChain100Setup : public TestingSetup {
      99                 :            :     TestChain100Setup(
     100                 :            :         const ChainType chain_type = ChainType::REGTEST,
     101                 :            :         const std::vector<const char*>& extra_args = {},
     102                 :            :         const bool coins_db_in_memory = true,
     103                 :            :         const bool block_tree_db_in_memory = true);
     104                 :            : 
     105                 :            :     /**
     106                 :            :      * Create a new block with just given transactions, coinbase paying to
     107                 :            :      * scriptPubKey, and try to add it to the current chain.
     108                 :            :      * If no chainstate is specified, default to the active.
     109                 :            :      */
     110                 :            :     CBlock CreateAndProcessBlock(const std::vector<CMutableTransaction>& txns,
     111                 :            :                                  const CScript& scriptPubKey,
     112                 :            :                                  Chainstate* chainstate = nullptr);
     113                 :            : 
     114                 :            :     /**
     115                 :            :      * Create a new block with just given transactions, coinbase paying to
     116                 :            :      * scriptPubKey.
     117                 :            :      */
     118                 :            :     CBlock CreateBlock(
     119                 :            :         const std::vector<CMutableTransaction>& txns,
     120                 :            :         const CScript& scriptPubKey,
     121                 :            :         Chainstate& chainstate);
     122                 :            : 
     123                 :            :     //! Mine a series of new blocks on the active chain.
     124                 :            :     void mineBlocks(int num_blocks);
     125                 :            : 
     126                 :            :     /**
     127                 :            :      * Create a transaction and submit to the mempool.
     128                 :            :      *
     129                 :            :      * @param input_transaction  The transaction to spend
     130                 :            :      * @param input_vout         The vout to spend from the input_transaction
     131                 :            :      * @param input_height       The height of the block that included the input_transaction
     132                 :            :      * @param input_signing_key  The key to spend the input_transaction
     133                 :            :      * @param output_destination Where to send the output
     134                 :            :      * @param output_amount      How much to send
     135                 :            :      * @param submit             Whether or not to submit to mempool
     136                 :            :      */
     137                 :            :     CMutableTransaction CreateValidMempoolTransaction(CTransactionRef input_transaction,
     138                 :            :                                                       int input_vout,
     139                 :            :                                                       int input_height,
     140                 :            :                                                       CKey input_signing_key,
     141                 :            :                                                       CScript output_destination,
     142                 :            :                                                       CAmount output_amount = CAmount(1 * COIN),
     143                 :            :                                                       bool submit = true);
     144                 :            : 
     145                 :            :     /** Create transactions spending from m_coinbase_txns. These transactions will only spend coins
     146                 :            :      * that exist in the current chain, but may be premature coinbase spends, have missing
     147                 :            :      * signatures, or violate some other consensus rules. They should only be used for testing
     148                 :            :      * mempool consistency. All transactions will have some random number of inputs and outputs
     149                 :            :      * (between 1 and 24). Transactions may or may not be dependent upon each other; if dependencies
     150                 :            :      * exit, every parent will always be somewhere in the list before the child so each transaction
     151                 :            :      * can be submitted in the same order they appear in the list.
     152                 :            :      * @param[in]   submit      When true, submit transactions to the mempool.
     153                 :            :      *                          When false, return them but don't submit them.
     154                 :            :      * @returns A vector of transactions that can be submitted to the mempool.
     155                 :            :      */
     156                 :            :     std::vector<CTransactionRef> PopulateMempool(FastRandomContext& det_rand, size_t num_transactions, bool submit);
     157                 :            : 
     158                 :            :     /** Mock the mempool minimum feerate by adding a transaction and calling TrimToSize(0),
     159                 :            :      * simulating the mempool "reaching capacity" and evicting by descendant feerate.  Note that
     160                 :            :      * this clears the mempool, and the new minimum feerate will depend on the maximum feerate of
     161                 :            :      * transactions removed, so this must be called while the mempool is empty.
     162                 :            :      *
     163                 :            :      * @param target_feerate    The new mempool minimum feerate after this function returns.
     164                 :            :      *                          Must be above max(incremental feerate, min relay feerate),
     165                 :            :      *                          or 1sat/vB with default settings.
     166                 :            :      */
     167                 :            :     void MockMempoolMinFee(const CFeeRate& target_feerate);
     168                 :            : 
     169                 :            :     std::vector<CTransactionRef> m_coinbase_txns; // For convenience, coinbase transactions
     170                 :            :     CKey coinbaseKey; // private/public key needed to spend coinbase transactions
     171                 :            : };
     172                 :            : 
     173                 :            : /**
     174                 :            :  * Make a test setup that has disk access to the debug.log file disabled. Can
     175                 :            :  * be used in "hot loops", for example fuzzing or benchmarking.
     176                 :            :  */
     177                 :            : template <class T = const BasicTestingSetup>
     178                 :          1 : std::unique_ptr<T> MakeNoLogFileContext(const ChainType chain_type = ChainType::REGTEST, const std::vector<const char*>& extra_args = {})
     179                 :            : {
     180         [ +  - ]:          1 :     const std::vector<const char*> arguments = Cat(
     181         [ +  - ]:          1 :         {
     182                 :            :             "-nodebuglogfile",
     183                 :            :             "-nodebug",
     184                 :            :         },
     185                 :          1 :         extra_args);
     186                 :            : 
     187         [ -  + ]:          1 :     return std::make_unique<T>(chain_type, arguments);
     188                 :          1 : }
     189                 :            : 
     190                 :            : CBlock getBlock13b8a();
     191                 :            : 
     192                 :            : // define an implicit conversion here so that uint256 may be used directly in BOOST_CHECK_*
     193                 :            : std::ostream& operator<<(std::ostream& os, const uint256& num);
     194                 :            : 
     195                 :            : /**
     196                 :            :  * BOOST_CHECK_EXCEPTION predicates to check the specific validation error.
     197                 :            :  * Use as
     198                 :            :  * BOOST_CHECK_EXCEPTION(code that throws, exception type, HasReason("foo"));
     199                 :            :  */
     200                 :            : class HasReason
     201                 :            : {
     202                 :            : public:
     203                 :          0 :     explicit HasReason(const std::string& reason) : m_reason(reason) {}
     204                 :          0 :     bool operator()(const std::exception& e) const
     205                 :            :     {
     206                 :          0 :         return std::string(e.what()).find(m_reason) != std::string::npos;
     207                 :          0 :     };
     208                 :            : 
     209                 :            : private:
     210                 :            :     const std::string m_reason;
     211                 :            : };
     212                 :            : 
     213                 :            : #endif // BITCOIN_TEST_UTIL_SETUP_COMMON_H

Generated by: LCOV version 1.14