|
|
@ -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> |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 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) |
|
|
|
unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHeader *pblock, const Consensus::Params& params) |
|
|
|
{ |
|
|
|
{ |
|
|
|
assert(pindexLast != nullptr); |
|
|
|
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
|
|
|
|
// 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) |
|
|
|