# Difference between revisions of "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 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.

## 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[1] with a probability of success p:

$\displaystyle{ p = \frac{T+1}{2^{256} \cdot 2^H} }$

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:

$\displaystyle{ D = \frac{1}{p} = \frac{2^{256}}{T+1} \cdot 2^H, }$

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[2] 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.