LCOV - code coverage report
Current view: top level - src/test - validation_block_tests.cpp (source / functions) Coverage Total Hit
Test: total_coverage.info Lines: 98.5 % 195 192
Test Date: 2026-06-07 07:49:58 Functions: 100.0 % 21 21
Branches: 53.8 % 634 341

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

Generated by: LCOV version 2.0-1