Bitcoin Forum

Bitcoin => Bitcoin Technical Support => Topic started by: haylingwriters on May 30, 2018, 08:44:40 AM



Title: Difficulty with Difficulty
Post by: haylingwriters on May 30, 2018, 08:44:40 AM
I am using Bitcoin Core 64-bit, 0.16.0 (on Windows 7) and I am playing around with PHP to access Bitcoin client with RPC.

It's all going quite well but I have hit a bit of a roadblock trying to process Difficulty.

The "GetDifficulty" RPC call returns a value like
4306949573981.513


How do I transform this value into an actual target like 0x1b0404cb ?


Title: Re: Difficulty with Difficulty
Post by: bob123 on May 30, 2018, 09:02:52 AM
~~Edit: Statement corrected, calculation in second post~~



For example:
If the packed target from the block is 0x1b0404cb, the target you are looking for is:

Code:
0x0404cb * 2**(8*(0x1b - 3)) = 0x00000000000404CB000000000000000000000000000000000000000000000000


Source: https://en.bitcoin.it/wiki/Difficulty#How_is_difficulty_stored_in_blocks.3F (https://en.bitcoin.it/wiki/Difficulty#How_is_difficulty_stored_in_blocks.3F)


Title: Re: Difficulty with Difficulty
Post by: haylingwriters on May 30, 2018, 09:27:36 AM
First, you need to convert your decimal number into hex.
Then you will have a packed represantation of the target.

Thanks for the quick reply. A problem I have is how to interpret the number returned, for example

4306949573981.513

What does the period represent? Is it simply a decimal point? If so, do I ignore the digits to the right of the decimal point when converting the number to hex?

Does the period represent a floating point number?


Title: Re: Difficulty with Difficulty
Post by: bob123 on May 30, 2018, 12:06:25 PM
First, you need to convert your decimal number into hex.
Then you will have a packed represantation of the target.

Thanks for the quick reply. A problem I have is how to interpret the number returned, for example

4306949573981.513

What does the period represent? Is it simply a decimal point? If so, do I ignore the digits to the right of the decimal point when converting the number to hex?

Does the period represent a floating point number?


I have to correct my statement, please ignore those two sentences.


getDifficulty() returns the factor (decimal) which describes how much more difficult it is to find a block, relative to the difficulty at the highest possible target (highest target = lowest difficulty).

Difficulty = largest possible target / current target.


So to calculate the target you have to divide the largest possible target (0x00000000FFFF0000000000000000000000000000000000000000000000000000) by the difficulty.



In your case:


Largest possible target:
(0x00000000FFFF0000000000000000000000000000000000000000000000000000)16 = (26959535291011309493156476344723991336010898738574164086137773096960)10

Largest target / difficulty:
26959535291011309493156476344723991336010898738574164086137773096960) / 4306949573981.513 = 6.2595428 * 1054

Converted into hex: 0x415A48FC207EB1A005D72938CFF3FCCF0A000000000000

Now padding with leading 0's until length = 64: 0x000000000000000000415A48FC207EB1A005D72938CFF3FCCF0A000000000000


So the current target is :
000000000000000000415A48FC207EB1A005D72938CFF3FCCF0A000000000000
The hash of the latest block (https://blockchain.info/de/block/0000000000000000003424388ace9f93daaaf2980e41fcf0408e0c6f10800de9) is
0000000000000000003424388ace9f93daaaf2980e41fcf0408e0c6f10800de9


Title: Re: Difficulty with Difficulty
Post by: haylingwriters on May 30, 2018, 02:49:50 PM
Excellent response. Thank you. I found that the digits to the right of the decimal point in the value returned from Getdifficulty can safely be ignored.

Now, once I have figured how to handle the very large numbers in PHP I should be in good shape.


Title: Re: Difficulty with Difficulty
Post by: LoyceV on May 30, 2018, 07:17:52 PM
Now, once I have figured how to handle the very large numbers in PHP I should be in good shape.
Can you use bc (https://linux.die.net/man/1/bc) (see PHP: BC Math (http://php.net/manual/en/book.bc.php)?

Code:
echo 26959535291011309493156476344723991336010898738574164086137773096960 / 4306949573981.513 | bc
6259542822111301937734005144785305510317670759324150579



Title: Re: Difficulty with Difficulty
Post by: haylingwriters on May 31, 2018, 01:20:07 PM
Now, once I have figured how to handle the very large numbers in PHP I should be in good shape.
Can you use bc (https://linux.die.net/man/1/bc) (see PHP: BC Math (http://php.net/manual/en/book.bc.php)?

Code:
echo 26959535291011309493156476344723991336010898738574164086137773096960 / 4306949573981.513 | bc
6259542822111301937734005144785305510317670759324150579



The actual arithmetic (floating point) was not too much of a problem, the challenge was converting the result to hex. I managed to find a bit of code on the web which did the job a treat.

https://stackoverflow.com/questions/5301034/how-to-generate-random-64-bit-value-as-decimal-string-in-php/5302533#5302533  (https://stackoverflow.com/questions/5301034/how-to-generate-random-64-bit-value-as-decimal-string-in-php/5302533#5302533)

(Section 2 of the above link.)