You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

pow.cpp 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. #include "pow.h"
  6. #include "chainparams.h"
  7. #include "core/block.h"
  8. #include "main.h"
  9. #include "timedata.h"
  10. #include "uint256.h"
  11. #include "util.h"
  12. unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHeader *pblock)
  13. {
  14. unsigned int nProofOfWorkLimit = Params().ProofOfWorkLimit().GetCompact();
  15. // Genesis block
  16. if (pindexLast == NULL)
  17. return nProofOfWorkLimit;
  18. // Only change once per interval
  19. if ((pindexLast->nHeight+1) % Params().Interval() != 0)
  20. {
  21. if (Params().AllowMinDifficultyBlocks())
  22. {
  23. // Special difficulty rule for testnet:
  24. // If the new block's timestamp is more than 2* 10 minutes
  25. // then allow mining of a min-difficulty block.
  26. if (pblock->GetBlockTime() > pindexLast->GetBlockTime() + Params().TargetSpacing()*2)
  27. return nProofOfWorkLimit;
  28. else
  29. {
  30. // Return the last non-special-min-difficulty-rules-block
  31. const CBlockIndex* pindex = pindexLast;
  32. while (pindex->pprev && pindex->nHeight % Params().Interval() != 0 && pindex->nBits == nProofOfWorkLimit)
  33. pindex = pindex->pprev;
  34. return pindex->nBits;
  35. }
  36. }
  37. return pindexLast->nBits;
  38. }
  39. // Go back by what we want to be 14 days worth of blocks
  40. const CBlockIndex* pindexFirst = pindexLast;
  41. for (int i = 0; pindexFirst && i < Params().Interval()-1; i++)
  42. pindexFirst = pindexFirst->pprev;
  43. assert(pindexFirst);
  44. // Limit adjustment step
  45. int64_t nActualTimespan = pindexLast->GetBlockTime() - pindexFirst->GetBlockTime();
  46. LogPrintf(" nActualTimespan = %d before bounds\n", nActualTimespan);
  47. if (nActualTimespan < Params().TargetTimespan()/4)
  48. nActualTimespan = Params().TargetTimespan()/4;
  49. if (nActualTimespan > Params().TargetTimespan()*4)
  50. nActualTimespan = Params().TargetTimespan()*4;
  51. // Retarget
  52. uint256 bnNew;
  53. uint256 bnOld;
  54. bnNew.SetCompact(pindexLast->nBits);
  55. bnOld = bnNew;
  56. bnNew *= nActualTimespan;
  57. bnNew /= Params().TargetTimespan();
  58. if (bnNew > Params().ProofOfWorkLimit())
  59. bnNew = Params().ProofOfWorkLimit();
  60. /// debug print
  61. LogPrintf("GetNextWorkRequired RETARGET\n");
  62. LogPrintf("Params().TargetTimespan() = %d nActualTimespan = %d\n", Params().TargetTimespan(), nActualTimespan);
  63. LogPrintf("Before: %08x %s\n", pindexLast->nBits, bnOld.ToString());
  64. LogPrintf("After: %08x %s\n", bnNew.GetCompact(), bnNew.ToString());
  65. return bnNew.GetCompact();
  66. }
  67. bool CheckProofOfWork(uint256 hash, unsigned int nBits)
  68. {
  69. bool fNegative;
  70. bool fOverflow;
  71. uint256 bnTarget;
  72. if (Params().SkipProofOfWorkCheck())
  73. return true;
  74. bnTarget.SetCompact(nBits, &fNegative, &fOverflow);
  75. // Check range
  76. if (fNegative || bnTarget == 0 || fOverflow || bnTarget > Params().ProofOfWorkLimit())
  77. return error("CheckProofOfWork() : nBits below minimum work");
  78. // Check proof of work matches claimed amount
  79. if (hash > bnTarget)
  80. return error("CheckProofOfWork() : hash doesn't match nBits");
  81. return true;
  82. }
  83. void UpdateTime(CBlockHeader* pblock, const CBlockIndex* pindexPrev)
  84. {
  85. pblock->nTime = std::max(pindexPrev->GetMedianTimePast()+1, GetAdjustedTime());
  86. // Updating time can change work required on testnet:
  87. if (Params().AllowMinDifficultyBlocks())
  88. pblock->nBits = GetNextWorkRequired(pindexPrev, pblock);
  89. }
  90. uint256 GetProofIncrement(unsigned int nBits)
  91. {
  92. uint256 bnTarget;
  93. bool fNegative;
  94. bool fOverflow;
  95. bnTarget.SetCompact(nBits, &fNegative, &fOverflow);
  96. if (fNegative || fOverflow || bnTarget == 0)
  97. return 0;
  98. // We need to compute 2**256 / (bnTarget+1), but we can't represent 2**256
  99. // as it's too large for a uint256. However, as 2**256 is at least as large
  100. // as bnTarget+1, it is equal to ((2**256 - bnTarget - 1) / (bnTarget+1)) + 1,
  101. // or ~bnTarget / (nTarget+1) + 1.
  102. return (~bnTarget / (bnTarget + 1)) + 1;
  103. }