Skip to content

Commit

Permalink
Comment, cast and assert cleanup in pow.cpp
Browse files Browse the repository at this point in the history
Summary:
Comments improved and a superfluous assert which is
covered by subsequent code is removed

Test Plan:
make check
../qa/pull-tester/rpc-tests.py -extended

Reviewers: #bitcoin_abc, deadalnix, kyuupichan

Reviewed By: #bitcoin_abc, kyuupichan

Differential Revision: https://reviews.bitcoinabc.org/D635
  • Loading branch information
ftrader committed Nov 5, 2017
1 parent 167ff8b commit 2bc42bd
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions src/pow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

/**
* Compute the next required proof of work using the legacy Bitcoin difficulty
* adjustement + Emergency Difficulty Adjustement (EDA).
* adjustment + Emergency Difficulty Adjustment (EDA).
*/
static uint32_t GetNextEDAWorkRequired(const CBlockIndex *pindexPrev,
const CBlockHeader *pblock,
Expand Down Expand Up @@ -55,13 +55,13 @@ static uint32_t GetNextEDAWorkRequired(const CBlockIndex *pindexPrev,
return pindex->nBits;
}

// We can't go bellow the minimum, so early bail.
// We can't go below the minimum, so bail early.
uint32_t nBits = pindexPrev->nBits;
if (nBits == nProofOfWorkLimit) {
return nProofOfWorkLimit;
}

// If producing the last 6 block took less than 12h, we keep the same
// If producing the last 6 blocks took less than 12h, we keep the same
// difficulty.
const CBlockIndex *pindex6 = pindexPrev->GetAncestor(nHeight - 7);
assert(pindex6);
Expand All @@ -71,14 +71,15 @@ static uint32_t GetNextEDAWorkRequired(const CBlockIndex *pindexPrev,
return nBits;
}

// If producing the last 6 block took more than 12h, increase the difficulty
// target by 1/4 (which reduces the difficulty by 20%). This ensure the
// chain do not get stuck in case we lose hashrate abruptly.
// If producing the last 6 blocks took more than 12h, increase the
// difficulty target by 1/4 (which reduces the difficulty by 20%).
// This ensures that the chain does not get stuck in case we lose
// hashrate abruptly.
arith_uint256 nPow;
nPow.SetCompact(nBits);
nPow += (nPow >> 2);

// Make sure we do not go bellow allowed values.
// Make sure we do not go below allowed values.
const arith_uint256 bnPowLimit = UintToArith256(params.powLimit);
if (nPow > bnPowLimit) nPow = bnPowLimit;

Expand Down Expand Up @@ -158,7 +159,7 @@ bool CheckProofOfWork(uint256 hash, uint32_t nBits,
}

/**
* Compute the a target based on the work done between 2 blocks and the time
* Compute a target based on the work done between 2 blocks and the time
* required to produce that work.
*/
static arith_uint256 ComputeTarget(const CBlockIndex *pindexFirst,
Expand All @@ -175,9 +176,9 @@ static arith_uint256 ComputeTarget(const CBlockIndex *pindexFirst,
work *= params.nPowTargetSpacing;

// In order to avoid difficulty cliffs, we bound the amplitude of the
// adjustement we are going to do.
assert(pindexLast->nTime > pindexFirst->nTime);
int64_t nActualTimespan = pindexLast->nTime - pindexFirst->nTime;
// adjustment we are going to do to a factor in [0.5, 2].
int64_t nActualTimespan =
int64_t(pindexLast->nTime) - int64_t(pindexFirst->nTime);
if (nActualTimespan > 288 * params.nPowTargetSpacing) {
nActualTimespan = 288 * params.nPowTargetSpacing;
} else if (nActualTimespan < 72 * params.nPowTargetSpacing) {
Expand All @@ -202,7 +203,7 @@ static const CBlockIndex *GetSuitableBlock(const CBlockIndex *pindex) {
assert(pindex->nHeight >= 3);

/**
* In order to avoid a block is a very skewed timestamp to have too much
* In order to avoid a block with a very skewed timestamp having too much
* influence, we select the median of the 3 top most blocks as a starting
* point.
*/
Expand Down Expand Up @@ -252,7 +253,7 @@ uint32_t GetNextCashWorkRequired(const CBlockIndex *pindexPrev,
return UintToArith256(params.powLimit).GetCompact();
}

// Compute the difficulty based on the full adjustement interval.
// Compute the difficulty based on the full adjustment interval.
const uint32_t nHeight = pindexPrev->nHeight;
assert(nHeight >= params.DifficultyAdjustmentInterval());

Expand Down

0 comments on commit 2bc42bd

Please sign in to comment.