Mining-Pools

挖礦區塊頭位反轉

  • January 25, 2019

在嘗試了解探勘過程並在 python 中檢查時……我很困惑來自/到池的哪些資訊在 Little Endian 中,哪些需要在建構塊頭 + 散列時反轉,哪些不需要。??

例如……讓我們從連接到solo.ckpool.org 對mining.authorize 的響應開始…… job_id,prevhash,coinb1,coinb2,merkle_branch,version,nbits,ntime,clean_jobs =

{'id': None, 'method': 'mining.notify', 'params': ['59bc8dfc0000029f',
       '0eba48f47bc0ab4bb35b230849868bf1d79aeb19006eed460000000000000000',
       '01000000010000000000000000000000000000000000000000000000000000000000000000ffffffff35039a6807000404d5bc5904fc873d140c',
       '0a636b706f6f6c112f736f6c6f2e636b706f6f6c2e6f72672fffffffff03c4626c4e000000001976a914fc6123f4bfd3a840b4387ab90e9801e98fb17cf888acb1caca00000000001976a914f4cbe6c6bb3a8535c963169c22963d3a20e7686988ac0000000000000000266a24aa21a9edab3b87f3963e55a23f3cea5e4435bf6637328fd5e329bb8fdac7d31c3a01d26200000000',
       ['c251d18568d2cdb07c376d5f3304a2161e3d09d289d1a7e53737199171d73ae6',
        '4f9bcac3ec00fc8238db04b7758a1b673bdf38b93e48159678b60b51517dd563',
        '2aadad2c9c8e6df5c71264524b090cdf89830f12949527f7872c243bc1dc552c',
        '8c5edf39c0d85c9853308eef3ce6f9b71c8d6584ca4120d411bc75987bb2f254',
        '48751c2ae0ddc05f5d504a3d6039e7d5afb3d5f2a6aaac93bd5822e5f8efc328',
        '3e9b5c6b986c2ddc09bf8604eb2bd2a3b49b46f11042c282331a206dadb4296e',
        'c65f502b11b325abc0fac847e8fc5cd300169160e7244b29dd5acc3d0e4b8605',
        'afe7d2287020c4291f609a772adbeafc952a8de0ced38a7346acb6c7103bec6d',
        '0caa545ae63de39d47baea100ea15a18061235d7e1a372fbca88edf9ca5ac797',
        'c5bfff15b4dd64e7faac98f1bba118fb11990e4ff146cb45c7f4af6b93e2ea03',
        'a8805201e8152c1f5835c9aa024752fb34aeb7fa55dd3af0ae6109918f328dbb'],
       '20000000',
       '1801310b',
       '59bcd504',
       True]}

基於池響應….我可以通過

coinbase = coinb1 + extranonce1 + extranonce2 + coinb2 
coinbase_hash_bin = hashlib.sha256(hashlib.sha256(binascii.unhexlify(coinbase)).digest()).digest()

同樣包含在 merkle 樹中,通過散列獲得 merkle_root。我不確定我是否需要在這裡反轉位?

但是要建立一個塊頭……我應該這樣做……

header = version + prevhash + merkle_root + ntime + nbits + nonce

或這個..

header = binascii.hexlify(binascii.unhexlify(version)[::-1]).decode('utf-8') + \
prevhash + \
merkle_root + \
binascii.hexlify(binascii.unhexlify(ntime)[::-1]).decode('utf-8') + \
binascii.hexlify(binascii.unhexlify(nbits)[::-1]).decode('utf-8') + \
binascii.hexlify(binascii.unhexlify(nonce)[::-1]).decode('utf-8')

區塊頭:000000200EBA48F47BC0AB4BB35B230849868BF1D79AEB19006EED4600000000000000B22FA1225C2592C27DF5331D40D9B33FFCE99B26C0AEAD8EF404D5BC590B3101181

有人可以發布一個最近的塊頭的例子……我認為我做錯了什麼!

我正在比較這個雜湊

hash = hashlib.sha256(hashlib.sha256(binascii.unhexlify(header)).digest()).digest()[::-1]

目標為“000000000001310b000000000000000000000000000000000000000000000000”

讓我們看看這個塊,因為它只有一個交易(coinbase):000000000000000000eb2d0ed97a7b2cff7f1408417dca83908004beb6fd9b95

讓我們獲取原始的十六進制數據:

0000002053f7ebc735f54ac8d4ebcc1eeb3d3bdea89603bdd27431000000000000000000ff964ec70ea5a5356a04fdb044ca6c59ffda2dd6be02c63d9ea58fb6d46e01991a22bb590b310118f6df26f801010000000001010000000000000000000000000000000000000000000000000000000000000000ffffffff3103b46707244d696e656420627920416e74506f6f6c6b2f4542312f4144362f4e59412f332059bb221ab8080000615c0300ffffffff02807c814a000000001976a914660371326d3a2e064c278b20107a65dad847e8a988ac0000000000000000266a24aa21a9ede2f61c3f71d1defd3fa999dfa36953755c690689799962b48bebd836974e8cf90120000000000000000000000000000000000000000000000000000000000000000000000000

區塊頭有 6 個組成部分:

  1. 首先,我們有版本(4 個字節)00000020,它是 的反轉字節0x20000000
  2. 然後我們有前一個塊散列(32 字節),也以相反的字節順序排列,53f7ebc735f54ac8d4ebcc1eeb3d3bdea89603bdd27431000000000000000000也就是這個塊
  3. 接下來我們有交易的 merkle 根(32 個字節),以相反的字節順序,ff964ec70ea5a5356a04fdb044ca6c59ffda2dd6be02c63d9ea58fb6d46e0199. 因為只有一筆交易,即 coinbase 交易,所以默克爾根正好等於該交易的 txid。
  4. 然後我們有時間戳(4 個字節),以相反的字節順序,1a22bb59它是0x59bb221a十六進製或1505436186十進制。
  5. 然後我們有’bits’欄位的反轉字節(4個字節),0b3101180x1801310b
  6. 然後是隨機數(4 個字節),f6df26f80xf826dff6

然後在標頭之後,我們有一個字節01,這意味著有一個交易,緊隨其後。如您在此處所見,該一筆交易只是編碼的 coinbase 交易。

當然,只需要對區塊頭進行雜湊處理,交易也不需要。

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