Add a difficulty adjustement mechanism in case of abrupt hashrate loss
Summary:
We don't want the ABC chain to be stale in case the hashrate supporting it decreases. It is unknown at this stage how much hashrate we can secure short term.
The algorithm look at the MTP of the current chain tip and the MTP of the chain tip 6 blocks in the past. The expected difference is 1h when the chain operates normally. If the difference grow bigger than 12h, the difficulty target is bumped up by 1/4 .
Because the MTP will take about 6 blocks to reflect the change in difficulty, several adjstement in a row are to be expected. Running the adjustement 6 time in a row, which is what we expect in case the chain is close to the limit.
This algorithm is compatible with the existing chain and therefore do not need an activation mechanism. Triggering it require to lose 92% of the hashrate abruptly, which never happened. It also do not allow to ressurect a dead chain. You need to be able to mine enough block to move the MTP for it to kick in. A miner will have to bet the chain will be valluable.
Test Plan:
make check
Added unit test for the retargeting algorithm.
Run a full sync of both the main chain and the testnet.
Reviewers: freetrader, awemany, sickpig, kyuupichan, #bitcoin_abc
Reviewed By: kyuupichan, #bitcoin_abc
Differential Revision: https://reviews.bitcoinabc.org/D298