|
|
@ -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) |
|
|
|