mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-07-03 20:21:34 +02:00
Use arith_uint256 where necessary
Also add conversion from/to uint256 where needed.
This commit is contained in:
15
src/pow.cpp
15
src/pow.cpp
@ -5,6 +5,7 @@
|
||||
|
||||
#include "pow.h"
|
||||
|
||||
#include "arith_uint256.h"
|
||||
#include "chain.h"
|
||||
#include "chainparams.h"
|
||||
#include "primitives/block.h"
|
||||
@ -56,8 +57,8 @@ unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHead
|
||||
nActualTimespan = Params().TargetTimespan()*4;
|
||||
|
||||
// Retarget
|
||||
uint256 bnNew;
|
||||
uint256 bnOld;
|
||||
arith_uint256 bnNew;
|
||||
arith_uint256 bnOld;
|
||||
bnNew.SetCompact(pindexLast->nBits);
|
||||
bnOld = bnNew;
|
||||
bnNew *= nActualTimespan;
|
||||
@ -79,7 +80,7 @@ bool CheckProofOfWork(uint256 hash, unsigned int nBits)
|
||||
{
|
||||
bool fNegative;
|
||||
bool fOverflow;
|
||||
uint256 bnTarget;
|
||||
arith_uint256 bnTarget;
|
||||
|
||||
if (Params().SkipProofOfWorkCheck())
|
||||
return true;
|
||||
@ -91,22 +92,22 @@ bool CheckProofOfWork(uint256 hash, unsigned int nBits)
|
||||
return error("CheckProofOfWork() : nBits below minimum work");
|
||||
|
||||
// Check proof of work matches claimed amount
|
||||
if (hash > bnTarget)
|
||||
if (UintToArith256(hash) > bnTarget)
|
||||
return error("CheckProofOfWork() : hash doesn't match nBits");
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
uint256 GetBlockProof(const CBlockIndex& block)
|
||||
arith_uint256 GetBlockProof(const CBlockIndex& block)
|
||||
{
|
||||
uint256 bnTarget;
|
||||
arith_uint256 bnTarget;
|
||||
bool fNegative;
|
||||
bool fOverflow;
|
||||
bnTarget.SetCompact(block.nBits, &fNegative, &fOverflow);
|
||||
if (fNegative || fOverflow || bnTarget == 0)
|
||||
return 0;
|
||||
// We need to compute 2**256 / (bnTarget+1), but we can't represent 2**256
|
||||
// as it's too large for a uint256. However, as 2**256 is at least as large
|
||||
// as it's too large for a arith_uint256. However, as 2**256 is at least as large
|
||||
// as bnTarget+1, it is equal to ((2**256 - bnTarget - 1) / (bnTarget+1)) + 1,
|
||||
// or ~bnTarget / (nTarget+1) + 1.
|
||||
return (~bnTarget / (bnTarget + 1)) + 1;
|
||||
|
Reference in New Issue
Block a user