LCOV - code coverage report
Current view: top level - src/test - validation_block_tests.cpp (source / functions) Hit Total Coverage
Test: coverage.lcov Lines: 199 200 99.5 %
Date: 2022-04-21 14:51:19 Functions: 15 15 100.0 %
Legend: Modified by patch:
Lines: hit not hit | Branches: + taken - not taken # not executed

Not modified by patch:
Lines: hit not hit | Branches: + taken - not taken # not executed
Branches: 36 38 94.7 %

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

Generated by: LCOV version 0-eol-96201-ge66f56f4af6a