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