Protocol

區塊 32256 的新目標是如何計算的?

  • July 6, 2017

我正在嘗試計算 Block 32256 的新目標,但沒有得到預期的結果。這是我所做的:

  1. 前一個區塊的時間戳(32255) =1262152739
  2. 區塊時間戳(32255 - 2015 = 30240) =1261130161
  3. 差異:1262152739 - 1261130161 = 1022578
  4. 請注意,沒有限制調整(1/2 週 < 1022578< 8 週)
  5. 目前目標 (0x1d00ffff) =26959535291011309493156476344723991336010898738574164086137773096960
  6. 將差值乘以目前目標

= 1022578 * 26959535291011309493156476344723991336010898738574164086137773096960

=27568227678811762838892963267635169612395352810293691562874591737943162880 7. 除以兩週

27568227678811762838892963267635169612395352810293691562874591737943162880 / 1209600

=22791193517536179595645637622052884930882401463536451358196587084939

據我了解,這應該是塊 32256 和以下 2015 塊的新目標。然而,事實並非如此——blockexplorer 中給出的正確新目標是:

22791060871177364286867400663010583169263383106957897897309909286912

正如你所看到的,我的結果有點像,但不正確。我在這裡想念什麼?

相關問題:

實際目標(以及難度)由其緊湊的 32 位編碼決定。

完成上述計算後,應將其四捨五入到最接近的可緊湊表示的目標(24 位精度,256 的倍數)。那是在塊內編碼的目標,也是最重要的。

正如 pieter 所說,您需要將目標轉換為其“位”值,這是目標的 4 字節壓縮值。以下 python 程式碼將 (7) 中的原始值轉換為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]) &gt; 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))

>&gt;&gt; bits_bytes = target_int2bits(22791193517536179595645637622052884930882401463536451358196587084939)
>&gt;&gt; bin2hex(bits_bytes)
'1d00d86a'
>&gt;&gt; # this ^^ is the value in blockexplorer.com in brackets.
>&gt;&gt; # display the "bits" as an integer:
>&gt;&gt; bits2target_int(bits_bytes)
22791060871177364286867400663010583169263383106957897897309909286912L
>&gt;&gt; # this ^^ is the value at the end of your answer.

引用自:https://bitcoin.stackexchange.com/questions/22581