16
9
Every 2016 blocks one needs to calculate new bits value. What is the formula to calculate it?
16
9
Every 2016 blocks one needs to calculate new bits value. What is the formula to calculate it?
21
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:
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
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.
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:
bits converted from 'compact' representation to the target it represents)2^(256-32)-1), set it to the maximum targetbits valuebits to its highest possible value, 0x1d00ffff, which represents a difficulty of 1; this is the 'special-min-difficulty rule'bits to the same as in the last non-special-min-difficulty rule blockbits to the same as in the last blockOne thing to add is that the target never changes +- a factor of 4 per change – goodguys_activate – 2013-03-01T13:02:22.763
@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 – 2013-03-01T19:58:26.667
Thank you & I see now, I'm still working on my C++ to English translation skills ;) – goodguys_activate – 2013-03-01T20:19:43.827
1 2^224 is not what you presented, did you mean 2^224-1? http://www.wolframalpha.com/input/?i=2%5E224+in+hex
Yes, I did. Thanks! I've fixed my answer accordingly. – Chris Moore – 2012-02-15T06:45:39.573
4
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:
The code for the CBigNum class is at bignum.h.
2
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 – 2018-05-12T17:59:28.827
@JamesBond I just added it – mulllhausen – 2018-05-15T04:31:34.220
Are you asking how the difficulty is calculated, or how bits corresponds to difficulty? Or both? – Chris Moore – 2012-02-15T05:19:14.873
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 – 2012-02-15T05:51:13.710
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 – 2012-02-15T06:20:57.937
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 – 2012-02-15T06:23:23.470