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