From 865b61b503157ac5dff9ad682a92d3590c8a5712 Mon Sep 17 00:00:00 2001 From: UdjinM6 Date: Fri, 1 Dec 2017 08:15:11 +0300 Subject: [PATCH] Unify GetNextWorkRequired (#1737) * unify GetNextWorkRequired * drop enum --- src/chainparams.cpp | 6 ++++++ src/consensus/params.h | 2 ++ src/pow.cpp | 31 +++++++------------------------ src/pow.h | 7 ------- 4 files changed, 15 insertions(+), 31 deletions(-) diff --git a/src/chainparams.cpp b/src/chainparams.cpp index 80466e055..21177b528 100644 --- a/src/chainparams.cpp +++ b/src/chainparams.cpp @@ -96,6 +96,8 @@ public: consensus.nPowTargetSpacing = 2.5 * 60; // Dash: 2.5 minutes consensus.fPowAllowMinDifficultyBlocks = false; consensus.fPowNoRetargeting = false; + consensus.nPowKGWHeight = 15200; + consensus.nPowDGWHeight = 34140; consensus.nRuleChangeActivationThreshold = 1916; // 95% of 2016 consensus.nMinerConfirmationWindow = 2016; // nPowTargetTimespan / nPowTargetSpacing consensus.vDeployments[Consensus::DEPLOYMENT_TESTDUMMY].bit = 28; @@ -235,6 +237,8 @@ public: consensus.nPowTargetSpacing = 2.5 * 60; // Dash: 2.5 minutes consensus.fPowAllowMinDifficultyBlocks = true; consensus.fPowNoRetargeting = false; + consensus.nPowKGWHeight = 4001; // nPowKGWHeight >= nPowDGWHeight means "no KGW" + consensus.nPowDGWHeight = 4001; consensus.nRuleChangeActivationThreshold = 1512; // 75% for testchains consensus.nMinerConfirmationWindow = 2016; // nPowTargetTimespan / nPowTargetSpacing consensus.vDeployments[Consensus::DEPLOYMENT_TESTDUMMY].bit = 28; @@ -352,6 +356,8 @@ public: consensus.nPowTargetSpacing = 2.5 * 60; // Dash: 2.5 minutes consensus.fPowAllowMinDifficultyBlocks = true; consensus.fPowNoRetargeting = true; + consensus.nPowKGWHeight = 15200; // same as mainnet + consensus.nPowDGWHeight = 34140; // same as mainnet consensus.nRuleChangeActivationThreshold = 108; // 75% for testchains consensus.nMinerConfirmationWindow = 144; // Faster than normal for regtest (144 instead of 2016) consensus.vDeployments[Consensus::DEPLOYMENT_TESTDUMMY].bit = 28; diff --git a/src/consensus/params.h b/src/consensus/params.h index a759e0c26..e85949fe0 100644 --- a/src/consensus/params.h +++ b/src/consensus/params.h @@ -79,6 +79,8 @@ struct Params { bool fPowNoRetargeting; int64_t nPowTargetSpacing; int64_t nPowTargetTimespan; + int nPowKGWHeight; + int nPowDGWHeight; int64_t DifficultyAdjustmentInterval() const { return nPowTargetTimespan / nPowTargetSpacing; } uint256 nMinimumChainWork; uint256 defaultAssumeValid; diff --git a/src/pow.cpp b/src/pow.cpp index 785451c82..307759a46 100644 --- a/src/pow.cpp +++ b/src/pow.cpp @@ -171,33 +171,16 @@ unsigned int GetNextWorkRequiredBTC(const CBlockIndex* pindexLast, const CBlockH unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHeader *pblock, const Consensus::Params& params) { - unsigned int retarget = DIFF_DGW; - - // mainnet/regtest share a configuration - if (Params().NetworkIDString() == CBaseChainParams::MAIN || Params().NetworkIDString() == CBaseChainParams::REGTEST) { - if (pindexLast->nHeight + 1 >= 34140) retarget = DIFF_DGW; - else if (pindexLast->nHeight + 1 >= 15200) retarget = DIFF_KGW; - else retarget = DIFF_BTC; - // testnet -- we want a lot of coins in existance early on - } else { - if (pindexLast->nHeight + 1 >= 4001) retarget = DIFF_DGW; - else retarget = DIFF_BTC; + // Most recent algo first + if (pindexLast->nHeight + 1 >= params.nPowDGWHeight) { + return DarkGravityWave(pindexLast, params); } - - // Bitcoin style retargeting - if (retarget == DIFF_BTC) - { - return GetNextWorkRequiredBTC(pindexLast, pblock, params); - } - - // Retarget using Kimoto Gravity Wave - else if (retarget == DIFF_KGW) - { + else if (pindexLast->nHeight + 1 >= params.nPowKGWHeight) { return KimotoGravityWell(pindexLast, params); } - - // Retarget using Dark Gravity Wave 3 by default - return DarkGravityWave(pindexLast, params); + else { + return GetNextWorkRequiredBTC(pindexLast, pblock, params); + } } // for DIFF_BTC only! diff --git a/src/pow.h b/src/pow.h index 864be944a..62939b979 100644 --- a/src/pow.h +++ b/src/pow.h @@ -15,13 +15,6 @@ class CBlockIndex; class uint256; class arith_uint256; -// Define difficulty retarget algorithms -enum DiffMode { - DIFF_DEFAULT = 0, // Default to invalid 0 - DIFF_BTC = 1, // Retarget every x blocks (Bitcoin style) - DIFF_KGW = 2, // Retarget using Kimoto Gravity Well - DIFF_DGW = 3, // Retarget using Dark Gravity Wave v3 -}; unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHeader *pblock, const Consensus::Params&); unsigned int CalculateNextWorkRequired(const CBlockIndex* pindexLast, int64_t nFirstBlockTime, const Consensus::Params&);