[doc][trivial] Remove source forge from Debian watch.
[bitcoinplatinum.git] / src / pow.cpp
blob5ace3fbc9b5bcd5126312a347eb90a2da4494203
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2014 The Bitcoin Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 #include "pow.h"
8 #include "arith_uint256.h"
9 #include "chain.h"
10 #include "primitives/block.h"
11 #include "uint256.h"
12 #include "util.h"
14 unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHeader *pblock, const Consensus::Params& params)
16 unsigned int nProofOfWorkLimit = UintToArith256(params.powLimit).GetCompact();
18 // Genesis block
19 if (pindexLast == NULL)
20 return nProofOfWorkLimit;
22 // Only change once per difficulty adjustment interval
23 if ((pindexLast->nHeight+1) % params.DifficultyAdjustmentInterval() != 0)
25 if (params.fPowAllowMinDifficultyBlocks)
27 // Special difficulty rule for testnet:
28 // If the new block's timestamp is more than 2* 10 minutes
29 // then allow mining of a min-difficulty block.
30 if (pblock->GetBlockTime() > pindexLast->GetBlockTime() + params.nPowTargetSpacing*2)
31 return nProofOfWorkLimit;
32 else
34 // Return the last non-special-min-difficulty-rules-block
35 const CBlockIndex* pindex = pindexLast;
36 while (pindex->pprev && pindex->nHeight % params.DifficultyAdjustmentInterval() != 0 && pindex->nBits == nProofOfWorkLimit)
37 pindex = pindex->pprev;
38 return pindex->nBits;
41 return pindexLast->nBits;
44 // Go back by what we want to be 14 days worth of blocks
45 int nHeightFirst = pindexLast->nHeight - (params.DifficultyAdjustmentInterval()-1);
46 assert(nHeightFirst >= 0);
47 const CBlockIndex* pindexFirst = pindexLast->GetAncestor(nHeightFirst);
48 assert(pindexFirst);
50 return CalculateNextWorkRequired(pindexLast, pindexFirst->GetBlockTime(), params);
53 unsigned int CalculateNextWorkRequired(const CBlockIndex* pindexLast, int64_t nFirstBlockTime, const Consensus::Params& params)
55 if (params.fPowNoRetargeting)
56 return pindexLast->nBits;
58 // Limit adjustment step
59 int64_t nActualTimespan = pindexLast->GetBlockTime() - nFirstBlockTime;
60 LogPrintf(" nActualTimespan = %d before bounds\n", nActualTimespan);
61 if (nActualTimespan < params.nPowTargetTimespan/4)
62 nActualTimespan = params.nPowTargetTimespan/4;
63 if (nActualTimespan > params.nPowTargetTimespan*4)
64 nActualTimespan = params.nPowTargetTimespan*4;
66 // Retarget
67 const arith_uint256 bnPowLimit = UintToArith256(params.powLimit);
68 arith_uint256 bnNew;
69 arith_uint256 bnOld;
70 bnNew.SetCompact(pindexLast->nBits);
71 bnOld = bnNew;
72 bnNew *= nActualTimespan;
73 bnNew /= params.nPowTargetTimespan;
75 if (bnNew > bnPowLimit)
76 bnNew = bnPowLimit;
78 /// debug print
79 LogPrintf("GetNextWorkRequired RETARGET\n");
80 LogPrintf("params.nPowTargetTimespan = %d nActualTimespan = %d\n", params.nPowTargetTimespan, nActualTimespan);
81 LogPrintf("Before: %08x %s\n", pindexLast->nBits, bnOld.ToString());
82 LogPrintf("After: %08x %s\n", bnNew.GetCompact(), bnNew.ToString());
84 return bnNew.GetCompact();
87 bool CheckProofOfWork(uint256 hash, unsigned int nBits, const Consensus::Params& params)
89 bool fNegative;
90 bool fOverflow;
91 arith_uint256 bnTarget;
93 bnTarget.SetCompact(nBits, &fNegative, &fOverflow);
95 // Check range
96 if (fNegative || bnTarget == 0 || fOverflow || bnTarget > UintToArith256(params.powLimit))
97 return error("CheckProofOfWork(): nBits below minimum work");
99 // Check proof of work matches claimed amount
100 if (UintToArith256(hash) > bnTarget)
101 return error("CheckProofOfWork(): hash doesn't match nBits");
103 return true;
106 arith_uint256 GetBlockProof(const CBlockIndex& block)
108 arith_uint256 bnTarget;
109 bool fNegative;
110 bool fOverflow;
111 bnTarget.SetCompact(block.nBits, &fNegative, &fOverflow);
112 if (fNegative || fOverflow || bnTarget == 0)
113 return 0;
114 // We need to compute 2**256 / (bnTarget+1), but we can't represent 2**256
115 // as it's too large for a arith_uint256. However, as 2**256 is at least as large
116 // as bnTarget+1, it is equal to ((2**256 - bnTarget - 1) / (bnTarget+1)) + 1,
117 // or ~bnTarget / (nTarget+1) + 1.
118 return (~bnTarget / (bnTarget + 1)) + 1;
121 int64_t GetBlockProofEquivalentTime(const CBlockIndex& to, const CBlockIndex& from, const CBlockIndex& tip, const Consensus::Params& params)
123 arith_uint256 r;
124 int sign = 1;
125 if (to.nChainWork > from.nChainWork) {
126 r = to.nChainWork - from.nChainWork;
127 } else {
128 r = from.nChainWork - to.nChainWork;
129 sign = -1;
131 r = r * arith_uint256(params.nPowTargetSpacing) / GetBlockProof(tip);
132 if (r.bits() > 63) {
133 return sign * std::numeric_limits<int64_t>::max();
135 return sign * r.GetLow64();