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> // IWYU pragma: export
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> // IWYU pragma: export
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 : : std::ostream& operator<<(typename std::enable_if<std::is_enum<T>::value, std::ostream>::type& stream, const T& e)
39 : : {
40 : : 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 : : util::SignalInterrupt m_interrupt;
51 : : node::NodeContext m_node; // keep as first member to be destructed last
52 : :
53 : : explicit BasicTestingSetup(const ChainType chainType = ChainType::MAIN, const std::vector<const char*>& extra_args = {});
54 : : ~BasicTestingSetup();
55 : :
56 : : const fs::path m_path_root;
57 : : ArgsManager m_args;
58 : : };
59 : :
60 : : /** Testing setup that performs all steps up until right before
61 : : * ChainstateManager gets initialized. Meant for testing ChainstateManager
62 : : * initialization behaviour.
63 : : */
64 : : struct ChainTestingSetup : public BasicTestingSetup {
65 : : node::CacheSizes m_cache_sizes{};
66 : : bool m_coins_db_in_memory{true};
67 : : bool m_block_tree_db_in_memory{true};
68 : :
69 : : explicit ChainTestingSetup(const ChainType chainType = ChainType::MAIN, const std::vector<const char*>& extra_args = {});
70 : : ~ChainTestingSetup();
71 : :
72 : : // Supplies a chainstate, if one is needed
73 : : void LoadVerifyActivateChainstate();
74 : : };
75 : :
76 : : /** Testing setup that configures a complete environment.
77 : : */
78 : 0 : struct TestingSetup : public ChainTestingSetup {
79 : : explicit TestingSetup(
80 : : const ChainType chainType = ChainType::MAIN,
81 : : const std::vector<const char*>& extra_args = {},
82 : : const bool coins_db_in_memory = true,
83 : : const bool block_tree_db_in_memory = true);
84 : : };
85 : :
86 : : /** Identical to TestingSetup, but chain set to regtest */
87 : : struct RegTestingSetup : public TestingSetup {
88 : : RegTestingSetup()
89 : : : TestingSetup{ChainType::REGTEST} {}
90 : : };
91 : :
92 : : class CBlock;
93 : : struct CMutableTransaction;
94 : : class CScript;
95 : :
96 : : /**
97 : : * Testing fixture that pre-creates a 100-block REGTEST-mode block chain
98 : : */
99 : : struct TestChain100Setup : public TestingSetup {
100 : : TestChain100Setup(
101 : : const ChainType chain_type = ChainType::REGTEST,
102 : : const std::vector<const char*>& extra_args = {},
103 : : const bool coins_db_in_memory = true,
104 : : const bool block_tree_db_in_memory = true);
105 : :
106 : : /**
107 : : * Create a new block with just given transactions, coinbase paying to
108 : : * scriptPubKey, and try to add it to the current chain.
109 : : * If no chainstate is specified, default to the active.
110 : : */
111 : : CBlock CreateAndProcessBlock(const std::vector<CMutableTransaction>& txns,
112 : : const CScript& scriptPubKey,
113 : : Chainstate* chainstate = nullptr);
114 : :
115 : : /**
116 : : * Create a new block with just given transactions, coinbase paying to
117 : : * scriptPubKey.
118 : : */
119 : : CBlock CreateBlock(
120 : : const std::vector<CMutableTransaction>& txns,
121 : : const CScript& scriptPubKey,
122 : : Chainstate& chainstate);
123 : :
124 : : //! Mine a series of new blocks on the active chain.
125 : : void mineBlocks(int num_blocks);
126 : :
127 : : /**
128 : : * Create a transaction, optionally setting the fee based on the feerate.
129 : : * Note: The feerate may not be met exactly depending on whether the signatures can have different sizes.
130 : : *
131 : : * @param input_transactions The transactions to spend
132 : : * @param inputs Outpoints with which to construct transaction vin.
133 : : * @param input_height The height of the block that included the input transactions.
134 : : * @param input_signing_keys The keys to spend the input transactions.
135 : : * @param outputs Transaction vout.
136 : : * @param feerate The feerate the transaction should pay.
137 : : * @param fee_output The index of the output to take the fee from.
138 : : * @return The transaction and the fee it pays
139 : : */
140 : : std::pair<CMutableTransaction, CAmount> CreateValidTransaction(const std::vector<CTransactionRef>& input_transactions,
141 : : const std::vector<COutPoint>& inputs,
142 : : int input_height,
143 : : const std::vector<CKey>& input_signing_keys,
144 : : const std::vector<CTxOut>& outputs,
145 : : const std::optional<CFeeRate>& feerate,
146 : : const std::optional<uint32_t>& fee_output);
147 : : /**
148 : : * Create a transaction and, optionally, submit to the mempool.
149 : : *
150 : : * @param input_transactions The transactions to spend
151 : : * @param inputs Outpoints with which to construct transaction vin.
152 : : * @param input_height The height of the block that included the input transaction(s).
153 : : * @param input_signing_keys The keys to spend inputs.
154 : : * @param outputs Transaction vout.
155 : : * @param submit Whether or not to submit to mempool
156 : : */
157 : : CMutableTransaction CreateValidMempoolTransaction(const std::vector<CTransactionRef>& input_transactions,
158 : : const std::vector<COutPoint>& inputs,
159 : : int input_height,
160 : : const std::vector<CKey>& input_signing_keys,
161 : : const std::vector<CTxOut>& outputs,
162 : : bool submit = true);
163 : :
164 : : /**
165 : : * Create a 1-in-1-out transaction and, optionally, submit to the mempool.
166 : : *
167 : : * @param input_transaction The transaction to spend
168 : : * @param input_vout The vout to spend from the input_transaction
169 : : * @param input_height The height of the block that included the input_transaction
170 : : * @param input_signing_key The key to spend the input_transaction
171 : : * @param output_destination Where to send the output
172 : : * @param output_amount How much to send
173 : : * @param submit Whether or not to submit to mempool
174 : : */
175 : : CMutableTransaction CreateValidMempoolTransaction(CTransactionRef input_transaction,
176 : : uint32_t input_vout,
177 : : int input_height,
178 : : CKey input_signing_key,
179 : : CScript output_destination,
180 : : CAmount output_amount = CAmount(1 * COIN),
181 : : bool submit = true);
182 : :
183 : : /** Create transactions spending from m_coinbase_txns. These transactions will only spend coins
184 : : * that exist in the current chain, but may be premature coinbase spends, have missing
185 : : * signatures, or violate some other consensus rules. They should only be used for testing
186 : : * mempool consistency. All transactions will have some random number of inputs and outputs
187 : : * (between 1 and 24). Transactions may or may not be dependent upon each other; if dependencies
188 : : * exit, every parent will always be somewhere in the list before the child so each transaction
189 : : * can be submitted in the same order they appear in the list.
190 : : * @param[in] submit When true, submit transactions to the mempool.
191 : : * When false, return them but don't submit them.
192 : : * @returns A vector of transactions that can be submitted to the mempool.
193 : : */
194 : : std::vector<CTransactionRef> PopulateMempool(FastRandomContext& det_rand, size_t num_transactions, bool submit);
195 : :
196 : : /** Mock the mempool minimum feerate by adding a transaction and calling TrimToSize(0),
197 : : * simulating the mempool "reaching capacity" and evicting by descendant feerate. Note that
198 : : * this clears the mempool, and the new minimum feerate will depend on the maximum feerate of
199 : : * transactions removed, so this must be called while the mempool is empty.
200 : : *
201 : : * @param target_feerate The new mempool minimum feerate after this function returns.
202 : : * Must be above max(incremental feerate, min relay feerate),
203 : : * or 1sat/vB with default settings.
204 : : */
205 : : void MockMempoolMinFee(const CFeeRate& target_feerate);
206 : :
207 : : std::vector<CTransactionRef> m_coinbase_txns; // For convenience, coinbase transactions
208 : : CKey coinbaseKey; // private/public key needed to spend coinbase transactions
209 : : };
210 : :
211 : : /**
212 : : * Make a test setup that has disk access to the debug.log file disabled. Can
213 : : * be used in "hot loops", for example fuzzing or benchmarking.
214 : : */
215 : : template <class T = const BasicTestingSetup>
216 : 0 : std::unique_ptr<T> MakeNoLogFileContext(const ChainType chain_type = ChainType::REGTEST, const std::vector<const char*>& extra_args = {})
217 : : {
218 [ # # ]: 0 : const std::vector<const char*> arguments = Cat(
219 [ # # ]: 0 : {
220 : : "-nodebuglogfile",
221 : : "-nodebug",
222 : : },
223 : 0 : extra_args);
224 : :
225 [ # # ]: 0 : return std::make_unique<T>(chain_type, arguments);
226 : 0 : }
227 : :
228 : : CBlock getBlock13b8a();
229 : :
230 : : // define an implicit conversion here so that uint256 may be used directly in BOOST_CHECK_*
231 : : std::ostream& operator<<(std::ostream& os, const uint256& num);
232 : :
233 : : /**
234 : : * BOOST_CHECK_EXCEPTION predicates to check the specific validation error.
235 : : * Use as
236 : : * BOOST_CHECK_EXCEPTION(code that throws, exception type, HasReason("foo"));
237 : : */
238 : : class HasReason
239 : : {
240 : : public:
241 : : explicit HasReason(const std::string& reason) : m_reason(reason) {}
242 : : bool operator()(const std::exception& e) const
243 : : {
244 : : return std::string(e.what()).find(m_reason) != std::string::npos;
245 : : };
246 : :
247 : : private:
248 : : const std::string m_reason;
249 : : };
250 : :
251 : : #endif // BITCOIN_TEST_UTIL_SETUP_COMMON_H
|