Browse Source

Digishield difficulty adjustment.

Changed block time to 2 minutes.
cn
Jianping Wu 6 years ago
parent
commit
30e907bac6
  1. 4
      src/chainparams.cpp
  2. 64
      src/pow.cpp

4
src/chainparams.cpp

@ -83,8 +83,8 @@ public:
consensus.BIP65Height = 918684; // bab3041e8977e0dc3eeff63fe707b92bde1dd449d8efafb248c27c8264cc311a consensus.BIP65Height = 918684; // bab3041e8977e0dc3eeff63fe707b92bde1dd449d8efafb248c27c8264cc311a
consensus.BIP66Height = 811879; // 7aceee012833fa8952f8835d8b1b3ae233cd6ab08fdb27a771d2bd7bdc491894 consensus.BIP66Height = 811879; // 7aceee012833fa8952f8835d8b1b3ae233cd6ab08fdb27a771d2bd7bdc491894
consensus.powLimit = uint256S("000fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"); consensus.powLimit = uint256S("000fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
consensus.nPowTargetTimespan = 3.5 * 24 * 60 * 60; // 3.5 days consensus.nPowTargetTimespan = 2.0 * 60; // Two minutes
consensus.nPowTargetSpacing = 2.5 * 60; consensus.nPowTargetSpacing = 2.0 * 60; // Two minutes
consensus.fPowAllowMinDifficultyBlocks = false; consensus.fPowAllowMinDifficultyBlocks = false;
consensus.fPowNoRetargeting = false; consensus.fPowNoRetargeting = false;
consensus.nRuleChangeActivationThreshold = 6048; // 75% of 8064 consensus.nRuleChangeActivationThreshold = 6048; // 75% of 8064

64
src/pow.cpp

@ -1,5 +1,6 @@
// Copyright (c) 2009-2010 Satoshi Nakamoto // 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 // Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
@ -11,33 +12,45 @@
#include <uint256.h> #include <uint256.h>
#include <util.h> #include <util.h>
unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHeader *pblock, const Consensus::Params& params) // Copy and modified from CalculateDogecoinNextWorkRequired (dogecoin.cpp)
unsigned int CalculateDigishieldNextWorkRequired(const CBlockIndex* pindexLast, int64_t nFirstBlockTime, const Consensus::Params& params)
{ {
assert(pindexLast != nullptr); const int64_t retargetTimespan = params.nPowTargetTimespan;
unsigned int nProofOfWorkLimit = UintToArith256(params.powLimit).GetCompact(); const int64_t nActualTimespan = pindexLast->GetBlockTime() - nFirstBlockTime;
int64_t nModulatedTimespan = nActualTimespan;
int64_t nMaxTimespan;
int64_t nMinTimespan;
// Only change once per difficulty adjustment interval // amplitude filter - thanks to daft27 for this code
if ((pindexLast->nHeight+1) % params.DifficultyAdjustmentInterval() != 0) nModulatedTimespan = retargetTimespan + (nModulatedTimespan - retargetTimespan) / 8;
{
if (params.fPowAllowMinDifficultyBlocks) nMinTimespan = retargetTimespan - (retargetTimespan / 4);
{ nMaxTimespan = retargetTimespan + (retargetTimespan / 2);
// Special difficulty rule for testnet:
// If the new block's timestamp is more than 2* 10 minutes // Limit adjustment step
// then allow mining of a min-difficulty block. if (nModulatedTimespan < nMinTimespan)
if (pblock->GetBlockTime() > pindexLast->GetBlockTime() + params.nPowTargetSpacing*2) nModulatedTimespan = nMinTimespan;
return nProofOfWorkLimit; else if (nModulatedTimespan > nMaxTimespan)
else nModulatedTimespan = nMaxTimespan;
{
// Return the last non-special-min-difficulty-rules-block // Retarget
const CBlockIndex* pindex = pindexLast; const arith_uint256 bnPowLimit = UintToArith256(params.powLimit);
while (pindex->pprev && pindex->nHeight % params.DifficultyAdjustmentInterval() != 0 && pindex->nBits == nProofOfWorkLimit) arith_uint256 bnNew;
pindex = pindex->pprev; arith_uint256 bnOld;
return pindex->nBits; bnNew.SetCompact(pindexLast->nBits);
} bnOld = bnNew;
} bnNew *= nModulatedTimespan;
return pindexLast->nBits; 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);
// Go back by what we want to be 14 days worth of blocks // 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. // 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 // 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); 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) unsigned int CalculateNextWorkRequired(const CBlockIndex* pindexLast, int64_t nFirstBlockTime, const Consensus::Params& params)

Loading…
Cancel
Save