From 797713284c52d6fb8c1a43a4c38f56255c3feaee Mon Sep 17 00:00:00 2001 From: Eirik Ogilvie-Wigley Date: Fri, 2 Aug 2019 19:37:02 -0600 Subject: [PATCH] Update main_tests for shorter block times --- src/test/main_tests.cpp | 83 ++++++++++++++++++++++++++--------------- 1 file changed, 53 insertions(+), 30 deletions(-) diff --git a/src/test/main_tests.cpp b/src/test/main_tests.cpp index b731e8913..8af388473 100644 --- a/src/test/main_tests.cpp +++ b/src/test/main_tests.cpp @@ -10,78 +10,101 @@ #include #include + BOOST_FIXTURE_TEST_SUITE(main_tests, TestingSetup) +const CAmount INITIAL_SUBSIDY = 12.5 * COIN; + +static int GetTotalHalvings(const Consensus::Params& consensusParams) { + return consensusParams.vUpgrades[Consensus::UPGRADE_BLOSSOM].nActivationHeight == Consensus::NetworkUpgrade::NO_ACTIVATION_HEIGHT ? 64 : 65; +} + static void TestBlockSubsidyHalvings(const Consensus::Params& consensusParams) { - int maxHalvings = 64; - CAmount nInitialSubsidy = 12.5 * COIN; - - CAmount nPreviousSubsidy = nInitialSubsidy * 2; // for height == 0 - BOOST_CHECK_EQUAL(nPreviousSubsidy, nInitialSubsidy * 2); - for (int nHalvings = 0; nHalvings < maxHalvings; nHalvings++) { - int nHeight; - if (nHalvings > 0) // Check subsidy right at halvings + bool blossomActive = false; + int blossomActivationHeight = consensusParams.vUpgrades[Consensus::UPGRADE_BLOSSOM].nActivationHeight; + int nHeight = consensusParams.nSubsidySlowStartInterval; + BOOST_CHECK_EQUAL(GetBlockSubsidy(nHeight, consensusParams), INITIAL_SUBSIDY); + CAmount nPreviousSubsidy = INITIAL_SUBSIDY; + for (int nHalvings = 1; nHalvings < GetTotalHalvings(consensusParams); nHalvings++) { + if (blossomActive) { + if (nHeight == blossomActivationHeight) { + int preBlossomHeight = (nHalvings - 1) * consensusParams.nPreBlossomSubsidyHalvingInterval + consensusParams.SubsidySlowStartShift(); + nHeight += (preBlossomHeight - blossomActivationHeight) * Consensus::BLOSSOM_POW_TARGET_SPACING_RATIO; + } else { + nHeight += consensusParams.nPostBlossomSubsidyHalvingInterval; + } + } else { nHeight = nHalvings * consensusParams.nPreBlossomSubsidyHalvingInterval + consensusParams.SubsidySlowStartShift(); - else // Check subsidy just after end of slow start - nHeight = consensusParams.nSubsidySlowStartInterval; + if (consensusParams.NetworkUpgradeActive(nHeight, Consensus::UPGRADE_BLOSSOM)) { + nHeight = blossomActivationHeight; + blossomActive = true; + } + } + BOOST_CHECK_EQUAL(GetBlockSubsidy(nHeight - 1, consensusParams), nPreviousSubsidy); CAmount nSubsidy = GetBlockSubsidy(nHeight, consensusParams); - BOOST_CHECK(nSubsidy <= nInitialSubsidy); + BOOST_CHECK(nSubsidy <= INITIAL_SUBSIDY); BOOST_CHECK_EQUAL(nSubsidy, nPreviousSubsidy / 2); nPreviousSubsidy = nSubsidy; } - BOOST_CHECK_EQUAL(GetBlockSubsidy((maxHalvings * consensusParams.nPreBlossomSubsidyHalvingInterval) + consensusParams.SubsidySlowStartShift(), consensusParams), 0); + BOOST_CHECK_EQUAL(GetBlockSubsidy(nHeight, consensusParams), 0); } -static void TestBlockSubsidyHalvings(int nSubsidySlowStartInterval, int nPreBlossomSubsidyHalvingInterval) +static void TestBlockSubsidyHalvings(int nSubsidySlowStartInterval, int nPreBlossomSubsidyHalvingInterval, int blossomActivationHeight) { Consensus::Params consensusParams; consensusParams.nSubsidySlowStartInterval = nSubsidySlowStartInterval; consensusParams.nPreBlossomSubsidyHalvingInterval = nPreBlossomSubsidyHalvingInterval; + consensusParams.nPostBlossomSubsidyHalvingInterval = nPreBlossomSubsidyHalvingInterval * 2; + consensusParams.vUpgrades[Consensus::UPGRADE_BLOSSOM].nActivationHeight = blossomActivationHeight; TestBlockSubsidyHalvings(consensusParams); } BOOST_AUTO_TEST_CASE(block_subsidy_test) { TestBlockSubsidyHalvings(Params(CBaseChainParams::MAIN).GetConsensus()); // As in main - TestBlockSubsidyHalvings(50, 150); // As in regtest - TestBlockSubsidyHalvings(500, 1000); // Just another interval + TestBlockSubsidyHalvings(20000, Consensus::PRE_BLOSSOM_HALVING_INTERVAL, Consensus::NetworkUpgrade::NO_ACTIVATION_HEIGHT); // Pre-Blossom + TestBlockSubsidyHalvings(50, 150, 80); // As in regtest + TestBlockSubsidyHalvings(500, 1000, 900); // Just another interval + TestBlockSubsidyHalvings(500, 1000, 3000); // Multiple halvings before activation } BOOST_AUTO_TEST_CASE(subsidy_limit_test) { const Consensus::Params& consensusParams = Params(CBaseChainParams::MAIN).GetConsensus(); + CAmount nSum = 0; + int nHeight = 0; // Mining slow start - for (int nHeight = 0; nHeight < consensusParams.nSubsidySlowStartInterval; nHeight ++) { + for (; nHeight < consensusParams.nSubsidySlowStartInterval; nHeight++) { CAmount nSubsidy = GetBlockSubsidy(nHeight, consensusParams); BOOST_CHECK(nSubsidy <= 12.5 * COIN); nSum += nSubsidy; BOOST_CHECK(MoneyRange(nSum)); } BOOST_CHECK_EQUAL(nSum, 12500000000000ULL); - // Remainder of first period - for (int nHeight = consensusParams.nSubsidySlowStartInterval; nHeight < consensusParams.nPreBlossomSubsidyHalvingInterval + consensusParams.SubsidySlowStartShift(); nHeight ++) { - CAmount nSubsidy = GetBlockSubsidy(nHeight, consensusParams); - BOOST_CHECK(nSubsidy <= 12.5 * COIN); - nSum += nSubsidy; - BOOST_CHECK(MoneyRange(nSum)); - } - BOOST_CHECK_EQUAL(nSum, 1050000000000000ULL); + // Regular mining - for (int nHeight = consensusParams.nPreBlossomSubsidyHalvingInterval + consensusParams.SubsidySlowStartShift(); nHeight < 56000000; nHeight += 1000) { - CAmount nSubsidy = GetBlockSubsidy(nHeight, consensusParams); - BOOST_CHECK(nSubsidy <= 12.5 * COIN); - nSum += nSubsidy * 1000; - BOOST_CHECK(MoneyRange(nSum)); - } + CAmount nSubsidy; + do { + nSubsidy = GetBlockSubsidy(nHeight, consensusParams); + nSum += nSubsidy; + BOOST_ASSERT(MoneyRange(nSum)); + ++nHeight; + } while (nSubsidy > 0); + // Changing the block interval from 10 to 2.5 minutes causes truncation // effects to occur earlier (from the 9th halving interval instead of the // 11th), decreasing the total monetary supply by 0.0693 ZEC. If the // transaction output field is widened, this discrepancy will become smaller // or disappear entirely. //BOOST_CHECK_EQUAL(nSum, 2099999997690000ULL); + // Reducing the interval further to 1.25 minutes has a similar effect, + // decreasing the total monetary supply by another 0.09240 ZEC. + // TODO Change this assert when setting the blossom activation height + // Note that these numbers may or may not change depending on the activation height BOOST_CHECK_EQUAL(nSum, 2099999990760000ULL); + // BOOST_CHECK_EQUAL(nSum, 2099999981520000LL); } bool ReturnFalse() { return false; }