4

I'm trying to calculate the new target for Block 32256, but I'm not getting the expected result. Here's what I did:

  1. Timestamp of the previous block (32255) = 1262152739
  2. Timestamp of block (32255 - 2015 = 30240) = 1261130161
  3. Diff: 1262152739 - 1261130161 = 1022578
  4. Note that there is no limit adjustment (1/2 weeks < 1022578 < 8 weeks)
  5. Current target (0x1d00ffff) = 26959535291011309493156476344723991336010898738574164086137773096960
  6. Multiply the difference with current target
    = 1022578 * 26959535291011309493156476344723991336010898738574164086137773096960
    = 27568227678811762838892963267635169612395352810293691562874591737943162880
  7. Divide by two weeks
    27568227678811762838892963267635169612395352810293691562874591737943162880 / 1209600
    = 22791193517536179595645637622052884930882401463536451358196587084939

From what I understand, this should be the new target for block 32256 and the following 2015 blocks. However, it isn't - the correct new target as given in blockexplorer is:

22791060871177364286867400663010583169263383106957897897309909286912

As you can see, my result was kind of in the ballpark, but not correct. What am I missing here?

Related questions:

kvikshaug
  • 43
  • 3

2 Answers2

3

The actual target (and thus difficulty) is determined by its compact 32-bit encoding.

After the computation you did above, you should round it to the nearest compactly-representable target (24-bit accuracy, multiple of 256). That is the target encoded inside blocks, and the one that matters.

Pieter Wuille
  • 105,497
  • 9
  • 194
  • 308
0

as pieter says, you need to convert the target into its "bits" value, which is a 4 byte compressed value for the target. the following python code converts from your original value in (7) into the value given in blockexplorer.com:

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

>>> bits_bytes = target_int2bits(22791193517536179595645637622052884930882401463536451358196587084939)
>>> bin2hex(bits_bytes)
'1d00d86a'
>>> # this ^^ is the value in blockexplorer.com in brackets.
>>> # display the "bits" as an integer:
>>> bits2target_int(bits_bytes)
22791060871177364286867400663010583169263383106957897897309909286912L
>>> # this ^^ is the value at the end of your answer.
mulllhausen
  • 1,713
  • 2
  • 15
  • 33