QIP 9 - Changing the Difficulty Adjustment Algorithm

Qtum
Qtum
Published in
5 min readOct 1, 2019

--

Preface

Qtum employs the PoS consensus mechanism with the block interval of 128 seconds. The actual network operating results show that the average block interval is about 144 seconds and the block interval fluctuates greatly. To solve this issue, QIP-9 proposes a new difficulty adjustment algorithm reducing the average block interval to 128 seconds and greatly reducing the fluctuation of the block interval by increasing `nPoWTargetTimespan`. Also, the existing Network Weight estimation algorithm results in a too-large variance, and the fluctuation can reach 40%. It is difficult for users to obtain more accurate Network Weight data. QIP-9 significantly reduces the variance of the calculation results by fixing the value of the denominator in the original algorithm, making the estimated staking yield more accurate.

QIP-9 Difficulty Adjustment Algorithm

Analysis of Qtum’s Difficulty Adjustment Algorithm

You can click on the links in references [1] and [2] to view the core changes made in the QIP-9 difficulty adjustment algorithm. There are two changes:

  1. Change the difficulty adjustment algorithm from linear type to exponential type;
  2. Change `nPoWTargetTimespan` from the original 960 to 4,000.

For UTXO containing coins with the number of `coins`, the condition for it to stake (mine) a block is Hash(kernel) < coins × T, where T is the mining target. Combined with the relationship between the next block mining target T(N+1) and the current block mining target Tn defined in the formula of Qtum’s difficulty adjustment algorithm [1], the mining difficulty adjustment algorithm can be simplified to

Where tn is the block interval of the current block, α=64∙(⌊nPoWTargetTimespan128⌋+1) is the adjustment factor. If the current block interval is greater than 128 seconds, the mining target of the next block will be increased, and the difficulty will be reduced, thereby stabilizing the block interval, and vice versa. The degree of difficulty adjustment can be controlled by changing. The larger the adjustment, the greater the change of difficulty, and the block interval fluctuation will be correspondingly larger. By calculation, the QIP-9 hard fork adjusts the value of from 512 to 2048.

As of the block height of 390000, the average interval of all PoS blocks on the Qtum mainnet is 144.22 seconds, with a standard deviation of 152.95 seconds. In the ideal state (the block interval obeys a geometric distribution with an expected value of 128 seconds), the block interval is 128 seconds with a standard deviation of 119.73 seconds. It can be seen that the average interval and standard deviation of the block interval in the practice are much larger than the design value. This is due to the difficulty adjustment parameter α=512 being too small.

Changes Made to the Difficulty Adjustment Algorithm in QIP-9

It is not difficult to learn from the difficulty adjustment algorithm formula that the increase of can result in the smaller changes in the mining difficulty, as well as the smaller block interval fluctuation. We simulated the effect of the two algorithms on the block interval expectation, standard deviation, and the number of blocks with large interval (block interval of more than 10 minutes) when taking different values, resulting in the following data:

It can be seen that QIP-9 exponential adjustment algorithm performs better than linear adjustment algorithm. The QIP-9 algorithm is very close to the design value of 128 seconds in average block interval, which reflects its correctness. According to the data in the above table, when > 2000, the indicators change very slowly, and the block interval expectation and standard deviation are pretty close to the ideal value. Therefore, the algorithm adjustment of QIP-9 is reasonable.

QIP-9 Network Weight Estimation

Introduction to the Network Weight Estimation Algorithm

Network Weight is defined as the total amount of coins being staked on the entire network. Network Weight is estimated because the actual value cannot be known on a decentralized network.

It replaces the block interval on the denominator with the designed average of 128. Next, we will compare the effects of the two algorithms through simulation.

Comparison of the Network Weight Estimation Algorithm

We simulated both the original and the new Network Weight estimation algorithm separately. The first figure is the simulation results for the original difficulty adjustment algorithm (linear type), and the second figure is the simulation results for QIP-9 difficulty adjustment algorithm (exponential type):

We can learn that in the case of the original difficulty adjustment algorithm and = 512, the simulation results of the two Network Weight estimation algorithms are roughly the same. While it is smaller, the variance of the Network Weight estimation proposed by QIP-9 is larger than the original algorithm. In the case of QIP-9 difficulty adjustment algorithm and = 2048, the curve of new Network Weight estimation is smoother and the variance is smaller, which is a better algorithm under the realistic conditions of less fluctuation of actual Network Weight. So, after the hard fork, the Network Weight estimation algorithm proposed by QIP-9 will be better than the original algorithm.

Impact of QIP-9 Hard Fork Upgrade

The most significant change that QIP-9 will bring is that the average block interval is shortened from 144.7 seconds to 128.0 seconds, and the average number of blocks generated per day increases significantly from 597 to 675. Also, the decrease of average block interval increases the annual mining yield by 13% (e.g. 7% →7.91%), greatly increasing the mining yield, and increases the Transactions Per Second (TPS) by 13%.

Since the difficulty adjustment change brought about by QIP-9 will be a consensus change, users of the Qtum Core wallet must update their version before the Hard Fork block height, to ensure normal use.

References

  1. Difficulty adjustment algorithm https://github.com/qtumproject/qtum/blob/733061a2b54d4d50365b07e9b48a9a8d455eadd6/src/pow.cpp#L109-L122
  2. nPowTargetTimespanV2 https://github.com/qtumproject/qtum/blob/733061a2b54d4d50365b07e9b48a9a8d455eadd6/src/chainparams.cpp#L96

--

--