# Block difficulty

Block difficulty is a measure of how difficult it is to complete a Proof-of-Work puzzle of the target block. It's the mathematical expectation of the number of attempts, required before the block with a valid hash will be generated. The value of block difficulty could be calculated from parameters of Proof-of-Work puzzle: block target and hash-sorting number.

The value of block difficulty is predefined by the protocol. According to JaxNet protocol, blocks located on the same chain have the same value of difficulty within an epoch between consecutive difficulty adjustments. So difficulty may remain fixed for thousands consecutive blocks on the chain. The predefined difficulty of the next block on the chain is known as a current difficulty of this chain. Beacon chain and every shard chain in Jax.Network has its own current difficulty.

Block difficulty is also called work since it estimates the amount of effort made by miners to mine this block. Another term, used to refer to difficulty, is weight. The sum of block difficulties on the target chain is a chain weight. In the Proof-of-Work consensus, in the event of the fork, the chain with a higher weight is called "the longest chain". According to the longest chain rule, miner should mine on top of the last block of the longest chain.

Since the blockchain network is a rather new field of knowledge, there is no commonly used concepts and notations. There are few concepts related to difficulty which sometimes could cause confusion.

### Difficulty expressed in Bitcoin units

It has become a tradition that difficulty in Bitcoin is expressed in units called "Bitcoin unit difficulty" or "Bitcoin initial difficulty". This unit equals $\displaystyle{ \scriptsize 2^{32} }$ hashes. This unit is commonly used in Bitcoin community and various Bitcoin block explorers.

However, it's not the end of the story. Since the launch of Bitcoin, the difficulty of Bitcoin blocks has grown by the factor of trillion. Obviously, people don't like astronomic numbers. Therefore popular blockchain explorers express difficulty in units "T". However, on some websites "T" denotes $\displaystyle{ \scriptsize 10^{12} }$ while on others it means $\displaystyle{ \scriptsize 2^{40} }$.

Explorers for Jax.Network use similar approach to express the difficulty in the convenient form. For example, the difficulty of the beacon block is expressed in the unit which is equal to $\displaystyle{ \scriptsize 2^{64} }$ hashes. It corresponds to the initial block difficulty on the beacon chain in Jax.Network. Similarly, on shard chains the unit difficulty is $\displaystyle{ \scriptsize 2^{60} }$ hashes. The difficulty of every shard chain is expressed with this unit.

### Share difficulty

Share difficulty or pool difficulty is a difficulty of the task assigned by the mining pool to the miner. This task is a simplified version of the regular block-mining task with increased target parameter. The difficulty of this task is calculated the same way as a block difficulty.

The common source of confusion here is a form in which pool submits parameter target to the miner. It is transmitted as a ratio between initial Bitcoin target and pool target. The value of this ratio is close to the value of difficulty expressed in bitcoin units. Therefore, developers sometimes call this variable difficulty.

### Variables named difficulty

In the code of Bitcoin there are some variables which have the word "difficulty" in their names. This fact often confuses developers.

## Relation between difficulty and target

It's important to realize that block generation is not a long, set problem (like doing a million hashes), but more like a lottery. Each hash basically gives you a random number between 0 and the maximum value of a 256-bit number (which is huge). Only a small fraction of these hashes are below or equal target and pass hash-sorting filter. However, mining of the next block candidate for the chain can be modelled as a Bernoulli process with a probability of success p: \begin{align} \small p = \frac{T+1}{2^{256} \cdot 2^H}, \end{align} where T is a block target and H is a hash-sorting number. In this statistical model, "attempt" is one randomly generated hash and "success" is an event when hash meets both Proof-of-Work conditions.

Then the mathematical expectation of the number of attempts, required before block with a valid hash will be generated, is given by the formula: \begin{align} \small D = \frac{1}{p} = \frac{2^{256}}{T+1} \cdot 2^H, \end{align} where D is a block difficulty expressed in hashes, T is a block target and H is a hash-sorting number.

So the difficulty of Proof-of-Work puzzle in Jax.Network is determined by two parameters: target and hash-sorting number. Target is recorded into the block header in the compact form called Bits. Hash-sorting number is defined to be equal 10 by the protocol. However, in test networks another value could be set through the modification of the config file.

Block difficulty is also called work since it estimates the amount of effort made by miners to mine this block. In JaxNetD the function which calculates block difficulty is called CalcWork. It could be found in this file in GitLab.

One can notice that if hash-sorting number is set to zero, then the formula for difficulty becomes a standard formula for difficulty used in Bitcoin.