2020-12-31 05:48:25 -03:00
|
|
|
// Copyright (c) 2014-2020 The Bitcoin Core developers
|
2014-12-13 01:09:33 -03:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
2014-03-18 06:11:00 -03:00
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
2017-11-09 21:57:53 -03:00
|
|
|
#include <chainparams.h>
|
|
|
|
#include <net.h>
|
2020-09-23 11:17:50 -03:00
|
|
|
#include <signet.h>
|
2019-04-25 11:09:29 -04:00
|
|
|
#include <uint256.h>
|
2019-02-28 17:02:35 -03:00
|
|
|
#include <validation.h>
|
2014-02-18 21:54:11 -03:00
|
|
|
|
2019-11-05 17:18:59 -03:00
|
|
|
#include <test/util/setup_common.h>
|
2015-03-09 11:04:12 -03:00
|
|
|
|
2014-02-18 21:54:11 -03:00
|
|
|
#include <boost/test/unit_test.hpp>
|
|
|
|
|
2019-02-28 17:02:35 -03:00
|
|
|
BOOST_FIXTURE_TEST_SUITE(validation_tests, TestingSetup)
|
2014-02-18 21:54:11 -03:00
|
|
|
|
2015-04-01 11:03:11 -03:00
|
|
|
static void TestBlockSubsidyHalvings(const Consensus::Params& consensusParams)
|
|
|
|
{
|
|
|
|
int maxHalvings = 64;
|
|
|
|
CAmount nInitialSubsidy = 50 * COIN;
|
|
|
|
|
|
|
|
CAmount nPreviousSubsidy = nInitialSubsidy * 2; // for height == 0
|
|
|
|
BOOST_CHECK_EQUAL(nPreviousSubsidy, nInitialSubsidy * 2);
|
|
|
|
for (int nHalvings = 0; nHalvings < maxHalvings; nHalvings++) {
|
|
|
|
int nHeight = nHalvings * consensusParams.nSubsidyHalvingInterval;
|
|
|
|
CAmount nSubsidy = GetBlockSubsidy(nHeight, consensusParams);
|
|
|
|
BOOST_CHECK(nSubsidy <= nInitialSubsidy);
|
|
|
|
BOOST_CHECK_EQUAL(nSubsidy, nPreviousSubsidy / 2);
|
|
|
|
nPreviousSubsidy = nSubsidy;
|
|
|
|
}
|
|
|
|
BOOST_CHECK_EQUAL(GetBlockSubsidy(maxHalvings * consensusParams.nSubsidyHalvingInterval, consensusParams), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void TestBlockSubsidyHalvings(int nSubsidyHalvingInterval)
|
|
|
|
{
|
|
|
|
Consensus::Params consensusParams;
|
|
|
|
consensusParams.nSubsidyHalvingInterval = nSubsidyHalvingInterval;
|
|
|
|
TestBlockSubsidyHalvings(consensusParams);
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(block_subsidy_test)
|
|
|
|
{
|
2020-09-24 10:11:27 -03:00
|
|
|
const auto chainParams = CreateChainParams(*m_node.args, CBaseChainParams::MAIN);
|
2015-11-28 11:04:35 -03:00
|
|
|
TestBlockSubsidyHalvings(chainParams->GetConsensus()); // As in main
|
2015-04-01 11:03:11 -03:00
|
|
|
TestBlockSubsidyHalvings(150); // As in regtest
|
|
|
|
TestBlockSubsidyHalvings(1000); // Just another interval
|
|
|
|
}
|
|
|
|
|
2014-02-18 21:54:11 -03:00
|
|
|
BOOST_AUTO_TEST_CASE(subsidy_limit_test)
|
|
|
|
{
|
2020-09-24 10:11:27 -03:00
|
|
|
const auto chainParams = CreateChainParams(*m_node.args, CBaseChainParams::MAIN);
|
2014-04-22 19:46:19 -03:00
|
|
|
CAmount nSum = 0;
|
2014-04-02 20:00:08 -03:00
|
|
|
for (int nHeight = 0; nHeight < 14000000; nHeight += 1000) {
|
2015-11-28 11:04:35 -03:00
|
|
|
CAmount nSubsidy = GetBlockSubsidy(nHeight, chainParams->GetConsensus());
|
2014-02-18 21:54:11 -03:00
|
|
|
BOOST_CHECK(nSubsidy <= 50 * COIN);
|
|
|
|
nSum += nSubsidy * 1000;
|
|
|
|
BOOST_CHECK(MoneyRange(nSum));
|
|
|
|
}
|
2018-04-09 04:50:19 -03:00
|
|
|
BOOST_CHECK_EQUAL(nSum, CAmount{2099999997690000});
|
2014-02-18 21:54:11 -03:00
|
|
|
}
|
|
|
|
|
2020-09-23 11:17:50 -03:00
|
|
|
BOOST_AUTO_TEST_CASE(signet_parse_tests)
|
|
|
|
{
|
|
|
|
ArgsManager signet_argsman;
|
|
|
|
signet_argsman.ForceSetArg("-signetchallenge", "51"); // set challenge to OP_TRUE
|
|
|
|
const auto signet_params = CreateChainParams(signet_argsman, CBaseChainParams::SIGNET);
|
|
|
|
CBlock block;
|
2020-10-02 01:30:50 -03:00
|
|
|
BOOST_CHECK(signet_params->GetConsensus().signet_challenge == std::vector<uint8_t>{OP_TRUE});
|
2020-09-23 11:17:50 -03:00
|
|
|
CScript challenge{OP_TRUE};
|
|
|
|
|
|
|
|
// empty block is invalid
|
|
|
|
BOOST_CHECK(!SignetTxs::Create(block, challenge));
|
|
|
|
BOOST_CHECK(!CheckSignetBlockSolution(block, signet_params->GetConsensus()));
|
|
|
|
|
|
|
|
// no witness commitment
|
|
|
|
CMutableTransaction cb;
|
|
|
|
cb.vout.emplace_back(0, CScript{});
|
|
|
|
block.vtx.push_back(MakeTransactionRef(cb));
|
2020-12-24 10:37:17 -03:00
|
|
|
block.vtx.push_back(MakeTransactionRef(cb)); // Add dummy tx to exercise merkle root code
|
2020-09-23 11:17:50 -03:00
|
|
|
BOOST_CHECK(!SignetTxs::Create(block, challenge));
|
|
|
|
BOOST_CHECK(!CheckSignetBlockSolution(block, signet_params->GetConsensus()));
|
|
|
|
|
|
|
|
// no header is treated valid
|
|
|
|
std::vector<uint8_t> witness_commitment_section_141{0xaa, 0x21, 0xa9, 0xed};
|
|
|
|
for (int i = 0; i < 32; ++i) {
|
|
|
|
witness_commitment_section_141.push_back(0xff);
|
|
|
|
}
|
|
|
|
cb.vout.at(0).scriptPubKey = CScript{} << OP_RETURN << witness_commitment_section_141;
|
|
|
|
block.vtx.at(0) = MakeTransactionRef(cb);
|
|
|
|
BOOST_CHECK(SignetTxs::Create(block, challenge));
|
|
|
|
BOOST_CHECK(CheckSignetBlockSolution(block, signet_params->GetConsensus()));
|
|
|
|
|
|
|
|
// no data after header, valid
|
|
|
|
std::vector<uint8_t> witness_commitment_section_325{0xec, 0xc7, 0xda, 0xa2};
|
|
|
|
cb.vout.at(0).scriptPubKey = CScript{} << OP_RETURN << witness_commitment_section_141 << witness_commitment_section_325;
|
|
|
|
block.vtx.at(0) = MakeTransactionRef(cb);
|
|
|
|
BOOST_CHECK(SignetTxs::Create(block, challenge));
|
|
|
|
BOOST_CHECK(CheckSignetBlockSolution(block, signet_params->GetConsensus()));
|
|
|
|
|
|
|
|
// Premature end of data, invalid
|
|
|
|
witness_commitment_section_325.push_back(0x01);
|
|
|
|
witness_commitment_section_325.push_back(0x51);
|
|
|
|
cb.vout.at(0).scriptPubKey = CScript{} << OP_RETURN << witness_commitment_section_141 << witness_commitment_section_325;
|
|
|
|
block.vtx.at(0) = MakeTransactionRef(cb);
|
|
|
|
BOOST_CHECK(!SignetTxs::Create(block, challenge));
|
|
|
|
BOOST_CHECK(!CheckSignetBlockSolution(block, signet_params->GetConsensus()));
|
|
|
|
|
|
|
|
// has data, valid
|
|
|
|
witness_commitment_section_325.push_back(0x00);
|
|
|
|
cb.vout.at(0).scriptPubKey = CScript{} << OP_RETURN << witness_commitment_section_141 << witness_commitment_section_325;
|
|
|
|
block.vtx.at(0) = MakeTransactionRef(cb);
|
|
|
|
BOOST_CHECK(SignetTxs::Create(block, challenge));
|
|
|
|
BOOST_CHECK(CheckSignetBlockSolution(block, signet_params->GetConsensus()));
|
|
|
|
|
|
|
|
// Extraneous data, invalid
|
|
|
|
witness_commitment_section_325.push_back(0x00);
|
|
|
|
cb.vout.at(0).scriptPubKey = CScript{} << OP_RETURN << witness_commitment_section_141 << witness_commitment_section_325;
|
|
|
|
block.vtx.at(0) = MakeTransactionRef(cb);
|
|
|
|
BOOST_CHECK(!SignetTxs::Create(block, challenge));
|
|
|
|
BOOST_CHECK(!CheckSignetBlockSolution(block, signet_params->GetConsensus()));
|
|
|
|
}
|
|
|
|
|
2019-04-25 11:09:29 -04:00
|
|
|
//! Test retrieval of valid assumeutxo values.
|
|
|
|
BOOST_AUTO_TEST_CASE(test_assumeutxo)
|
|
|
|
{
|
|
|
|
const auto params = CreateChainParams(*m_node.args, CBaseChainParams::REGTEST);
|
|
|
|
|
|
|
|
// These heights don't have assumeutxo configurations associated, per the contents
|
|
|
|
// of chainparams.cpp.
|
|
|
|
std::vector<int> bad_heights{0, 100, 111, 115, 209, 211};
|
|
|
|
|
|
|
|
for (auto empty : bad_heights) {
|
|
|
|
const auto out = ExpectedAssumeutxo(empty, *params);
|
|
|
|
BOOST_CHECK(!out);
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto out110 = *ExpectedAssumeutxo(110, *params);
|
|
|
|
BOOST_CHECK_EQUAL(out110.hash_serialized, uint256S("76fd7334ac7c1baf57ddc0c626f073a655a35d98a4258cd1382c8cc2b8392e10"));
|
|
|
|
BOOST_CHECK_EQUAL(out110.nChainTx, (unsigned int)110);
|
|
|
|
|
|
|
|
const auto out210 = *ExpectedAssumeutxo(210, *params);
|
|
|
|
BOOST_CHECK_EQUAL(out210.hash_serialized, uint256S("9c5ed99ef98544b34f8920b6d1802f72ac28ae6e2bd2bd4c316ff10c230df3f2"));
|
|
|
|
BOOST_CHECK_EQUAL(out210.nChainTx, (unsigned int)210);
|
|
|
|
}
|
|
|
|
|
2014-02-18 21:54:11 -03:00
|
|
|
BOOST_AUTO_TEST_SUITE_END()
|