Update subsidy tests to account for mining slow start

This commit is contained in:
Jack Grigg 2016-04-23 13:16:17 +12:00
parent bcb34c0802
commit 91818cf28c
1 changed files with 28 additions and 6 deletions

View File

@ -20,18 +20,23 @@ static void TestBlockSubsidyHalvings(const Consensus::Params& consensusParams)
CAmount nPreviousSubsidy = nInitialSubsidy * 2; // for height == 0 CAmount nPreviousSubsidy = nInitialSubsidy * 2; // for height == 0
BOOST_CHECK_EQUAL(nPreviousSubsidy, nInitialSubsidy * 2); BOOST_CHECK_EQUAL(nPreviousSubsidy, nInitialSubsidy * 2);
for (int nHalvings = 0; nHalvings < maxHalvings; nHalvings++) { for (int nHalvings = 0; nHalvings < maxHalvings; nHalvings++) {
int nHeight = nHalvings * consensusParams.nSubsidyHalvingInterval; int nHeight;
if (nHalvings > 0) // Check subsidy right at halvings
nHeight = nHalvings * consensusParams.nSubsidyHalvingInterval + consensusParams.SubsidySlowStartShift();
else // Check subsidy just after end of slow start
nHeight = consensusParams.nSubsidySlowStartInterval;
CAmount nSubsidy = GetBlockSubsidy(nHeight, consensusParams); CAmount nSubsidy = GetBlockSubsidy(nHeight, consensusParams);
BOOST_CHECK(nSubsidy <= nInitialSubsidy); BOOST_CHECK(nSubsidy <= nInitialSubsidy);
BOOST_CHECK_EQUAL(nSubsidy, nPreviousSubsidy / 2); BOOST_CHECK_EQUAL(nSubsidy, nPreviousSubsidy / 2);
nPreviousSubsidy = nSubsidy; nPreviousSubsidy = nSubsidy;
} }
BOOST_CHECK_EQUAL(GetBlockSubsidy(maxHalvings * consensusParams.nSubsidyHalvingInterval, consensusParams), 0); BOOST_CHECK_EQUAL(GetBlockSubsidy((maxHalvings * consensusParams.nSubsidyHalvingInterval) + consensusParams.SubsidySlowStartShift(), consensusParams), 0);
} }
static void TestBlockSubsidyHalvings(int nSubsidyHalvingInterval) static void TestBlockSubsidyHalvings(int nSubsidySlowStartInterval, int nSubsidyHalvingInterval)
{ {
Consensus::Params consensusParams; Consensus::Params consensusParams;
consensusParams.nSubsidySlowStartInterval = nSubsidySlowStartInterval;
consensusParams.nSubsidyHalvingInterval = nSubsidyHalvingInterval; consensusParams.nSubsidyHalvingInterval = nSubsidyHalvingInterval;
TestBlockSubsidyHalvings(consensusParams); TestBlockSubsidyHalvings(consensusParams);
} }
@ -39,15 +44,32 @@ static void TestBlockSubsidyHalvings(int nSubsidyHalvingInterval)
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(150); // As in regtest TestBlockSubsidyHalvings(50, 150); // As in regtest
TestBlockSubsidyHalvings(1000); // Just another interval TestBlockSubsidyHalvings(500, 1000); // Just another interval
} }
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;
for (int nHeight = 0; nHeight < 14000000; nHeight += 1000) { // Mining slow start
for (int nHeight = 0; nHeight < consensusParams.nSubsidySlowStartInterval; nHeight ++) {
CAmount nSubsidy = GetBlockSubsidy(nHeight, consensusParams);
BOOST_CHECK(nSubsidy <= 50 * COIN);
nSum += nSubsidy;
BOOST_CHECK(MoneyRange(nSum));
}
BOOST_CHECK_EQUAL(nSum, 12500000000000ULL);
// Remainder of first period
for (int nHeight = consensusParams.nSubsidySlowStartInterval; nHeight < consensusParams.nSubsidyHalvingInterval + consensusParams.SubsidySlowStartShift(); nHeight ++) {
CAmount nSubsidy = GetBlockSubsidy(nHeight, consensusParams);
BOOST_CHECK(nSubsidy <= 50 * COIN);
nSum += nSubsidy;
BOOST_CHECK(MoneyRange(nSum));
}
BOOST_CHECK_EQUAL(nSum, 1050000000000000ULL);
// Regular mining
for (int nHeight = consensusParams.nSubsidyHalvingInterval + consensusParams.SubsidySlowStartShift(); nHeight < 14000000; nHeight += 1000) {
CAmount nSubsidy = GetBlockSubsidy(nHeight, consensusParams); CAmount nSubsidy = GetBlockSubsidy(nHeight, consensusParams);
BOOST_CHECK(nSubsidy <= 50 * COIN); BOOST_CHECK(nSubsidy <= 50 * COIN);
nSum += nSubsidy * 1000; nSum += nSubsidy * 1000;