Difficulty is an integer value, which informally represents an average amount of work (number of hashes calculated) one needs to perform in order to create the current block. It adjusts for every new block: increases if the whole network hashrate is growing and vice versa. The aim of the adjusting is to keep average speed of creating new block about 120 seconds.
Target is a hex-representation of value 2**256/difficulty. Bytecoin uses it internally while checking the proof-of-work. The hash of the block header must be less of equal of the current target.
Difficulty = 1 imply target = FF..FF and means that any hash will satisfy it. This value was set for the genesis block, but now is much larger, so as the network hashrate.
This is the algorithm for calculating next block's difficulty:
- Let the last block height is 'H'.
- case 'H' of:
- 1-720: Take the blocks from height '1' to 'H'.
- 721-735: Take the blocks from height '1' to min(720,'H'-15).
- else: Take the blocks from height 'H-734' to 'H-15'.
- Sort all blocks by their [link timestamps]. Let the amount of this array is 'N'.
- if 'N' > 600 cut the first floor[('N-599')/2] elements and the last ceil[('N-599')/2] ones.
- Let 'S' be the sum of rest blocks' difficulty values and 'T' is timestamp difference between the last and the first block.
- The new difficulty is '(S*120 + T - 1)/T'.
General idea the algorithm is to sum all work nodes have done and divide it by the time they have spent. The measure of work is the corresponding difficulty values in each block. But due to unaccurate and untrusted timestamps we cannot determine exact time interval between blocks. A user can shift his timestamp forward into the future and the next time intervals might be improbable small or even negative. Presumably there will be not many such incidents, so we can just sort the timestamps and cut-off the outliers (no more than 20%). The range of the rest values is the time which was spent for 80% of corresponding blocks.