Bitcoin Forum
May 24, 2024, 02:02:48 AM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: Difficulty with Difficulty  (Read 201 times)
haylingwriters (OP)
Newbie
*
Offline Offline

Activity: 4
Merit: 0


View Profile
May 30, 2018, 08:44:40 AM
 #1

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 ?
bob123
Legendary
*
Offline Offline

Activity: 1624
Merit: 2481



View Profile WWW
May 30, 2018, 09:02:52 AM
Last edit: May 30, 2018, 12:08:05 PM by bob123
 #2

~~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

haylingwriters (OP)
Newbie
*
Offline Offline

Activity: 4
Merit: 0


View Profile
May 30, 2018, 09:27:36 AM
 #3

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?
bob123
Legendary
*
Offline Offline

Activity: 1624
Merit: 2481



View Profile WWW
May 30, 2018, 12:06:25 PM
Merited by DarkStar_ (2)
 #4

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 is
0000000000000000003424388ace9f93daaaf2980e41fcf0408e0c6f10800de9

haylingwriters (OP)
Newbie
*
Offline Offline

Activity: 4
Merit: 0


View Profile
May 30, 2018, 02:49:50 PM
 #5

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.
LoyceV
Legendary
*
Offline Offline

Activity: 3318
Merit: 16687


Thick-Skinned Gang Leader and Golden Feather 2021


View Profile WWW
May 30, 2018, 07:17:52 PM
 #6

Now, once I have figured how to handle the very large numbers in PHP I should be in good shape.
Can you use bc (see PHP: BC Math?

Code:
echo 26959535291011309493156476344723991336010898738574164086137773096960 / 4306949573981.513 | bc
6259542822111301937734005144785305510317670759324150579


haylingwriters (OP)
Newbie
*
Offline Offline

Activity: 4
Merit: 0


View Profile
May 31, 2018, 01:20:07 PM
 #7

Now, once I have figured how to handle the very large numbers in PHP I should be in good shape.
Can you use bc (see PHP: BC Math?

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

(Section 2 of the above link.)
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!