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 <consensus/amount.h>
10 : : #include <kernel/caches.h>
11 : : #include <key.h>
12 : : #include <node/caches.h>
13 : : #include <node/context.h> // IWYU pragma: export
14 : : #include <primitives/transaction.h>
15 : : #include <random.h>
16 : : #include <test/util/random.h>
17 : : #include <util/chaintype.h> // IWYU pragma: export
18 : : #include <util/fs.h>
19 : : #include <util/signalinterrupt.h>
20 : : #include <util/vector.h>
21 : :
22 : : #include <cstddef>
23 : : #include <cstdint>
24 : : #include <functional>
25 : : #include <memory>
26 : : #include <optional>
27 : : #include <string>
28 : : #include <utility>
29 : : #include <vector>
30 : :
31 : : class CFeeRate;
32 : :
33 : : /** Retrieve the command line arguments. */
34 : : extern const std::function<std::vector<const char*>()> G_TEST_COMMAND_LINE_ARGUMENTS;
35 : :
36 : : /** Retrieve the unit test name. */
37 : : extern const std::function<std::string()> G_TEST_GET_FULL_NAME;
38 : :
39 : : static constexpr CAmount CENT{1000000};
40 : :
41 : : /** Register common test args. Shared across binaries that rely on the test framework. */
42 : : void SetupCommonTestArgs(ArgsManager& argsman);
43 : :
44 [ + - + - : 789 : struct TestOpts {
+ - + - ]
[ + + ]
45 : : std::vector<const char*> extra_args{};
46 : : bool coins_db_in_memory{true};
47 : : bool block_tree_db_in_memory{true};
48 : : bool setup_net{true};
49 : : bool setup_validation_interface{true};
50 : : bool min_validation_cache{false}; // Equivalent of -maxsigcachebytes=0
51 : : };
52 : :
53 : : /** Basic testing setup.
54 : : * This just configures logging, data dir and chain parameters.
55 : : */
56 : : struct BasicTestingSetup {
57 : : util::SignalInterrupt m_interrupt;
58 : : node::NodeContext m_node; // keep as first member to be destructed last
59 : :
60 : : FastRandomContext m_rng;
61 : : /** Seed the global RNG state and m_rng for testing and log the seed value. This affects all randomness, except GetStrongRandBytes(). */
62 : 695 : void SeedRandomForTest(SeedRand seed)
63 : : {
64 : 695 : SeedRandomStateForTest(seed);
65 : 695 : m_rng.Reseed(GetRandHash());
66 : 695 : }
67 : :
68 : : explicit BasicTestingSetup(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 : : /** @brief Test-specific arguments and settings.
75 : : *
76 : : * This member is intended to be the primary source of settings for code
77 : : * being tested by unit tests. It exists to make tests more self-contained
78 : : * and reduce reliance on global state.
79 : : *
80 : : * Usage guidelines:
81 : : * 1. Prefer using m_args where possible in test code.
82 : : * 2. If m_args is not accessible, use m_node.args as a fallback.
83 : : * 3. Avoid direct references to gArgs in test code.
84 : : *
85 : : * Note: Currently, m_node.args points to gArgs for backwards
86 : : * compatibility. In the future, it will point to m_args to further isolate
87 : : * test environments.
88 : : *
89 : : * @see https://github.com/bitcoin/bitcoin/issues/25055 for additional context.
90 : : */
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 : : kernel::CacheSizes m_kernel_cache_sizes{node::CalculateCacheSizes(m_args).kernel};
100 : : bool m_coins_db_in_memory{true};
101 : : bool m_block_tree_db_in_memory{true};
102 : : std::function<void()> m_make_chainman{};
103 : :
104 : : explicit ChainTestingSetup(ChainType chainType = ChainType::MAIN, TestOpts = {});
105 : : ~ChainTestingSetup();
106 : :
107 : : // Supplies a chainstate, if one is needed
108 : : void LoadVerifyActivateChainstate();
109 : : };
110 : :
111 : : /** Testing setup that configures a complete environment.
112 : : */
113 : 124 : struct TestingSetup : public ChainTestingSetup {
114 : : explicit TestingSetup(
115 : : ChainType chainType = ChainType::MAIN,
116 : : TestOpts = {});
117 : : };
118 : :
119 : : /** Identical to TestingSetup, but chain set to regtest */
120 : 34 : struct RegTestingSetup : public TestingSetup {
121 : 34 : RegTestingSetup()
122 [ + - ]: 68 : : TestingSetup{ChainType::REGTEST} {}
123 : : };
124 : :
125 : : /** Identical to TestingSetup, but chain set to testnet4 */
126 : 1 : struct Testnet4Setup : public TestingSetup {
127 : 1 : Testnet4Setup()
128 [ + - ]: 2 : : TestingSetup{ChainType::TESTNET4} {}
129 : : };
130 : :
131 : : class CBlock;
132 : : class CScript;
133 : :
134 : : /**
135 : : * Testing fixture that pre-creates a 100-block REGTEST-mode block chain
136 : : */
137 : : struct TestChain100Setup : public TestingSetup {
138 : : TestChain100Setup(
139 : : ChainType chain_type = ChainType::REGTEST,
140 : : TestOpts = {});
141 : :
142 : : /**
143 : : * Create a new block with just given transactions, coinbase paying to
144 : : * scriptPubKey, and try to add it to the current chain.
145 : : */
146 : : CBlock CreateAndProcessBlock(const std::vector<CMutableTransaction>& txns,
147 : : const CScript& scriptPubKey);
148 : :
149 : : /**
150 : : * Create a new block with just given transactions, coinbase paying to
151 : : * scriptPubKey.
152 : : */
153 : : CBlock CreateBlock(
154 : : const std::vector<CMutableTransaction>& txns,
155 : : const CScript& scriptPubKey);
156 : :
157 : : //! Mine a series of new blocks on the active chain.
158 : : void mineBlocks(int num_blocks);
159 : :
160 : : /**
161 : : * Create a transaction, optionally setting the fee based on the feerate.
162 : : * Note: The feerate may not be met exactly depending on whether the signatures can have different sizes.
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 transactions.
167 : : * @param input_signing_keys The keys to spend the input transactions.
168 : : * @param outputs Transaction vout.
169 : : * @param feerate The feerate the transaction should pay.
170 : : * @param fee_output The index of the output to take the fee from.
171 : : * @return The transaction and the fee it pays
172 : : */
173 : : std::pair<CMutableTransaction, CAmount> CreateValidTransaction(const std::vector<CTransactionRef>& input_transactions,
174 : : const std::vector<COutPoint>& inputs,
175 : : int input_height,
176 : : const std::vector<CKey>& input_signing_keys,
177 : : const std::vector<CTxOut>& outputs,
178 : : const std::optional<CFeeRate>& feerate,
179 : : const std::optional<uint32_t>& fee_output);
180 : : /**
181 : : * Create a transaction and, optionally, submit to the mempool.
182 : : *
183 : : * @param input_transactions The transactions to spend
184 : : * @param inputs Outpoints with which to construct transaction vin.
185 : : * @param input_height The height of the block that included the input transaction(s).
186 : : * @param input_signing_keys The keys to spend inputs.
187 : : * @param outputs Transaction vout.
188 : : * @param submit Whether or not to submit to mempool
189 : : */
190 : : CMutableTransaction CreateValidMempoolTransaction(const std::vector<CTransactionRef>& input_transactions,
191 : : const std::vector<COutPoint>& inputs,
192 : : int input_height,
193 : : const std::vector<CKey>& input_signing_keys,
194 : : const std::vector<CTxOut>& outputs,
195 : : bool submit = true);
196 : :
197 : : /**
198 : : * Create a 1-in-1-out transaction and, optionally, submit to the mempool.
199 : : *
200 : : * @param input_transaction The transaction to spend
201 : : * @param input_vout The vout to spend from the input_transaction
202 : : * @param input_height The height of the block that included the input_transaction
203 : : * @param input_signing_key The key to spend the input_transaction
204 : : * @param output_destination Where to send the output
205 : : * @param output_amount How much to send
206 : : * @param submit Whether or not to submit to mempool
207 : : */
208 : : CMutableTransaction CreateValidMempoolTransaction(CTransactionRef input_transaction,
209 : : uint32_t input_vout,
210 : : int input_height,
211 : : CKey input_signing_key,
212 : : CScript output_destination,
213 : : CAmount output_amount = CAmount(1 * COIN),
214 : : bool submit = true);
215 : :
216 : : /** Create transactions spending from m_coinbase_txns. These transactions will only spend coins
217 : : * that exist in the current chain, but may be premature coinbase spends, have missing
218 : : * signatures, or violate some other consensus rules. They should only be used for testing
219 : : * mempool consistency. All transactions will have some random number of inputs and outputs
220 : : * (between 1 and 24). Transactions may or may not be dependent upon each other; if dependencies
221 : : * exit, every parent will always be somewhere in the list before the child so each transaction
222 : : * can be submitted in the same order they appear in the list.
223 : : * @param[in] submit When true, submit transactions to the mempool.
224 : : * When false, return them but don't submit them.
225 : : * @returns A vector of transactions that can be submitted to the mempool.
226 : : */
227 : : std::vector<CTransactionRef> PopulateMempool(FastRandomContext& det_rand, size_t num_transactions, bool submit);
228 : :
229 : : std::vector<CTransactionRef> m_coinbase_txns; // For convenience, coinbase transactions
230 : : CKey coinbaseKey; // private/public key needed to spend coinbase transactions
231 : : };
232 : :
233 : : /**
234 : : * Make a test setup that has disk access to the debug.log file disabled. Can
235 : : * be used in "hot loops", for example fuzzing or benchmarking.
236 : : */
237 : : template <class T = const BasicTestingSetup>
238 : : std::unique_ptr<T> MakeNoLogFileContext(const ChainType chain_type = ChainType::REGTEST, TestOpts opts = {})
239 : : {
240 : : opts.extra_args = Cat(
241 : : {
242 : : "-nodebuglogfile",
243 : : "-nodebug",
244 : : },
245 : : opts.extra_args);
246 : :
247 : : return std::make_unique<T>(chain_type, opts);
248 : : }
249 : :
250 : : CBlock getBlock13b8a();
251 : :
252 : : #endif // BITCOIN_TEST_UTIL_SETUP_COMMON_H
|