Bitcoin Core  22.99.0
P2P Digital Currency
validation_block_tests.cpp
Go to the documentation of this file.
1 // Copyright (c) 2018-2020 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 <miner.h>
11 #include <pow.h>
12 #include <random.h>
13 #include <script/standard.h>
14 #include <test/util/script.h>
15 #include <test/util/setup_common.h>
16 #include <util/time.h>
17 #include <validation.h>
18 #include <validationinterface.h>
19 
20 #include <thread>
21 
24  std::shared_ptr<CBlock> Block(const uint256& prev_hash);
25  std::shared_ptr<const CBlock> GoodBlock(const uint256& prev_hash);
26  std::shared_ptr<const CBlock> BadBlock(const uint256& prev_hash);
27  std::shared_ptr<CBlock> FinalizeBlock(std::shared_ptr<CBlock> pblock);
28  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);
29 };
30 } // namespace validation_block_tests
31 
33 
34 struct TestSubscriber final : public CValidationInterface {
36 
37  explicit TestSubscriber(uint256 tip) : m_expected_tip(tip) {}
38 
39  void UpdatedBlockTip(const CBlockIndex* pindexNew, const CBlockIndex* pindexFork, bool fInitialDownload) override
40  {
41  BOOST_CHECK_EQUAL(m_expected_tip, pindexNew->GetBlockHash());
42  }
43 
44  void BlockConnected(const std::shared_ptr<const CBlock>& block, const CBlockIndex* pindex) override
45  {
46  BOOST_CHECK_EQUAL(m_expected_tip, block->hashPrevBlock);
47  BOOST_CHECK_EQUAL(m_expected_tip, pindex->pprev->GetBlockHash());
48 
49  m_expected_tip = block->GetHash();
50  }
51 
52  void BlockDisconnected(const std::shared_ptr<const CBlock>& block, const CBlockIndex* pindex) override
53  {
54  BOOST_CHECK_EQUAL(m_expected_tip, block->GetHash());
55  BOOST_CHECK_EQUAL(m_expected_tip, pindex->GetBlockHash());
56 
57  m_expected_tip = block->hashPrevBlock;
58  }
59 };
60 
61 std::shared_ptr<CBlock> MinerTestingSetup::Block(const uint256& prev_hash)
62 {
63  static int i = 0;
64  static uint64_t time = Params().GenesisBlock().nTime;
65 
66  auto ptemplate = BlockAssembler(m_node.chainman->ActiveChainstate(), *m_node.mempool, Params()).CreateNewBlock(CScript{} << i++ << OP_TRUE);
67  auto pblock = std::make_shared<CBlock>(ptemplate->block);
68  pblock->hashPrevBlock = prev_hash;
69  pblock->nTime = ++time;
70 
71  // Make the coinbase transaction with two outputs:
72  // One zero-value one that has a unique pubkey to make sure that blocks at the same height can have a different hash
73  // Another one that has the coinbase reward in a P2WSH with OP_TRUE as witness program to make it easy to spend
74  CMutableTransaction txCoinbase(*pblock->vtx[0]);
75  txCoinbase.vout.resize(2);
76  txCoinbase.vout[1].scriptPubKey = P2WSH_OP_TRUE;
77  txCoinbase.vout[1].nValue = txCoinbase.vout[0].nValue;
78  txCoinbase.vout[0].nValue = 0;
79  txCoinbase.vin[0].scriptWitness.SetNull();
80  // Always pad with OP_0 at the end to avoid bad-cb-length error
81  txCoinbase.vin[0].scriptSig = CScript{} << WITH_LOCK(::cs_main, return m_node.chainman->m_blockman.LookupBlockIndex(prev_hash)->nHeight + 1) << OP_0;
82  pblock->vtx[0] = MakeTransactionRef(std::move(txCoinbase));
83 
84  return pblock;
85 }
86 
87 std::shared_ptr<CBlock> MinerTestingSetup::FinalizeBlock(std::shared_ptr<CBlock> pblock)
88 {
89  const CBlockIndex* prev_block{WITH_LOCK(::cs_main, return m_node.chainman->m_blockman.LookupBlockIndex(pblock->hashPrevBlock))};
90  GenerateCoinbaseCommitment(*pblock, prev_block, Params().GetConsensus());
91 
92  pblock->hashMerkleRoot = BlockMerkleRoot(*pblock);
93 
94  while (!CheckProofOfWork(pblock->GetHash(), pblock->nBits, Params().GetConsensus())) {
95  ++(pblock->nNonce);
96  }
97 
98  // submit block header, so that miner can get the block height from the
99  // global state and the node has the topology of the chain
100  BlockValidationState ignored;
101  BOOST_CHECK(Assert(m_node.chainman)->ProcessNewBlockHeaders({pblock->GetBlockHeader()}, ignored, Params()));
102 
103  return pblock;
104 }
105 
106 // construct a valid block
107 std::shared_ptr<const CBlock> MinerTestingSetup::GoodBlock(const uint256& prev_hash)
108 {
109  return FinalizeBlock(Block(prev_hash));
110 }
111 
112 // construct an invalid block (but with a valid header)
113 std::shared_ptr<const CBlock> MinerTestingSetup::BadBlock(const uint256& prev_hash)
114 {
115  auto pblock = Block(prev_hash);
116 
117  CMutableTransaction coinbase_spend;
118  coinbase_spend.vin.push_back(CTxIn(COutPoint(pblock->vtx[0]->GetHash(), 0), CScript(), 0));
119  coinbase_spend.vout.push_back(pblock->vtx[0]->vout[0]);
120 
121  CTransactionRef tx = MakeTransactionRef(coinbase_spend);
122  pblock->vtx.push_back(tx);
123 
124  auto ret = FinalizeBlock(pblock);
125  return ret;
126 }
127 
128 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)
129 {
130  if (height <= 0 || blocks.size() >= max_size) return;
131 
132  bool gen_invalid = InsecureRandRange(100) < invalid_rate;
133  bool gen_fork = InsecureRandRange(100) < branch_rate;
134 
135  const std::shared_ptr<const CBlock> pblock = gen_invalid ? BadBlock(root) : GoodBlock(root);
136  blocks.push_back(pblock);
137  if (!gen_invalid) {
138  BuildChain(pblock->GetHash(), height - 1, invalid_rate, branch_rate, max_size, blocks);
139  }
140 
141  if (gen_fork) {
142  blocks.push_back(GoodBlock(root));
143  BuildChain(blocks.back()->GetHash(), height - 1, invalid_rate, branch_rate, max_size, blocks);
144  }
145 }
146 
147 BOOST_AUTO_TEST_CASE(processnewblock_signals_ordering)
148 {
149  // build a large-ish chain that's likely to have some forks
150  std::vector<std::shared_ptr<const CBlock>> blocks;
151  while (blocks.size() < 50) {
152  blocks.clear();
153  BuildChain(Params().GenesisBlock().GetHash(), 100, 15, 10, 500, blocks);
154  }
155 
156  bool ignored;
157  // Connect the genesis block and drain any outstanding events
158  BOOST_CHECK(Assert(m_node.chainman)->ProcessNewBlock(Params(), std::make_shared<CBlock>(Params().GenesisBlock()), true, &ignored));
160 
161  // subscribe to events (this subscriber will validate event ordering)
162  const CBlockIndex* initial_tip = nullptr;
163  {
164  LOCK(cs_main);
165  initial_tip = m_node.chainman->ActiveChain().Tip();
166  }
167  auto sub = std::make_shared<TestSubscriber>(initial_tip->GetBlockHash());
169 
170  // create a bunch of threads that repeatedly process a block generated above at random
171  // this will create parallelism and randomness inside validation - the ValidationInterface
172  // will subscribe to events generated during block validation and assert on ordering invariance
173  std::vector<std::thread> threads;
174  for (int i = 0; i < 10; i++) {
175  threads.emplace_back([&]() {
176  bool ignored;
177  FastRandomContext insecure;
178  for (int i = 0; i < 1000; i++) {
179  auto block = blocks[insecure.randrange(blocks.size() - 1)];
180  Assert(m_node.chainman)->ProcessNewBlock(Params(), block, true, &ignored);
181  }
182 
183  // to make sure that eventually we process the full chain - do it here
184  for (auto block : blocks) {
185  if (block->vtx.size() == 1) {
186  bool processed = Assert(m_node.chainman)->ProcessNewBlock(Params(), block, true, &ignored);
187  assert(processed);
188  }
189  }
190  });
191  }
192 
193  for (auto& t : threads) {
194  t.join();
195  }
197 
199 
200  LOCK(cs_main);
201  BOOST_CHECK_EQUAL(sub->m_expected_tip, m_node.chainman->ActiveChain().Tip()->GetBlockHash());
202 }
203 
221 BOOST_AUTO_TEST_CASE(mempool_locks_reorg)
222 {
223  bool ignored;
224  auto ProcessBlock = [&](std::shared_ptr<const CBlock> block) -> bool {
225  return Assert(m_node.chainman)->ProcessNewBlock(Params(), block, /* fForceProcessing */ true, /* fNewBlock */ &ignored);
226  };
227 
228  // Process all mined blocks
229  BOOST_REQUIRE(ProcessBlock(std::make_shared<CBlock>(Params().GenesisBlock())));
230  auto last_mined = GoodBlock(Params().GenesisBlock().GetHash());
231  BOOST_REQUIRE(ProcessBlock(last_mined));
232 
233  // Run the test multiple times
234  for (int test_runs = 3; test_runs > 0; --test_runs) {
235  BOOST_CHECK_EQUAL(last_mined->GetHash(), m_node.chainman->ActiveChain().Tip()->GetBlockHash());
236 
237  // Later on split from here
238  const uint256 split_hash{last_mined->hashPrevBlock};
239 
240  // Create a bunch of transactions to spend the miner rewards of the
241  // most recent blocks
242  std::vector<CTransactionRef> txs;
243  for (int num_txs = 22; num_txs > 0; --num_txs) {
245  mtx.vin.push_back(CTxIn{COutPoint{last_mined->vtx[0]->GetHash(), 1}, CScript{}});
246  mtx.vin[0].scriptWitness.stack.push_back(WITNESS_STACK_ELEM_OP_TRUE);
247  mtx.vout.push_back(last_mined->vtx[0]->vout[1]);
248  mtx.vout[0].nValue -= 1000;
249  txs.push_back(MakeTransactionRef(mtx));
250 
251  last_mined = GoodBlock(last_mined->GetHash());
252  BOOST_REQUIRE(ProcessBlock(last_mined));
253  }
254 
255  // Mature the inputs of the txs
256  for (int j = COINBASE_MATURITY; j > 0; --j) {
257  last_mined = GoodBlock(last_mined->GetHash());
258  BOOST_REQUIRE(ProcessBlock(last_mined));
259  }
260 
261  // Mine a reorg (and hold it back) before adding the txs to the mempool
262  const uint256 tip_init{last_mined->GetHash()};
263 
264  std::vector<std::shared_ptr<const CBlock>> reorg;
265  last_mined = GoodBlock(split_hash);
266  reorg.push_back(last_mined);
267  for (size_t j = COINBASE_MATURITY + txs.size() + 1; j > 0; --j) {
268  last_mined = GoodBlock(last_mined->GetHash());
269  reorg.push_back(last_mined);
270  }
271 
272  // Add the txs to the tx pool
273  {
274  LOCK(cs_main);
275  for (const auto& tx : txs) {
276  const MempoolAcceptResult result = AcceptToMemoryPool(m_node.chainman->ActiveChainstate(), *m_node.mempool, tx, false /* bypass_limits */);
277  BOOST_REQUIRE(result.m_result_type == MempoolAcceptResult::ResultType::VALID);
278  }
279  }
280 
281  // Check that all txs are in the pool
282  {
283  LOCK(m_node.mempool->cs);
284  BOOST_CHECK_EQUAL(m_node.mempool->mapTx.size(), txs.size());
285  }
286 
287  // Run a thread that simulates an RPC caller that is polling while
288  // validation is doing a reorg
289  std::thread rpc_thread{[&]() {
290  // This thread is checking that the mempool either contains all of
291  // the transactions invalidated by the reorg, or none of them, and
292  // not some intermediate amount.
293  while (true) {
294  LOCK(m_node.mempool->cs);
295  if (m_node.mempool->mapTx.size() == 0) {
296  // We are done with the reorg
297  break;
298  }
299  // Internally, we might be in the middle of the reorg, but
300  // externally the reorg to the most-proof-of-work chain should
301  // be atomic. So the caller assumes that the returned mempool
302  // is consistent. That is, it has all txs that were there
303  // before the reorg.
304  assert(m_node.mempool->mapTx.size() == txs.size());
305  continue;
306  }
307  LOCK(cs_main);
308  // We are done with the reorg, so the tip must have changed
309  assert(tip_init != m_node.chainman->ActiveChain().Tip()->GetBlockHash());
310  }};
311 
312  // Submit the reorg in this thread to invalidate and remove the txs from the tx pool
313  for (const auto& b : reorg) {
314  ProcessBlock(b);
315  }
316  // Check that the reorg was eventually successful
317  BOOST_CHECK_EQUAL(last_mined->GetHash(), m_node.chainman->ActiveChain().Tip()->GetBlockHash());
318 
319  // We can join the other thread, which returns when the reorg was successful
320  rpc_thread.join();
321  }
322 }
323 
324 BOOST_AUTO_TEST_CASE(witness_commitment_index)
325 {
326  CScript pubKey;
327  pubKey << 1 << OP_TRUE;
328  auto ptemplate = BlockAssembler(m_node.chainman->ActiveChainstate(), *m_node.mempool, Params()).CreateNewBlock(pubKey);
329  CBlock pblock = ptemplate->block;
330 
331  CTxOut witness;
333  witness.scriptPubKey[0] = OP_RETURN;
334  witness.scriptPubKey[1] = 0x24;
335  witness.scriptPubKey[2] = 0xaa;
336  witness.scriptPubKey[3] = 0x21;
337  witness.scriptPubKey[4] = 0xa9;
338  witness.scriptPubKey[5] = 0xed;
339 
340  // A witness larger than the minimum size is still valid
341  CTxOut min_plus_one = witness;
343 
344  CTxOut invalid = witness;
345  invalid.scriptPubKey[0] = OP_VERIFY;
346 
347  CMutableTransaction txCoinbase(*pblock.vtx[0]);
348  txCoinbase.vout.resize(4);
349  txCoinbase.vout[0] = witness;
350  txCoinbase.vout[1] = witness;
351  txCoinbase.vout[2] = min_plus_one;
352  txCoinbase.vout[3] = invalid;
353  pblock.vtx[0] = MakeTransactionRef(std::move(txCoinbase));
354 
356 }
CValidationInterface
Implement this to subscribe to events generated in validation.
Definition: validationinterface.h:78
CTxIn
An input of a transaction.
Definition: transaction.h:65
CMutableTransaction::vin
std::vector< CTxIn > vin
Definition: transaction.h:346
validation_block_tests::MinerTestingSetup
Definition: validation_block_tests.cpp:23
RegTestingSetup
Identical to TestingSetup, but chain set to regtest.
Definition: setup_common.h:104
TestSubscriber::m_expected_tip
uint256 m_expected_tip
Definition: validation_block_tests.cpp:35
RegisterSharedValidationInterface
void RegisterSharedValidationInterface(std::shared_ptr< CValidationInterface > callbacks)
Register subscriber.
Definition: validationinterface.cpp:121
assert
assert(!tx.IsCoinBase())
validation_block_tests
Definition: validation_block_tests.cpp:22
pow.h
COINBASE_MATURITY
static const int COINBASE_MATURITY
Coinbase transaction outputs can only be spent after this number of new blocks (network rule)
Definition: consensus.h:19
BlockValidationState
Definition: validation.h:140
setup_common.h
TestSubscriber::BlockConnected
void BlockConnected(const std::shared_ptr< const CBlock > &block, const CBlockIndex *pindex) override
Notifies listeners of a block being connected.
Definition: validation_block_tests.cpp:44
NodeContext::mempool
std::unique_ptr< CTxMemPool > mempool
Definition: context.h:44
MempoolAcceptResult::m_result_type
const ResultType m_result_type
Definition: validation.h:165
AcceptToMemoryPool
MempoolAcceptResult AcceptToMemoryPool(CChainState &active_chainstate, CTxMemPool &pool, const CTransactionRef &tx, bool bypass_limits, bool test_accept)
(Try to) add a transaction to the memory pool.
Definition: validation.cpp:1040
CBlockIndex::pprev
CBlockIndex * pprev
pointer to the index of the predecessor of this block
Definition: chain.h:144
m_node
NodeContext & m_node
Definition: bitcoin-node.cpp:38
InsecureRandRange
static uint64_t InsecureRandRange(uint64_t range)
Definition: setup_common.h:68
validationinterface.h
validation.h
WITH_LOCK
#define WITH_LOCK(cs, code)
Run code while locking a mutex.
Definition: sync.h:270
validation_block_tests::MinerTestingSetup::GoodBlock
std::shared_ptr< const CBlock > GoodBlock(const uint256 &prev_hash)
Definition: validation_block_tests.cpp:107
MakeTransactionRef
static CTransactionRef MakeTransactionRef(Tx &&txIn)
Definition: transaction.h:387
CheckProofOfWork
bool CheckProofOfWork(uint256 hash, unsigned int nBits, const Consensus::Params &params)
Check whether a block hash satisfies the proof-of-work requirement specified by nBits.
Definition: pow.cpp:74
BlockAssembler
Generate a new block, without valid proof-of-work.
Definition: miner.h:126
chainparams.h
CTransactionRef
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:386
BOOST_FIXTURE_TEST_SUITE
#define BOOST_FIXTURE_TEST_SUITE(a, b)
Definition: object.cpp:14
TestSubscriber::UpdatedBlockTip
void UpdatedBlockTip(const CBlockIndex *pindexNew, const CBlockIndex *pindexFork, bool fInitialDownload) override
Notifies listeners when the block chain tip advances.
Definition: validation_block_tests.cpp:39
BOOST_AUTO_TEST_SUITE_END
BOOST_AUTO_TEST_SUITE_END()
Assert
#define Assert(val)
Identity function.
Definition: check.h:57
OP_VERIFY
@ OP_VERIFY
Definition: script.h:103
WITNESS_STACK_ELEM_OP_TRUE
static const std::vector< uint8_t > WITNESS_STACK_ELEM_OP_TRUE
Definition: script.h:11
SyncWithValidationInterfaceQueue
void SyncWithValidationInterfaceQueue()
This is a synonym for the following, which asserts certain locks are not held: std::promise<void> pro...
Definition: validationinterface.cpp:160
script.h
OP_0
@ OP_0
Definition: script.h:69
OP_RETURN
@ OP_RETURN
Definition: script.h:104
random.h
BOOST_AUTO_TEST_CASE
BOOST_AUTO_TEST_CASE(processnewblock_signals_ordering)
Definition: validation_block_tests.cpp:147
TestSubscriber::BlockDisconnected
void BlockDisconnected(const std::shared_ptr< const CBlock > &block, const CBlockIndex *pindex) override
Notifies listeners of a block being disconnected.
Definition: validation_block_tests.cpp:52
CTxOut
An output of a transaction.
Definition: transaction.h:128
CTxOut::scriptPubKey
CScript scriptPubKey
Definition: transaction.h:132
validation_block_tests::MinerTestingSetup::FinalizeBlock
std::shared_ptr< CBlock > FinalizeBlock(std::shared_ptr< CBlock > pblock)
Definition: validation_block_tests.cpp:87
miner.h
GenerateCoinbaseCommitment
std::vector< unsigned char > GenerateCoinbaseCommitment(CBlock &block, const CBlockIndex *pindexPrev, const Consensus::Params &consensusParams)
Produce the necessary coinbase commitment for a block (modifies the hash, don't call for mined blocks...
Definition: validation.cpp:2984
time.h
UnregisterSharedValidationInterface
void UnregisterSharedValidationInterface(std::shared_ptr< CValidationInterface > callbacks)
Unregister subscriber.
Definition: validationinterface.cpp:135
standard.h
CBlockHeader::nTime
uint32_t nTime
Definition: block.h:27
MINIMUM_WITNESS_COMMITMENT
static constexpr size_t MINIMUM_WITNESS_COMMITMENT
Minimum size of a witness commitment structure.
Definition: validation.h:19
validation_block_tests::MinerTestingSetup::BadBlock
std::shared_ptr< const CBlock > BadBlock(const uint256 &prev_hash)
Definition: validation_block_tests.cpp:113
CChainParams::GenesisBlock
const CBlock & GenesisBlock() const
Definition: chainparams.h:95
uint256
256-bit opaque blob.
Definition: uint256.h:124
GetWitnessCommitmentIndex
int GetWitnessCommitmentIndex(const CBlock &block)
Compute at which vout of the block's coinbase transaction the witness commitment occurs,...
Definition: validation.h:161
CScript
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:405
CBlockIndex::GetBlockHash
uint256 GetBlockHash() const
Definition: chain.h:246
OP_TRUE
@ OP_TRUE
Definition: script.h:77
merkle.h
prevector::resize
void resize(size_type new_size)
Definition: prevector.h:316
validation_block_tests::MinerTestingSetup::Block
std::shared_ptr< CBlock > Block(const uint256 &prev_hash)
Definition: validation_block_tests.cpp:61
MempoolAcceptResult::ResultType::VALID
@ VALID
CBlock
Definition: block.h:62
CMutableTransaction::vout
std::vector< CTxOut > vout
Definition: transaction.h:347
BlockAssembler::CreateNewBlock
std::unique_ptr< CBlockTemplate > CreateNewBlock(const CScript &scriptPubKeyIn)
Construct a new block template with coinbase to scriptPubKeyIn.
Definition: miner.cpp:102
CBlock::vtx
std::vector< CTransactionRef > vtx
Definition: block.h:66
cs_main
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate.
Definition: validation.cpp:116
NodeContext::chainman
std::unique_ptr< ChainstateManager > chainman
Definition: context.h:47
LOCK
#define LOCK(cs)
Definition: sync.h:226
validation_block_tests::MinerTestingSetup::BuildChain
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)
Definition: validation_block_tests.cpp:128
TestSubscriber::TestSubscriber
TestSubscriber(uint256 tip)
Definition: validation_block_tests.cpp:37
MempoolAcceptResult
Validation result for a single transaction mempool acceptance.
Definition: validation.h:159
Params
const CChainParams & Params()
Return the currently selected parameters.
Definition: chainparams.cpp:536
FastRandomContext::randrange
uint64_t randrange(uint64_t range) noexcept
Generate a random integer in the range [0..range).
Definition: random.h:190
TestSubscriber
Definition: validation_block_tests.cpp:34
COutPoint
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:26
CMutableTransaction
A mutable version of CTransaction.
Definition: transaction.h:344
CBlockIndex
The block chain is a tree shaped structure starting with the genesis block at the root,...
Definition: chain.h:137
FastRandomContext
Fast randomness source.
Definition: random.h:119
BOOST_CHECK
#define BOOST_CHECK(expr)
Definition: object.cpp:17
BOOST_CHECK_EQUAL
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:18
BlockMerkleRoot
uint256 BlockMerkleRoot(const CBlock &block, bool *mutated)
Definition: merkle.cpp:65
P2WSH_OP_TRUE
static const CScript P2WSH_OP_TRUE
Definition: script.h:12