src/PoW/DifficultyCalculator.cpp
// Computes the proof-of-work difficulty that the next block should comply
// with. Takes an iterator over past block headers information, from latest
// (highest height) to oldest (lowest height).
//
// The difficulty calculation is based on both Digishield and GravityWave
// family of difficulty computation, coming to something very close to Zcash.
// The reference difficulty is an average of the difficulty over a window of
// DIFFICULTY_ADJUST_WINDOW blocks. The corresponding timespan is calculated
// by using the difference between the median timestamps at the beginning
// and the end of the window.
//
// The secondary proof-of-work factor is calculated along the same lines, as
// an adjustment on the deviation against the ideal value.
Computes the proof-of-work difficulty that the next block should comply
with. Takes an iterator over past block headers information, from latest
(highest height) to oldest (lowest height).

The difficulty calculation is based on both Digishield and GravityWave
family of difficulty computation, coming to something very close to Zcash.
The reference difficulty is an average of the difficulty over a window of
DIFFICULTY_ADJUST_WINDOW blocks. The corresponding timespan is calculated
by using the difference between the median timestamps at the beginning
and the end of the window.

The secondary proof-of-work factor is calculated along the same lines, as
an adjustment on the deviation against the ideal value.
Computes the proof - of - work difficulty that the next block should comply with . Takes an iterator over past block headers information from latest ( highest height ) to oldest ( lowest height ) . The difficulty calculation is based on both Digishield and GravityWave family of difficulty computation coming to something very close to Zcash . The reference difficulty is an average of the difficulty over a window of DIFFICULTY_ADJUST_WINDOW blocks . The corresponding timespan is calculated by using the difference between the median timestamps at the beginning and the end of the window . The secondary proof - of - work factor is calculated along the same lines as an adjustment on the deviation against the ideal value .
HeaderInfo DifficultyCalculator::CalculateNextDifficulty(const BlockHeader& header) const
{
	const std::vector<HeaderInfo> difficultyData = DifficultyLoader(m_pBlockDB).LoadDifficultyData(header);
	const std::vector<HeaderInfo> difficultyDataSkipFirst(difficultyData.cbegin() + 1, difficultyData.cend());
	const uint64_t sec_pow_scaling = SecondaryPOWScaling(header.GetHeight(), difficultyDataSkipFirst);
	const uint64_t ts_delta = difficultyData[DIFFICULTY_ADJUST_WINDOW].GetTimestamp() - difficultyData[0].GetTimestamp();
	uint64_t difficultySum = 0;
	for (const HeaderInfo& headerInfo : difficultyDataSkipFirst)
	{
		difficultySum += headerInfo.GetDifficulty();
	}
	const uint64_t actual = Damp(ts_delta, BLOCK_TIME_WINDOW, DAMP_FACTOR);
	const uint64_t adj_ts = Clamp(actual, BLOCK_TIME_WINDOW, CLAMP_FACTOR);
	const uint64_t difficulty = (std::max)(MIN_DIFFICULTY, difficultySum * BLOCK_TIME_SEC / adj_ts);
	return HeaderInfo::FromDiffAndScaling(difficulty, sec_pow_scaling);
}
HeaderInfo DifficultyCalculator :: CalculateNextDifficulty ( const BlockHeader & header ) const { const std :: vector < HeaderInfo > difficultyData = DifficultyLoader ( m_pBlockDB ) . LoadDifficultyData ( header ) ; const std :: vector < HeaderInfo > difficultyDataSkipFirst ( difficultyData . cbegin ( ) + 1 , difficultyData . cend ( ) ) ; const uint64_t sec_pow_scaling = SecondaryPOWScaling ( header . GetHeight ( ) , difficultyDataSkipFirst ) ; const uint64_t ts_delta = difficultyData [ DIFFICULTY_ADJUST_WINDOW ] . GetTimestamp ( ) - difficultyData [ 0 ] . GetTimestamp ( ) ; uint64_t difficultySum = 0 ; for ( const HeaderInfo & headerInfo : difficultyDataSkipFirst ) { difficultySum += headerInfo . GetDifficulty ( ) ; } const uint64_t actual = Damp ( ts_delta , BLOCK_TIME_WINDOW , DAMP_FACTOR ) ; const uint64_t adj_ts = Clamp ( actual , BLOCK_TIME_WINDOW , CLAMP_FACTOR ) ; const uint64_t difficulty = ( std :: max ) ( MIN_DIFFICULTY , difficultySum * BLOCK_TIME_SEC / adj_ts ) ; return HeaderInfo :: FromDiffAndScaling ( difficulty , sec_pow_scaling ) ; }
Computes the proof-of-work difficulty that the next block should comply
with.
Computes the proof - of - work difficulty that the next block should comply with .
// Create vector of difficulty data running from earliest // to latest, and pad with simulated pre-genesis data to allow earlier // adjustment if there isn't enough window data length will be // DIFFICULTY_ADJUST_WINDOW + 1 (for initial block time bound) // First, get the ratio of secondary PoW vs primary, skipping initial header // Get the timestamp delta across the window // Get the difficulty sum of the last DIFFICULTY_ADJUST_WINDOW elements // adjust time delta toward goal subject to dampening and clamping // minimum difficulty avoids getting stuck due to dampening