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