# Target

Target is a parameter of the Proof-of-Work puzzle. The value of target determines the difficulty of mining this block. In Jax.Network, as well as in the vast majority of Proof-of-Work blockchain networks, this parameter is often represented by a 256-bit number. Every block has its own value of target which is recorded into block header in the compact form.

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

In order to have completed Proof-of-Work, block should have a hash which less than or equal to target. This rule is used in Bitcoin and majority of other cryptocurrencies. However, in Jax.Network there is one extra rule: the hash of the block should pass a hash-sorting filter for the target chain.

The rule "hash less than target or equals it" has an origin in implementation of Bitcoin by Satoshi Nakamoto. However, many authors increment the value of target from code by one and recall this value as target. This approach has its benefits since slightly simplifies the formula for difficulty. Also the rule "hash less than target" sounds shorter.

## 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} \label{eq:Prb} \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} \label{eq:Diff} \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.

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.

## Compact form of target

The value of target is packed into the block header in compact form which occupy 4 bytes. This compact form is often called Bits. The target can be derived from Bits via the same predefined formula which is used in Bitcoin. For example if a target packed in a block appears as 0x1b0404cb its hexadecimal version will look as following:

0x00000000000404CB000000000000000000000000000000000000000000000000


The implementation of functions BigToCompact and CompactToBig used for conversion could be found in JaxNetD repository on GitLab or in BTCD Bitcoin implementation.

## Maximum value of target

The value of target is often bounded by some maximum value defined by the protocol.

In Jax.Network on the beacon chain the maximum target is $\displaystyle{ \scriptsize 2^{204} }$. In Go implementation this constant has a name mainNetPowLimitBeacon. Its value is set with the following code:

mainNetPowLimitBeacon = new(big.Int).Sub(new(big.Int).Lsh(bigOne, 204), bigOne)


On every shard chain the maximum target is $\displaystyle{ \scriptsize 2^{208} }$. In Go implementation this constant has a name mainNetPowLimitShard. Its value is set with the following code:

mainNetPowLimitShard = new(big.Int).Sub(new(big.Int).Lsh(bigOne, 208), bigOne)


The respective code could be found in the config file in the GitLab repository.

Notice that on Jax.Network testnet other values are used.

## Initial value of target

Targets of first blocks on chains are determined by two constant in the config file which could be found in the GitLab repository.

### Beacon chain

In Jax.Network on the beacon chain the initial value of target is $\displaystyle{ \scriptsize 2^{202} }$. The hexadecimal representation of this target is

0x0000000000000400000000000000000000000000000000000000000000000000


In Go implementation this constant has a name mainNetPowLimitBitsBeacon. Its value is set through the compact form:

mainNetPowLimitBitsBeacon uint32 = 0x1a040000


### Shard chains

On first three shard chains the initial value of target is $\displaystyle{ \scriptsize 2^{206} }$. The hexadecimal representation of this target is

0x0000000000004000000000000000000000000000000000000000000000000000


In Go implementation this constant has a name mainNetPowLimitBitsShard. Its value is set through the compact form:

mainNetPowLimitBitsShard uint32 = 0x1a400000


On every shard chain with index above 3, the initial target is 32 times the target of the beacon chain block to which this shard chain is attached.

## Pool target

Besides standard and compact form, there is a third way to represent the target used by the mining pool protocol called Stratum.

During pooled mining, a mining pool operator broadcasts tasks to individual miners. Usually these tasks are much easier than the original mining task. Instead of solving Proof-of-Work puzzle with the original value of target taken from the block header, miners solve the puzzle with higher target supplied by the mining pool. This target is derived from pool difficulty and called pool target.

In order to communicate the value of pool target from pool operator to individual miners, Stratum protocol uses the method Set_Difficulty. However, instead of sending target in the compact form, this method sends this value as a ratio between Bitcoin truncated initial target and pool target. This ratio is calculated by the function GetDifficultyRatio. The Go implementation of this function could be found in the following file in GitLab. The formula for this ratio is below: \begin{align} \small d = \frac{2^{224}-2^{208}}{T}, \end{align} where T is a pool target.