Update main_tests for shorter block times

This commit is contained in:
Eirik Ogilvie-Wigley 2019-08-02 19:37:02 -06:00
parent 27ee4d64f2
commit 797713284c
1 changed files with 53 additions and 30 deletions

View File

@ -10,78 +10,101 @@
#include <boost/signals2/signal.hpp> #include <boost/signals2/signal.hpp>
#include <boost/test/unit_test.hpp> #include <boost/test/unit_test.hpp>
BOOST_FIXTURE_TEST_SUITE(main_tests, TestingSetup) 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) static void TestBlockSubsidyHalvings(const Consensus::Params& consensusParams)
{ {
int maxHalvings = 64; bool blossomActive = false;
CAmount nInitialSubsidy = 12.5 * COIN; int blossomActivationHeight = consensusParams.vUpgrades[Consensus::UPGRADE_BLOSSOM].nActivationHeight;
int nHeight = consensusParams.nSubsidySlowStartInterval;
CAmount nPreviousSubsidy = nInitialSubsidy * 2; // for height == 0 BOOST_CHECK_EQUAL(GetBlockSubsidy(nHeight, consensusParams), INITIAL_SUBSIDY);
BOOST_CHECK_EQUAL(nPreviousSubsidy, nInitialSubsidy * 2); CAmount nPreviousSubsidy = INITIAL_SUBSIDY;
for (int nHalvings = 0; nHalvings < maxHalvings; nHalvings++) { for (int nHalvings = 1; nHalvings < GetTotalHalvings(consensusParams); nHalvings++) {
int nHeight; if (blossomActive) {
if (nHalvings > 0) // Check subsidy right at halvings 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(); nHeight = nHalvings * consensusParams.nPreBlossomSubsidyHalvingInterval + consensusParams.SubsidySlowStartShift();
else // Check subsidy just after end of slow start if (consensusParams.NetworkUpgradeActive(nHeight, Consensus::UPGRADE_BLOSSOM)) {
nHeight = consensusParams.nSubsidySlowStartInterval; nHeight = blossomActivationHeight;
blossomActive = true;
}
}
BOOST_CHECK_EQUAL(GetBlockSubsidy(nHeight - 1, consensusParams), nPreviousSubsidy);
CAmount nSubsidy = GetBlockSubsidy(nHeight, consensusParams); CAmount nSubsidy = GetBlockSubsidy(nHeight, consensusParams);
BOOST_CHECK(nSubsidy <= nInitialSubsidy); BOOST_CHECK(nSubsidy <= INITIAL_SUBSIDY);
BOOST_CHECK_EQUAL(nSubsidy, nPreviousSubsidy / 2); BOOST_CHECK_EQUAL(nSubsidy, nPreviousSubsidy / 2);
nPreviousSubsidy = nSubsidy; 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; Consensus::Params consensusParams;
consensusParams.nSubsidySlowStartInterval = nSubsidySlowStartInterval; consensusParams.nSubsidySlowStartInterval = nSubsidySlowStartInterval;
consensusParams.nPreBlossomSubsidyHalvingInterval = nPreBlossomSubsidyHalvingInterval; consensusParams.nPreBlossomSubsidyHalvingInterval = nPreBlossomSubsidyHalvingInterval;
consensusParams.nPostBlossomSubsidyHalvingInterval = nPreBlossomSubsidyHalvingInterval * 2;
consensusParams.vUpgrades[Consensus::UPGRADE_BLOSSOM].nActivationHeight = blossomActivationHeight;
TestBlockSubsidyHalvings(consensusParams); TestBlockSubsidyHalvings(consensusParams);
} }
BOOST_AUTO_TEST_CASE(block_subsidy_test) BOOST_AUTO_TEST_CASE(block_subsidy_test)
{ {
TestBlockSubsidyHalvings(Params(CBaseChainParams::MAIN).GetConsensus()); // As in main TestBlockSubsidyHalvings(Params(CBaseChainParams::MAIN).GetConsensus()); // As in main
TestBlockSubsidyHalvings(50, 150); // As in regtest TestBlockSubsidyHalvings(20000, Consensus::PRE_BLOSSOM_HALVING_INTERVAL, Consensus::NetworkUpgrade::NO_ACTIVATION_HEIGHT); // Pre-Blossom
TestBlockSubsidyHalvings(500, 1000); // Just another interval 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) BOOST_AUTO_TEST_CASE(subsidy_limit_test)
{ {
const Consensus::Params& consensusParams = Params(CBaseChainParams::MAIN).GetConsensus(); const Consensus::Params& consensusParams = Params(CBaseChainParams::MAIN).GetConsensus();
CAmount nSum = 0; CAmount nSum = 0;
int nHeight = 0;
// Mining slow start // Mining slow start
for (int nHeight = 0; nHeight < consensusParams.nSubsidySlowStartInterval; nHeight ++) { for (; nHeight < consensusParams.nSubsidySlowStartInterval; nHeight++) {
CAmount nSubsidy = GetBlockSubsidy(nHeight, consensusParams); CAmount nSubsidy = GetBlockSubsidy(nHeight, consensusParams);
BOOST_CHECK(nSubsidy <= 12.5 * COIN); BOOST_CHECK(nSubsidy <= 12.5 * COIN);
nSum += nSubsidy; nSum += nSubsidy;
BOOST_CHECK(MoneyRange(nSum)); BOOST_CHECK(MoneyRange(nSum));
} }
BOOST_CHECK_EQUAL(nSum, 12500000000000ULL); 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 // Regular mining
for (int nHeight = consensusParams.nPreBlossomSubsidyHalvingInterval + consensusParams.SubsidySlowStartShift(); nHeight < 56000000; nHeight += 1000) { CAmount nSubsidy;
CAmount nSubsidy = GetBlockSubsidy(nHeight, consensusParams); do {
BOOST_CHECK(nSubsidy <= 12.5 * COIN); nSubsidy = GetBlockSubsidy(nHeight, consensusParams);
nSum += nSubsidy * 1000; nSum += nSubsidy;
BOOST_CHECK(MoneyRange(nSum)); BOOST_ASSERT(MoneyRange(nSum));
} ++nHeight;
} while (nSubsidy > 0);
// Changing the block interval from 10 to 2.5 minutes causes truncation // Changing the block interval from 10 to 2.5 minutes causes truncation
// effects to occur earlier (from the 9th halving interval instead of the // effects to occur earlier (from the 9th halving interval instead of the
// 11th), decreasing the total monetary supply by 0.0693 ZEC. If the // 11th), decreasing the total monetary supply by 0.0693 ZEC. If the
// transaction output field is widened, this discrepancy will become smaller // transaction output field is widened, this discrepancy will become smaller
// or disappear entirely. // or disappear entirely.
//BOOST_CHECK_EQUAL(nSum, 2099999997690000ULL); //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, 2099999990760000ULL);
// BOOST_CHECK_EQUAL(nSum, 2099999981520000LL);
} }
bool ReturnFalse() { return false; } bool ReturnFalse() { return false; }