- Blockchain By Example
- Bellaj Badr Richard Horrocks Xun (Brian) Wu
- 245字
- 2021-06-10 18:53:44
Difficulty adjustment time interval
Difficulty is a concept used to express how difficult it is to reach the current hash target in comparison with the initial hash target used to mine the genesis block. The difficulty value is not used internally in Bitcoin, but it's a metric used to express a target's height change.
In Bitcoin, after each 2,016 blocks, each node looks at the time stamps of the past 2,015 blocks and adjusts the difficulty using the following function defined in src/consensus/params.h:
int64_t DifficultyAdjustmentInterval() const {
return nPowTargetTimespan / nPowTargetSpacing;
}
Both parameters,nPowTargetTimespan and nPowTargetSpacing, are defined in chainparams.cpp, where we will set the following new values to keep block generation at 2.5 minutes:
consensus.nPowTargetTimespan = 24 * 60 * 60;
consensus.nPowTargetSpacing = 2.5 * 60;
The nPowTargetSpacing parameter indicates the average time (2.5 minutes) in which it should be possible to solve the computational problem of a new transaction block, whereas the nPowTargetTimespan parameter adjusts the time interval (a day) during which the difficulty of the proof-of-work problem should be recalculated and adjusted. Therefore, difficulty is adjusted every 576 blocks (24 * 60 * 60 / 2.5 * 60).
If the coins were generated too quickly since the last adjustment on average, the difficulty would be increased. If they were generated too slowly, it would be decreased. We therefore have to change the value of the consensus.nMinerConfirmationWindow parameter from 2016 to 576 in the chainparams.cpp file.