Branch data Line data Source code
1 : : // Copyright (c) 2018-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 <chain.h>
6 : : #include <chainparams.h>
7 : : #include <consensus/consensus.h>
8 : : #include <consensus/merkle.h>
9 : : #include <consensus/params.h>
10 : : #include <consensus/validation.h>
11 : : #include <node/block_template_manager.h>
12 : : #include <node/blockstorage.h>
13 : : #include <node/miner.h>
14 : : #include <pow.h>
15 : : #include <primitives/block.h>
16 : : #include <primitives/transaction.h>
17 : : #include <random.h>
18 : : #include <script/script.h>
19 : : #include <sync.h>
20 : : #include <test/util/common.h>
21 : : #include <test/util/mining.h>
22 : : #include <test/util/script.h>
23 : : #include <test/util/setup_common.h>
24 : : #include <txmempool.h>
25 : : #include <uint256.h>
26 : : #include <util/check.h>
27 : : #include <validation.h>
28 : : #include <validationinterface.h>
29 : :
30 : : #include <boost/test/unit_test.hpp>
31 : :
32 : : #include <cstddef>
33 : : #include <cstdint>
34 : : #include <memory>
35 : : #include <span>
36 : : #include <thread>
37 : : #include <utility>
38 : : #include <vector>
39 : :
40 : : using kernel::ChainstateRole;
41 : :
42 : : namespace validation_block_tests {
43 : 9 : struct MinerTestingSetup : public RegTestingSetup {
44 : : /// Prepare a coinbase-only block extending the known header at `prev_hash`.
45 : : /// The coinbase has a unique zero-value output and a reward output spendable with OP_TRUE.
46 : : /// Call FinalizeBlock() after any further edits to update commitments, mine the block,
47 : : /// and submit its header.
48 : : std::shared_ptr<CBlock> Block(const uint256& prev_hash);
49 : : std::shared_ptr<const CBlock> GoodBlock(const uint256& prev_hash);
50 : : std::shared_ptr<const CBlock> BadBlock(const uint256& prev_hash);
51 : : std::shared_ptr<CBlock> FinalizeBlock(std::shared_ptr<CBlock> pblock);
52 : : void BuildChain(const uint256& root, int height, unsigned int invalid_rate, unsigned int branch_rate, unsigned int max_size, std::vector<std::shared_ptr<const CBlock>>& blocks);
53 : : };
54 : : } // namespace validation_block_tests
55 : :
56 : : BOOST_FIXTURE_TEST_SUITE(validation_block_tests, MinerTestingSetup)
57 : :
58 : : struct TestSubscriber final : public CValidationInterface {
59 : : uint256 m_expected_tip;
60 : :
61 : 1 : explicit TestSubscriber(uint256 tip) : m_expected_tip(tip) {}
62 : :
63 : 61 : void UpdatedBlockTip(const CBlockIndex* pindexNew, const CBlockIndex* pindexFork, bool fInitialDownload) override
64 : : {
65 [ + - ]: 61 : BOOST_CHECK_EQUAL(m_expected_tip, pindexNew->GetBlockHash());
66 : 61 : }
67 : :
68 : 154 : void BlockConnected(const ChainstateRole& role, const std::shared_ptr<const CBlock>& block, const CBlockIndex* pindex) override
69 : : {
70 [ + - ]: 154 : BOOST_CHECK_EQUAL(m_expected_tip, block->hashPrevBlock);
71 [ + - ]: 154 : BOOST_CHECK_EQUAL(m_expected_tip, pindex->pprev->GetBlockHash());
72 : :
73 : 154 : m_expected_tip = block->GetHash();
74 : 154 : }
75 : :
76 : 93 : void BlockDisconnected(const std::shared_ptr<const CBlock>& block, const CBlockIndex* pindex) override
77 : : {
78 [ + - ]: 93 : BOOST_CHECK_EQUAL(m_expected_tip, block->GetHash());
79 [ + - ]: 93 : BOOST_CHECK_EQUAL(m_expected_tip, pindex->GetBlockHash());
80 : :
81 : 93 : m_expected_tip = block->hashPrevBlock;
82 : 93 : }
83 : : };
84 : :
85 : 1131 : std::shared_ptr<CBlock> MinerTestingSetup::Block(const uint256& prev_hash)
86 : : {
87 [ + + ]: 1131 : const auto& chainman{*Assert(m_node.chainman)};
88 : 1131 : static int i = 0;
89 [ + + + - ]: 1131 : static uint64_t time{chainman.GetParams().GenesisBlock().nTime};
90 : :
91 [ + - ]: 1131 : auto& block_template_manager{*Assert(m_node.block_template_manager)};
92 : 1131 : auto block_template{block_template_manager.CreateNewTemplate({
93 : : .use_mempool = false,
94 [ + - ]: 1131 : .coinbase_output_script = CScript{} << i++ << OP_TRUE,
95 : 1131 : })};
96 [ + - ]: 1131 : auto pblock = std::make_shared<CBlock>(block_template->block);
97 [ + - + - : 3393 : const auto& parent{*Assert(WITH_LOCK(::cs_main, return chainman.m_blockman.LookupBlockIndex(prev_hash)))};
+ - + - ]
98 [ + - ]: 1131 : SwitchBlockParent(*pblock, parent, /*new_time=*/++time, chainman.GetConsensus());
99 : :
100 : : // Make the coinbase transaction with two outputs:
101 : : // One zero-value one that has a unique pubkey to make sure that blocks at the same height can have a different hash
102 : : // Another one that has the coinbase reward in a P2WSH with OP_TRUE as witness program to make it easy to spend
103 [ + - ]: 1131 : CMutableTransaction txCoinbase(*pblock->vtx[0]);
104 [ + - ]: 1131 : txCoinbase.vout.resize(2);
105 : 1131 : txCoinbase.vout[1].scriptPubKey = P2WSH_OP_TRUE;
106 [ + - ]: 1131 : txCoinbase.vout[1].nValue = txCoinbase.vout[0].nValue;
107 : 1131 : txCoinbase.vout[0].nValue = 0;
108 [ + - + - ]: 2262 : txCoinbase.vin[0].scriptWitness = {};
109 [ + - - + ]: 2262 : pblock->vtx[0] = MakeTransactionRef(std::move(txCoinbase));
110 : :
111 : 1131 : return pblock;
112 [ + - + - ]: 3393 : }
113 : :
114 : 1131 : std::shared_ptr<CBlock> MinerTestingSetup::FinalizeBlock(std::shared_ptr<CBlock> pblock)
115 : : {
116 [ + - + - ]: 3393 : const CBlockIndex* prev_block{WITH_LOCK(::cs_main, return m_node.chainman->m_blockman.LookupBlockIndex(pblock->hashPrevBlock))};
117 : 1131 : m_node.chainman->GenerateCoinbaseCommitment(*pblock, prev_block);
118 : :
119 : 1131 : pblock->hashMerkleRoot = BlockMerkleRoot(*pblock);
120 : :
121 [ + + ]: 2266 : while (!CheckProofOfWork(pblock->GetHash(), pblock->nBits, Params().GetConsensus())) {
122 : 1135 : ++(pblock->nNonce);
123 : : }
124 : :
125 : : // submit block header, so that miner can get the block height from the
126 : : // global state and the node has the topology of the chain
127 [ + - ]: 1131 : BlockValidationState ignored;
128 [ + - + - : 2262 : BOOST_CHECK(Assert(m_node.chainman)->ProcessNewBlockHeaders({{*pblock}}, true, ignored));
+ - + - ]
129 : :
130 : 1131 : return pblock;
131 : 1131 : }
132 : :
133 : : // construct a valid block
134 : 1077 : std::shared_ptr<const CBlock> MinerTestingSetup::GoodBlock(const uint256& prev_hash)
135 : : {
136 [ + - - + : 1077 : return FinalizeBlock(Block(prev_hash));
- + ]
137 : : }
138 : :
139 : : // construct an invalid block (but with a valid header)
140 : 54 : std::shared_ptr<const CBlock> MinerTestingSetup::BadBlock(const uint256& prev_hash)
141 : : {
142 : 54 : auto pblock = Block(prev_hash);
143 : :
144 [ + - ]: 54 : CMutableTransaction coinbase_spend;
145 [ + - ]: 54 : coinbase_spend.vin.emplace_back(COutPoint(pblock->vtx[0]->GetHash(), 0), CScript(), 0);
146 [ + - ]: 54 : coinbase_spend.vout.push_back(pblock->vtx[0]->vout[0]);
147 : :
148 [ + - ]: 54 : CTransactionRef tx = MakeTransactionRef(coinbase_spend);
149 [ + - ]: 54 : pblock->vtx.push_back(tx);
150 : :
151 [ + - + - ]: 108 : auto ret = FinalizeBlock(pblock);
152 [ - + ]: 54 : return ret;
153 [ + - + - ]: 216 : }
154 : :
155 : : // NOLINTNEXTLINE(misc-no-recursion)
156 : 361 : void MinerTestingSetup::BuildChain(const uint256& root, int height, const unsigned int invalid_rate, const unsigned int branch_rate, const unsigned int max_size, std::vector<std::shared_ptr<const CBlock>>& blocks)
157 : : {
158 [ + - + - ]: 722 : if (height <= 0 || blocks.size() >= max_size) return;
159 : :
160 : 361 : bool gen_invalid = m_rng.randrange(100U) < invalid_rate;
161 : 361 : bool gen_fork = m_rng.randrange(100U) < branch_rate;
162 : :
163 [ + + ]: 361 : const std::shared_ptr<const CBlock> pblock = gen_invalid ? BadBlock(root) : GoodBlock(root);
164 [ + - ]: 361 : blocks.push_back(pblock);
165 [ + + ]: 361 : if (!gen_invalid) {
166 [ + - + - ]: 307 : BuildChain(pblock->GetHash(), height - 1, invalid_rate, branch_rate, max_size, blocks);
167 : : }
168 : :
169 [ + + ]: 361 : if (gen_fork) {
170 [ + - - + ]: 62 : blocks.push_back(GoodBlock(root));
171 [ + - + - ]: 31 : BuildChain(blocks.back()->GetHash(), height - 1, invalid_rate, branch_rate, max_size, blocks);
172 : : }
173 : 361 : }
174 : :
175 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(processnewblock_signals_ordering)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
176 : : {
177 : : // build a large-ish chain that's likely to have some forks
178 : 1 : std::vector<std::shared_ptr<const CBlock>> blocks;
179 [ - + + + ]: 24 : while (blocks.size() < 50) {
180 : 23 : blocks.clear();
181 [ + - + - : 23 : BuildChain(Params().GenesisBlock().GetHash(), 100, 15, 10, 500, blocks);
+ - ]
182 : : }
183 : :
184 : 1 : bool ignored;
185 : : // Connect the genesis block and drain any outstanding events
186 [ + - + - : 3 : BOOST_CHECK(Assert(m_node.chainman)->ProcessNewBlock(std::make_shared<CBlock>(Params().GenesisBlock()), true, true, &ignored));
+ - + - +
- + - + -
- + + - ]
187 [ + - ]: 1 : m_node.validation_signals->SyncWithValidationInterfaceQueue();
188 : :
189 : : // subscribe to events (this subscriber will validate event ordering)
190 : 1 : const CBlockIndex* initial_tip = nullptr;
191 : 1 : {
192 [ + - ]: 1 : LOCK(cs_main);
193 [ + - - + : 2 : initial_tip = m_node.chainman->ActiveChain().Tip();
+ - ]
194 : 0 : }
195 [ + - ]: 1 : auto sub = std::make_shared<TestSubscriber>(initial_tip->GetBlockHash());
196 [ + - + - ]: 2 : m_node.validation_signals->RegisterSharedValidationInterface(sub);
197 : :
198 : : // create a bunch of threads that repeatedly process a block generated above at random
199 : : // this will create parallelism and randomness inside validation - the ValidationInterface
200 : : // will subscribe to events generated during block validation and assert on ordering invariance
201 : 1 : std::vector<std::thread> threads;
202 [ + - ]: 1 : threads.reserve(10);
203 [ + + ]: 11 : for (int i = 0; i < 10; i++) {
204 [ + - ]: 20 : threads.emplace_back([&]() {
205 : 10 : bool ignored;
206 : 10 : FastRandomContext insecure;
207 [ + + ]: 10010 : for (int i = 0; i < 1000; i++) {
208 [ - + + - ]: 10000 : const auto& block = blocks[insecure.randrange(blocks.size() - 1)];
209 [ + - + - ]: 10000 : Assert(m_node.chainman)->ProcessNewBlock(block, true, true, &ignored);
210 : : }
211 : :
212 : : // to make sure that eventually we process the full chain - do it here
213 [ + + ]: 1700 : for (const auto& block : blocks) {
214 [ - + + + ]: 1690 : if (block->vtx.size() == 1) {
215 [ + - + - ]: 1480 : bool processed = Assert(m_node.chainman)->ProcessNewBlock(block, true, true, &ignored);
216 [ - + ]: 1480 : assert(processed);
217 : : }
218 : : }
219 : 10 : });
220 : : }
221 : :
222 [ + + ]: 11 : for (auto& t : threads) {
223 [ + - ]: 10 : t.join();
224 : : }
225 [ + - ]: 1 : m_node.validation_signals->SyncWithValidationInterfaceQueue();
226 : :
227 [ + - + - ]: 2 : m_node.validation_signals->UnregisterSharedValidationInterface(sub);
228 : :
229 [ + - ]: 1 : LOCK(cs_main);
230 [ + - + - : 2 : BOOST_CHECK_EQUAL(sub->m_expected_tip, m_node.chainman->ActiveChain().Tip()->GetBlockHash());
- + + - +
- ]
231 [ + - ]: 2 : }
232 : :
233 : : /**
234 : : * Test that mempool updates happen atomically with reorgs.
235 : : *
236 : : * This prevents RPC clients, among others, from retrieving immediately-out-of-date mempool data
237 : : * during large reorgs.
238 : : *
239 : : * The test verifies this by creating a chain of `num_txs` blocks, matures their coinbases, and then
240 : : * submits txns spending from their coinbase to the mempool. A fork chain is then processed,
241 : : * invalidating the txns and evicting them from the mempool.
242 : : *
243 : : * We verify that the mempool updates atomically by polling it continuously
244 : : * from another thread during the reorg and checking that its size only changes
245 : : * once. The size changing exactly once indicates that the polling thread's
246 : : * view of the mempool is either consistent with the chain state before reorg,
247 : : * or consistent with the chain state after the reorg, and not just consistent
248 : : * with some intermediate state during the reorg.
249 : : */
250 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(mempool_locks_reorg)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
251 : : {
252 : 1 : bool ignored;
253 : 741 : auto ProcessBlock = [&](std::shared_ptr<const CBlock> block) -> bool {
254 : 740 : return Assert(m_node.chainman)->ProcessNewBlock(block, /*force_processing=*/true, /*min_pow_checked=*/true, /*new_block=*/&ignored);
255 : 1 : };
256 : :
257 : : // Process all mined blocks
258 [ + - + - : 3 : BOOST_REQUIRE(ProcessBlock(std::make_shared<CBlock>(Params().GenesisBlock())));
+ - + - +
- - + ]
259 : 1 : auto last_mined = GoodBlock(Params().GenesisBlock().GetHash());
260 [ + - + - : 4 : BOOST_REQUIRE(ProcessBlock(last_mined));
+ - + - +
- ]
261 : :
262 : : // Run the test multiple times
263 [ + + ]: 4 : for (int test_runs = 3; test_runs > 0; --test_runs) {
264 [ + - + + : 12 : BOOST_CHECK_EQUAL(last_mined->GetHash(), WITH_LOCK(Assert(m_node.chainman)->GetMutex(), return m_node.chainman->ActiveChain().Tip()->GetBlockHash()));
+ - + - +
- ]
265 : :
266 : : // Later on split from here
267 : 3 : const uint256 split_hash{last_mined->hashPrevBlock};
268 : :
269 : : // Create a bunch of transactions to spend the miner rewards of the
270 : : // most recent blocks
271 : 3 : std::vector<CTransactionRef> txs;
272 [ + + ]: 69 : for (int num_txs = 22; num_txs > 0; --num_txs) {
273 [ + - ]: 66 : CMutableTransaction mtx;
274 [ + - ]: 66 : mtx.vin.emplace_back(COutPoint{last_mined->vtx[0]->GetHash(), 1}, CScript{});
275 [ + - + - : 132 : mtx.vin[0].scriptWitness = CScriptWitness{{WITNESS_STACK_ELEM_OP_TRUE}};
- + - + +
+ - - ]
276 [ + - ]: 66 : mtx.vout.push_back(last_mined->vtx[0]->vout[1]);
277 [ + - ]: 66 : mtx.vout[0].nValue -= 1000;
278 [ + - + - : 132 : txs.push_back(MakeTransactionRef(mtx));
- + ]
279 : :
280 [ + - + - : 132 : last_mined = GoodBlock(last_mined->GetHash());
- + ]
281 [ + - + - : 264 : BOOST_REQUIRE(ProcessBlock(last_mined));
+ - + - +
- ]
282 : 66 : }
283 : :
284 : : // Mature the inputs of the txs
285 [ + + ]: 303 : for (int j = COINBASE_MATURITY; j > 0; --j) {
286 [ + - + - : 600 : last_mined = GoodBlock(last_mined->GetHash());
- + ]
287 [ + - + - : 1200 : BOOST_REQUIRE(ProcessBlock(last_mined));
+ - + - +
- ]
288 : : }
289 : :
290 : : // Mine a reorg (and hold it back) before adding the txs to the mempool
291 [ + - ]: 3 : const uint256 tip_init{last_mined->GetHash()};
292 : :
293 : 3 : std::vector<std::shared_ptr<const CBlock>> reorg;
294 [ + - - + ]: 6 : last_mined = GoodBlock(split_hash);
295 [ + - ]: 3 : reorg.push_back(last_mined);
296 [ - + + + ]: 372 : for (size_t j = COINBASE_MATURITY + txs.size() + 1; j > 0; --j) {
297 [ + - + - : 738 : last_mined = GoodBlock(last_mined->GetHash());
- + ]
298 [ + - ]: 369 : reorg.push_back(last_mined);
299 : : }
300 : :
301 : : // Add the txs to the tx pool
302 : 3 : {
303 [ + - ]: 3 : LOCK(cs_main);
304 [ + + ]: 69 : for (const auto& tx : txs) {
305 [ + - ]: 66 : const MempoolAcceptResult result = m_node.chainman->ProcessTransaction(tx);
306 [ + - + - ]: 132 : BOOST_REQUIRE(result.m_result_type == MempoolAcceptResult::ResultType::VALID);
307 : 66 : }
308 : 0 : }
309 : :
310 : : // Check that all txs are in the pool
311 : 3 : {
312 [ + - - + : 3 : BOOST_CHECK_EQUAL(m_node.mempool->size(), txs.size());
+ - + - ]
313 : : }
314 : :
315 : : // Run a thread that simulates an RPC caller that is polling while
316 : : // validation is doing a reorg
317 : 6 : std::thread rpc_thread{[&]() {
318 : : // This thread is checking that the mempool either contains all of
319 : : // the transactions invalidated by the reorg, or none of them, and
320 : : // not some intermediate amount.
321 : 8652949 : while (true) {
322 : 4326476 : LOCK(m_node.mempool->cs);
323 [ + - + + ]: 4326476 : if (m_node.mempool->size() == 0) {
324 : : // We are done with the reorg
325 : : break;
326 : : }
327 : : // Internally, we might be in the middle of the reorg, but
328 : : // externally the reorg to the most-proof-of-work chain should
329 : : // be atomic. So the caller assumes that the returned mempool
330 : : // is consistent. That is, it has all txs that were there
331 : : // before the reorg.
332 [ + - - + : 4326473 : assert(m_node.mempool->size() == txs.size());
- + ]
333 [ + - ]: 4326473 : continue;
334 : 4326473 : }
335 : 3 : LOCK(cs_main);
336 : : // We are done with the reorg, so the tip must have changed
337 [ + - - + : 6 : assert(tip_init != m_node.chainman->ActiveChain().Tip()->GetBlockHash());
- + ]
338 [ + - ]: 6 : }};
339 : :
340 : : // Submit the reorg in this thread to invalidate and remove the txs from the tx pool
341 [ + + ]: 375 : for (const auto& b : reorg) {
342 [ + - + - ]: 1116 : ProcessBlock(b);
343 : : }
344 : : // Check that the reorg was eventually successful
345 [ + - + + : 12 : BOOST_CHECK_EQUAL(last_mined->GetHash(), WITH_LOCK(Assert(m_node.chainman)->GetMutex(), return m_node.chainman->ActiveChain().Tip()->GetBlockHash()));
+ - + - +
- ]
346 : :
347 : : // We can join the other thread, which returns when the reorg was successful
348 [ + - ]: 3 : rpc_thread.join();
349 : 3 : }
350 [ + - ]: 67 : }
351 : :
352 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(witness_commitment_index)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
353 : : {
354 : 1 : LOCK(Assert(m_node.chainman)->GetMutex());
355 : 1 : CScript pubKey;
356 [ + - + - ]: 1 : pubKey << 1 << OP_TRUE;
357 [ + - ]: 1 : auto& block_template_manager{*Assert(m_node.block_template_manager)};
358 : 1 : auto block_template{block_template_manager.CreateNewTemplate({
359 : : .coinbase_output_script = pubKey,
360 : 0 : })};
361 [ + - ]: 1 : CBlock pblock{block_template->block};
362 : :
363 : 1 : CTxOut witness;
364 : 1 : witness.scriptPubKey.resize(MINIMUM_WITNESS_COMMITMENT);
365 [ + - ]: 1 : witness.scriptPubKey[0] = OP_RETURN;
366 [ + - ]: 1 : witness.scriptPubKey[1] = 0x24;
367 [ + - ]: 1 : witness.scriptPubKey[2] = 0xaa;
368 [ + - ]: 1 : witness.scriptPubKey[3] = 0x21;
369 [ + - ]: 1 : witness.scriptPubKey[4] = 0xa9;
370 [ + - ]: 1 : witness.scriptPubKey[5] = 0xed;
371 : :
372 : : // A witness larger than the minimum size is still valid
373 : 1 : CTxOut min_plus_one = witness;
374 : 1 : min_plus_one.scriptPubKey.resize(MINIMUM_WITNESS_COMMITMENT + 1);
375 : :
376 : 1 : CTxOut invalid = witness;
377 [ + - ]: 1 : invalid.scriptPubKey[0] = OP_VERIFY;
378 : :
379 [ + - ]: 1 : CMutableTransaction txCoinbase(*pblock.vtx[0]);
380 [ + - ]: 1 : txCoinbase.vout.resize(4);
381 : 1 : txCoinbase.vout[0] = witness;
382 : 1 : txCoinbase.vout[1] = witness;
383 : 1 : txCoinbase.vout[2] = min_plus_one;
384 : 1 : txCoinbase.vout[3] = invalid;
385 [ + - - + ]: 2 : pblock.vtx[0] = MakeTransactionRef(std::move(txCoinbase));
386 : :
387 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(GetWitnessCommitmentIndex(pblock), 2);
388 [ + - + - ]: 3 : }
389 : :
390 [ + - + - : 7 : BOOST_FIXTURE_TEST_CASE(switch_block_parent_target, RegTestingSetup)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
391 : : {
392 : 1 : const auto& params{m_node.chainman->GetParams()};
393 : 1 : const auto& consensus{params.GetConsensus()};
394 : 1 : const auto& genesis{params.GenesisBlock()};
395 : 1 : const auto parent_hash{genesis.GetHash()};
396 : 1 : CBlockIndex parent{genesis};
397 : 1 : parent.phashBlock = &parent_hash;
398 : : // Regtest disables retargeting, so this synthetic target cannot occur in a real regtest chain.
399 : : // Its testnet-style 20-minute min-difficulty rule still applies, restoring the pow limit only above the threshold.
400 : 1 : --parent.nBits;
401 : :
402 : 1 : const auto threshold{parent.nTime + uint32_t(2 * consensus.nPowTargetSpacing)};
403 : 1 : CBlock block{genesis};
404 [ + - ]: 1 : SwitchBlockParent(block, parent, threshold, consensus);
405 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(block.nBits, parent.nBits);
406 [ + - ]: 1 : SwitchBlockParent(block, parent, threshold + 1, consensus);
407 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(block.nBits, genesis.nBits);
408 : 1 : }
409 : :
410 [ + - + - : 7 : BOOST_FIXTURE_TEST_CASE(switch_block_parent_coinbase, RegTestingSetup)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
411 : : {
412 : 1 : auto& chainman{*Assert(m_node.chainman)};
413 : 1 : const auto& consensus{chainman.GetConsensus()};
414 [ + - - + : 4 : const CBlockIndex* genesis{WITH_LOCK(::cs_main, return chainman.ActiveChain().Genesis())};
+ - ]
415 : 1 : const int length{consensus.nSubsidyHalvingInterval};
416 : 1 : std::vector<std::shared_ptr<CBlock>> chain;
417 [ + - + - : 2 : BOOST_REQUIRE(BuildChain(m_node, genesis, CScript{} << OP_TRUE, length, chain));
+ - + - +
- ]
418 [ + - - + : 1 : BOOST_CHECK_EQUAL(chain[0]->vtx[0]->vin[0].scriptSig.size(), 2);
+ - ]
419 [ + + ]: 151 : for (int i{0}; i < length; ++i) {
420 [ + - ]: 150 : const auto& block{chain[i]};
421 [ + - + - ]: 150 : BOOST_CHECK_EQUAL(block->vtx[0]->nLockTime, i);
422 [ + - + - : 150 : BOOST_CHECK_EQUAL(block->vtx[0]->vout[0].nValue, GetBlockSubsidy(i + 1, consensus));
+ - ]
423 [ + - + - : 600 : BOOST_REQUIRE(chainman.ProcessNewBlock(block, /*force_processing=*/true, /*min_pow_checked=*/true, nullptr));
+ - + - +
- ]
424 : : }
425 [ + - + + : 3 : BOOST_CHECK_EQUAL(WITH_LOCK(::cs_main, return chainman.ActiveChain().Height()), length);
+ - ]
426 : 1 : }
427 : : BOOST_AUTO_TEST_SUITE_END()
|