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

Not modified by patch:
Lines: hit not hit | Branches: + taken - not taken # not executed
Branches: 8 8 100.0 %

           Branch data     Line data    Source code
#       1                 :            : // Copyright (c) 2014-2021 The Bitcoin Core developers
#       2                 :            : // Distributed under the MIT software license, see the accompanying
#       3                 :            : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
#       4                 :            : 
#       5                 :            : #include <chainparams.h>
#       6                 :            : #include <consensus/amount.h>
#       7                 :            : #include <net.h>
#       8                 :            : #include <signet.h>
#       9                 :            : #include <uint256.h>
#      10                 :            : #include <validation.h>
#      11                 :            : 
#      12                 :            : #include <test/util/setup_common.h>
#      13                 :            : 
#      14                 :            : #include <boost/test/unit_test.hpp>
#      15                 :            : 
#      16                 :            : BOOST_FIXTURE_TEST_SUITE(validation_tests, TestingSetup)
#      17                 :            : 
#      18                 :            : static void TestBlockSubsidyHalvings(const Consensus::Params& consensusParams)
#      19                 :          3 : {
#      20                 :          3 :     int maxHalvings = 64;
#      21                 :          3 :     CAmount nInitialSubsidy = 50 * COIN;
#      22                 :            : 
#      23                 :          3 :     CAmount nPreviousSubsidy = nInitialSubsidy * 2; // for height == 0
#      24                 :          3 :     BOOST_CHECK_EQUAL(nPreviousSubsidy, nInitialSubsidy * 2);
#      25         [ +  + ]:        195 :     for (int nHalvings = 0; nHalvings < maxHalvings; nHalvings++) {
#      26                 :        192 :         int nHeight = nHalvings * consensusParams.nSubsidyHalvingInterval;
#      27                 :        192 :         CAmount nSubsidy = GetBlockSubsidy(nHeight, consensusParams);
#      28                 :        192 :         BOOST_CHECK(nSubsidy <= nInitialSubsidy);
#      29                 :        192 :         BOOST_CHECK_EQUAL(nSubsidy, nPreviousSubsidy / 2);
#      30                 :        192 :         nPreviousSubsidy = nSubsidy;
#      31                 :        192 :     }
#      32                 :          3 :     BOOST_CHECK_EQUAL(GetBlockSubsidy(maxHalvings * consensusParams.nSubsidyHalvingInterval, consensusParams), 0);
#      33                 :          3 : }
#      34                 :            : 
#      35                 :            : static void TestBlockSubsidyHalvings(int nSubsidyHalvingInterval)
#      36                 :          2 : {
#      37                 :          2 :     Consensus::Params consensusParams;
#      38                 :          2 :     consensusParams.nSubsidyHalvingInterval = nSubsidyHalvingInterval;
#      39                 :          2 :     TestBlockSubsidyHalvings(consensusParams);
#      40                 :          2 : }
#      41                 :            : 
#      42                 :            : BOOST_AUTO_TEST_CASE(block_subsidy_test)
#      43                 :          1 : {
#      44                 :          1 :     const auto chainParams = CreateChainParams(*m_node.args, CBaseChainParams::MAIN);
#      45                 :          1 :     TestBlockSubsidyHalvings(chainParams->GetConsensus()); // As in main
#      46                 :          1 :     TestBlockSubsidyHalvings(150); // As in regtest
#      47                 :          1 :     TestBlockSubsidyHalvings(1000); // Just another interval
#      48                 :          1 : }
#      49                 :            : 
#      50                 :            : BOOST_AUTO_TEST_CASE(subsidy_limit_test)
#      51                 :          1 : {
#      52                 :          1 :     const auto chainParams = CreateChainParams(*m_node.args, CBaseChainParams::MAIN);
#      53                 :          1 :     CAmount nSum = 0;
#      54         [ +  + ]:      14001 :     for (int nHeight = 0; nHeight < 14000000; nHeight += 1000) {
#      55                 :      14000 :         CAmount nSubsidy = GetBlockSubsidy(nHeight, chainParams->GetConsensus());
#      56                 :      14000 :         BOOST_CHECK(nSubsidy <= 50 * COIN);
#      57                 :      14000 :         nSum += nSubsidy * 1000;
#      58                 :      14000 :         BOOST_CHECK(MoneyRange(nSum));
#      59                 :      14000 :     }
#      60                 :          1 :     BOOST_CHECK_EQUAL(nSum, CAmount{2099999997690000});
#      61                 :          1 : }
#      62                 :            : 
#      63                 :            : BOOST_AUTO_TEST_CASE(signet_parse_tests)
#      64                 :          1 : {
#      65                 :          1 :     ArgsManager signet_argsman;
#      66                 :          1 :     signet_argsman.ForceSetArg("-signetchallenge", "51"); // set challenge to OP_TRUE
#      67                 :          1 :     const auto signet_params = CreateChainParams(signet_argsman, CBaseChainParams::SIGNET);
#      68                 :          1 :     CBlock block;
#      69                 :          1 :     BOOST_CHECK(signet_params->GetConsensus().signet_challenge == std::vector<uint8_t>{OP_TRUE});
#      70                 :          1 :     CScript challenge{OP_TRUE};
#      71                 :            : 
#      72                 :            :     // empty block is invalid
#      73                 :          1 :     BOOST_CHECK(!SignetTxs::Create(block, challenge));
#      74                 :          1 :     BOOST_CHECK(!CheckSignetBlockSolution(block, signet_params->GetConsensus()));
#      75                 :            : 
#      76                 :            :     // no witness commitment
#      77                 :          1 :     CMutableTransaction cb;
#      78                 :          1 :     cb.vout.emplace_back(0, CScript{});
#      79                 :          1 :     block.vtx.push_back(MakeTransactionRef(cb));
#      80                 :          1 :     block.vtx.push_back(MakeTransactionRef(cb)); // Add dummy tx to exercise merkle root code
#      81                 :          1 :     BOOST_CHECK(!SignetTxs::Create(block, challenge));
#      82                 :          1 :     BOOST_CHECK(!CheckSignetBlockSolution(block, signet_params->GetConsensus()));
#      83                 :            : 
#      84                 :            :     // no header is treated valid
#      85                 :          1 :     std::vector<uint8_t> witness_commitment_section_141{0xaa, 0x21, 0xa9, 0xed};
#      86         [ +  + ]:         33 :     for (int i = 0; i < 32; ++i) {
#      87                 :         32 :         witness_commitment_section_141.push_back(0xff);
#      88                 :         32 :     }
#      89                 :          1 :     cb.vout.at(0).scriptPubKey = CScript{} << OP_RETURN << witness_commitment_section_141;
#      90                 :          1 :     block.vtx.at(0) = MakeTransactionRef(cb);
#      91                 :          1 :     BOOST_CHECK(SignetTxs::Create(block, challenge));
#      92                 :          1 :     BOOST_CHECK(CheckSignetBlockSolution(block, signet_params->GetConsensus()));
#      93                 :            : 
#      94                 :            :     // no data after header, valid
#      95                 :          1 :     std::vector<uint8_t> witness_commitment_section_325{0xec, 0xc7, 0xda, 0xa2};
#      96                 :          1 :     cb.vout.at(0).scriptPubKey = CScript{} << OP_RETURN << witness_commitment_section_141 << witness_commitment_section_325;
#      97                 :          1 :     block.vtx.at(0) = MakeTransactionRef(cb);
#      98                 :          1 :     BOOST_CHECK(SignetTxs::Create(block, challenge));
#      99                 :          1 :     BOOST_CHECK(CheckSignetBlockSolution(block, signet_params->GetConsensus()));
#     100                 :            : 
#     101                 :            :     // Premature end of data, invalid
#     102                 :          1 :     witness_commitment_section_325.push_back(0x01);
#     103                 :          1 :     witness_commitment_section_325.push_back(0x51);
#     104                 :          1 :     cb.vout.at(0).scriptPubKey = CScript{} << OP_RETURN << witness_commitment_section_141 << witness_commitment_section_325;
#     105                 :          1 :     block.vtx.at(0) = MakeTransactionRef(cb);
#     106                 :          1 :     BOOST_CHECK(!SignetTxs::Create(block, challenge));
#     107                 :          1 :     BOOST_CHECK(!CheckSignetBlockSolution(block, signet_params->GetConsensus()));
#     108                 :            : 
#     109                 :            :     // has data, valid
#     110                 :          1 :     witness_commitment_section_325.push_back(0x00);
#     111                 :          1 :     cb.vout.at(0).scriptPubKey = CScript{} << OP_RETURN << witness_commitment_section_141 << witness_commitment_section_325;
#     112                 :          1 :     block.vtx.at(0) = MakeTransactionRef(cb);
#     113                 :          1 :     BOOST_CHECK(SignetTxs::Create(block, challenge));
#     114                 :          1 :     BOOST_CHECK(CheckSignetBlockSolution(block, signet_params->GetConsensus()));
#     115                 :            : 
#     116                 :            :     // Extraneous data, invalid
#     117                 :          1 :     witness_commitment_section_325.push_back(0x00);
#     118                 :          1 :     cb.vout.at(0).scriptPubKey = CScript{} << OP_RETURN << witness_commitment_section_141 << witness_commitment_section_325;
#     119                 :          1 :     block.vtx.at(0) = MakeTransactionRef(cb);
#     120                 :          1 :     BOOST_CHECK(!SignetTxs::Create(block, challenge));
#     121                 :          1 :     BOOST_CHECK(!CheckSignetBlockSolution(block, signet_params->GetConsensus()));
#     122                 :          1 : }
#     123                 :            : 
#     124                 :            : //! Test retrieval of valid assumeutxo values.
#     125                 :            : BOOST_AUTO_TEST_CASE(test_assumeutxo)
#     126                 :          1 : {
#     127                 :          1 :     const auto params = CreateChainParams(*m_node.args, CBaseChainParams::REGTEST);
#     128                 :            : 
#     129                 :            :     // These heights don't have assumeutxo configurations associated, per the contents
#     130                 :            :     // of chainparams.cpp.
#     131                 :          1 :     std::vector<int> bad_heights{0, 100, 111, 115, 209, 211};
#     132                 :            : 
#     133         [ +  + ]:          6 :     for (auto empty : bad_heights) {
#     134                 :          6 :         const auto out = ExpectedAssumeutxo(empty, *params);
#     135                 :          6 :         BOOST_CHECK(!out);
#     136                 :          6 :     }
#     137                 :            : 
#     138                 :          1 :     const auto out110 = *ExpectedAssumeutxo(110, *params);
#     139                 :          1 :     BOOST_CHECK_EQUAL(out110.hash_serialized.ToString(), "1ebbf5850204c0bdb15bf030f47c7fe91d45c44c712697e4509ba67adb01c618");
#     140                 :          1 :     BOOST_CHECK_EQUAL(out110.nChainTx, 110U);
#     141                 :            : 
#     142                 :          1 :     const auto out210 = *ExpectedAssumeutxo(200, *params);
#     143                 :          1 :     BOOST_CHECK_EQUAL(out210.hash_serialized.ToString(), "51c8d11d8b5c1de51543c579736e786aa2736206d1e11e627568029ce092cf62");
#     144                 :          1 :     BOOST_CHECK_EQUAL(out210.nChainTx, 200U);
#     145                 :          1 : }
#     146                 :            : 
#     147                 :            : BOOST_AUTO_TEST_SUITE_END()

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