From 30e907bac6150f6381af923750b0e0d0d4520a92 Mon Sep 17 00:00:00 2001 From: Jianping Wu Date: Tue, 9 Oct 2018 11:29:40 -0700 Subject: [PATCH] Digishield difficulty adjustment. Changed block time to 2 minutes. --- src/chainparams.cpp | 8 +++--- src/pow.cpp | 66 +++++++++++++++++++++++++++------------------ 2 files changed, 44 insertions(+), 30 deletions(-) diff --git a/src/chainparams.cpp b/src/chainparams.cpp index c4b3087e9..0f97a1b62 100644 --- a/src/chainparams.cpp +++ b/src/chainparams.cpp @@ -83,8 +83,8 @@ public: consensus.BIP65Height = 918684; // bab3041e8977e0dc3eeff63fe707b92bde1dd449d8efafb248c27c8264cc311a consensus.BIP66Height = 811879; // 7aceee012833fa8952f8835d8b1b3ae233cd6ab08fdb27a771d2bd7bdc491894 consensus.powLimit = uint256S("000fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"); - consensus.nPowTargetTimespan = 3.5 * 24 * 60 * 60; // 3.5 days - consensus.nPowTargetSpacing = 2.5 * 60; + consensus.nPowTargetTimespan = 2.0 * 60; // Two minutes + consensus.nPowTargetSpacing = 2.0 * 60; // Two minutes consensus.fPowAllowMinDifficultyBlocks = false; consensus.fPowNoRetargeting = false; consensus.nRuleChangeActivationThreshold = 6048; // 75% of 8064 @@ -132,8 +132,8 @@ public: printf("recalculating params for mainnet.\n"); printf("old mainnet genesis nonce: %d\n", genesis.nNonce); printf("old mainnet genesis hash: %s\n", hashGenesisBlock.ToString().c_str()); - // deliberately empty for loop finds nonce value. - for(genesis.nNonce = 1000; hashTarget < UintToArith256(genesis.GetPoWHash()); genesis.nNonce++) { + // deliberately empty for loop finds nonce value. + for(genesis.nNonce = 1000; hashTarget < UintToArith256(genesis.GetPoWHash()); genesis.nNonce++) { printf("JWU nNonce: %d\n\n", genesis.nNonce); } printf("new mainnet genesis merkle root: %s\n", genesis.hashMerkleRoot.ToString().c_str()); diff --git a/src/pow.cpp b/src/pow.cpp index 903676b84..71292b803 100644 --- a/src/pow.cpp +++ b/src/pow.cpp @@ -1,5 +1,6 @@ // Copyright (c) 2009-2010 Satoshi Nakamoto -// Copyright (c) 2009-2017 The Bitcoin Core developers +// Copyright (c) 2009-2014 The Bitcoin Core developers +// Copyright (c) 2014-2015 The Dogecoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -11,33 +12,45 @@ #include #include +// Copy and modified from CalculateDogecoinNextWorkRequired (dogecoin.cpp) +unsigned int CalculateDigishieldNextWorkRequired(const CBlockIndex* pindexLast, int64_t nFirstBlockTime, const Consensus::Params& params) +{ + const int64_t retargetTimespan = params.nPowTargetTimespan; + const int64_t nActualTimespan = pindexLast->GetBlockTime() - nFirstBlockTime; + int64_t nModulatedTimespan = nActualTimespan; + int64_t nMaxTimespan; + int64_t nMinTimespan; + + // amplitude filter - thanks to daft27 for this code + nModulatedTimespan = retargetTimespan + (nModulatedTimespan - retargetTimespan) / 8; + + nMinTimespan = retargetTimespan - (retargetTimespan / 4); + nMaxTimespan = retargetTimespan + (retargetTimespan / 2); + + // Limit adjustment step + if (nModulatedTimespan < nMinTimespan) + nModulatedTimespan = nMinTimespan; + else if (nModulatedTimespan > nMaxTimespan) + nModulatedTimespan = nMaxTimespan; + + // Retarget + const arith_uint256 bnPowLimit = UintToArith256(params.powLimit); + arith_uint256 bnNew; + arith_uint256 bnOld; + bnNew.SetCompact(pindexLast->nBits); + bnOld = bnNew; + bnNew *= nModulatedTimespan; + bnNew /= retargetTimespan; + + if (bnNew > bnPowLimit) + bnNew = bnPowLimit; + + return bnNew.GetCompact(); +} + unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHeader *pblock, const Consensus::Params& params) { assert(pindexLast != nullptr); - unsigned int nProofOfWorkLimit = UintToArith256(params.powLimit).GetCompact(); - - // Only change once per difficulty adjustment interval - if ((pindexLast->nHeight+1) % params.DifficultyAdjustmentInterval() != 0) - { - if (params.fPowAllowMinDifficultyBlocks) - { - // Special difficulty rule for testnet: - // If the new block's timestamp is more than 2* 10 minutes - // then allow mining of a min-difficulty block. - if (pblock->GetBlockTime() > pindexLast->GetBlockTime() + params.nPowTargetSpacing*2) - return nProofOfWorkLimit; - else - { - // Return the last non-special-min-difficulty-rules-block - const CBlockIndex* pindex = pindexLast; - while (pindex->pprev && pindex->nHeight % params.DifficultyAdjustmentInterval() != 0 && pindex->nBits == nProofOfWorkLimit) - pindex = pindex->pprev; - return pindex->nBits; - } - } - return pindexLast->nBits; - } - // Go back by what we want to be 14 days worth of blocks // Litecoin: This fixes an issue where a 51% attack can change difficulty at will. // Go back the full period unless it's the first retarget after genesis. Code courtesy of Art Forz @@ -52,7 +65,8 @@ unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHead assert(pindexFirst); - return CalculateNextWorkRequired(pindexLast, pindexFirst->GetBlockTime(), params); + //return CalculateNextWorkRequired(pindexLast, pindexFirst->GetBlockTime(), params); + return CalculateDigishieldNextWorkRequired(pindexLast, pindexFirst->GetBlockTime(), params); } unsigned int CalculateNextWorkRequired(const CBlockIndex* pindexLast, int64_t nFirstBlockTime, const Consensus::Params& params)