# Evolution of AdaptivePoW

after my post last week, the acknowledged expert in the DAA (difficulty adjustment algorithms) zawy https://github.com/zawy12/difficulty-algorithms/issues was kind enough to not only provide feedback, but we ended up working together to fix the problems of my initial solution.

i am a generalist and am always keen on the feedback from specialists in whatever area, but when i am able to work together and we can evaluate my sometimes extreme ideas, with years of specialist experience, then the result is usually quite good.

First, the N*N decay in difficulty that i used in the original implementation was offchain, so it didnt feedback into the existing digishield DAA that the komodo chains have. Changing that to update the onchain diff made it rescue a stranded chain in 10 minutes, instead of 8 hours! It became so fast, it made one of its weaknesses that much worse.

The problem is that if the diff can drop so fast, then attackers will mine while the diff is low until it gets too high. Then just wait until it is low again, but if it gets low in 4 blocks, it ends up with an on/off hashrate effect, which ends up generating a lot more blocks per day than the target rate. I was so single minded in fixing the diff stranding problem, i neglected to make sure it couldnt be abused.

So there is the other side of the DAA problem, how to make the difficulty go up as fast as you can, without being too unstable. After several iterations, zawy proposed a method that i really like that is based on moving averages of the onchain diff. This method will rapidly increase diff and reduce the number of easy blocks when the hashrate increases (either by double or 2 million times).

Due to the desirability of symmetric algorithms, i took the liberty of using the MA idea in both directions with an elapsed time trigger. So if the last 12 blocks took less than 11 blocktimes, we check to see if we should increase the difficulty. If it took more than 13 blocktimes, we check to see if we should decrease the diff. All of this has the highest priority logic detecting if we have a very long delayed block, and if we do, instead of using N*N decay to use a much softer exponential decay based on 3^delay/blocktime.

Thanks to zawy who patiently explained to me all the maths needed and the exact MA equation, i just had to implement it and now we have a test chain that is live with what just might be the most responsive DAA that is very hard to abuse and can recover from any level of diff stranding.

AdaptivePoW might still improve if we can find a better set of MA filters, my theory is based on prime numbers, but sometimes more isnt better. Of course, if anybody can find a miner attack or mining pattern that gives disproportionate gain by switching on and off, i will try to fix it.