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

link|improve this question

72% accept rate
Are you asking how the difficulty is calculated, or how bits corresponds to difficulty? Or both? – Chris Moore Feb 15 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 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 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 at 6:23
feedback

2 Answers

up vote 3 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
    • if the difference is less than half a week, set it to half a week
    • 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
link|improve this answer
2^224 is not what you presented, did you mean 2^224-1? wolframalpha.com/input/?i=2%5E224+in+hex – ThePiachu Feb 15 at 6:42
Yes, I did. Thanks! I've fixed my answer accordingly. – Chris Moore Feb 15 at 6:45
feedback

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.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

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