A Difficulty Adjustment Algorithm Based on Exponential Decay

2024-08-31 · Ryan X. Charles

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, tt', and the desired time TT (ten minutes). The difficulty of the previous block was dd, and our goal is to find DD, the new difficulty. First, set D1=TtdD_1 = \frac{T}{t'} \cdot d. This is the difficulty that would have been set if the time between the two blocks was exactly TT.

Next, we wish to change the difficulty to have the following properties:

  • Immediately after the previous block is mined, the new difficulty should be very high.
  • At ten minutes (TT), the new difficulty should be exactly D1D_1.
  • Beyond ten minutes, the difficulty should decay until it reaches a minimum.
  • Blocks with a future timestamp are invalid. This incentivizes accurate timestamps because blocks from the past have a higher difficulty, which no one wants to mine. The most rational strategy is to mine blocks with the correct timestamp.

These properties ensure the block interval stays very close to ten minutes.

We can use exponential decay to achieve these properties. Let tt be the time since the previous block. Let m=10m=10 be the maximum increase in difficulty immediately after the previous block. Let k=ln(m)k = \ln(m) and D2=D1ek(tT1)D_2 = D_1 \cdot e^{-k \cdot (\frac{t}{T}-1)}. The new difficulty is D=max(D2,dmin)D = \max(D_2, d_{\text{min}}), where dmind_{\text{min}} is the minimum difficulty, currently set to 20002000.

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.


Earlier Blog Posts


Back to Blog

Home · About · Blog · Privacy · Terms
X · Telegram · Discord · reddit · GitHub
Copyright © 2024 Ryan X. Charles LLC