Dash Core  0.12.2.1
P2P Digital Currency
pow.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2015 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.
5 
6 #include "pow.h"
7 
8 #include "arith_uint256.h"
9 #include "chain.h"
10 #include "chainparams.h"
11 #include "primitives/block.h"
12 #include "uint256.h"
13 #include "util.h"
14 
15 #include <math.h>
16 
17 unsigned int static KimotoGravityWell(const CBlockIndex* pindexLast, const Consensus::Params& params) {
18  const CBlockIndex *BlockLastSolved = pindexLast;
19  const CBlockIndex *BlockReading = pindexLast;
20  uint64_t PastBlocksMass = 0;
21  int64_t PastRateActualSeconds = 0;
22  int64_t PastRateTargetSeconds = 0;
23  double PastRateAdjustmentRatio = double(1);
24  arith_uint256 PastDifficultyAverage;
25  arith_uint256 PastDifficultyAveragePrev;
26  double EventHorizonDeviation;
27  double EventHorizonDeviationFast;
28  double EventHorizonDeviationSlow;
29 
30  uint64_t pastSecondsMin = params.nPowTargetTimespan * 0.025;
31  uint64_t pastSecondsMax = params.nPowTargetTimespan * 7;
32  uint64_t PastBlocksMin = pastSecondsMin / params.nPowTargetSpacing;
33  uint64_t PastBlocksMax = pastSecondsMax / params.nPowTargetSpacing;
34 
35  if (BlockLastSolved == NULL || BlockLastSolved->nHeight == 0 || (uint64_t)BlockLastSolved->nHeight < PastBlocksMin) { return UintToArith256(params.powLimit).GetCompact(); }
36 
37  for (unsigned int i = 1; BlockReading && BlockReading->nHeight > 0; i++) {
38  if (PastBlocksMax > 0 && i > PastBlocksMax) { break; }
39  PastBlocksMass++;
40 
41  PastDifficultyAverage.SetCompact(BlockReading->nBits);
42  if (i > 1) {
43  // handle negative arith_uint256
44  if(PastDifficultyAverage >= PastDifficultyAveragePrev)
45  PastDifficultyAverage = ((PastDifficultyAverage - PastDifficultyAveragePrev) / i) + PastDifficultyAveragePrev;
46  else
47  PastDifficultyAverage = PastDifficultyAveragePrev - ((PastDifficultyAveragePrev - PastDifficultyAverage) / i);
48  }
49  PastDifficultyAveragePrev = PastDifficultyAverage;
50 
51  PastRateActualSeconds = BlockLastSolved->GetBlockTime() - BlockReading->GetBlockTime();
52  PastRateTargetSeconds = params.nPowTargetSpacing * PastBlocksMass;
53  PastRateAdjustmentRatio = double(1);
54  if (PastRateActualSeconds < 0) { PastRateActualSeconds = 0; }
55  if (PastRateActualSeconds != 0 && PastRateTargetSeconds != 0) {
56  PastRateAdjustmentRatio = double(PastRateTargetSeconds) / double(PastRateActualSeconds);
57  }
58  EventHorizonDeviation = 1 + (0.7084 * pow((double(PastBlocksMass)/double(28.2)), -1.228));
59  EventHorizonDeviationFast = EventHorizonDeviation;
60  EventHorizonDeviationSlow = 1 / EventHorizonDeviation;
61 
62  if (PastBlocksMass >= PastBlocksMin) {
63  if ((PastRateAdjustmentRatio <= EventHorizonDeviationSlow) || (PastRateAdjustmentRatio >= EventHorizonDeviationFast))
64  { assert(BlockReading); break; }
65  }
66  if (BlockReading->pprev == NULL) { assert(BlockReading); break; }
67  BlockReading = BlockReading->pprev;
68  }
69 
70  arith_uint256 bnNew(PastDifficultyAverage);
71  if (PastRateActualSeconds != 0 && PastRateTargetSeconds != 0) {
72  bnNew *= PastRateActualSeconds;
73  bnNew /= PastRateTargetSeconds;
74  }
75 
76  if (bnNew > UintToArith256(params.powLimit)) {
77  bnNew = UintToArith256(params.powLimit);
78  }
79 
80  return bnNew.GetCompact();
81 }
82 
83 unsigned int static DarkGravityWave(const CBlockIndex* pindexLast, const Consensus::Params& params) {
84  /* current difficulty formula, dash - DarkGravity v3, written by Evan Duffield - evan@dash.org */
85  const arith_uint256 bnPowLimit = UintToArith256(params.powLimit);
86  int64_t nPastBlocks = 24;
87 
88  // make sure we have at least (nPastBlocks + 1) blocks, otherwise just return powLimit
89  if (!pindexLast || pindexLast->nHeight < nPastBlocks) {
90  return bnPowLimit.GetCompact();
91  }
92 
93  const CBlockIndex *pindex = pindexLast;
94  arith_uint256 bnPastTargetAvg;
95 
96  for (unsigned int nCountBlocks = 1; nCountBlocks <= nPastBlocks; nCountBlocks++) {
97  arith_uint256 bnTarget = arith_uint256().SetCompact(pindex->nBits);
98  if (nCountBlocks == 1) {
99  bnPastTargetAvg = bnTarget;
100  } else {
101  // NOTE: that's not an average really...
102  bnPastTargetAvg = (bnPastTargetAvg * nCountBlocks + bnTarget) / (nCountBlocks + 1);
103  }
104 
105  if(nCountBlocks != nPastBlocks) {
106  assert(pindex->pprev); // should never fail
107  pindex = pindex->pprev;
108  }
109  }
110 
111  arith_uint256 bnNew(bnPastTargetAvg);
112 
113  int64_t nActualTimespan = pindexLast->GetBlockTime() - pindex->GetBlockTime();
114  // NOTE: is this accurate? nActualTimespan counts it for (nPastBlocks - 1) blocks only...
115  int64_t nTargetTimespan = nPastBlocks * params.nPowTargetSpacing;
116 
117  if (nActualTimespan < nTargetTimespan/3)
118  nActualTimespan = nTargetTimespan/3;
119  if (nActualTimespan > nTargetTimespan*3)
120  nActualTimespan = nTargetTimespan*3;
121 
122  // Retarget
123  bnNew *= nActualTimespan;
124  bnNew /= nTargetTimespan;
125 
126  if (bnNew > bnPowLimit) {
127  bnNew = bnPowLimit;
128  }
129 
130  return bnNew.GetCompact();
131 }
132 
133 unsigned int GetNextWorkRequiredBTC(const CBlockIndex* pindexLast, const CBlockHeader *pblock, const Consensus::Params& params)
134 {
135  unsigned int nProofOfWorkLimit = UintToArith256(params.powLimit).GetCompact();
136 
137  // Genesis block
138  if (pindexLast == NULL)
139  return nProofOfWorkLimit;
140 
141  // Only change once per interval
142  if ((pindexLast->nHeight+1) % params.DifficultyAdjustmentInterval() != 0)
143  {
144  if (params.fPowAllowMinDifficultyBlocks)
145  {
146  // Special difficulty rule for testnet:
147  // If the new block's timestamp is more than 2* 2.5 minutes
148  // then allow mining of a min-difficulty block.
149  if (pblock->GetBlockTime() > pindexLast->GetBlockTime() + params.nPowTargetSpacing*2)
150  return nProofOfWorkLimit;
151  else
152  {
153  // Return the last non-special-min-difficulty-rules-block
154  const CBlockIndex* pindex = pindexLast;
155  while (pindex->pprev && pindex->nHeight % params.DifficultyAdjustmentInterval() != 0 && pindex->nBits == nProofOfWorkLimit)
156  pindex = pindex->pprev;
157  return pindex->nBits;
158  }
159  }
160  return pindexLast->nBits;
161  }
162 
163  // Go back by what we want to be 1 day worth of blocks
164  int nHeightFirst = pindexLast->nHeight - (params.DifficultyAdjustmentInterval()-1);
165  assert(nHeightFirst >= 0);
166  const CBlockIndex* pindexFirst = pindexLast->GetAncestor(nHeightFirst);
167  assert(pindexFirst);
168 
169  return CalculateNextWorkRequired(pindexLast, pindexFirst->GetBlockTime(), params);
170 }
171 
172 unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHeader *pblock, const Consensus::Params& params)
173 {
174  unsigned int retarget = DIFF_DGW;
175 
176  // mainnet/regtest share a configuration
177  if (Params().NetworkIDString() == CBaseChainParams::MAIN || Params().NetworkIDString() == CBaseChainParams::REGTEST) {
178  if (pindexLast->nHeight + 1 >= 34140) retarget = DIFF_DGW;
179  else if (pindexLast->nHeight + 1 >= 15200) retarget = DIFF_KGW;
180  else retarget = DIFF_BTC;
181  // testnet -- we want a lot of coins in existance early on
182  } else {
183  if (pindexLast->nHeight + 1 >= 4001) retarget = DIFF_DGW;
184  else retarget = DIFF_BTC;
185  }
186 
187  // Bitcoin style retargeting
188  if (retarget == DIFF_BTC)
189  {
190  return GetNextWorkRequiredBTC(pindexLast, pblock, params);
191  }
192 
193  // Retarget using Kimoto Gravity Wave
194  else if (retarget == DIFF_KGW)
195  {
196  return KimotoGravityWell(pindexLast, params);
197  }
198 
199  // Retarget using Dark Gravity Wave 3 by default
200  return DarkGravityWave(pindexLast, params);
201 }
202 
203 // for DIFF_BTC only!
204 unsigned int CalculateNextWorkRequired(const CBlockIndex* pindexLast, int64_t nFirstBlockTime, const Consensus::Params& params)
205 {
206  if (params.fPowNoRetargeting)
207  return pindexLast->nBits;
208 
209  // Limit adjustment step
210  int64_t nActualTimespan = pindexLast->GetBlockTime() - nFirstBlockTime;
211  LogPrintf(" nActualTimespan = %d before bounds\n", nActualTimespan);
212  if (nActualTimespan < params.nPowTargetTimespan/4)
213  nActualTimespan = params.nPowTargetTimespan/4;
214  if (nActualTimespan > params.nPowTargetTimespan*4)
215  nActualTimespan = params.nPowTargetTimespan*4;
216 
217  // Retarget
218  const arith_uint256 bnPowLimit = UintToArith256(params.powLimit);
219  arith_uint256 bnNew;
220  arith_uint256 bnOld;
221  bnNew.SetCompact(pindexLast->nBits);
222  bnOld = bnNew;
223  bnNew *= nActualTimespan;
224  bnNew /= params.nPowTargetTimespan;
225 
226  if (bnNew > bnPowLimit)
227  bnNew = bnPowLimit;
228 
230  LogPrintf("GetNextWorkRequired RETARGET\n");
231  LogPrintf("params.nPowTargetTimespan = %d nActualTimespan = %d\n", params.nPowTargetTimespan, nActualTimespan);
232  LogPrintf("Before: %08x %s\n", pindexLast->nBits, bnOld.ToString());
233  LogPrintf("After: %08x %s\n", bnNew.GetCompact(), bnNew.ToString());
234 
235  return bnNew.GetCompact();
236 }
237 
238 bool CheckProofOfWork(uint256 hash, unsigned int nBits, const Consensus::Params& params)
239 {
240  bool fNegative;
241  bool fOverflow;
242  arith_uint256 bnTarget;
243 
244  bnTarget.SetCompact(nBits, &fNegative, &fOverflow);
245 
246  // Check range
247  if (fNegative || bnTarget == 0 || fOverflow || bnTarget > UintToArith256(params.powLimit))
248  return error("CheckProofOfWork(): nBits below minimum work");
249 
250  // Check proof of work matches claimed amount
251  if (UintToArith256(hash) > bnTarget)
252  return error("CheckProofOfWork(): hash doesn't match nBits");
253 
254  return true;
255 }
256 
258 {
259  arith_uint256 bnTarget;
260  bool fNegative;
261  bool fOverflow;
262  bnTarget.SetCompact(block.nBits, &fNegative, &fOverflow);
263  if (fNegative || fOverflow || bnTarget == 0)
264  return 0;
265  // We need to compute 2**256 / (bnTarget+1), but we can't represent 2**256
266  // as it's too large for a arith_uint256. However, as 2**256 is at least as large
267  // as bnTarget+1, it is equal to ((2**256 - bnTarget - 1) / (bnTarget+1)) + 1,
268  // or ~bnTarget / (nTarget+1) + 1.
269  return (~bnTarget / (bnTarget + 1)) + 1;
270 }
271 
272 int64_t GetBlockProofEquivalentTime(const CBlockIndex& to, const CBlockIndex& from, const CBlockIndex& tip, const Consensus::Params& params)
273 {
274  arith_uint256 r;
275  int sign = 1;
276  if (to.nChainWork > from.nChainWork) {
277  r = to.nChainWork - from.nChainWork;
278  } else {
279  r = from.nChainWork - to.nChainWork;
280  sign = -1;
281  }
282  r = r * arith_uint256(params.nPowTargetSpacing) / GetBlockProof(tip);
283  if (r.bits() > 63) {
284  return sign * std::numeric_limits<int64_t>::max();
285  }
286  return sign * r.GetLow64();
287 }
arith_uint256 nChainWork
(memory only) Total amount of work (expected number of hashes) in the chain up to and including this ...
Definition: chain.h:125
std::string ToString() const
int64_t GetBlockProofEquivalentTime(const CBlockIndex &to, const CBlockIndex &from, const CBlockIndex &tip, const Consensus::Params &params)
Definition: pow.cpp:272
unsigned int nBits
Definition: chain.h:143
Definition: pow.h:23
bool fPowAllowMinDifficultyBlocks
Definition: params.h:78
arith_uint256 & SetCompact(uint32_t nCompact, bool *pfNegative=NULL, bool *pfOverflow=NULL)
Definition: pow.h:21
unsigned int GetNextWorkRequiredBTC(const CBlockIndex *pindexLast, const CBlockHeader *pblock, const Consensus::Params &params)
Definition: pow.cpp:133
int64_t GetBlockTime() const
Definition: block.h:66
unsigned int CalculateNextWorkRequired(const CBlockIndex *pindexLast, int64_t nFirstBlockTime, const Consensus::Params &params)
Definition: pow.cpp:204
uint32_t GetCompact(bool fNegative=false) const
arith_uint256 UintToArith256(const uint256 &a)
bool fPowNoRetargeting
Definition: params.h:79
#define LogPrintf(...)
Definition: util.h:98
unsigned int GetNextWorkRequired(const CBlockIndex *pindexLast, const CBlockHeader *pblock, const Consensus::Params &params)
Definition: pow.cpp:172
static bool error(const char *format)
Definition: util.h:131
int64_t nPowTargetSpacing
Definition: params.h:80
arith_uint256 GetBlockProof(const CBlockIndex &block)
Definition: pow.cpp:257
bool CheckProofOfWork(uint256 hash, unsigned int nBits, const Consensus::Params &params)
Definition: pow.cpp:238
int64_t DifficultyAdjustmentInterval() const
Definition: params.h:82
unsigned static int DarkGravityWave(const CBlockIndex *pindexLast, const Consensus::Params &params)
Definition: pow.cpp:83
int64_t nPowTargetTimespan
Definition: params.h:81
unsigned static int KimotoGravityWell(const CBlockIndex *pindexLast, const Consensus::Params &params)
Definition: pow.cpp:17
const CChainParams & Params()
uint64_t GetLow64() const
CBlockIndex * GetAncestor(int height)
Efficiently find an ancestor of this block.
Definition: chain.cpp:78
Definition: pow.h:22
uint256 powLimit
Definition: params.h:77
static const std::string MAIN
int64_t GetBlockTime() const
Definition: chain.h:223
CBlockIndex * pprev
pointer to the index of the predecessor of this block
Definition: chain.h:107
int nHeight
height of the entry in the chain. The genesis block has height 0
Definition: chain.h:113
unsigned int bits() const
static const std::string REGTEST