BSHA3  0.17.99
P2P Blockchain, based on Bitcoin
pow.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2018 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 <primitives/block.h>
11 #include <uint256.h>
12 #include <logging.h>
13 
14 unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHeader *pblock, const Consensus::Params& params)
15 {
16  assert(pindexLast != nullptr);
17  unsigned int nProofOfWorkLimit = UintToArith256(params.powLimit).GetCompact();
18 
19  // Only change once per difficulty adjustment interval
20  if ((pindexLast->nHeight+1) % params.DifficultyAdjustmentInterval() != 0)
21  {
23  {
24  // Special difficulty rule for testnet:
25  // If the new block's timestamp is more than 2* 10 minutes
26  // then allow mining of a min-difficulty block.
27  if (pblock->GetBlockTime() > pindexLast->GetBlockTime() + params.nPowTargetSpacing*2)
28  return nProofOfWorkLimit;
29  else
30  {
31  // Return the last non-special-min-difficulty-rules-block
32  const CBlockIndex* pindex = pindexLast;
33  while (pindex->pprev && pindex->nHeight % params.DifficultyAdjustmentInterval() != 0 && pindex->nBits == nProofOfWorkLimit)
34  pindex = pindex->pprev;
35  return pindex->nBits;
36  }
37  }
38  return pindexLast->nBits;
39  }
40 
41  // Go back by what we want to be 14 days worth of blocks
42  int nHeightFirst = pindexLast->nHeight - (params.DifficultyAdjustmentInterval()-1);
43  assert(nHeightFirst >= 0);
44  const CBlockIndex* pindexFirst = pindexLast->GetAncestor(nHeightFirst);
45  assert(pindexFirst);
46 
47  return CalculateNextWorkRequired(pindexLast, pindexFirst->GetBlockTime(), params);
48 }
49 
50 unsigned int CalculateNextWorkRequired(const CBlockIndex* pindexLast, int64_t nFirstBlockTime, const Consensus::Params& params)
51 {
52  if (params.fPowNoRetargeting)
53  return pindexLast->nBits;
54 
55  // Limit adjustment step
56  int64_t nActualTimespan = pindexLast->GetBlockTime() - nFirstBlockTime;
57  if (nActualTimespan < params.nPowTargetTimespan/4)
58  nActualTimespan = params.nPowTargetTimespan/4;
59  if (nActualTimespan > params.nPowTargetTimespan*4)
60  nActualTimespan = params.nPowTargetTimespan*4;
61 
62  // Retarget
63  const arith_uint256 bnPowLimit = UintToArith256(params.powLimit);
64  arith_uint256 bnNew;
65  bnNew.SetCompact(pindexLast->nBits);
66  bnNew *= nActualTimespan;
67  bnNew /= params.nPowTargetTimespan;
68 
69  if (bnNew > bnPowLimit)
70  bnNew = bnPowLimit;
71 
72  return bnNew.GetCompact();
73 }
74 
75 bool CheckProofOfWork(uint256 hash, unsigned int nBits, const Consensus::Params& params)
76 {
77  bool fNegative;
78  bool fOverflow;
79  arith_uint256 bnTarget;
80 
81  bnTarget.SetCompact(nBits, &fNegative, &fOverflow);
82 
83  // Check range
84  if (fNegative || bnTarget == 0 || fOverflow || bnTarget > UintToArith256(params.powLimit))
85  return false;
86 
87  //TODO
88  //LogPrintf("%s", bnTarget.ToString());
89  // Check proof of work matches claimed amount
90  if (UintToArith256(hash) > bnTarget)
91  return false;
92 
93  return true;
94 }
int64_t GetBlockTime() const
Definition: chain.h:297
int64_t nPowTargetTimespan
Definition: params.h:61
CBlockIndex * pprev
pointer to the index of the predecessor of this block
Definition: chain.h:177
bool fPowNoRetargeting
Definition: params.h:59
bool fPowAllowMinDifficultyBlocks
Definition: params.h:58
unsigned int CalculateNextWorkRequired(const CBlockIndex *pindexLast, int64_t nFirstBlockTime, const Consensus::Params &params)
Definition: pow.cpp:50
uint32_t GetCompact(bool fNegative=false) const
arith_uint256 UintToArith256(const uint256 &a)
uint256 powLimit
Proof of work parameters.
Definition: params.h:57
unsigned int GetNextWorkRequired(const CBlockIndex *pindexLast, const CBlockHeader *pblock, const Consensus::Params &params)
Definition: pow.cpp:14
int64_t nPowTargetSpacing
Definition: params.h:60
Parameters that influence chain consensus.
Definition: params.h:40
bool CheckProofOfWork(uint256 hash, unsigned int nBits, const Consensus::Params &params)
Check whether a block hash satisfies the proof-of-work requirement specified by nBits.
Definition: pow.cpp:75
int64_t GetBlockTime() const
Definition: block.h:67
256-bit unsigned big integer.
int64_t DifficultyAdjustmentInterval() const
Definition: params.h:62
256-bit opaque blob.
Definition: uint256.h:122
The block chain is a tree shaped structure starting with the genesis block at the root...
Definition: chain.h:170
arith_uint256 & SetCompact(uint32_t nCompact, bool *pfNegative=nullptr, bool *pfOverflow=nullptr)
The "compact" format is a representation of a whole number N using an unsigned 32bit number similar t...
int nHeight
height of the entry in the chain. The genesis block has height 0
Definition: chain.h:183
CBlockIndex * GetAncestor(int height)
Efficiently find an ancestor of this block.
Definition: chain.cpp:110
uint32_t nBits
Definition: chain.h:213
Nodes collect new transactions into a block, hash them into a hash tree, and scan through nonce value...
Definition: block.h:20