Branch data Line data Source code
1 : : // Copyright (c) 2026-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 : : #include <addresstype.h>
6 : : #include <chain.h>
7 : : #include <consensus/amount.h>
8 : : #include <consensus/merkle.h>
9 : : #include <node/block_template_manager.h>
10 : : #include <node/kernel_notifications.h>
11 : : #include <node/mining_types.h>
12 : : #include <primitives/block.h>
13 : : #include <primitives/transaction.h>
14 : : #include <pubkey.h>
15 : : #include <script/interpreter.h>
16 : : #include <script/script.h>
17 : : #include <sync.h>
18 : : #include <test/fuzz/FuzzedDataProvider.h>
19 : : #include <test/fuzz/fuzz.h>
20 : : #include <test/fuzz/util.h>
21 : : #include <test/util/mining.h>
22 : : #include <test/util/script.h>
23 : : #include <test/util/setup_common.h>
24 : : #include <test/util/time.h>
25 : : #include <txmempool.h>
26 : : #include <uint256.h>
27 : : #include <util/check.h>
28 : : #include <validation.h>
29 : : #include <validationinterface.h>
30 : :
31 : : #include <algorithm>
32 : : #include <cstdint>
33 : : #include <memory>
34 : : #include <utility>
35 : : #include <vector>
36 : :
37 : :
38 : : namespace {
39 : :
40 : : TestingSetup* g_setup;
41 : :
42 : : /** Vector of blocks to keep references to blocks (to enable fuzzing input to pick one to build upon) */
43 : : static std::vector<std::shared_ptr<CBlock>> g_blocks;
44 : : /** CTxIns for spending outputs, which can be unspent, already spent, or an immature coinbase. */
45 : : static std::vector<CTxIn> g_spend_candidate_txins;
46 : : /** Static P2SH_OP_TRUE script */
47 : : static const CScript P2SH_OP_TRUE = CScript() << OP_HASH160 << ToByteVector(ScriptHash(CScript() << OP_TRUE)) << OP_EQUAL;
48 : : /** Static P2SH_OP_TRUE unlock script */
49 : : static const CScript P2SH_OP_TRUE_UNLOCK = CScript() << MakeUCharSpan(CScript() << OP_TRUE);
50 : : /** Static TAPROOT_OP_TRUE script and its witness */
51 : : static CScript TAPROOT_OP_TRUE;
52 : : static std::vector<std::vector<uint8_t>> TAPROOT_OP_TRUE_WITNESS;
53 : :
54 : : /**
55 : : * Initialize TAPROOT_OP_TRUE and TAPROOT_OP_TRUE_WITNESS static variables.
56 : : */
57 : 1 : static void InitTaprootScript()
58 : : {
59 [ + - + - ]: 1 : uint256 merkle_tree_hash = ComputeTapleafHash(TAPROOT_LEAF_TAPSCRIPT, MakeUCharSpan(CScript() << OP_TRUE));
60 : 2 : uint256 internal_key{std::vector<uint8_t>(32, 1)};
61 : 1 : auto res = XOnlyPubKey(internal_key).CreateTapTweak(&merkle_tree_hash);
62 : 1 : Assert(res.has_value());
63 : 1 : auto control = ToByteVector(internal_key);
64 [ - + + - ]: 1 : control.insert(control.begin(), TAPROOT_LEAF_TAPSCRIPT | (res->second ? 1 : 0));
65 : :
66 [ + - + - ]: 2 : TAPROOT_OP_TRUE = CScript() << OP_1 << ToByteVector(res->first);
67 : 1 : TAPROOT_OP_TRUE_WITNESS.clear();
68 [ + - + - : 2 : TAPROOT_OP_TRUE_WITNESS.emplace_back(ToByteVector(CScript() << OP_TRUE));
+ - ]
69 [ + - ]: 1 : TAPROOT_OP_TRUE_WITNESS.emplace_back(std::move(control));
70 : 1 : }
71 : :
72 : : /**
73 : : * Given a transaction and an output index, create a CTxIn that can be
74 : : * used to spend it.
75 : : */
76 : 5481 : static CTxIn GetSpendingScript(const CTransaction& tx, uint32_t vout_index)
77 : : {
78 [ - + ]: 5481 : Assert(vout_index < tx.vout.size());
79 [ + - ]: 5481 : const CTxOut& output = tx.vout[vout_index];
80 : :
81 [ + - ]: 5481 : CTxIn res{COutPoint(tx.GetHash(), vout_index)};
82 [ + + ]: 5481 : if (output.scriptPubKey == P2WSH_OP_TRUE) {
83 : 1935 : res.scriptSig = CScript();
84 [ + - ]: 1935 : res.scriptWitness.stack.push_back(WITNESS_STACK_ELEM_OP_TRUE);
85 [ + + ]: 3546 : } else if (output.scriptPubKey == P2SH_OP_TRUE) {
86 : 804 : res.scriptSig = P2SH_OP_TRUE_UNLOCK;
87 [ + + ]: 2742 : } else if (output.scriptPubKey == CScript()) {
88 [ + - ]: 1075 : res.scriptSig = CScript() << OP_TRUE;
89 [ + + ]: 1667 : } else if (output.scriptPubKey == TAPROOT_OP_TRUE) {
90 : 745 : res.scriptSig = CScript();
91 [ + - ]: 745 : res.scriptWitness.stack = TAPROOT_OP_TRUE_WITNESS;
92 : : }
93 : :
94 : 5481 : return res;
95 : 0 : }
96 : :
97 : : /**
98 : : * Add a spend candidate CTxIn unless the output is unspendable.
99 : : */
100 : 5813 : static void MaybeAddSpendCandidate(std::vector<CTxIn>& pool, const CTransaction& tx, uint32_t vout_index)
101 : : {
102 [ - + ]: 5813 : Assert(vout_index < tx.vout.size());
103 [ + + ]: 5813 : if (tx.vout[vout_index].scriptPubKey.IsUnspendable()) return;
104 [ + - ]: 10962 : pool.push_back(GetSpendingScript(tx, vout_index));
105 : : }
106 : :
107 : :
108 : : /**
109 : : * Read the block from the BlockManager and add it to g_blocks.
110 : : */
111 : 202 : static void LoadCurrentBlock(Chainstate& chainstate, CBlockIndex* current_block)
112 : : {
113 : : // Read the block from the BlockManager.
114 : 202 : Assert(current_block->nHeight >= 0);
115 : : // Resize g_blocks if needed.
116 [ - + + + ]: 202 : if (g_blocks.size() <= (size_t)current_block->nHeight) {
117 : 2 : g_blocks.resize(current_block->nHeight + 1);
118 : : }
119 : :
120 [ - + ]: 202 : g_blocks[current_block->nHeight] = std::make_shared<CBlock>();
121 : 202 : Assert(chainstate.m_blockman.ReadBlock(*g_blocks[current_block->nHeight], *current_block));
122 : :
123 : : // Iterate all transaction outputs.
124 [ + + ]: 414 : for (const auto& tx : g_blocks[current_block->nHeight]->vtx) {
125 [ - + + + ]: 655 : for (uint32_t vout_index{0}; vout_index < tx->vout.size(); ++vout_index) {
126 : 443 : MaybeAddSpendCandidate(g_spend_candidate_txins, *tx, vout_index);
127 : : }
128 : : }
129 : 202 : }
130 : :
131 : : /**
132 : : * Read the Chainstate object into g_blocks.
133 : : * Then fill g_spend_candidate_txins with inputs that can be tried by the target.
134 : : */
135 : 1 : static void LoadCurrentChain()
136 : : {
137 : : // Clear existing data.
138 : 1 : g_blocks.clear();
139 : 1 : g_spend_candidate_txins.clear();
140 : :
141 : 1 : {
142 : 1 : LOCK(::cs_main);
143 : : // Retrieve the current chainstate.
144 [ + - + - ]: 1 : auto& chainstate = Assert(g_setup->m_node.chainman)->ActiveChainstate();
145 : : // Make sure it contains a valid mempool.
146 [ + - ]: 1 : Assert(chainstate.GetMempool());
147 : :
148 : : // Traverse the chain from tip to genesis.
149 [ - + ]: 1 : auto current_block = chainstate.m_chain.Tip();
150 : :
151 [ + + ]: 202 : while (current_block != nullptr) {
152 [ + - ]: 201 : LoadCurrentBlock(chainstate, current_block);
153 : : // Move to previous block.
154 : 201 : current_block = current_block->pprev;
155 : : }
156 : 1 : }
157 : :
158 : : // Reverse the order of g_spend_candidate_txins to have them in ascending order of
159 : : // block height.
160 : 1 : std::reverse(g_spend_candidate_txins.begin(), g_spend_candidate_txins.end());
161 : 1 : }
162 : :
163 : :
164 : : /**
165 : : * Reset the chainman in the testing setup object.
166 : : * Mine 2*COINBASE_MATURITY blocks to have spendable UTXOs.
167 : : * It is called once in the initialization function.
168 : : */
169 : 1 : void ResetChainman(TestingSetup& setup)
170 : : {
171 : 1 : SetMockTime(setup.m_node.chainman->GetParams().GenesisBlock().Time());
172 [ + - ]: 1 : setup.m_node.block_template_manager.reset();
173 [ + - ]: 1 : setup.m_node.chainman.reset();
174 : 1 : setup.m_node.notifications->m_shutdown_on_fatal_error = false;
175 : 1 : setup.m_make_chainman();
176 : 1 : setup.CreateBlockTemplateManager();
177 : 1 : setup.LoadVerifyActivateChainstate();
178 : :
179 [ + + ]: 201 : for (int i = 0; i < 2 * COINBASE_MATURITY; i++) {
180 : 200 : node::BlockCreateOptions options;
181 : 200 : options.coinbase_output_script = P2WSH_OP_TRUE;
182 [ + - ]: 200 : MineBlock(setup.m_node, options);
183 : 200 : }
184 : 1 : setup.m_node.validation_signals->SyncWithValidationInterfaceQueue();
185 : 1 : }
186 : :
187 : : /** Create additional transactions in the mempool that spend
188 : : * coins from mature blocks. Otherwise the mined chain only contains
189 : : * coinbase transactions.
190 : : */
191 : 1 : void AddExtraTxsToMempool(TestingSetup& setup)
192 : : {
193 : 1 : Assert(setup.m_node.chainman->ActiveChainstate().GetMempool()->size() == 0);
194 [ + + ]: 11 : for (size_t i = 1; i <= 10; i++) {
195 : 10 : CMutableTransaction ctx;
196 : 10 : ctx.version = CTransaction::CURRENT_VERSION;
197 [ + - ]: 10 : ctx.vin.resize(1);
198 : : // CTxIn is spendable as g_spend_candidate_txins comes from early blocks whose
199 : : // coinbases are mature.
200 [ + - ]: 10 : ctx.vin[0] = g_spend_candidate_txins[i];
201 [ + - ]: 10 : ctx.vout.resize(4);
202 : : // Arbitrarily create various outputs of different kinds in the same tx.
203 : : // P2WSH
204 : 10 : ctx.vout[0].nValue = CAmount(15 * COIN);
205 : 10 : ctx.vout[0].scriptPubKey = P2WSH_OP_TRUE;
206 : : // P2SH
207 : 10 : ctx.vout[1].nValue = CAmount(15 * COIN);
208 : 10 : ctx.vout[1].scriptPubKey = P2SH_OP_TRUE;
209 : : // Taproot
210 : 10 : ctx.vout[2].nValue = CAmount(10 * COIN);
211 : 10 : ctx.vout[2].scriptPubKey = TAPROOT_OP_TRUE;
212 : : // Empty script
213 : 10 : ctx.vout[3].nValue = CAmount(10 * COIN);
214 : 10 : ctx.vout[3].scriptPubKey = CScript();
215 : :
216 [ + - ]: 10 : LOCK(::cs_main);
217 : : // Add transaction to the mempool.
218 [ + - + - ]: 20 : const MempoolAcceptResult ctx_result = setup.m_node.chainman->ProcessTransaction(MakeTransactionRef(ctx));
219 [ + - ]: 10 : Assert(ctx_result.m_result_type == MempoolAcceptResult::ResultType::VALID);
220 : :
221 [ + - + - : 10 : Assert(setup.m_node.chainman->ActiveChainstate().GetMempool()->size() == i);
+ - ]
222 : : // Force the mempool to select this transaction even though its fee is zero.
223 [ + - + - : 10 : setup.m_node.chainman->ActiveChainstate().GetMempool()->PrioritiseTransaction(ctx.GetHash(), COIN);
+ - ]
224 [ + - ]: 30 : }
225 : 1 : }
226 : :
227 : : /** Initialize the chain for this target. */
228 : 1 : static void initialize_connect_block()
229 : : {
230 : : // Instantiate REGTEST chain.
231 : 1 : static auto testing_setup = MakeNoLogFileContext<TestingSetup>(
232 [ + - ]: 2 : /*chain_type=*/ChainType::REGTEST, TestOpts{
233 : : .extra_args = {
234 : : "-minrelaytxfee=0",
235 : : "-acceptnonstdtxn",
236 : : },
237 [ + - + - : 2 : });
+ - ]
238 : 1 : g_setup = testing_setup.get();
239 : :
240 : : // Reset the chainman in the testing setup object.
241 : 1 : ResetChainman(*g_setup);
242 : :
243 : : // Initialize Taproot script declared as static variables.
244 : 1 : InitTaprootScript();
245 : :
246 : : // Load the chain mined in ResetChainman in global variables g_blocks and
247 : : // g_spend_candidate_txins, to make them available to pick by the target.
248 : 1 : LoadCurrentChain();
249 : :
250 : : // Prepare multiple transactions for block 201. They spend coins
251 : : // from various coinbases that are now mature enough.
252 : 1 : AddExtraTxsToMempool(*g_setup);
253 : : // Mine block 201, which contains the transactions added to the mempool.
254 : 1 : node::BlockCreateOptions options;
255 : 1 : options.coinbase_output_script = P2WSH_OP_TRUE;
256 [ + - ]: 1 : MineBlock(g_setup->m_node, options);
257 [ + - + - : 1 : Assert(g_setup->m_node.chainman->ActiveChainstate().GetMempool()->size() == 0);
+ - ]
258 : :
259 : : // Load the 201st block into g_blocks.
260 [ + - ]: 1 : LOCK(::cs_main);
261 [ + - + - ]: 1 : auto& chainstate = Assert(g_setup->m_node.chainman)->ActiveChainstate();
262 [ - + ]: 1 : auto current_block = chainstate.m_chain.Tip();
263 [ + - ]: 1 : LoadCurrentBlock(chainstate, current_block);
264 : 1 : }
265 : :
266 : : /**
267 : : * Read one transaction from the fuzzing input through the FuzzedDataProvider.
268 : : * It is intended to leave more space to craft complex transactions, especially
269 : : * with various script types (P2SH, P2WSH, TAPROOT, NOSCRIPT).
270 : : * It is exclusively used by ConsumeBlock to read transactions inside a block.
271 : : */
272 : 3595 : CTransactionRef ConsumeTransaction(FuzzedDataProvider& fuzzed_data_provider,
273 : : std::vector<CTxIn>& additional_txins,
274 : : bool coinbase = false,
275 : : int target_height = 0)
276 : : {
277 : 3595 : CMutableTransaction tx;
278 [ + + ]: 3595 : tx.version = fuzzed_data_provider.ConsumeBool() ?
279 : : CTransaction::CURRENT_VERSION :
280 : 625 : fuzzed_data_provider.ConsumeIntegral<uint32_t>();
281 [ + + ]: 3595 : tx.nLockTime = fuzzed_data_provider.ConsumeBool() ?
282 : : 0 :
283 : 818 : fuzzed_data_provider.ConsumeIntegral<uint32_t>();
284 : :
285 : : // Some harnesses want to explicitly read coinbase transactions from input.
286 [ + + ]: 3595 : if (coinbase) {
287 : : // vin size is hardcoded.
288 [ + - ]: 1459 : tx.vin.resize(1);
289 : 1459 : tx.vin[0].prevout.SetNull();
290 [ + + ]: 1459 : if (fuzzed_data_provider.ConsumeBool()) {
291 : : // 1/2 probability of a valid vin.
292 [ + - ]: 1154 : tx.vin[0].scriptSig = CScript() << target_height;
293 : : } else {
294 : : // Read arbitrary data from input as scriptSig.
295 : 305 : auto script_sig = ConsumeRandomLengthByteVector<unsigned char>(fuzzed_data_provider, 100);
296 : 305 : tx.vin[0].scriptSig.assign(script_sig.begin(), script_sig.end());
297 : 305 : }
298 : : } else {
299 : : // Read a normal transaction, with up to 10 inputs.
300 [ + + + + ]: 6603 : LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 10) {
301 [ + - ]: 4467 : auto& txin = tx.vin.emplace_back();
302 : : // Read an integer to choose a CTxIn or reuse one generated by the
303 : : // input. The content of the CTxIn is not read from the input per se.
304 [ - + - + ]: 4467 : uint32_t input_index = fuzzed_data_provider.ConsumeIntegralInRange<uint32_t>(0, g_spend_candidate_txins.size() + additional_txins.size() - 1);
305 [ - + + + ]: 4467 : if (input_index < g_spend_candidate_txins.size()) {
306 : : // Pick it from the spend candidates.
307 [ + - ]: 4143 : txin = g_spend_candidate_txins[input_index];
308 : : } else {
309 : : // Pick it in the additional_txins set.
310 [ - + + - ]: 324 : Assert((input_index - g_spend_candidate_txins.size()) < additional_txins.size());
311 [ - + + - ]: 324 : txin = additional_txins[input_index - g_spend_candidate_txins.size()];
312 : : }
313 : :
314 : : // Enable the fuzzer to mutate every CTxIn field after it is taken
315 : : // from the spend candidates.
316 [ + + ]: 4467 : if (fuzzed_data_provider.ConsumeBool()) {
317 : 2584 : txin.nSequence = ConsumeSequence(fuzzed_data_provider);
318 : : }
319 [ + + ]: 4467 : if (fuzzed_data_provider.ConsumeBool()) {
320 : 1509 : txin.prevout.n = fuzzed_data_provider.ConsumeIntegral<uint32_t>();
321 : : }
322 [ + + ]: 4467 : if (fuzzed_data_provider.ConsumeBool()) {
323 : 1331 : txin.prevout.hash = Txid::FromUint256(ConsumeUInt256(fuzzed_data_provider));
324 : : }
325 [ + + ]: 4467 : if (fuzzed_data_provider.ConsumeBool()) {
326 : 2718 : txin.scriptSig = ConsumeScript(fuzzed_data_provider);
327 : : }
328 [ + + ]: 4467 : if (fuzzed_data_provider.ConsumeBool()) {
329 : 1665 : txin.scriptWitness.stack.clear();
330 [ + + + + ]: 3980 : LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 10) {
331 [ + - ]: 2315 : txin.scriptWitness.stack.push_back(ConsumeRandomLengthByteVector<unsigned char>(fuzzed_data_provider, 100));
332 : : }
333 : : }
334 : : }
335 : : }
336 : :
337 : : // Read between 1 and 10 outputs.
338 [ + + + + : 19817 : LIMITED_WHILE(tx.vout.empty() || fuzzed_data_provider.ConsumeBool(), 10) {
+ + ]
339 [ + - ]: 8111 : auto& txout = tx.vout.emplace_back();
340 : : // Read CAmount to spend.
341 : 8111 : txout.nValue = fuzzed_data_provider.ConsumeIntegralInRange<CAmount>(-10, 50 * COIN + 10);
342 : :
343 : : // Read scriptPubKey type into one of the valid types.
344 : 8111 : CallOneOf(
345 : : fuzzed_data_provider,
346 : 2697 : [&] {
347 : : // P2WSH
348 : 2697 : txout.scriptPubKey = P2WSH_OP_TRUE;
349 : 2697 : },
350 : 1280 : [&] {
351 : : // P2SH
352 : 1280 : txout.scriptPubKey = P2SH_OP_TRUE;
353 : 1280 : },
354 : 1109 : [&] {
355 : : // Taproot
356 : 1109 : txout.scriptPubKey = TAPROOT_OP_TRUE;
357 : 1109 : },
358 : 1111 : [&] {
359 : : // Empty script
360 : 1111 : txout.scriptPubKey = CScript();
361 : 1111 : },
362 : 1914 : [&] {
363 : : // Read arbitrary scriptPubKey.
364 : 1914 : txout.scriptPubKey = ConsumeScript(fuzzed_data_provider);
365 : 1914 : });
366 : : }
367 : :
368 : : // Create the shared pointer to the CTransaction object.
369 [ + - ]: 3595 : auto res = MakeTransactionRef(tx);
370 : :
371 [ + + ]: 3595 : if (!coinbase) {
372 : : // Create spending scripts for CTxOuts so they can be spent in later
373 : : // transactions. Do it here as the transaction hash is definitive.
374 [ - + + + ]: 7506 : for (uint32_t i{0}; i < res->vout.size(); ++i) {
375 [ + - ]: 5370 : MaybeAddSpendCandidate(additional_txins, *res, i);
376 : : }
377 : : }
378 : :
379 : 3595 : return res;
380 : 3595 : }
381 : :
382 : : /**
383 : : * Consume a block from the fuzzing input.
384 : : * It builds a block on top of the given prev_block.
385 : : */
386 : 1459 : CBlock ConsumeBlock(FuzzedDataProvider& fuzzed_data_provider, const CBlock& prev_block, int target_height,
387 : : std::vector<CTxIn>& additional_txins)
388 : : {
389 : 1459 : CBlock block;
390 : :
391 : : // Initialize header fields.
392 [ + - ]: 1459 : block.nVersion = g_blocks.back()->nVersion;
393 [ + - ]: 1459 : block.hashPrevBlock = prev_block.GetHash();
394 : 1459 : block.nTime = g_blocks.back()->nTime + 2;
395 : 1459 : block.nBits = g_blocks.back()->nBits;
396 : :
397 : : // Give the fuzzer input the ability to mutate block header fields.
398 [ + + ]: 1459 : if (fuzzed_data_provider.ConsumeBool()) {
399 : 114 : block.nVersion = fuzzed_data_provider.ConsumeIntegral<int32_t>();
400 : : }
401 [ + + ]: 1459 : if (fuzzed_data_provider.ConsumeBool()) {
402 : 121 : block.hashPrevBlock = ConsumeUInt256(fuzzed_data_provider);
403 : : }
404 : :
405 [ + + ]: 1459 : if (fuzzed_data_provider.ConsumeBool()) {
406 : 259 : block.nTime = fuzzed_data_provider.ConsumeIntegral<uint32_t>();
407 : : }
408 [ + + ]: 1459 : if (fuzzed_data_provider.ConsumeBool()) {
409 : 155 : block.nBits = fuzzed_data_provider.ConsumeIntegral<uint32_t>();
410 : : }
411 : :
412 : : // Read the coinbase transaction from the input.
413 [ + - - + ]: 2918 : block.vtx.push_back(ConsumeTransaction(fuzzed_data_provider, additional_txins, true, target_height));
414 : :
415 : : // Read up to 5 transactions from the input.
416 [ + + + + ]: 3595 : LIMITED_WHILE(fuzzed_data_provider.ConsumeBool(), 5) {
417 [ - + + - ]: 4272 : block.vtx.push_back(ConsumeTransaction(fuzzed_data_provider, additional_txins));
418 : : }
419 : :
420 : : // Commit witness.
421 [ + + ]: 1459 : if (fuzzed_data_provider.ConsumeBool()) {
422 [ + - ]: 198 : g_setup->m_node.chainman->GenerateCoinbaseCommitment(block, nullptr);
423 : : }
424 : :
425 : : // Set hashMerkleRoot to expected value.
426 [ + - ]: 1459 : block.hashMerkleRoot = BlockMerkleRoot(block);
427 : : // Let the fuzzer mutate hashMerkleRoot.
428 [ + + ]: 1459 : if (fuzzed_data_provider.ConsumeBool()) {
429 : 212 : block.hashMerkleRoot = ConsumeUInt256(fuzzed_data_provider);
430 : : }
431 : :
432 : : // Read the nonce from the input.
433 : 1459 : block.nNonce = fuzzed_data_provider.ConsumeIntegral<uint32_t>();
434 : :
435 : 1459 : return block;
436 : 0 : }
437 : :
438 : :
439 [ + - ]: 1935 : FUZZ_TARGET(connect_block, .init = initialize_connect_block)
440 : : {
441 : 1459 : SeedRandomStateForTest(SeedRand::ZEROS);
442 : 1459 : FuzzedDataProvider fuzzed_data_provider(buffer.data(), buffer.size());
443 : 1459 : FakeNodeClock clock{g_blocks.back()->Time() + 2s};
444 : :
445 [ + - ]: 1459 : LOCK(::cs_main);
446 [ + - ]: 1459 : g_setup->m_node.chainman->m_validation_cache.m_script_execution_cache.TestOnlyReset();
447 [ + - ]: 1459 : Chainstate& active_chainstate = g_setup->m_node.chainman->ActiveChainstate();
448 [ - + ]: 1459 : CBlockIndex* active_tip = active_chainstate.m_chain.Tip();
449 [ + - - + : 1459 : Assert(active_tip->GetBlockHash() == g_blocks.back()->GetHash());
+ - ]
450 [ + - + - ]: 1459 : CCoinsViewCache active_coins(&active_chainstate.CoinsTip());
451 : :
452 : : // Read a new block from the data provider.
453 : 1459 : std::vector<CTxIn> additional_txins;
454 [ + - ]: 1459 : CBlock block = ConsumeBlock(fuzzed_data_provider, *g_blocks.back(), active_tip->nHeight + 1, additional_txins);
455 : :
456 : : // Duplicate a transaction (not the coinbase) from the previous block
457 : : // to hit the BIP30 check.
458 [ + + ]: 1459 : if (fuzzed_data_provider.ConsumeBool()) {
459 [ - + ]: 60 : const auto& duplicates = g_blocks.back()->vtx;
460 [ - + + - ]: 60 : block.vtx.push_back(duplicates[fuzzed_data_provider.ConsumeIntegralInRange<size_t>(1, duplicates.size() - 1)]);
461 : : }
462 : :
463 : : // Compute new CBlockIndex object.
464 [ + - ]: 1459 : uint256 current_hash = block.GetHash();
465 : 1459 : CBlockIndex new_index(block);
466 : 1459 : new_index.pprev = active_tip;
467 : 1459 : new_index.nHeight = active_tip->nHeight + 1;
468 : 1459 : new_index.phashBlock = ¤t_hash;
469 : :
470 : : // Try to connect the block.
471 [ + - ]: 1459 : BlockValidationState state;
472 [ + - ]: 1459 : bool connected = active_chainstate.ConnectBlock(block,
473 : : state,
474 : : &new_index,
475 : : active_coins,
476 : : /*fJustCheck=*/true);
477 [ + - ]: 1459 : Assert(connected == state.IsValid());
478 [ + - ]: 2918 : }
479 : :
480 : : } // namespace
|