Fix zip208 founders reward calculation and update test

This commit is contained in:
Eirik Ogilvie-Wigley 2019-07-30 17:41:33 -06:00
parent 8865f4b6f6
commit 1188c9adf7
7 changed files with 31 additions and 21 deletions

View File

@ -18,12 +18,12 @@ namespace Consensus {
// postBlossom: // postBlossom:
// 1 = (H - SS) / preInterval + (height - H) / postInterval // 1 = (H - SS) / preInterval + (height - H) / postInterval
// height = H + postInterval + (SS - H) * (postInterval / preInterval) // height = H + postInterval + (SS - H) * (postInterval / preInterval)
// height = H + postInterval + (SS - H) / R // height = H + postInterval + (SS - H) * R
bool blossomActive = NetworkUpgradeActive(nHeight, Consensus::UPGRADE_BLOSSOM); bool blossomActive = NetworkUpgradeActive(nHeight, Consensus::UPGRADE_BLOSSOM);
if (blossomActive) { if (blossomActive) {
int blossomActivationHeight = vUpgrades[Consensus::UPGRADE_BLOSSOM].nActivationHeight; int blossomActivationHeight = vUpgrades[Consensus::UPGRADE_BLOSSOM].nActivationHeight;
return blossomActivationHeight + nPostBlossomSubsidyHalvingInterval return blossomActivationHeight + nPostBlossomSubsidyHalvingInterval
- (SubsidySlowStartShift() - blossomActivationHeight) / BLOSSOM_POW_TARGET_SPACING_RATIO - 1; + (SubsidySlowStartShift() - blossomActivationHeight) * BLOSSOM_POW_TARGET_SPACING_RATIO - 1;
} else { } else {
return nPreBlossomSubsidyHalvingInterval + SubsidySlowStartShift() - 1; return nPreBlossomSubsidyHalvingInterval + SubsidySlowStartShift() - 1;
} }

View File

@ -67,7 +67,7 @@ static const unsigned int POST_BLOSSOM_POW_TARGET_SPACING = 75;
static_assert(POST_BLOSSOM_POW_TARGET_SPACING < PRE_BLOSSOM_POW_TARGET_SPACING, "Blossom target spacing must be less than pre-Blossom target spacing."); static_assert(POST_BLOSSOM_POW_TARGET_SPACING < PRE_BLOSSOM_POW_TARGET_SPACING, "Blossom target spacing must be less than pre-Blossom target spacing.");
static const unsigned int PRE_BLOSSOM_HALVING_INTERVAL = 840000; static const unsigned int PRE_BLOSSOM_HALVING_INTERVAL = 840000;
static const unsigned int PRE_BLOSSOM_REGTEST_HALVING_INTERVAL = 150; static const unsigned int PRE_BLOSSOM_REGTEST_HALVING_INTERVAL = 150;
static const unsigned int BLOSSOM_POW_TARGET_SPACING_RATIO = PRE_BLOSSOM_POW_TARGET_SPACING / POST_BLOSSOM_POW_TARGET_SPACING; static const int BLOSSOM_POW_TARGET_SPACING_RATIO = PRE_BLOSSOM_POW_TARGET_SPACING / POST_BLOSSOM_POW_TARGET_SPACING;
static const unsigned int POST_BLOSSOM_HALVING_INTERVAL = PRE_BLOSSOM_HALVING_INTERVAL * BLOSSOM_POW_TARGET_SPACING_RATIO; static const unsigned int POST_BLOSSOM_HALVING_INTERVAL = PRE_BLOSSOM_HALVING_INTERVAL * BLOSSOM_POW_TARGET_SPACING_RATIO;
static const unsigned int POST_BLOSSOM_REGTEST_HALVING_INTERVAL = PRE_BLOSSOM_REGTEST_HALVING_INTERVAL * BLOSSOM_POW_TARGET_SPACING_RATIO; static const unsigned int POST_BLOSSOM_REGTEST_HALVING_INTERVAL = PRE_BLOSSOM_REGTEST_HALVING_INTERVAL * BLOSSOM_POW_TARGET_SPACING_RATIO;

View File

@ -13,6 +13,7 @@
#include <vector> #include <vector>
#include <boost/filesystem.hpp> #include <boost/filesystem.hpp>
#include "util.h" #include "util.h"
#include "utiltest.h"
// To run tests: // To run tests:
// ./zcash-gtest --gtest_filter="founders_reward_test.*" // ./zcash-gtest --gtest_filter="founders_reward_test.*"
@ -119,6 +120,15 @@ TEST(founders_reward_test, general) {
EXPECT_DEATH(params.GetFoundersRewardAddressAtHeight(maxHeight+1), "nHeight"); EXPECT_DEATH(params.GetFoundersRewardAddressAtHeight(maxHeight+1), "nHeight");
} }
TEST(founders_reward_test, get_last_block_blossom) {
int blossomActivationHeight = /*slowStartShift*/ + Consensus::PRE_BLOSSOM_REGTEST_HALVING_INTERVAL / 2; // = 75
auto params = RegtestActivateBlossom(false, blossomActivationHeight);
int slowStartShift = params.SubsidySlowStartShift(); // 0 for regtest
EXPECT_EQ(Consensus::PRE_BLOSSOM_REGTEST_HALVING_INTERVAL + slowStartShift - 1, params.GetLastFoundersRewardBlockHeight(0));
EXPECT_EQ(Consensus::PRE_BLOSSOM_REGTEST_HALVING_INTERVAL + slowStartShift - 1, params.GetLastFoundersRewardBlockHeight(blossomActivationHeight - 1));
int blossomBlocks = (Consensus::PRE_BLOSSOM_REGTEST_HALVING_INTERVAL- blossomActivationHeight) * Consensus::BLOSSOM_POW_TARGET_SPACING_RATIO;
EXPECT_EQ(blossomActivationHeight + blossomBlocks + slowStartShift - 1, params.GetLastFoundersRewardBlockHeight(blossomActivationHeight));
}
#define NUM_MAINNET_FOUNDER_ADDRESSES 48 #define NUM_MAINNET_FOUNDER_ADDRESSES 48
@ -147,7 +157,7 @@ TEST(founders_reward_test, regtest) {
// Test that 10% founders reward is fully rewarded after the first halving and slow start shift. // Test that 10% founders reward is fully rewarded after the first halving and slow start shift.
// On Mainnet, this would be 2,100,000 ZEC after 850,000 blocks (840,000 + 10,000). // On Mainnet, this would be 2,100,000 ZEC after 850,000 blocks (840,000 + 10,000).
TEST(founders_reward_test, slow_start_subsidy) { TEST(founders_reward_test, slow_start_subsidy) { // TODO: Update this test when the Blossom activation height is set
SelectParams(CBaseChainParams::MAIN); SelectParams(CBaseChainParams::MAIN);
CChainParams params = Params(); CChainParams params = Params();

View File

@ -79,8 +79,8 @@ TEST(PoW, DifficultyAveraging) {
} }
TEST(PoW, DifficultyAveragingBlossom) { TEST(PoW, DifficultyAveragingBlossom) {
TestDifficultyAveragigingImpl(ActivateBlossom(true)); TestDifficultyAveragigingImpl(RegtestActivateBlossom(true));
DeactivateBlossom(); RegtestDeactivateBlossom();
} }
TEST(PoW, MinDifficultyRules) { TEST(PoW, MinDifficultyRules) {

View File

@ -32,7 +32,7 @@ BOOST_AUTO_TEST_CASE(get_next_work)
BOOST_AUTO_TEST_CASE(get_next_work_blossom) BOOST_AUTO_TEST_CASE(get_next_work_blossom)
{ {
const Consensus::Params& params = ActivateBlossom(true); const Consensus::Params& params = RegtestActivateBlossom(true);
BOOST_CHECK_EQUAL(75, params.PoWTargetSpacing(0)); BOOST_CHECK_EQUAL(75, params.PoWTargetSpacing(0));
int64_t nLastRetargetTime = 1000000000; // NOTE: Not an actual block time int64_t nLastRetargetTime = 1000000000; // NOTE: Not an actual block time
@ -42,7 +42,7 @@ BOOST_AUTO_TEST_CASE(get_next_work_blossom)
BOOST_CHECK_GT(0x1d011998, BOOST_CHECK_GT(0x1d011998,
CalculateNextWorkRequired(bnAvg, nThisTime, nLastRetargetTime, params, 0)); CalculateNextWorkRequired(bnAvg, nThisTime, nLastRetargetTime, params, 0));
DeactivateBlossom(); RegtestDeactivateBlossom();
} }
/* Test the constraint on the upper bound for next work */ /* Test the constraint on the upper bound for next work */
@ -61,7 +61,7 @@ BOOST_AUTO_TEST_CASE(get_next_work_pow_limit)
BOOST_AUTO_TEST_CASE(get_next_work_pow_limit_blossom) BOOST_AUTO_TEST_CASE(get_next_work_pow_limit_blossom)
{ {
const Consensus::Params& params = ActivateBlossom(true); const Consensus::Params& params = RegtestActivateBlossom(true);
int64_t nLastRetargetTime = 1231006505; int64_t nLastRetargetTime = 1231006505;
int64_t nThisTime = 1233061996; int64_t nThisTime = 1233061996;
@ -70,7 +70,7 @@ BOOST_AUTO_TEST_CASE(get_next_work_pow_limit_blossom)
BOOST_CHECK_EQUAL(0x1f07ffff, BOOST_CHECK_EQUAL(0x1f07ffff,
CalculateNextWorkRequired(bnAvg, nThisTime, nLastRetargetTime, params, 0)); CalculateNextWorkRequired(bnAvg, nThisTime, nLastRetargetTime, params, 0));
DeactivateBlossom(); RegtestDeactivateBlossom();
} }
/* Test the constraint on the lower bound for actual time taken */ /* Test the constraint on the lower bound for actual time taken */
@ -91,7 +91,7 @@ BOOST_AUTO_TEST_CASE(get_next_work_lower_limit_actual)
BOOST_AUTO_TEST_CASE(get_next_work_lower_limit_actual_blossom) BOOST_AUTO_TEST_CASE(get_next_work_lower_limit_actual_blossom)
{ {
const Consensus::Params& params = ActivateBlossom(true); const Consensus::Params& params = RegtestActivateBlossom(true);
int64_t nLastRetargetTime = 1000000000; // NOTE: Not an actual block time int64_t nLastRetargetTime = 1000000000; // NOTE: Not an actual block time
int64_t nThisTime = 1000000458; int64_t nThisTime = 1000000458;
@ -100,7 +100,7 @@ BOOST_AUTO_TEST_CASE(get_next_work_lower_limit_actual_blossom)
BOOST_CHECK_EQUAL(0x1c04bceb, BOOST_CHECK_EQUAL(0x1c04bceb,
CalculateNextWorkRequired(bnAvg, nThisTime, nLastRetargetTime, params, 0)); CalculateNextWorkRequired(bnAvg, nThisTime, nLastRetargetTime, params, 0));
DeactivateBlossom(); RegtestDeactivateBlossom();
} }
/* Test the constraint on the upper bound for actual time taken */ /* Test the constraint on the upper bound for actual time taken */
@ -120,7 +120,7 @@ BOOST_AUTO_TEST_CASE(get_next_work_upper_limit_actual)
BOOST_AUTO_TEST_CASE(get_next_work_upper_limit_actual_blossom) BOOST_AUTO_TEST_CASE(get_next_work_upper_limit_actual_blossom)
{ {
const Consensus::Params& params = ActivateBlossom(true); const Consensus::Params& params = RegtestActivateBlossom(true);
int64_t nLastRetargetTime = 1000000000; // NOTE: Not an actual block time int64_t nLastRetargetTime = 1000000000; // NOTE: Not an actual block time
int64_t nThisTime = 1000002908; int64_t nThisTime = 1000002908;
@ -129,7 +129,7 @@ BOOST_AUTO_TEST_CASE(get_next_work_upper_limit_actual_blossom)
BOOST_CHECK_EQUAL(0x1c4a93bb, BOOST_CHECK_EQUAL(0x1c4a93bb,
CalculateNextWorkRequired(bnAvg, nThisTime, nLastRetargetTime, params, 0)); CalculateNextWorkRequired(bnAvg, nThisTime, nLastRetargetTime, params, 0));
DeactivateBlossom(); RegtestDeactivateBlossom();
} }
void GetBlockProofEquivalentTimeImpl(const Consensus::Params& params) { void GetBlockProofEquivalentTimeImpl(const Consensus::Params& params) {
@ -160,8 +160,8 @@ BOOST_AUTO_TEST_CASE(GetBlockProofEquivalentTime_test)
BOOST_AUTO_TEST_CASE(GetBlockProofEquivalentTime_test_blossom) BOOST_AUTO_TEST_CASE(GetBlockProofEquivalentTime_test_blossom)
{ {
GetBlockProofEquivalentTimeImpl(ActivateBlossom(true)); GetBlockProofEquivalentTimeImpl(RegtestActivateBlossom(true));
DeactivateBlossom(); RegtestDeactivateBlossom();
} }
BOOST_AUTO_TEST_SUITE_END() BOOST_AUTO_TEST_SUITE_END()

View File

@ -200,18 +200,18 @@ void RegtestDeactivateSapling() {
UpdateNetworkUpgradeParameters(Consensus::UPGRADE_OVERWINTER, Consensus::NetworkUpgrade::NO_ACTIVATION_HEIGHT); UpdateNetworkUpgradeParameters(Consensus::UPGRADE_OVERWINTER, Consensus::NetworkUpgrade::NO_ACTIVATION_HEIGHT);
} }
const Consensus::Params& ActivateBlossom(bool updatePow) { const Consensus::Params& RegtestActivateBlossom(bool updatePow, int blossomActivationHeight) {
SelectParams(CBaseChainParams::REGTEST); SelectParams(CBaseChainParams::REGTEST);
UpdateNetworkUpgradeParameters(Consensus::UPGRADE_OVERWINTER, Consensus::NetworkUpgrade::ALWAYS_ACTIVE); UpdateNetworkUpgradeParameters(Consensus::UPGRADE_OVERWINTER, Consensus::NetworkUpgrade::ALWAYS_ACTIVE);
UpdateNetworkUpgradeParameters(Consensus::UPGRADE_SAPLING, Consensus::NetworkUpgrade::ALWAYS_ACTIVE); UpdateNetworkUpgradeParameters(Consensus::UPGRADE_SAPLING, Consensus::NetworkUpgrade::ALWAYS_ACTIVE);
UpdateNetworkUpgradeParameters(Consensus::UPGRADE_BLOSSOM, Consensus::NetworkUpgrade::ALWAYS_ACTIVE); UpdateNetworkUpgradeParameters(Consensus::UPGRADE_BLOSSOM, blossomActivationHeight);
if (updatePow) { if (updatePow) {
UpdateRegtestPow(32, 16, uint256S("0007ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")); UpdateRegtestPow(32, 16, uint256S("0007ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"));
} }
return Params().GetConsensus(); return Params().GetConsensus();
} }
void DeactivateBlossom() { void RegtestDeactivateBlossom() {
UpdateRegtestPow(0, 0, uint256S("0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f")); UpdateRegtestPow(0, 0, uint256S("0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f0f"));
UpdateNetworkUpgradeParameters(Consensus::UPGRADE_BLOSSOM, Consensus::NetworkUpgrade::NO_ACTIVATION_HEIGHT); UpdateNetworkUpgradeParameters(Consensus::UPGRADE_BLOSSOM, Consensus::NetworkUpgrade::NO_ACTIVATION_HEIGHT);
UpdateNetworkUpgradeParameters(Consensus::UPGRADE_SAPLING, Consensus::NetworkUpgrade::NO_ACTIVATION_HEIGHT); UpdateNetworkUpgradeParameters(Consensus::UPGRADE_SAPLING, Consensus::NetworkUpgrade::NO_ACTIVATION_HEIGHT);

View File

@ -43,9 +43,9 @@ const Consensus::Params& RegtestActivateSapling();
void RegtestDeactivateSapling(); void RegtestDeactivateSapling();
const Consensus::Params& ActivateBlossom(bool updatePow); const Consensus::Params& RegtestActivateBlossom(bool updatePow, int blossomActivationHeight = Consensus::NetworkUpgrade::ALWAYS_ACTIVE);
void DeactivateBlossom(); void RegtestDeactivateBlossom();
libzcash::SaplingExtendedSpendingKey GetTestMasterSaplingSpendingKey(); libzcash::SaplingExtendedSpendingKey GetTestMasterSaplingSpendingKey();