2014-03-10 08:46:53 -07:00
|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
2018-10-09 11:29:40 -07:00
|
|
|
// Copyright (c) 2009-2014 The Bitcoin Core developers
|
|
|
|
// Copyright (c) 2014-2015 The Dogecoin Core developers
|
2014-12-13 12:09:33 +08:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
2014-03-10 08:46:53 -07:00
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
2017-11-10 13:57:53 +13:00
|
|
|
#include <pow.h>
|
2014-03-10 08:46:53 -07:00
|
|
|
|
2017-11-10 13:57:53 +13:00
|
|
|
#include <arith_uint256.h>
|
|
|
|
#include <chain.h>
|
|
|
|
#include <primitives/block.h>
|
|
|
|
#include <uint256.h>
|
2017-01-29 07:00:00 -08:00
|
|
|
#include <util.h>
|
2014-03-10 08:46:53 -07:00
|
|
|
|
2018-10-09 11:29:40 -07:00
|
|
|
// Copy and modified from CalculateDogecoinNextWorkRequired (dogecoin.cpp)
|
|
|
|
unsigned int CalculateDigishieldNextWorkRequired(const CBlockIndex* pindexLast, int64_t nFirstBlockTime, const Consensus::Params& params)
|
|
|
|
{
|
2019-02-08 17:14:09 -08:00
|
|
|
if (params.fPowNoRetargeting)
|
|
|
|
return pindexLast->nBits;
|
|
|
|
|
2018-10-09 11:29:40 -07:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2015-02-15 02:21:42 +01:00
|
|
|
unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHeader *pblock, const Consensus::Params& params)
|
2014-03-10 08:46:53 -07:00
|
|
|
{
|
2017-08-07 07:36:37 +02:00
|
|
|
assert(pindexLast != nullptr);
|
2014-03-10 08:46:53 -07:00
|
|
|
// Go back by what we want to be 14 days worth of blocks
|
2019-01-28 17:21:28 -08:00
|
|
|
// Kevacoin: This fixes an issue where a 51% attack can change difficulty at will.
|
2017-01-29 07:00:00 -08:00
|
|
|
// Go back the full period unless it's the first retarget after genesis. Code courtesy of Art Forz
|
|
|
|
int blockstogoback = params.DifficultyAdjustmentInterval()-1;
|
|
|
|
if ((pindexLast->nHeight+1) != params.DifficultyAdjustmentInterval())
|
|
|
|
blockstogoback = params.DifficultyAdjustmentInterval();
|
|
|
|
|
|
|
|
// Go back by what we want to be 14 days worth of blocks
|
|
|
|
const CBlockIndex* pindexFirst = pindexLast;
|
|
|
|
for (int i = 0; pindexFirst && i < blockstogoback; i++)
|
|
|
|
pindexFirst = pindexFirst->pprev;
|
|
|
|
|
2014-03-10 08:46:53 -07:00
|
|
|
assert(pindexFirst);
|
|
|
|
|
2018-10-09 11:29:40 -07:00
|
|
|
return CalculateDigishieldNextWorkRequired(pindexLast, pindexFirst->GetBlockTime(), params);
|
2015-02-21 12:57:44 +00:00
|
|
|
}
|
|
|
|
|
2015-02-15 02:21:42 +01:00
|
|
|
bool CheckProofOfWork(uint256 hash, unsigned int nBits, const Consensus::Params& params)
|
2014-03-10 08:46:53 -07:00
|
|
|
{
|
|
|
|
bool fNegative;
|
|
|
|
bool fOverflow;
|
2014-12-16 15:43:03 +01:00
|
|
|
arith_uint256 bnTarget;
|
2014-09-04 16:23:42 -03:00
|
|
|
|
2014-03-10 08:46:53 -07:00
|
|
|
bnTarget.SetCompact(nBits, &fNegative, &fOverflow);
|
|
|
|
|
|
|
|
// Check range
|
2015-03-25 15:00:32 -04:00
|
|
|
if (fNegative || bnTarget == 0 || fOverflow || bnTarget > UintToArith256(params.powLimit))
|
2015-12-02 03:15:42 +01:00
|
|
|
return false;
|
2014-03-10 08:46:53 -07:00
|
|
|
|
|
|
|
// Check proof of work matches claimed amount
|
2014-12-16 15:43:03 +01:00
|
|
|
if (UintToArith256(hash) > bnTarget)
|
2015-12-02 03:15:42 +01:00
|
|
|
return false;
|
2014-03-10 08:46:53 -07:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|