Python

計算 BitcoinGold 塊頭雜湊

  • May 5, 2020

我正在嘗試計算塊雜湊 #632295

{ 
'version': 536870912,  
'prev_block_hash': '000000017590609fd72cadc5fd05732cfa1760bd882213afbb7b788c42e115c8', 
'merkle_root': '20e8e04b62d9cd21dbcee23e445fda60a07bc5785a0a2b0b419227c6a2ec936a',  
'reserved': '00000000000000000000000000000000000000000000000000000000',  
'timestamp': 1588602316,  
'bits': 486818083,  
'nonce': '0085c0dd0000000000000000000000000000000000000000000000000106002e',  
'solution': '441cf79198cc4b0fb4dd5f9fa7be645adc4197ff655b555d4139051c9d7b82419f4824f055a5c993390569681ac67061ce041776751d6d72129c18e446dd31d4cc1e78e5d7b4ba440b1d188caffe6da433f73d9d53cfefc36505da3b9270521650a6930064', 
'height': 632295 
}

這就是我計算比特幣雜湊的程式碼的樣子,但它不適用於 BitcoinGold。

block_array: List = list()
block_array.append(struct.pack('<L', block['version']))
block_array.append(bytes.fromhex(block['prev_block_hash'])[::-1])
block_array.append(bytes.fromhex(block['merkle_root'])[::-1])
block_array.append(struct.pack("<L", block['timestamp']))
block_array.append(struct.pack("<L", block['bits']))
block_array.append(struct.pack("<L", block['nonce']))

header_hex: bytes = bytes.join(b'', block_array)
hash = double_sha256(header_hex)

接下來,我轉向文件: https ://github.com/BTCGPU/BTCGPU/wiki/Technical-Spec

這表明我需要再添加 3 個欄位:

nHeight,nReserved,nSolution

我相信 bitcoingold 的程式碼應該是這樣的:

block_array: List = list()
block_array.append(struct.pack('<L', block['version']))
block_array.append(bytes.fromhex(block['prev_block_hash'])[::-1])
block_array.append(bytes.fromhex(block['merkle_root'])[::-1])
block_array.append(struct.pack("<L", block['height']))  # 632295
block_array.append(struct.pack("<L", block['reversed']))  # 0
block_array.append(struct.pack("<L", block['timestamp']))
block_array.append(struct.pack("<L", block['bits']))
block_array.append(struct.pack("<L", block['nonce'])))
block_array.append(struct.pack("B", block['solution']))

header_hex: bytes = bytes.join(b'', block_array)
hash = double_sha256(header_hex)

但這不起作用,因為nonce 不是 int。

作為執行程式碼的結果,應該得到這個雜湊: 00000003dd91f04483e990a1ecc5176abdfb18f40ae2ca6b0d203127bc150998

如何轉換“nonce”和“solution”?

解決了。

block_array: List = list()
block_array.append(struct.pack('<L', block['version']))
block_array.append(bytes.fromhex(block['prev_block_hash'])[::-1])
block_array.append(bytes.fromhex(block['merkle_root'])[::-1])
block_array.append(struct.pack("<L", block['height']))  # 632295
block_array.append(bytes.fromhex(block['reserved']))  # 0
block_array.append(struct.pack("<L", block['timestamp']))
block_array.append(struct.pack("<L", block['bits']))
block_array.append(bytes.fromhex(block['nonce'])[::-1])
block_array.append(bytes.fromhex(block['solution'])[::-1])

header_hex: bytes = bytes.join(b'', block_array)
hash = double_sha256(header_hex)

樂趣

def double_sha256(bytes_str: bytes):
   result = sha256(sha256(bytes_str).digest()).digest()
   return result[::-1]  # little endian

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