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