description |
---|
An explanation of how difficulty and hashrate are calculated. |
Difficulty in Zilliqa is represented as an integer representing the number of zero bits prefixing a 'boundary' value. Solutions are determined to be valid or not by checking that the candidate is lower than this boundary. The boundary value is 256 bits long.
To illustrate this, let's convert difficulty 20 to a boundary. First, difficulty 0 is represented as the following:
Binary: 0b1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111
Hex: 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
A difficulty of 20 means that there are 20 leading zeros.
Binary: 0b0000000000000000000011111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111
Hex: 0x00000fffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
A valid solution is an ethash candidate (the hash of the next block template and the nonce candidate) that has a value lower than that boundary. In this case, any nonce produces a hash between 0x0000000000000000000000000000000000000000000000000000000000000000
and 0x00000fffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
will be accepted by the network.
The following python snippet generates the boundary from the difficulty.
def to_boundary(diff):
"""Converts a numerical difficulty to a boundary value.
Args:
diff (int): the Zilliqa difficulty (number of prefixed zeros).
Returns:
int: the target boundary to compare the solution to.
"""
result = pow(2, 256)/pow(2, diff)
return result
The expected number of hashes is the expected number of candidates to go through before finding a solution. This number is not indicative of the minimum or maximum number of candidates to generate. It is an expected value which is converged towards over a period of time.
The relationship between the expected hashes and the boundary is as follows: expected hashes = 2^256 / boundary,
or alternatively 2 ^ difficulty
.
The following python snippet generates the expected hashes from the difficulty:
def to_hashes(diff):
"""Converts a numerical difficulty to the absolute expected number of hashes required.
Args:
diff (int): the Zilliqa difficulty (number of prefixed zeros).
Returns:
int: the absolute expected number of hashes required.
"""
result = pow(2, diff)
return result
The expected hashrate is the hashrate required to find a solution within the proof-of-submission window. Since the proof-of-work window is defined as 150 seconds in Zilliqa, the expected hashrate can be calculated with the formula: expected hashrate = expected hashes / 150
.
The following python snippet generates the expected hashrate from the difficulty:
def to_hashrate(diff):
"""Converts a numerical difficulty to the hashrate (H/s) required to obtain a solution
within the proof-of-work window.
Args:
diff (int): the Zilliqa difficulty (number of prefixed zeros).
Returns:
int: the hashrate (H/s) required to obtain a solution within the proof-of-work window.
"""
pow_window = 150
return to_hashes(diff)/pow_window
There is an on-going conversation in the Zilliqa forum on this topic.
{% embed url="https://forum.zilliqa.com/t/difficulty-adjustment-issues/446" %}
The first thing to derive is the expected number of nodes that pass proof-of-work with your hashrate in one DS epoch. To do this, you apply the formula: number of nodes = expected hashrate / your hashrate
.
For instance, if the difficulty is 30 and you have 55 Mh/s worth of hashing power, then:
difficulty = 30
expected hashes = 2^30 (or 1 Gh)
expected hashrate = expected hashes / 60 = 18 Mh/s
your hashrate = 55 Mh/s
number of nodes = your hashrate / expected hashrate = ~3 nodes
From the above, you'd expect that you have enough hash power to find solutions for three nodes every DS epoch.
What if the expected number of nodes is less than 1? The figure can now be converted into the number of DS epochs you would expect to wait before having a node that finds a solution.
number of epochs = 1 / number of nodes