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