protocol – calculate new “bits” worth?

0
70


What does the bits subject signify?

To begin with, we have to perceive what the ‘bits’ subject means.

Bits is in ‘compact’ format. That is sort of like a floating level format, however it represents massive integers fairly than arbitrary actual numbers. The primary byte signifies the variety of bytes the represented quantity takes up, and the following one to 3 bytes give essentially the most important digits of the quantity. If the 2nd byte has a price higher than 127 then the quantity is interpreted as being detrimental.

To transform a optimistic integer to ‘compact’ format, we:

  • convert the integer into base 256.
  • if the primary (most vital) digit is larger than 127 (0x7f), prepend a zero digit
  • the primary byte of the ‘compact’ format is the variety of digits within the above base 256 illustration, together with the prepended zero if it is current
  • the next three bytes are the primary three digits of the above illustration. If lower than three digits are current, then a number of of the final bytes of the compact illustration might be zero.

Instance 1 – Convert 1000 to ‘compact’ format

For instance, to signify 1000 in ‘compact’ format, we convert to base 256:

1000 = (0x03)*256 + (0xe8)*1

So we have now a 2 digit base 256 quantity:

03 e8

The primary digit shouldn’t be higher than 0x7f, so we do not prepend a zero digit:

03 e8

Then the compact illustration turns into:

02 03 e8 00

Instance 2 – Convert max goal to ‘compact’ format

The minimal problem has a goal of two^(256-32)-1. Let’s signify that in ‘compact’ format. First we convert it to base 256:

ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff

That is 28 0xff digits. The primary digit is larger than 0x7f, so we prepend a zero digit:

00 ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff

Now it is 29 digits lengthy. hex(29) = 0x1d. So the ‘compact’ illustration of that is:

1d 00 ff ff

Discover we have misplaced a number of ‘ff’ digits there. We have solely saved 2 bytes of precision, what with the scale byte and the prepended zero byte utilizing up two of the 4 accessible bytes. If we had been to transform again from ‘compact’ format to see what quantity we have truly saved, we get:

ff ff 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

which is the truth is the utmost goal utilized by Bitcoin. That is what an issue of 1 units the block hash goal to be.

How is the worth of the bits subject calculated?

Now that we all know what the bits subject means, we will take a look at how its worth is determined. Within the official shopper, the bits worth is calculated by operate GetNextWorkRequired() in src/foremost.cpp, which does the next:

  • if we’re engaged on a block that is a a number of of 2016 (each 2 weeks)
    • take a look at the timestamps on the final block, and the block 2015 blocks earlier than it
    • calculate the distinction in these two timestamps
    • if the distinction is larger than 8 weeks, set it to eight weeks; this prevents the problem reducing by greater than an element of 4
    • if the distinction is lower than half every week, set it to half every week; this prevents the problem growing by greater than an element of 4
    • multiply the distinction by the present goal (ie. the present bits transformed from ‘compact’ illustration to the goal it represents)
    • divide the consequence by 2 weeks
    • if the result’s higher than the utmost goal (2^(256-32)-1), set it to the utmost goal
    • convert the consequence to ‘compact’ kind, and use that as the brand new bits worth
  • in any other case (we’re engaged on a block that is NOT a a number of of 2016
    • if we’re on testnet and it is later than 15 Feb 2012
      • if it has been greater than 20 minutes because the final block was discovered
        • set bits to its highest potential worth, 0x1d00ffff, which represents an issue of 1; that is the ‘special-min-difficulty rule’
      • in any other case
        • set bits to the identical as within the final non-special-min-difficulty rule block
    • in any other case (we’re not on testnet, or it is earlier than 15 Feb 2012)
      • set bits to the identical as within the final block

LEAVE A REPLY

Please enter your comment!
Please enter your name here