diff --git a/src/pow.h b/src/pow.h --- a/src/pow.h +++ b/src/pow.h @@ -14,10 +14,18 @@ class CBlockIndex; class uint256; -unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHeader *pblock, const Consensus::Params&); -unsigned int CalculateNextWorkRequired(const CBlockIndex* pindexLast, int64_t nFirstBlockTime, const Consensus::Params&); +unsigned int GetNextWorkRequired(const CBlockIndex *pindexLast, + const CBlockHeader *pblock, + const Consensus::Params &); +unsigned int CalculateNextWorkRequired(const CBlockIndex *pindexLast, + int64_t nFirstBlockTime, + const Consensus::Params &); -/** Check whether a block hash satisfies the proof-of-work requirement specified by nBits */ -bool CheckProofOfWork(uint256 hash, unsigned int nBits, const Consensus::Params&); +/** + * Check whether a block hash satisfies the proof-of-work requirement specified + * by nBits + */ +bool CheckProofOfWork(uint256 hash, unsigned int nBits, + const Consensus::Params &); #endif // BITCOIN_POW_H diff --git a/src/pow.cpp b/src/pow.cpp --- a/src/pow.cpp +++ b/src/pow.cpp @@ -10,56 +10,61 @@ #include "primitives/block.h" #include "uint256.h" -unsigned int GetNextWorkRequired(const CBlockIndex* pindexLast, const CBlockHeader *pblock, const Consensus::Params& params) -{ - unsigned int nProofOfWorkLimit = UintToArith256(params.powLimit).GetCompact(); +unsigned int GetNextWorkRequired(const CBlockIndex *pindexLast, + const CBlockHeader *pblock, + const Consensus::Params ¶ms) { + unsigned int nProofOfWorkLimit = + UintToArith256(params.powLimit).GetCompact(); // Genesis block - if (pindexLast == NULL) - return nProofOfWorkLimit; + if (pindexLast == NULL) return nProofOfWorkLimit; // Only change once per difficulty adjustment interval - if ((pindexLast->nHeight+1) % params.DifficultyAdjustmentInterval() != 0) - { - if (params.fPowAllowMinDifficultyBlocks) - { + if ((pindexLast->nHeight + 1) % params.DifficultyAdjustmentInterval() != + 0) { + if (params.fPowAllowMinDifficultyBlocks) { // Special difficulty rule for testnet: - // If the new block's timestamp is more than 2* 10 minutes - // then allow mining of a min-difficulty block. - if (pblock->GetBlockTime() > pindexLast->GetBlockTime() + params.nPowTargetSpacing*2) + // If the new block's timestamp is more than 2* 10 minutes then + // allow mining of a min-difficulty block. + if (pblock->GetBlockTime() > + pindexLast->GetBlockTime() + params.nPowTargetSpacing * 2) { return nProofOfWorkLimit; - else - { - // Return the last non-special-min-difficulty-rules-block - const CBlockIndex* pindex = pindexLast; - while (pindex->pprev && pindex->nHeight % params.DifficultyAdjustmentInterval() != 0 && pindex->nBits == nProofOfWorkLimit) - pindex = pindex->pprev; - return pindex->nBits; } + + // Return the last non-special-min-difficulty-rules-block + const CBlockIndex *pindex = pindexLast; + while (pindex->pprev && + pindex->nHeight % params.DifficultyAdjustmentInterval() != + 0 && + pindex->nBits == nProofOfWorkLimit) + pindex = pindex->pprev; + return pindex->nBits; } return pindexLast->nBits; } // Go back by what we want to be 14 days worth of blocks - int nHeightFirst = pindexLast->nHeight - (params.DifficultyAdjustmentInterval()-1); + int nHeightFirst = + pindexLast->nHeight - (params.DifficultyAdjustmentInterval() - 1); assert(nHeightFirst >= 0); - const CBlockIndex* pindexFirst = pindexLast->GetAncestor(nHeightFirst); + const CBlockIndex *pindexFirst = pindexLast->GetAncestor(nHeightFirst); assert(pindexFirst); - return CalculateNextWorkRequired(pindexLast, pindexFirst->GetBlockTime(), params); + return CalculateNextWorkRequired(pindexLast, pindexFirst->GetBlockTime(), + params); } -unsigned int CalculateNextWorkRequired(const CBlockIndex* pindexLast, int64_t nFirstBlockTime, const Consensus::Params& params) -{ - if (params.fPowNoRetargeting) - return pindexLast->nBits; +unsigned int CalculateNextWorkRequired(const CBlockIndex *pindexLast, + int64_t nFirstBlockTime, + const Consensus::Params ¶ms) { + if (params.fPowNoRetargeting) return pindexLast->nBits; // Limit adjustment step int64_t nActualTimespan = pindexLast->GetBlockTime() - nFirstBlockTime; - if (nActualTimespan < params.nPowTargetTimespan/4) - nActualTimespan = params.nPowTargetTimespan/4; - if (nActualTimespan > params.nPowTargetTimespan*4) - nActualTimespan = params.nPowTargetTimespan*4; + if (nActualTimespan < params.nPowTargetTimespan / 4) + nActualTimespan = params.nPowTargetTimespan / 4; + if (nActualTimespan > params.nPowTargetTimespan * 4) + nActualTimespan = params.nPowTargetTimespan * 4; // Retarget const arith_uint256 bnPowLimit = UintToArith256(params.powLimit); @@ -68,14 +73,13 @@ bnNew *= nActualTimespan; bnNew /= params.nPowTargetTimespan; - if (bnNew > bnPowLimit) - bnNew = bnPowLimit; + if (bnNew > bnPowLimit) bnNew = bnPowLimit; return bnNew.GetCompact(); } -bool CheckProofOfWork(uint256 hash, unsigned int nBits, const Consensus::Params& params) -{ +bool CheckProofOfWork(uint256 hash, unsigned int nBits, + const Consensus::Params ¶ms) { bool fNegative; bool fOverflow; arith_uint256 bnTarget; @@ -83,12 +87,12 @@ bnTarget.SetCompact(nBits, &fNegative, &fOverflow); // Check range - if (fNegative || bnTarget == 0 || fOverflow || bnTarget > UintToArith256(params.powLimit)) + if (fNegative || bnTarget == 0 || fOverflow || + bnTarget > UintToArith256(params.powLimit)) return false; // Check proof of work matches claimed amount - if (UintToArith256(hash) > bnTarget) - return false; + if (UintToArith256(hash) > bnTarget) return false; return true; }