2014-03-18 02:11:00 -07:00
|
|
|
// Copyright (c) 2014 The Bitcoin Core developers
|
2023-01-23 10:31:54 -08:00
|
|
|
// Copyright (c) 2016-2023 The Zcash developers
|
2014-12-12 20:09:33 -08:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
2019-07-18 07:16:09 -07:00
|
|
|
// file COPYING or https://www.opensource.org/licenses/mit-license.php .
|
2014-03-18 02:11:00 -07:00
|
|
|
|
2015-04-01 07:03:11 -07:00
|
|
|
#include "chainparams.h"
|
2014-02-18 16:54:11 -08:00
|
|
|
#include "main.h"
|
|
|
|
|
2015-03-09 07:04:12 -07:00
|
|
|
#include "test/test_bitcoin.h"
|
|
|
|
|
2015-04-01 07:03:11 -07:00
|
|
|
#include <boost/signals2/signal.hpp>
|
2014-02-18 16:54:11 -08:00
|
|
|
#include <boost/test/unit_test.hpp>
|
|
|
|
|
2019-08-02 18:37:02 -07:00
|
|
|
|
2015-03-09 07:04:12 -07:00
|
|
|
BOOST_FIXTURE_TEST_SUITE(main_tests, TestingSetup)
|
2014-02-18 16:54:11 -08:00
|
|
|
|
2019-08-02 18:37:02 -07:00
|
|
|
const CAmount INITIAL_SUBSIDY = 12.5 * COIN;
|
|
|
|
|
|
|
|
static int GetTotalHalvings(const Consensus::Params& consensusParams) {
|
2019-08-05 13:52:34 -07:00
|
|
|
// This assumes that BLOSSOM_POW_TARGET_SPACING_RATIO == 2
|
|
|
|
// and treats blossom activation as a halving event
|
2019-08-02 18:37:02 -07:00
|
|
|
return consensusParams.vUpgrades[Consensus::UPGRADE_BLOSSOM].nActivationHeight == Consensus::NetworkUpgrade::NO_ACTIVATION_HEIGHT ? 64 : 65;
|
|
|
|
}
|
|
|
|
|
2015-04-01 07:03:11 -07:00
|
|
|
static void TestBlockSubsidyHalvings(const Consensus::Params& consensusParams)
|
|
|
|
{
|
2019-08-02 18:37:02 -07:00
|
|
|
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 {
|
2019-07-29 12:44:53 -07:00
|
|
|
nHeight = nHalvings * consensusParams.nPreBlossomSubsidyHalvingInterval + consensusParams.SubsidySlowStartShift();
|
2019-08-02 18:37:02 -07:00
|
|
|
if (consensusParams.NetworkUpgradeActive(nHeight, Consensus::UPGRADE_BLOSSOM)) {
|
|
|
|
nHeight = blossomActivationHeight;
|
|
|
|
blossomActive = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
BOOST_CHECK_EQUAL(GetBlockSubsidy(nHeight - 1, consensusParams), nPreviousSubsidy);
|
2015-04-01 07:03:11 -07:00
|
|
|
CAmount nSubsidy = GetBlockSubsidy(nHeight, consensusParams);
|
2019-08-02 18:37:02 -07:00
|
|
|
BOOST_CHECK(nSubsidy <= INITIAL_SUBSIDY);
|
2015-04-01 07:03:11 -07:00
|
|
|
BOOST_CHECK_EQUAL(nSubsidy, nPreviousSubsidy / 2);
|
|
|
|
nPreviousSubsidy = nSubsidy;
|
|
|
|
}
|
2019-08-02 18:37:02 -07:00
|
|
|
BOOST_CHECK_EQUAL(GetBlockSubsidy(nHeight, consensusParams), 0);
|
2015-04-01 07:03:11 -07:00
|
|
|
}
|
|
|
|
|
2019-08-02 18:37:02 -07:00
|
|
|
static void TestBlockSubsidyHalvings(int nSubsidySlowStartInterval, int nPreBlossomSubsidyHalvingInterval, int blossomActivationHeight)
|
2015-04-01 07:03:11 -07:00
|
|
|
{
|
|
|
|
Consensus::Params consensusParams;
|
2016-04-22 18:16:17 -07:00
|
|
|
consensusParams.nSubsidySlowStartInterval = nSubsidySlowStartInterval;
|
2019-07-29 12:44:53 -07:00
|
|
|
consensusParams.nPreBlossomSubsidyHalvingInterval = nPreBlossomSubsidyHalvingInterval;
|
2019-08-05 13:52:34 -07:00
|
|
|
consensusParams.nPostBlossomSubsidyHalvingInterval = nPreBlossomSubsidyHalvingInterval * Consensus::BLOSSOM_POW_TARGET_SPACING_RATIO;
|
2019-08-02 18:37:02 -07:00
|
|
|
consensusParams.vUpgrades[Consensus::UPGRADE_BLOSSOM].nActivationHeight = blossomActivationHeight;
|
2015-04-01 07:03:11 -07:00
|
|
|
TestBlockSubsidyHalvings(consensusParams);
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(block_subsidy_test)
|
|
|
|
{
|
|
|
|
TestBlockSubsidyHalvings(Params(CBaseChainParams::MAIN).GetConsensus()); // As in main
|
2019-08-02 18:37:02 -07:00
|
|
|
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
|
2019-08-05 11:41:31 -07:00
|
|
|
TestBlockSubsidyHalvings(500, 1000, 3000); // Multiple halvings before Blossom activation
|
2015-04-01 07:03:11 -07:00
|
|
|
}
|
|
|
|
|
2014-02-18 16:54:11 -08:00
|
|
|
BOOST_AUTO_TEST_CASE(subsidy_limit_test)
|
|
|
|
{
|
2015-04-01 07:03:11 -07:00
|
|
|
const Consensus::Params& consensusParams = Params(CBaseChainParams::MAIN).GetConsensus();
|
2019-08-02 18:37:02 -07:00
|
|
|
|
2014-04-22 15:46:19 -07:00
|
|
|
CAmount nSum = 0;
|
2019-08-02 18:37:02 -07:00
|
|
|
int nHeight = 0;
|
2016-04-22 18:16:17 -07:00
|
|
|
// Mining slow start
|
2019-08-02 18:37:02 -07:00
|
|
|
for (; nHeight < consensusParams.nSubsidySlowStartInterval; nHeight++) {
|
2016-04-22 18:16:17 -07:00
|
|
|
CAmount nSubsidy = GetBlockSubsidy(nHeight, consensusParams);
|
2019-08-05 13:52:34 -07:00
|
|
|
BOOST_CHECK(nSubsidy <= INITIAL_SUBSIDY);
|
2016-04-22 18:16:17 -07:00
|
|
|
nSum += nSubsidy;
|
|
|
|
BOOST_CHECK(MoneyRange(nSum));
|
|
|
|
}
|
|
|
|
BOOST_CHECK_EQUAL(nSum, 12500000000000ULL);
|
2019-08-02 18:37:02 -07:00
|
|
|
|
2016-04-22 18:16:17 -07:00
|
|
|
// Regular mining
|
2019-08-02 18:37:02 -07:00
|
|
|
CAmount nSubsidy;
|
|
|
|
do {
|
|
|
|
nSubsidy = GetBlockSubsidy(nHeight, consensusParams);
|
2019-08-05 13:52:34 -07:00
|
|
|
BOOST_CHECK(nSubsidy <= INITIAL_SUBSIDY);
|
2019-08-02 18:37:02 -07:00
|
|
|
nSum += nSubsidy;
|
|
|
|
BOOST_ASSERT(MoneyRange(nSum));
|
|
|
|
++nHeight;
|
|
|
|
} while (nSubsidy > 0);
|
|
|
|
|
2016-06-06 22:12:16 -07:00
|
|
|
// Changing the block interval from 10 to 2.5 minutes causes truncation
|
|
|
|
// effects to occur earlier (from the 9th halving interval instead of the
|
2019-10-31 13:55:14 -07:00
|
|
|
// 11th), decreasing the total monetary supply by 0.0693 ZEC.
|
|
|
|
// BOOST_CHECK_EQUAL(nSum, 2099999997690000ULL);
|
2019-08-02 18:37:02 -07:00
|
|
|
// Reducing the interval further to 1.25 minutes has a similar effect,
|
|
|
|
// decreasing the total monetary supply by another 0.09240 ZEC.
|
2019-10-31 13:55:14 -07:00
|
|
|
// BOOST_CHECK_EQUAL(nSum, 2099999990760000ULL);
|
|
|
|
BOOST_CHECK_EQUAL(nSum, 2099999981520000LL);
|
2014-02-18 16:54:11 -08:00
|
|
|
}
|
|
|
|
|
2015-03-05 04:01:01 -08:00
|
|
|
bool ReturnFalse() { return false; }
|
|
|
|
bool ReturnTrue() { return true; }
|
|
|
|
|
|
|
|
BOOST_AUTO_TEST_CASE(test_combiner_all)
|
|
|
|
{
|
|
|
|
boost::signals2::signal<bool (), CombinerAll> Test;
|
|
|
|
BOOST_CHECK(Test());
|
|
|
|
Test.connect(&ReturnFalse);
|
|
|
|
BOOST_CHECK(!Test());
|
|
|
|
Test.connect(&ReturnTrue);
|
|
|
|
BOOST_CHECK(!Test());
|
|
|
|
Test.disconnect(&ReturnFalse);
|
|
|
|
BOOST_CHECK(Test());
|
|
|
|
Test.disconnect(&ReturnTrue);
|
|
|
|
BOOST_CHECK(Test());
|
|
|
|
}
|
2014-02-18 16:54:11 -08:00
|
|
|
BOOST_AUTO_TEST_SUITE_END()
|