LCOV - code coverage report
Current view: top level - src/test - validation_block_tests.cpp (source / functions) Coverage Total Hit
Test: total_coverage.info Lines: 99.0 % 192 190
Test Date: 2025-06-01 06:26:32 Functions: 100.0 % 21 21
Branches: 53.1 % 582 309

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

Generated by: LCOV version 2.0-1