pow.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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.
  5. #include "pow.h"
  6. #include "arith_uint256.h"
  7. #include "chain.h"
  8. #include "chainparams.h"
  9. #include "crypto/equihash.h"
  10. #include "primitives/block.h"
  11. #include "streams.h"
  12. #include "uint256.h"
  13. #include "util.h"
  14. #include "sodium.h"
  15. unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHeader *pblock, const Consensus::Params& params)
  16. {
  17. unsigned int nProofOfWorkLimit = UintToArith256(params.powLimit).GetCompact();
  18. // Genesis block
  19. if (pindexLast == NULL)
  20. return nProofOfWorkLimit;
  21. // Find the first block in the averaging interval
  22. const CBlockIndex* pindexFirst = pindexLast;
  23. arith_uint256 bnTot {0};
  24. for (int i = 0; pindexFirst && i < params.nPowAveragingWindow; i++) {
  25. arith_uint256 bnTmp;
  26. bnTmp.SetCompact(pindexFirst->nBits);
  27. bnTot += bnTmp;
  28. pindexFirst = pindexFirst->pprev;
  29. }
  30. // Check we have enough blocks
  31. if (pindexFirst == NULL)
  32. return nProofOfWorkLimit;
  33. arith_uint256 bnAvg {bnTot / params.nPowAveragingWindow};
  34. return CalculateNextWorkRequired(bnAvg, pindexLast->GetMedianTimePast(), pindexFirst->GetMedianTimePast(), params);
  35. }
  36. unsigned int CalculateNextWorkRequired(arith_uint256 bnAvg,
  37. int64_t nLastBlockTime, int64_t nFirstBlockTime,
  38. const Consensus::Params& params)
  39. {
  40. // Limit adjustment step
  41. // Use medians to prevent time-warp attacks
  42. int64_t nActualTimespan = nLastBlockTime - nFirstBlockTime;
  43. LogPrint("pow", " nActualTimespan = %d before dampening\n", nActualTimespan);
  44. nActualTimespan = params.AveragingWindowTimespan() + (nActualTimespan - params.AveragingWindowTimespan())/4;
  45. LogPrint("pow", " nActualTimespan = %d before bounds\n", nActualTimespan);
  46. if (nActualTimespan < params.MinActualTimespan())
  47. nActualTimespan = params.MinActualTimespan();
  48. if (nActualTimespan > params.MaxActualTimespan())
  49. nActualTimespan = params.MaxActualTimespan();
  50. // Retarget
  51. const arith_uint256 bnPowLimit = UintToArith256(params.powLimit);
  52. arith_uint256 bnNew {bnAvg};
  53. bnNew /= params.AveragingWindowTimespan();
  54. bnNew *= nActualTimespan;
  55. if (bnNew > bnPowLimit)
  56. bnNew = bnPowLimit;
  57. /// debug print
  58. LogPrint("pow", "GetNextWorkRequired RETARGET\n");
  59. LogPrint("pow", "params.AveragingWindowTimespan() = %d nActualTimespan = %d\n", params.AveragingWindowTimespan(), nActualTimespan);
  60. LogPrint("pow", "Current average: %08x %s\n", bnAvg.GetCompact(), bnAvg.ToString());
  61. LogPrint("pow", "After: %08x %s\n", bnNew.GetCompact(), bnNew.ToString());
  62. return bnNew.GetCompact();
  63. }
  64. bool CheckEquihashSolution(const CBlockHeader *pblock, const CChainParams& params)
  65. {
  66. unsigned int n = params.EquihashN();
  67. unsigned int k = params.EquihashK();
  68. // Hash state
  69. crypto_generichash_blake2b_state state;
  70. EhInitialiseState(n, k, state);
  71. // I = the block header minus nonce and solution.
  72. CEquihashInput I{*pblock};
  73. // I||V
  74. CDataStream ss(SER_NETWORK, PROTOCOL_VERSION);
  75. ss << I;
  76. ss << pblock->nNonce;
  77. // H(I||V||...
  78. crypto_generichash_blake2b_update(&state, (unsigned char*)&ss[0], ss.size());
  79. bool isValid;
  80. EhIsValidSolution(n, k, state, pblock->nSolution, isValid);
  81. if (!isValid)
  82. return error("CheckEquihashSolution(): invalid solution");
  83. return true;
  84. }
  85. bool CheckProofOfWork(uint256 hash, unsigned int nBits, const Consensus::Params& params)
  86. {
  87. bool fNegative;
  88. bool fOverflow;
  89. arith_uint256 bnTarget;
  90. bnTarget.SetCompact(nBits, &fNegative, &fOverflow);
  91. // Check range
  92. if (fNegative || bnTarget == 0 || fOverflow || bnTarget > UintToArith256(params.powLimit))
  93. return error("CheckProofOfWork(): nBits below minimum work");
  94. // Check proof of work matches claimed amount
  95. if (UintToArith256(hash) > bnTarget)
  96. return error("CheckProofOfWork(): hash doesn't match nBits");
  97. return true;
  98. }
  99. arith_uint256 GetBlockProof(const CBlockIndex& block)
  100. {
  101. arith_uint256 bnTarget;
  102. bool fNegative;
  103. bool fOverflow;
  104. bnTarget.SetCompact(block.nBits, &fNegative, &fOverflow);
  105. if (fNegative || fOverflow || bnTarget == 0)
  106. return 0;
  107. // We need to compute 2**256 / (bnTarget+1), but we can't represent 2**256
  108. // as it's too large for a arith_uint256. However, as 2**256 is at least as large
  109. // as bnTarget+1, it is equal to ((2**256 - bnTarget - 1) / (bnTarget+1)) + 1,
  110. // or ~bnTarget / (nTarget+1) + 1.
  111. return (~bnTarget / (bnTarget + 1)) + 1;
  112. }
  113. int64_t GetBlockProofEquivalentTime(const CBlockIndex& to, const CBlockIndex& from, const CBlockIndex& tip, const Consensus::Params& params)
  114. {
  115. arith_uint256 r;
  116. int sign = 1;
  117. if (to.nChainWork > from.nChainWork) {
  118. r = to.nChainWork - from.nChainWork;
  119. } else {
  120. r = from.nChainWork - to.nChainWork;
  121. sign = -1;
  122. }
  123. r = r * arith_uint256(params.nPowTargetSpacing) / GetBlockProof(tip);
  124. if (r.bits() > 63) {
  125. return sign * std::numeric_limits<int64_t>::max();
  126. }
  127. return sign * r.GetLow64();
  128. }