I have updated the difficulty adjustment algorithm (DAA) to be based on exponential decay. I was unhappy with both previous algorithms because they didn’t adjust quickly enough based on the current timestamp. The new DAA adjusts very quickly, up to the current moment, and also operates on only the previous two blocks. It is in production and I’m very pleased with it so far. Here’s how it works.
Consider the two previous blocks. Find the time between them, , and the desired time (ten minutes). The difficulty of the previous block was , and our goal is to find , the new difficulty. First, set . This is the difficulty that would have been set if the time between the two blocks was exactly .
Next, we wish to change the difficulty to have the following properties:
These properties ensure the block interval stays very close to ten minutes.
We can use exponential decay to achieve these properties. Let be the time since the previous block. Let be the maximum increase in difficulty immediately after the previous block. Let and . The new difficulty is , where is the minimum difficulty, currently set to .
This algorithm is currently running in production and I’ve been very pleased with it so far. It adjusts quickly and it keeps the block interval very close to ten minutes, so long as someone is mining. When no one mines (often the case in development), the difficulty drops to the minimum exponentially. That makes it fast to start mining again when you’re ready.