Every 2016 blocks one needs to calculate new bits value. What is the formula to calculate it?

  • Are you asking how the difficulty is calculated, or how bits corresponds to difficulty? Or both? – Chris Moore Feb 15 '12 at 5:19
  • I understand how to convert bits->difficulty, I'm asking how difficulty is adjusted. I'm guessing given timestamp from block n and n+2015, there is a formula for it that also takes into account some specific rounding in order to fir the bits field in a block n+2016. – ThePiachu Feb 15 '12 at 5:51
  • OK. I hope my answer tells you what you wanted to know. It's kind of long, but I wanted to explain the format as well as the details of how 'bits' is calculated. – Chris Moore Feb 15 '12 at 6:20
  • It's interesting that you ask about this just as the rules are about to change. After Feb 15th 2012, testnet gets easy after 20 minutes of fruitless block searching. – Chris Moore Feb 15 '12 at 6:23
up vote 21 down vote accepted

What does the bits field represent?

First of all, we need to understand what the 'bits' field means.

Bits is in 'compact' format. This is kind of like a floating point format, but it represents big integers rather than arbitrary real numbers. The first byte indicates the number of bytes the represented number takes up, and the next one to three bytes give the most significant digits of the number. If the 2nd byte has a value greater than 127 then the number is interpreted as being negative.

To convert a positive integer to 'compact' format, we:

  • convert the integer into base 256.
  • if the first (most significant) digit is greater than 127 (0x7f), prepend a zero digit
  • the first byte of the 'compact' format is the number of digits in the above base 256 representation, including the prepended zero if it's present
  • the following three bytes are the first three digits of the above representation. If less than three digits are present, then one or more of the last bytes of the compact representation will be zero.

Example 1 - Convert 1000 to 'compact' format

For example, to represent 1000 in 'compact' format, we convert to base 256:

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

So we have a 2 digit base 256 number:

03 e8

The first digit is not greater than 0x7f, so we don't prepend a zero digit:

03 e8

Then the compact representation becomes:

02 03 e8 00

Example 2 - Convert max target to 'compact' format

The minimum difficulty has a target of 2^(256-32)-1. Let's represent 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's 28 0xff digits. The first digit is greater 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's 29 digits long. hex(29) = 0x1d. So the 'compact' representation of this is:

1d 00 ff ff

Notice we've lost a lot of 'ff' digits there. We've only kept 2 bytes of precision, what with the size byte and the prepended zero byte using up two of the four available bytes. If we were to convert back from 'compact' format to see what number we've actually stored, 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 in fact the maximum target used by Bitcoin. This is what a difficulty of 1 sets the block hash target to be.

How is the value of the bits field calculated?

Now that we know what the bits field means, we can look at how its value is decided. In the official client, the bits value is calculated by function GetNextWorkRequired() in src/main.cpp, which does the following:

  • if we're working on a block that's a multiple of 2016 (every 2 weeks)
    • look at the timestamps on the last block, and the block 2015 blocks before it
    • calculate the difference in these two timestamps
    • if the difference is greater than 8 weeks, set it to 8 weeks; this prevents the difficulty decreasing by more than a factor of 4
    • if the difference is less than half a week, set it to half a week; this prevents the difficulty increasing by more than a factor of 4
    • multiply the difference by the current target (ie. the current bits converted from 'compact' representation to the target it represents)
    • divide the result by 2 weeks
    • if the result is greater than the maximum target (2^(256-32)-1), set it to the maximum target
    • convert the result to 'compact' form, and use that as the new bits value
  • otherwise (we're working on a block that's NOT a multiple of 2016
    • if we're on testnet and it's later than 15 Feb 2012
      • if it's been more than 20 minutes since the last block was found
        • set bits to its highest possible value, 0x1d00ffff, which represents a difficulty of 1; this is the 'special-min-difficulty rule'
      • otherwise
        • set bits to the same as in the last non-special-min-difficulty rule block
    • otherwise (we're not on testnet, or it's before 15 Feb 2012)
      • set bits to the same as in the last block
  • 1
    2^224 is not what you presented, did you mean 2^224-1? wolframalpha.com/input/?i=2%5E224+in+hex – ThePiachu Feb 15 '12 at 6:42
  • Yes, I did. Thanks! I've fixed my answer accordingly. – Chris Moore Feb 15 '12 at 6:45
  • One thing to add is that the target never changes +- a factor of 4 per change – AccidentallyObtuse Mar 1 '13 at 13:02
  • @makerofthings7 that's covered by the "if the difference is greater than 8 weeks, set it to 8 weeks" and the following line. I expanded those lines to make it clear what they're for. – Chris Moore Mar 1 '13 at 19:58
  • Thank you & I see now, I'm still working on my C++ to English translation skills ;) – AccidentallyObtuse Mar 1 '13 at 20:19

Here is the relevant code from main.cpp:

static const int64 nTargetTimespan = 14 * 24 * 60 * 60; // two weeks
static const int64 nTargetSpacing = 10 * 60;
static const int64 nInterval = nTargetTimespan / nTargetSpacing;

...

// Go back by what we want to be 14 days worth of blocks
const CBlockIndex* pindexFirst = pindexLast;
for (int i = 0; pindexFirst && i < nInterval-1; i++)
    pindexFirst = pindexFirst->pprev;
assert(pindexFirst);

// Limit adjustment step
int64 nActualTimespan = pindexLast->GetBlockTime() - pindexFirst->GetBlockTime();
printf(" nActualTimespan = %"PRI64d" before bounds\n", nActualTimespan);
if (nActualTimespan < nTargetTimespan/4)
    nActualTimespan = nTargetTimespan/4;
if (nActualTimespan > nTargetTimespan*4)
    nActualTimespan = nTargetTimespan*4;

// Retarget
CBigNum bnNew;
bnNew.SetCompact(pindexLast->nBits);
bnNew *= nActualTimespan;
bnNew /= nTargetTimespan;

if (bnNew > bnProofOfWorkLimit)
    bnNew = bnProofOfWorkLimit;

...

return bnNew.GetCompact();

So the important steps are:

  1. Translate the bits of block n+2015 to a BigNum target.
  2. Calculate the timespan between blocks n and n+2015 (as an integer number of seconds).
  3. Multiply the old target by the timespan.
  4. Divide the result by the desired timespan 2 weeks = 1209600 seconds. This is integer arithmetic so it's rounded down. The result is the new target.
  5. Convert the target to bits.

The code for the CBigNum class is at bignum.h.

i use the following python code to convert back and forth between "target" and "bits":

import binascii

def target_int2bits(target):
    # comprehensive explanation here: bitcoin.stackexchange.com/a/2926/2116

    # get in base 256 as a hex string
    target_hex = int2hex(target)

    bits = "00" if (hex2int(target_hex[: 2]) > 127) else ""
    bits += target_hex # append
    bits = hex2bin(bits)
    length = int2bin(len(bits), 1)

    # the bits value could be zero (0x00) so make sure it is at least 3 bytes
    bits += hex2bin("0000")

    # the bits value could be bigger than 3 bytes, so cut it down to size
    bits = bits[: 3]

    return length + bits

def bits2target_int(bits_bytes):
    exp = bin2int(bits_bytes[: 1]) # exponent is the first byte
    mult = bin2int(bits_bytes[1:]) # multiplier is all but the first byte
    return mult * (2 ** (8 * (exp - 3)))

def int2hex(intval):
    hex_str = hex(intval)[2:]
    if hex_str[-1] == "L":
        hex_str = hex_str[: -1]
    if len(hex_str) % 2:
        hex_str = "0" + hex_str
    return hex_str

def hex2int(hex_str):
    return int(hex_str, 16)

def hex2bin(hex_str):
    return binascii.a2b_hex(hex_str)

def int2bin(val, pad_length = False):
    hexval = int2hex(val)
    if pad_length: # specified in bytes
        hexval = hexval.zfill(2 * pad_length)
    return hex2bin(hexval)

def bin2hex(binary):
    # convert raw binary data to a hex string. also accepts ascii chars (0 - 255)
    return binascii.b2a_hex(binary)

def bin2int(binary):
    return hex2int(bin2hex(binary))
  • Where is def bin2int() block in your code? – James Bond May 12 at 17:59
  • @JamesBond I just added it – mulllhausen May 15 at 4:31

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.