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