# Difference between revisions of "Difficulty adjustment algorithm"

Ishyshatskyi (talk | contribs) |
|||

Line 65: | Line 65: | ||

=== Comparison with DAA in other blockchains === | === Comparison with DAA in other blockchains === | ||

− | DAA in Bitcoin,DAA in Ethereum, DAA in Bitcoin Cash | + | DAA in Bitcoin,DAA in [[Ethereum]], DAA in Bitcoin Cash |

== Legacy DAA == | == Legacy DAA == |

## Revision as of 18:55, 1 July 2022

**Difficulty adjustment algorithm** or **DAA** is a fundamental component of PoW blockchain protocol which determines the block difficulty based on the data recorded into previous blocks in the chain. It serves the crucial purpose of maintaining a stable transaction throughput by dynamically adjusting the block difficulty in response to the miners’ constantly changing computational power.^{[1]} Often DAA is a sensitive part of the protocol which requires a careful design so that miners can't game the system using coin-hopping strategies to manipulate the block difficulty for profit.

In Jax.Network, on the beacon chain the difficulty is adjusted every 2048 blocks. On shard chains difficulty is adjusted every 4096 blocks. It's important to understand that in contrast to other networks, according to JaxNet protocol, DAA adjusts only block difficulty and doesn't modify the K-coefficient. During the first year after the launch, Jax.Network was using a legacy DAA.

First difficulty adjustment algorithm was designed and implemented in Bitcoin by Satoshi Nakamoto.

## Motivation behind DAA

In the decentralised distributed network it's rather difficult to design an efficient and reliable clocks which can provide a universal time for all participants. Nevertheless, the common cryptocurrency design requires a tool that will regulate processes in the network based on the time elapsed in the real world.

The main characteristics of the chronometer in the real world is accuracy, reliability and convenience. However, in distributed networks, there are more important requirements such security against malicious actors, number of participants which can use it simultaneously and volume of the network traffic generated during its use. Although we have to admit that for people in the twenty first century the idea of distributed clocks, which doesn't accurately measure time, may sound weird.

Satoshi Nakamoto has found a decentralised solution which fulfil the purpose. His idea is to assign a Proof-of-Work puzzle to every block so that its mining will require some time. In this setting, the chain length becomes a projection of the real world time. This projection is very inaccurate. However, Bitcoin doesn't have a goal to measure time with high accuracy. It's designed to be a decentralised trustless cryptocurrency used by millions people allover the world.

### Poisson process model

Although the block mining time is a random variable, we can apply the following model and estimate time required to mine a tuple of consecutive blocks on the chain.

There are three main assumptions for the target time interval

- network hashrate [math]\displaystyle{ \scriptsize H }[/math] is stable
- block difficulty [math]\displaystyle{ \scriptsize D }[/math] remains constant
- block propagation delay, forking, orphan block rate are negligible

Then one can model block mining process as a Poisson point process with intensity [math]\displaystyle{ \scriptsize H/D }[/math]. In this model time required to mine next block has an exponential distribution with a parameter [math]\displaystyle{ \scriptsize H/D }[/math], where [math]\displaystyle{ \scriptsize D }[/math] is a block difficulty and [math]\displaystyle{ \scriptsize H }[/math] is a network hashrate.

Mining time of [math]\displaystyle{ \scriptsize k }[/math] consecutive blocks has an Erlang distribution with parameters [math]\displaystyle{ \scriptsize (k,H/D) }[/math]. It has a mean [math]\displaystyle{ \scriptsize kD/H }[/math] and variance [math]\displaystyle{ \scriptsize k(D/H)^2 }[/math]. So in this model mining time of [math]\displaystyle{ \scriptsize k }[/math] consecutive blocks has mathematical expectation [math]\displaystyle{ \scriptsize kD/H }[/math]. Relative standard deviation is [math]\displaystyle{ \scriptsize k^{-1/2} }[/math]. So this estimate is rather accurate for high values of [math]\displaystyle{ \scriptsize k }[/math].

### Accuracy of the model

The goal of DAA in Bitcoin is to adjusts the value of difficulty so that [math]\displaystyle{ \scriptsize D/H = 600 }[/math] seconds.

As a result, Bitcoin has a block rate about 6 blocks per hour and a maximum limit on the block size. This design allows to keep the network workload within the admissible range and avoid potential negative consequences for Bitcoin. Second, this mechanism has allowed many cryptocurrencies to schedule coin issuance and plan their tokenomics. This gave a new meaning to the term *digital scarcity*.^{[2]}

## Difficulty adjustment in JaxNet

### Motivation behind DAA in JaxNet

- Keep the difference between the creation time of (N+1)-th block and the creation time of the first block on the beacon chain around (600*N) seconds.

- Keep the difference between the creation time of (N+1)-th block and the creation time of the first block on the shard chain around (37.5*N) seconds.

*BeaconTimeDelta* = 600 seconds, *ShardTimeDelta* = 37500 milliseconds
^{[3]}

### Average block time

### Length of the epoch

Blockchain could be divided into segments of blocks between difficulty adjustment events. These segments of blocks are often called *epochs*. In Jax.Network these epochs have a fixed block count often called *length*. Epochs on the beacon chain have a length of 2048 blocks and epochs on every shard chain have a length of 4096 blocks.

In the reference implementation, epoch lengths are two constants *BeaconEpochLength* and *ShardEpochLength*. Their values are set in the file for chain parameters.^{[3]}

Since the expected average block time on the beacon chain is 10 minutes, the average BC epoch time span is 14 days, 5 hours and 20 minutes. On shard chains the expected average block time is 37.5 seconds. Therefore average epoch there takes 42 hours and 40 minutes.

### Description of DAA in Jax.Network

In the reference Go implementation, DAA is implemented in the function *calcNextRequiredDifficulty* which could be found in the GitLab repository. ^{[4]}

#### Step 1

#### Step 2

## Analysis of DAA

### Coin-hopping and timestamp cheating

### Comparison with DAA in other blockchains

DAA in Bitcoin,DAA in Ethereum, DAA in Bitcoin Cash

## Legacy DAA

Initially Jax.Network has been launched with another version DAA similar to one used in Bitcoin.