]> Git Repo - VerusCoin.git/blob - src/pow.cpp
Merge pull request #5227
[VerusCoin.git] / src / pow.cpp
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2014 The Bitcoin developers
3 // Distributed under the MIT/X11 software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
6 #include "pow.h"
7
8 #include "chain.h"
9 #include "chainparams.h"
10 #include "core/block.h"
11 #include "uint256.h"
12 #include "util.h"
13
14 unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHeader *pblock)
15 {
16     unsigned int nProofOfWorkLimit = Params().ProofOfWorkLimit().GetCompact();
17
18     // Genesis block
19     if (pindexLast == NULL)
20         return nProofOfWorkLimit;
21
22     // Only change once per interval
23     if ((pindexLast->nHeight+1) % Params().Interval() != 0)
24     {
25         if (Params().AllowMinDifficultyBlocks())
26         {
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().TargetSpacing()*2)
31                 return nProofOfWorkLimit;
32             else
33             {
34                 // Return the last non-special-min-difficulty-rules-block
35                 const CBlockIndex* pindex = pindexLast;
36                 while (pindex->pprev && pindex->nHeight % Params().Interval() != 0 && pindex->nBits == nProofOfWorkLimit)
37                     pindex = pindex->pprev;
38                 return pindex->nBits;
39             }
40         }
41         return pindexLast->nBits;
42     }
43
44     // Go back by what we want to be 14 days worth of blocks
45     const CBlockIndex* pindexFirst = pindexLast;
46     for (int i = 0; pindexFirst && i < Params().Interval()-1; i++)
47         pindexFirst = pindexFirst->pprev;
48     assert(pindexFirst);
49
50     // Limit adjustment step
51     int64_t nActualTimespan = pindexLast->GetBlockTime() - pindexFirst->GetBlockTime();
52     LogPrintf("  nActualTimespan = %d  before bounds\n", nActualTimespan);
53     if (nActualTimespan < Params().TargetTimespan()/4)
54         nActualTimespan = Params().TargetTimespan()/4;
55     if (nActualTimespan > Params().TargetTimespan()*4)
56         nActualTimespan = Params().TargetTimespan()*4;
57
58     // Retarget
59     uint256 bnNew;
60     uint256 bnOld;
61     bnNew.SetCompact(pindexLast->nBits);
62     bnOld = bnNew;
63     bnNew *= nActualTimespan;
64     bnNew /= Params().TargetTimespan();
65
66     if (bnNew > Params().ProofOfWorkLimit())
67         bnNew = Params().ProofOfWorkLimit();
68
69     /// debug print
70     LogPrintf("GetNextWorkRequired RETARGET\n");
71     LogPrintf("Params().TargetTimespan() = %d    nActualTimespan = %d\n", Params().TargetTimespan(), nActualTimespan);
72     LogPrintf("Before: %08x  %s\n", pindexLast->nBits, bnOld.ToString());
73     LogPrintf("After:  %08x  %s\n", bnNew.GetCompact(), bnNew.ToString());
74
75     return bnNew.GetCompact();
76 }
77
78 bool CheckProofOfWork(uint256 hash, unsigned int nBits)
79 {
80     bool fNegative;
81     bool fOverflow;
82     uint256 bnTarget;
83
84     if (Params().SkipProofOfWorkCheck())
85        return true;
86
87     bnTarget.SetCompact(nBits, &fNegative, &fOverflow);
88
89     // Check range
90     if (fNegative || bnTarget == 0 || fOverflow || bnTarget > Params().ProofOfWorkLimit())
91         return error("CheckProofOfWork() : nBits below minimum work");
92
93     // Check proof of work matches claimed amount
94     if (hash > bnTarget)
95         return error("CheckProofOfWork() : hash doesn't match nBits");
96
97     return true;
98 }
99
100 uint256 GetBlockProof(const CBlockIndex& block)
101 {
102     uint256 bnTarget;
103     bool fNegative;
104     bool fOverflow;
105     bnTarget.SetCompact(block.nBits, &fNegative, &fOverflow);
106     if (fNegative || fOverflow || bnTarget == 0)
107         return 0;
108     // We need to compute 2**256 / (bnTarget+1), but we can't represent 2**256
109     // as it's too large for a uint256. However, as 2**256 is at least as large
110     // as bnTarget+1, it is equal to ((2**256 - bnTarget - 1) / (bnTarget+1)) + 1,
111     // or ~bnTarget / (nTarget+1) + 1.
112     return (~bnTarget / (bnTarget + 1)) + 1;
113 }
This page took 0.03123 seconds and 4 git commands to generate.