LCOV - code coverage report
Current view: top level - src/test - validation_tests.cpp (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 98.3 % 229 225
Test Date: 2026-11-27 06:02:19 Functions: 100.0 % 14 14
Branches: 50.5 % 1078 544

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2014-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 <chainparams.h>
       6                 :             : #include <consensus/amount.h>
       7                 :             : #include <consensus/merkle.h>
       8                 :             : #include <core_io.h>
       9                 :             : #include <hash.h>
      10                 :             : #include <net.h>
      11                 :             : #include <signet.h>
      12                 :             : #include <uint256.h>
      13                 :             : #include <util/chaintype.h>
      14                 :             : #include <validation.h>
      15                 :             : 
      16                 :             : #include <string>
      17                 :             : 
      18                 :             : #include <test/util/setup_common.h>
      19                 :             : 
      20                 :             : #include <boost/test/unit_test.hpp>
      21                 :             : 
      22                 :             : BOOST_FIXTURE_TEST_SUITE(validation_tests, BasicTestingSetup)
      23                 :             : 
      24                 :           3 : static void TestBlockSubsidyHalvings(const Consensus::Params& consensusParams)
      25                 :             : {
      26                 :           3 :     int maxHalvings = 64;
      27                 :           3 :     CAmount nInitialSubsidy = 50 * COIN;
      28                 :             : 
      29                 :           3 :     CAmount nPreviousSubsidy = nInitialSubsidy * 2; // for height == 0
      30         [ +  - ]:           3 :     BOOST_CHECK_EQUAL(nPreviousSubsidy, nInitialSubsidy * 2);
      31         [ +  + ]:         195 :     for (int nHalvings = 0; nHalvings < maxHalvings; nHalvings++) {
      32                 :         192 :         int nHeight = nHalvings * consensusParams.nSubsidyHalvingInterval;
      33                 :         192 :         CAmount nSubsidy = GetBlockSubsidy(nHeight, consensusParams);
      34         [ +  - ]:         384 :         BOOST_CHECK(nSubsidy <= nInitialSubsidy);
      35         [ +  - ]:         192 :         BOOST_CHECK_EQUAL(nSubsidy, nPreviousSubsidy / 2);
      36                 :         192 :         nPreviousSubsidy = nSubsidy;
      37                 :             :     }
      38         [ +  - ]:           3 :     BOOST_CHECK_EQUAL(GetBlockSubsidy(maxHalvings * consensusParams.nSubsidyHalvingInterval, consensusParams), 0);
      39                 :           3 : }
      40                 :             : 
      41                 :           2 : static void TestBlockSubsidyHalvings(int nSubsidyHalvingInterval)
      42                 :             : {
      43                 :           2 :     Consensus::Params consensusParams;
      44                 :           2 :     consensusParams.nSubsidyHalvingInterval = nSubsidyHalvingInterval;
      45         [ +  - ]:           2 :     TestBlockSubsidyHalvings(consensusParams);
      46                 :           2 : }
      47                 :             : 
      48   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(block_subsidy_test)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
      49                 :             : {
      50                 :           1 :     const auto chainParams = CreateChainParams(*m_node.args, ChainType::MAIN);
      51         [ +  - ]:           1 :     TestBlockSubsidyHalvings(chainParams->GetConsensus()); // As in main
      52         [ +  - ]:           1 :     TestBlockSubsidyHalvings(150); // As in regtest
      53         [ +  - ]:           1 :     TestBlockSubsidyHalvings(1000); // Just another interval
      54                 :           1 : }
      55                 :             : 
      56   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(subsidy_limit_test)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
      57                 :             : {
      58                 :           1 :     const auto chainParams = CreateChainParams(*m_node.args, ChainType::MAIN);
      59                 :           1 :     CAmount nSum = 0;
      60         [ +  + ]:       14001 :     for (int nHeight = 0; nHeight < 14000000; nHeight += 1000) {
      61         [ +  - ]:       14000 :         CAmount nSubsidy = GetBlockSubsidy(nHeight, chainParams->GetConsensus());
      62   [ +  -  +  -  :       28000 :         BOOST_CHECK(nSubsidy <= 50 * COIN);
                   +  - ]
      63                 :       14000 :         nSum += nSubsidy * 1000;
      64   [ +  -  +  - ]:       28000 :         BOOST_CHECK(MoneyRange(nSum));
      65                 :             :     }
      66   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(nSum, CAmount{2099999997690000});
      67                 :           1 : }
      68                 :             : 
      69   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(signet_parse_tests)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
      70                 :             : {
      71                 :           1 :     ArgsManager signet_argsman;
      72   [ +  -  +  -  :           2 :     signet_argsman.ForceSetArg("-signetchallenge", "51"); // set challenge to OP_TRUE
                   +  - ]
      73         [ +  - ]:           1 :     const auto signet_params = CreateChainParams(signet_argsman, ChainType::SIGNET);
      74                 :           1 :     CBlock block;
      75   [ +  -  +  -  :           2 :     BOOST_CHECK(signet_params->GetConsensus().signet_challenge == std::vector<uint8_t>{OP_TRUE});
             +  -  +  - ]
      76         [ +  - ]:           1 :     CScript challenge{OP_TRUE};
      77                 :             : 
      78                 :             :     // empty block is invalid
      79   [ +  -  +  -  :           2 :     BOOST_CHECK(!SignetTxs::Create(block, challenge));
             +  -  +  - ]
      80   [ +  -  +  -  :           2 :     BOOST_CHECK(!CheckSignetBlockSolution(block, signet_params->GetConsensus()));
             +  -  +  - ]
      81                 :             : 
      82                 :             :     // no witness commitment
      83         [ +  - ]:           1 :     CMutableTransaction cb;
      84         [ +  - ]:           1 :     cb.vout.emplace_back(0, CScript{});
      85   [ +  -  +  -  :           2 :     block.vtx.push_back(MakeTransactionRef(cb));
                   -  + ]
      86   [ +  -  +  -  :           2 :     block.vtx.push_back(MakeTransactionRef(cb)); // Add dummy tx to exercise merkle root code
                   -  + ]
      87   [ +  -  +  -  :           2 :     BOOST_CHECK(!SignetTxs::Create(block, challenge));
             +  -  +  - ]
      88   [ +  -  +  -  :           2 :     BOOST_CHECK(!CheckSignetBlockSolution(block, signet_params->GetConsensus()));
             +  -  +  - ]
      89                 :             : 
      90                 :             :     // no header is treated valid
      91         [ +  - ]:           1 :     std::vector<uint8_t> witness_commitment_section_141{0xaa, 0x21, 0xa9, 0xed};
      92         [ +  + ]:          33 :     for (int i = 0; i < 32; ++i) {
      93         [ +  - ]:          32 :         witness_commitment_section_141.push_back(0xff);
      94                 :             :     }
      95   [ +  -  -  +  :           1 :     cb.vout.at(0).scriptPubKey = CScript{} << OP_RETURN << witness_commitment_section_141;
                   +  - ]
      96   [ +  -  +  -  :           2 :     block.vtx.at(0) = MakeTransactionRef(cb);
                   -  + ]
      97   [ +  -  +  -  :           2 :     BOOST_CHECK(SignetTxs::Create(block, challenge));
             +  -  +  - ]
      98   [ +  -  +  -  :           2 :     BOOST_CHECK(CheckSignetBlockSolution(block, signet_params->GetConsensus()));
             +  -  +  - ]
      99                 :             : 
     100                 :             :     // no data after header, valid
     101         [ +  - ]:           1 :     std::vector<uint8_t> witness_commitment_section_325{0xec, 0xc7, 0xda, 0xa2};
     102   [ +  -  -  +  :           1 :     cb.vout.at(0).scriptPubKey = CScript{} << OP_RETURN << witness_commitment_section_141 << witness_commitment_section_325;
             -  +  +  - ]
     103   [ +  -  +  -  :           2 :     block.vtx.at(0) = MakeTransactionRef(cb);
                   -  + ]
     104   [ +  -  +  -  :           2 :     BOOST_CHECK(SignetTxs::Create(block, challenge));
             +  -  +  - ]
     105   [ +  -  +  -  :           2 :     BOOST_CHECK(CheckSignetBlockSolution(block, signet_params->GetConsensus()));
             +  -  +  - ]
     106                 :             : 
     107                 :             :     // Premature end of data, invalid
     108         [ +  - ]:           1 :     witness_commitment_section_325.push_back(0x01);
     109         [ +  - ]:           1 :     witness_commitment_section_325.push_back(0x51);
     110   [ +  -  -  +  :           1 :     cb.vout.at(0).scriptPubKey = CScript{} << OP_RETURN << witness_commitment_section_141 << witness_commitment_section_325;
             -  +  +  - ]
     111   [ +  -  +  -  :           2 :     block.vtx.at(0) = MakeTransactionRef(cb);
                   -  + ]
     112   [ +  -  +  -  :           2 :     BOOST_CHECK(!SignetTxs::Create(block, challenge));
             +  -  +  - ]
     113   [ +  -  +  -  :           2 :     BOOST_CHECK(!CheckSignetBlockSolution(block, signet_params->GetConsensus()));
             +  -  +  - ]
     114                 :             : 
     115                 :             :     // has data, valid
     116         [ +  - ]:           1 :     witness_commitment_section_325.push_back(0x00);
     117   [ +  -  -  +  :           1 :     cb.vout.at(0).scriptPubKey = CScript{} << OP_RETURN << witness_commitment_section_141 << witness_commitment_section_325;
             -  +  +  - ]
     118   [ +  -  +  -  :           2 :     block.vtx.at(0) = MakeTransactionRef(cb);
                   -  + ]
     119   [ +  -  +  -  :           2 :     BOOST_CHECK(SignetTxs::Create(block, challenge));
             +  -  +  - ]
     120   [ +  -  +  -  :           2 :     BOOST_CHECK(CheckSignetBlockSolution(block, signet_params->GetConsensus()));
             +  -  +  - ]
     121                 :             : 
     122                 :             :     // Extraneous data, invalid
     123         [ +  - ]:           1 :     witness_commitment_section_325.push_back(0x00);
     124   [ +  -  -  +  :           1 :     cb.vout.at(0).scriptPubKey = CScript{} << OP_RETURN << witness_commitment_section_141 << witness_commitment_section_325;
             -  +  +  - ]
     125   [ +  -  +  -  :           2 :     block.vtx.at(0) = MakeTransactionRef(cb);
                   -  + ]
     126   [ +  -  +  -  :           2 :     BOOST_CHECK(!SignetTxs::Create(block, challenge));
             +  -  +  - ]
     127   [ +  -  +  -  :           2 :     BOOST_CHECK(!CheckSignetBlockSolution(block, signet_params->GetConsensus()));
                   +  - ]
     128                 :           2 : }
     129                 :             : 
     130                 :             : //! Test retrieval of valid assumeutxo values.
     131   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(test_assumeutxo)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     132                 :             : {
     133                 :           1 :     const auto params = CreateChainParams(*m_node.args, ChainType::REGTEST);
     134                 :             : 
     135                 :             :     // These heights don't have assumeutxo configurations associated, per the contents
     136                 :             :     // of kernel/chainparams.cpp.
     137         [ +  - ]:           1 :     std::vector<int> bad_heights{0, 100, 111, 115, 209, 211};
     138                 :             : 
     139         [ +  + ]:           7 :     for (auto empty : bad_heights) {
     140                 :           6 :         const auto out = params->AssumeutxoForHeight(empty);
     141   [ +  -  +  - ]:          12 :         BOOST_CHECK(!out);
     142                 :             :     }
     143                 :             : 
     144         [ +  - ]:           1 :     const auto out110 = *params->AssumeutxoForHeight(110);
     145   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(out110.hash_serialized.ToString(), "86e9a1205b418b16dde3a18a78c730e30137e28466bda5dbf6b33ab8fc05447c");
                   +  - ]
     146   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(out110.m_chain_tx_count, 111U);
     147                 :             : 
     148         [ +  - ]:           1 :     const auto out110_2 = *params->AssumeutxoForBlockhash(uint256{"135eec25a6fb277884e5824e7aa7d052c4868161c99a5122170b5266f86c273d"});
     149   [ +  -  +  -  :           1 :     BOOST_CHECK_EQUAL(out110_2.hash_serialized.ToString(), "86e9a1205b418b16dde3a18a78c730e30137e28466bda5dbf6b33ab8fc05447c");
                   +  - ]
     150   [ +  -  +  - ]:           1 :     BOOST_CHECK_EQUAL(out110_2.m_chain_tx_count, 111U);
     151                 :           1 : }
     152                 :             : 
     153   [ +  -  +  -  :           7 : BOOST_AUTO_TEST_CASE(block_malleation)
          +  -  +  -  -  
          +  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
          -  +  -  +  -  
          +  -  +  -  +  
          -  +  -  -  +  
          +  -  +  -  +  
          -  +  -  +  -  
          +  -  -  +  +  
                      - ]
     154                 :             : {
     155                 :             :     // Test utilities that calls `IsBlockMutated` and then clears the validity
     156                 :             :     // cache flags on `CBlock`.
     157                 :          17 :     auto is_mutated = [](CBlock& block, bool check_witness_root) {
     158   [ +  -  +  -  :           9 :         bool mutated{IsBlockMutated(block, check_witness_root)};
          +  -  +  -  +  
          -  +  -  +  -  
             +  -  +  - ]
     159                 :          16 :         block.fChecked = false;
     160                 :          16 :         block.m_checked_witness_commitment = false;
     161                 :          16 :         block.m_checked_merkle_root = false;
     162                 :          16 :         return mutated;
     163                 :             :     };
     164                 :           8 :     auto is_not_mutated = [&is_mutated](CBlock& block, bool check_witness_root) {
     165   [ +  -  +  -  :           7 :         return !is_mutated(block, check_witness_root);
          +  -  +  -  +  
             -  +  -  +  
                      - ]
     166                 :           1 :     };
     167                 :             : 
     168                 :             :     // Test utilities to create coinbase transactions and insert witness
     169                 :             :     // commitments.
     170                 :             :     //
     171                 :             :     // Note: this will not include the witness stack by default to avoid
     172                 :             :     // triggering the "no witnesses allowed for blocks that don't commit to
     173                 :             :     // witnesses" rule when testing other malleation vectors.
     174                 :           3 :     auto create_coinbase_tx = [](bool include_witness = false) {
     175                 :           2 :         CMutableTransaction coinbase;
     176         [ +  - ]:           2 :         coinbase.vin.resize(1);
     177         [ +  + ]:           2 :         if (include_witness) {
     178   [ +  -  +  -  :           2 :             coinbase.vin[0].scriptWitness = CScriptWitness{{std::vector<unsigned char>(32, 0x00)}};
          -  +  -  +  +  
                +  -  - ]
     179                 :             :         }
     180                 :             : 
     181         [ +  - ]:           2 :         coinbase.vout.resize(1);
     182                 :           2 :         coinbase.vout[0].scriptPubKey.resize(MINIMUM_WITNESS_COMMITMENT);
     183         [ -  + ]:           2 :         coinbase.vout[0].scriptPubKey[0] = OP_RETURN;
     184         [ -  + ]:           2 :         coinbase.vout[0].scriptPubKey[1] = 0x24;
     185         [ -  + ]:           2 :         coinbase.vout[0].scriptPubKey[2] = 0xaa;
     186         [ -  + ]:           2 :         coinbase.vout[0].scriptPubKey[3] = 0x21;
     187         [ -  + ]:           2 :         coinbase.vout[0].scriptPubKey[4] = 0xa9;
     188         [ -  + ]:           2 :         coinbase.vout[0].scriptPubKey[5] = 0xed;
     189                 :             : 
     190         [ +  - ]:           2 :         auto tx = MakeTransactionRef(coinbase);
     191         [ -  + ]:           2 :         assert(tx->IsCoinBase());
     192                 :           2 :         return tx;
     193         [ +  - ]:           3 :     };
     194                 :           3 :     auto insert_witness_commitment = [](CBlock& block, uint256 commitment) {
     195   [ +  -  +  -  :           2 :         assert(!block.vtx.empty() && block.vtx[0]->IsCoinBase() && !block.vtx[0]->vout.empty());
                   -  + ]
     196                 :             : 
     197                 :           2 :         CMutableTransaction mtx{*block.vtx[0]};
     198   [ +  -  +  -  :           6 :         CHash256().Write(commitment).Write(std::vector<unsigned char>(32, 0x00)).Finalize(commitment);
          +  -  +  -  +  
                -  -  + ]
     199   [ -  +  +  - ]:           4 :         memcpy(&mtx.vout[0].scriptPubKey[6], commitment.begin(), 32);
     200   [ +  -  -  + ]:           4 :         block.vtx[0] = MakeTransactionRef(mtx);
     201                 :           2 :     };
     202                 :             : 
     203                 :           1 :     {
     204                 :           1 :         CBlock block;
     205                 :             : 
     206                 :             :         // Empty block is expected to have merkle root of 0x0.
     207   [ +  -  +  -  :           2 :         BOOST_CHECK(block.vtx.empty());
                   +  - ]
     208                 :           1 :         block.hashMerkleRoot = uint256{1};
     209   [ +  -  +  -  :           2 :         BOOST_CHECK(is_mutated(block, /*check_witness_root=*/false));
             +  -  +  - ]
     210                 :           1 :         block.hashMerkleRoot = uint256{};
     211   [ +  -  +  -  :           2 :         BOOST_CHECK(is_not_mutated(block, /*check_witness_root=*/false));
             +  -  +  - ]
     212                 :             : 
     213                 :             :         // Block with a single coinbase tx is mutated if the merkle root is not
     214                 :             :         // equal to the coinbase tx's hash.
     215   [ +  -  -  + ]:           2 :         block.vtx.push_back(create_coinbase_tx());
     216   [ +  -  +  -  :           2 :         BOOST_CHECK(block.vtx[0]->GetHash().ToUint256() != block.hashMerkleRoot);
                   +  - ]
     217   [ +  -  +  -  :           2 :         BOOST_CHECK(is_mutated(block, /*check_witness_root=*/false));
             +  -  +  - ]
     218         [ +  - ]:           1 :         block.hashMerkleRoot = block.vtx[0]->GetHash().ToUint256();
     219   [ +  -  +  -  :           2 :         BOOST_CHECK(is_not_mutated(block, /*check_witness_root=*/false));
             +  -  +  - ]
     220                 :             : 
     221                 :             :         // Block with two transactions is mutated if the merkle root does not
     222                 :             :         // match the double sha256 of the concatenation of the two transaction
     223                 :             :         // hashes.
     224   [ +  -  +  -  :           2 :         block.vtx.push_back(MakeTransactionRef(CMutableTransaction{}));
                   -  + ]
     225   [ +  -  +  -  :           2 :         BOOST_CHECK(is_mutated(block, /*check_witness_root=*/false));
             +  -  +  - ]
     226         [ +  - ]:           1 :         HashWriter hasher;
     227         [ +  - ]:           1 :         hasher.write(block.vtx[0]->GetHash());
     228         [ +  - ]:           1 :         hasher.write(block.vtx[1]->GetHash());
     229         [ +  - ]:           1 :         block.hashMerkleRoot = hasher.GetHash();
     230   [ +  -  +  -  :           2 :         BOOST_CHECK(is_not_mutated(block, /*check_witness_root=*/false));
                   +  - ]
     231                 :             : 
     232                 :             :         // Block with two transactions is mutated if any node is duplicate.
     233                 :           1 :         {
     234                 :           1 :             block.vtx[1] = block.vtx[0];
     235         [ +  - ]:           1 :             HashWriter hasher;
     236         [ +  - ]:           1 :             hasher.write(block.vtx[0]->GetHash());
     237         [ +  - ]:           1 :             hasher.write(block.vtx[1]->GetHash());
     238         [ +  - ]:           1 :             block.hashMerkleRoot = hasher.GetHash();
     239   [ +  -  +  -  :           2 :             BOOST_CHECK(is_mutated(block, /*check_witness_root=*/false));
                   +  - ]
     240                 :             :         }
     241                 :             : 
     242                 :             :         // Blocks with 64-byte coinbase transactions are not considered mutated
     243                 :           1 :         block.vtx.clear();
     244                 :           1 :         {
     245         [ +  - ]:           1 :             CMutableTransaction mtx;
     246         [ +  - ]:           1 :             mtx.vin.resize(1);
     247         [ +  - ]:           1 :             mtx.vout.resize(1);
     248                 :           1 :             mtx.vout[0].scriptPubKey.resize(4);
     249   [ +  -  +  -  :           2 :             block.vtx.push_back(MakeTransactionRef(mtx));
                   -  + ]
     250         [ -  + ]:           1 :             block.hashMerkleRoot = block.vtx.back()->GetHash().ToUint256();
     251         [ -  + ]:           1 :             assert(block.vtx.back()->IsCoinBase());
     252         [ -  + ]:           1 :             assert(GetSerializeSize(TX_NO_WITNESS(block.vtx.back())) == 64);
     253                 :           0 :         }
     254   [ +  -  +  -  :           2 :         BOOST_CHECK(is_not_mutated(block, /*check_witness_root=*/false));
                   +  - ]
     255                 :           0 :     }
     256                 :             : 
     257                 :           1 :     {
     258                 :             :         // Test merkle root malleation
     259                 :             : 
     260                 :             :         // Pseudo code to mine transactions tx{1,2,3}:
     261                 :             :         //
     262                 :             :         // ```
     263                 :             :         // loop {
     264                 :             :         //   tx1 = random_tx()
     265                 :             :         //   tx2 = random_tx()
     266                 :             :         //   tx3 = deserialize_tx(txid(tx1) || txid(tx2));
     267                 :             :         //   if serialized_size_without_witness(tx3) == 64 {
     268                 :             :         //     print(hex(tx3))
     269                 :             :         //     break
     270                 :             :         //   }
     271                 :             :         // }
     272                 :             :         // ```
     273                 :             :         //
     274                 :             :         // The `random_tx` function used to mine the txs below simply created
     275                 :             :         // empty transactions with a random version field.
     276                 :           1 :         CMutableTransaction tx1;
     277   [ +  -  +  -  :           2 :         BOOST_CHECK(DecodeHexTx(tx1, "ff204bd0000000000000", /*try_no_witness=*/true, /*try_witness=*/false));
          +  -  +  -  +  
                      - ]
     278         [ +  - ]:           1 :         CMutableTransaction tx2;
     279   [ +  -  +  -  :           2 :         BOOST_CHECK(DecodeHexTx(tx2, "8ae53c92000000000000", /*try_no_witness=*/true, /*try_witness=*/false));
          +  -  +  -  +  
                      - ]
     280         [ +  - ]:           1 :         CMutableTransaction tx3;
     281   [ +  -  +  -  :           2 :         BOOST_CHECK(DecodeHexTx(tx3, "cdaf22d00002c6a7f848f8ae4d30054e61dcf3303d6fe01d282163341f06feecc10032b3160fcab87bdfe3ecfb769206ef2d991b92f8a268e423a6ef4d485f06", /*try_no_witness=*/true, /*try_witness=*/false));
          +  -  +  -  +  
                      - ]
     282                 :           1 :         {
     283                 :             :             // Verify that double_sha256(txid1||txid2) == txid3
     284         [ +  - ]:           1 :             HashWriter hasher;
     285   [ +  -  +  - ]:           1 :             hasher.write(tx1.GetHash());
     286   [ +  -  +  - ]:           1 :             hasher.write(tx2.GetHash());
     287   [ +  -  +  - ]:           1 :             assert(hasher.GetHash() == tx3.GetHash().ToUint256());
     288                 :             :             // Verify that tx3 is 64 bytes in size (without witness).
     289         [ -  + ]:           1 :             assert(GetSerializeSize(TX_NO_WITNESS(tx3)) == 64);
     290                 :             :         }
     291                 :             : 
     292                 :           1 :         CBlock block;
     293   [ +  -  +  -  :           2 :         block.vtx.push_back(MakeTransactionRef(tx1));
                   -  + ]
     294   [ +  -  +  -  :           2 :         block.vtx.push_back(MakeTransactionRef(tx2));
                   -  + ]
     295         [ +  - ]:           1 :         uint256 merkle_root = block.hashMerkleRoot = BlockMerkleRoot(block);
     296   [ +  -  +  -  :           2 :         BOOST_CHECK(is_not_mutated(block, /*check_witness_root=*/false));
                   +  - ]
     297                 :             : 
     298                 :             :         // Mutate the block by replacing the two transactions with one 64-byte
     299                 :             :         // transaction that serializes into the concatenation of the txids of
     300                 :             :         // the transactions in the unmutated block.
     301                 :           1 :         block.vtx.clear();
     302   [ +  -  +  -  :           2 :         block.vtx.push_back(MakeTransactionRef(tx3));
                   -  + ]
     303   [ +  -  +  -  :           2 :         BOOST_CHECK(!block.vtx.back()->IsCoinBase());
                   +  - ]
     304   [ +  -  +  -  :           3 :         BOOST_CHECK(BlockMerkleRoot(block) == merkle_root);
             +  -  +  - ]
     305   [ +  -  +  -  :           2 :         BOOST_CHECK(is_mutated(block, /*check_witness_root=*/false));
                   +  - ]
     306                 :           3 :     }
     307                 :             : 
     308                 :           1 :     {
     309                 :           1 :         CBlock block;
     310   [ +  -  -  + ]:           2 :         block.vtx.push_back(create_coinbase_tx(/*include_witness=*/true));
     311                 :           1 :         {
     312         [ +  - ]:           1 :             CMutableTransaction mtx;
     313         [ +  - ]:           1 :             mtx.vin.resize(1);
     314   [ +  -  +  -  :           2 :             mtx.vin[0].scriptWitness = CScriptWitness{{{0}}};
          -  +  -  +  +  
                +  -  - ]
     315   [ +  -  +  -  :           2 :             block.vtx.push_back(MakeTransactionRef(mtx));
                   -  + ]
     316                 :           0 :         }
     317         [ +  - ]:           1 :         block.hashMerkleRoot = BlockMerkleRoot(block);
     318                 :             :         // Block with witnesses is considered mutated if the witness commitment
     319                 :             :         // is not validated.
     320   [ +  -  +  -  :           2 :         BOOST_CHECK(is_mutated(block, /*check_witness_root=*/false));
             +  -  +  - ]
     321                 :             :         // Block with invalid witness commitment is considered mutated.
     322   [ +  -  +  -  :           2 :         BOOST_CHECK(is_mutated(block, /*check_witness_root=*/true));
             +  -  +  - ]
     323                 :             : 
     324                 :             :         // Block with valid commitment is not mutated
     325                 :           1 :         {
     326         [ +  - ]:           1 :             auto commitment{BlockWitnessMerkleRoot(block)};
     327         [ +  - ]:           1 :             insert_witness_commitment(block, commitment);
     328         [ +  - ]:           1 :             block.hashMerkleRoot = BlockMerkleRoot(block);
     329                 :             :         }
     330   [ +  -  +  -  :           2 :         BOOST_CHECK(is_not_mutated(block, /*check_witness_root=*/true));
             +  -  +  - ]
     331                 :             : 
     332                 :             :         // Malleating witnesses should be caught by `IsBlockMutated`.
     333                 :           1 :         {
     334         [ +  - ]:           1 :             CMutableTransaction mtx{*block.vtx[1]};
     335         [ -  + ]:           1 :             assert(!mtx.vin[0].scriptWitness[0].empty());
     336         [ +  - ]:           1 :             auto stack = mtx.vin[0].scriptWitness.ToVectors();
     337         [ +  - ]:           1 :             ++stack[0][0];
     338   [ +  -  +  -  :           2 :             mtx.vin[0].scriptWitness = CScriptWitness{stack};
                   -  + ]
     339   [ +  -  -  + ]:           2 :             block.vtx[1] = MakeTransactionRef(mtx);
     340                 :           1 :         }
     341                 :             :         // Without also updating the witness commitment, the merkle root should
     342                 :             :         // not change when changing one of the witnesses.
     343   [ +  -  +  -  :           3 :         BOOST_CHECK(block.hashMerkleRoot == BlockMerkleRoot(block));
             +  -  +  - ]
     344   [ +  -  +  -  :           2 :         BOOST_CHECK(is_mutated(block, /*check_witness_root=*/true));
             +  -  +  - ]
     345                 :           1 :         {
     346         [ +  - ]:           1 :             auto commitment{BlockWitnessMerkleRoot(block)};
     347         [ +  - ]:           1 :             insert_witness_commitment(block, commitment);
     348         [ +  - ]:           1 :             block.hashMerkleRoot = BlockMerkleRoot(block);
     349                 :             :         }
     350   [ +  -  +  -  :           2 :         BOOST_CHECK(is_not_mutated(block, /*check_witness_root=*/true));
             +  -  +  - ]
     351                 :             : 
     352                 :             :         // Test malleating the coinbase witness reserved value
     353                 :           1 :         {
     354         [ +  - ]:           1 :             CMutableTransaction mtx{*block.vtx[0]};
     355   [ +  -  +  - ]:           2 :             mtx.vin[0].scriptWitness = {};
     356   [ +  -  -  + ]:           2 :             block.vtx[0] = MakeTransactionRef(mtx);
     357         [ +  - ]:           1 :             block.hashMerkleRoot = BlockMerkleRoot(block);
     358                 :           0 :         }
     359   [ +  -  +  -  :           2 :         BOOST_CHECK(is_mutated(block, /*check_witness_root=*/true));
                   +  - ]
     360                 :           1 :     }
     361         [ +  - ]:           2 : }
     362                 :             : 
     363                 :             : BOOST_AUTO_TEST_SUITE_END()
        

Generated by: LCOV version 2.0-1