mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-01-18 22:35:39 +01:00
Merge pull request #4076
397668eDeduplicate uint* comparison operator logic (Pieter Wuille)df9eb5eMove {Get,Set}Compact from bignum to uint256 (Pieter Wuille)a703150Add multiplication and division to uint160/uint256 (Pieter Wuille)4d480c8Exception instead of assigning 0 in case of wrong vector length (Pieter Wuille)eb2cbd7Deduplicate shared code between uint160 and uint256 (Pieter Wuille)
This commit is contained in:
43
src/main.cpp
43
src/main.cpp
@@ -1207,13 +1207,13 @@ static const int64_t nInterval = nTargetTimespan / nTargetSpacing;
|
||||
//
|
||||
unsigned int ComputeMinWork(unsigned int nBase, int64_t nTime)
|
||||
{
|
||||
const CBigNum &bnLimit = Params().ProofOfWorkLimit();
|
||||
const uint256 &bnLimit = Params().ProofOfWorkLimit();
|
||||
// Testnet has min-difficulty blocks
|
||||
// after nTargetSpacing*2 time between blocks:
|
||||
if (TestNet() && nTime > nTargetSpacing*2)
|
||||
return bnLimit.GetCompact();
|
||||
|
||||
CBigNum bnResult;
|
||||
uint256 bnResult;
|
||||
bnResult.SetCompact(nBase);
|
||||
while (nTime > 0 && bnResult < bnLimit)
|
||||
{
|
||||
@@ -1272,8 +1272,10 @@ unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHead
|
||||
nActualTimespan = nTargetTimespan*4;
|
||||
|
||||
// Retarget
|
||||
CBigNum bnNew;
|
||||
uint256 bnNew;
|
||||
uint256 bnOld;
|
||||
bnNew.SetCompact(pindexLast->nBits);
|
||||
bnOld = bnNew;
|
||||
bnNew *= nActualTimespan;
|
||||
bnNew /= nTargetTimespan;
|
||||
|
||||
@@ -1283,23 +1285,25 @@ unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHead
|
||||
/// debug print
|
||||
LogPrintf("GetNextWorkRequired RETARGET\n");
|
||||
LogPrintf("nTargetTimespan = %d nActualTimespan = %d\n", nTargetTimespan, nActualTimespan);
|
||||
LogPrintf("Before: %08x %s\n", pindexLast->nBits, CBigNum().SetCompact(pindexLast->nBits).getuint256().ToString());
|
||||
LogPrintf("After: %08x %s\n", bnNew.GetCompact(), bnNew.getuint256().ToString());
|
||||
LogPrintf("Before: %08x %s\n", pindexLast->nBits, bnOld.ToString());
|
||||
LogPrintf("After: %08x %s\n", bnNew.GetCompact(), bnNew.ToString());
|
||||
|
||||
return bnNew.GetCompact();
|
||||
}
|
||||
|
||||
bool CheckProofOfWork(uint256 hash, unsigned int nBits)
|
||||
{
|
||||
CBigNum bnTarget;
|
||||
bnTarget.SetCompact(nBits);
|
||||
bool fNegative;
|
||||
bool fOverflow;
|
||||
uint256 bnTarget;
|
||||
bnTarget.SetCompact(nBits, &fNegative, &fOverflow);
|
||||
|
||||
// Check range
|
||||
if (bnTarget <= 0 || bnTarget > Params().ProofOfWorkLimit())
|
||||
if (fNegative || bnTarget == 0 || fOverflow || bnTarget > Params().ProofOfWorkLimit())
|
||||
return error("CheckProofOfWork() : nBits below minimum work");
|
||||
|
||||
// Check proof of work matches claimed amount
|
||||
if (hash > bnTarget.getuint256())
|
||||
if (hash > bnTarget)
|
||||
return error("CheckProofOfWork() : hash doesn't match nBits");
|
||||
|
||||
return true;
|
||||
@@ -1338,7 +1342,7 @@ void CheckForkWarningConditions()
|
||||
if (pindexBestForkTip && chainActive.Height() - pindexBestForkTip->nHeight >= 72)
|
||||
pindexBestForkTip = NULL;
|
||||
|
||||
if (pindexBestForkTip || (pindexBestInvalid && pindexBestInvalid->nChainWork > chainActive.Tip()->nChainWork + (chainActive.Tip()->GetBlockWork() * 6).getuint256()))
|
||||
if (pindexBestForkTip || (pindexBestInvalid && pindexBestInvalid->nChainWork > chainActive.Tip()->nChainWork + (chainActive.Tip()->GetBlockWork() * 6)))
|
||||
{
|
||||
if (!fLargeWorkForkFound)
|
||||
{
|
||||
@@ -1394,7 +1398,7 @@ void CheckForkWarningConditionsOnNewFork(CBlockIndex* pindexNewForkTip)
|
||||
// We define it this way because it allows us to only store the highest fork tip (+ base) which meets
|
||||
// the 7-block condition and from this always have the most-likely-to-cause-warning fork
|
||||
if (pfork && (!pindexBestForkTip || (pindexBestForkTip && pindexNewForkTip->nHeight > pindexBestForkTip->nHeight)) &&
|
||||
pindexNewForkTip->nChainWork - pfork->nChainWork > (pfork->GetBlockWork() * 7).getuint256() &&
|
||||
pindexNewForkTip->nChainWork - pfork->nChainWork > (pfork->GetBlockWork() * 7) &&
|
||||
chainActive.Height() - pindexNewForkTip->nHeight < 72)
|
||||
{
|
||||
pindexBestForkTip = pindexNewForkTip;
|
||||
@@ -1428,10 +1432,6 @@ void static InvalidChainFound(CBlockIndex* pindexNew)
|
||||
if (!pindexBestInvalid || pindexNew->nChainWork > pindexBestInvalid->nChainWork)
|
||||
{
|
||||
pindexBestInvalid = pindexNew;
|
||||
// The current code doesn't actually read the BestInvalidWork entry in
|
||||
// the block database anymore, as it is derived from the flags in block
|
||||
// index entry. We only write it for backward compatibility.
|
||||
pblocktree->WriteBestInvalidWork(CBigNum(pindexBestInvalid->nChainWork));
|
||||
uiInterface.NotifyBlocksChanged();
|
||||
}
|
||||
LogPrintf("InvalidChainFound: invalid block=%s height=%d log2_work=%.8g date=%s\n",
|
||||
@@ -2174,7 +2174,7 @@ CBlockIndex* AddToBlockIndex(CBlockHeader& block)
|
||||
pindexNew->pprev = (*miPrev).second;
|
||||
pindexNew->nHeight = pindexNew->pprev->nHeight + 1;
|
||||
}
|
||||
pindexNew->nChainWork = (pindexNew->pprev ? pindexNew->pprev->nChainWork : 0) + pindexNew->GetBlockWork().getuint256();
|
||||
pindexNew->nChainWork = (pindexNew->pprev ? pindexNew->pprev->nChainWork : 0) + pindexNew->GetBlockWork();
|
||||
pindexNew->RaiseValidity(BLOCK_VALID_TREE);
|
||||
|
||||
return pindexNew;
|
||||
@@ -2351,11 +2351,12 @@ bool CheckBlockHeader(const CBlockHeader& block, CValidationState& state, bool f
|
||||
return state.DoS(100, error("CheckBlockHeader() : block with timestamp before last checkpoint"),
|
||||
REJECT_CHECKPOINT, "time-too-old");
|
||||
}
|
||||
CBigNum bnNewBlock;
|
||||
bnNewBlock.SetCompact(block.nBits);
|
||||
CBigNum bnRequired;
|
||||
bool fOverflow = false;
|
||||
uint256 bnNewBlock;
|
||||
bnNewBlock.SetCompact(block.nBits, NULL, &fOverflow);
|
||||
uint256 bnRequired;
|
||||
bnRequired.SetCompact(ComputeMinWork(pcheckpoint->nBits, deltaTime));
|
||||
if (bnNewBlock > bnRequired)
|
||||
if (fOverflow || bnNewBlock > bnRequired)
|
||||
{
|
||||
return state.DoS(100, error("CheckBlockHeader() : block with too little proof-of-work"),
|
||||
REJECT_INVALID, "bad-diffbits");
|
||||
@@ -2926,7 +2927,7 @@ bool static LoadBlockIndexDB()
|
||||
BOOST_FOREACH(const PAIRTYPE(int, CBlockIndex*)& item, vSortedByHeight)
|
||||
{
|
||||
CBlockIndex* pindex = item.second;
|
||||
pindex->nChainWork = (pindex->pprev ? pindex->pprev->nChainWork : 0) + pindex->GetBlockWork().getuint256();
|
||||
pindex->nChainWork = (pindex->pprev ? pindex->pprev->nChainWork : 0) + pindex->GetBlockWork();
|
||||
pindex->nChainTx = (pindex->pprev ? pindex->pprev->nChainTx : 0) + pindex->nTx;
|
||||
if (pindex->IsValid(BLOCK_VALID_TRANSACTIONS))
|
||||
setBlockIndexValid.insert(pindex);
|
||||
|
||||
Reference in New Issue
Block a user