区块链知识

挖矿难度表示

2017-12-09  本文已影响279人  古则

比特币的挖矿难度可以使用Target Threshold,nBits或Difficulty表示,它们互相等价:

这三个值的转化关系可以采用下面的实例来说明:

首先获取哈希值为000000000003ba27aa200b1cecaad478d2b00432346c3f1f3986da1afd33e506的区块原生十六进制信息如下:

0100000050120119172a610421a6c3011dd330d9df07b63616c2cc1f1cd00200000000006657a9252aacd5c0b2940996ecff952228c3067cc38d4885efb5a4ac4247e9f337221b4d4c86041b0f2b57100401000000010000000000000000000000000000000000000000000000000000000000000000ffffffff08044c86041b020602ffffffff0100f2052a010000004341041b0e8c2567c12536aa13357b79a073dc4444acb83c4ec7a0e2f99dd7457516c5817242da796924ca4e99947d087fedf9ce467cb9f7c6287078f801df276fdf84ac000000000100000001032e38e9c0a84c6046d687d10556dcacc41d275ec55fc00779ac88fdf357a187000000008c493046022100c352d3dd993a981beba4a63ad15c209275ca9470abfcd57da93b58e4eb5dce82022100840792bc1f456062819f15d33ee7055cf7b5ee1af1ebcc6028d9cdb1c3af7748014104f46db5e9d61a9dc27b8d64ad23e7383a4e6ca164593c2527c038c0857eb67ee8e825dca65046b82c9331586c82e0fd1f633f25f87c161bc6f8a630121df2b3d3ffffffff0200e32321000000001976a914c398efa9c392ba6013c5e04ee729755ef7f58b3288ac000fe208010000001976a914948c765a6914d43f2a7ac177da2c2f6b52de3d7c88ac000000000100000001c33ebff2a709f13d9f9a7569ab16a32786af7d7e2de09265e41c61d078294ecf010000008a4730440220032d30df5ee6f57fa46cddb5eb8d0d9fe8de6b342d27942ae90a3231e0ba333e02203deee8060fdc70230a7f5b4ad7d7bc3e628cbe219a886b84269eaeb81e26b4fe014104ae31c31bf91278d99b8377a35bbce5b27d9fff15456839e919453fc7b3f721f0ba403ff96c9deeb680e5fd341c0fc3a7b90da4631ee39560639db462e9cb850fffffffff0240420f00000000001976a914b0dcbf97eabf4404e31d952477ce822dadbe7e1088acc060d211000000001976a9146b1281eec25ab4e1e0793ff4e08ab1abb3409cd988ac0000000001000000010b6072b386d4a773235237f64c1126ac3b240c84b917a3909ba1c43ded5f51f4000000008c493046022100bb1ad26df930a51cce110cf44f7a48c3c561fd977500b1ae5d6b6fd13d0b3f4a022100c5b42951acedff14abba2736fd574bdb465f3e6f8da12e2c5303954aca7f78f3014104a7135bfe824c97ecc01ec7d7e336185c81e2aa2c41ab175407c09484ce9694b44953fcb751206564a9c24dd094d42fdbfdd5aad3e063ce6af4cfaaea4ea14fbbffffffff0140420f00000000001976a91439aa3d569e06a1d7926dc4be1193c99bf2eb9ee088ac00000000

区块中nBits采用小端格式表示,解析区块信息,得到nBits字段值为0x4c86041b。因此转化为大端格式为0x1B04864C,这个值是Target Threshold的压缩格式表示,可以将它转化成256位的Target Threshold值:

开头的一个字节为指数,后面三个字节为系数,则:

Target Threshold = 0x04864C * 256 ^ (0x1B - 3) = 0x000000000004864c000000000000000000000000000000000000000000000000

计算出Target Threshold值为0x000000000004864c000000000000000000000000000000000000000000000000

再计算Difficulty的值,它有两个值,计算公式分别为:

bdiff = 0x00000000FFFF0000000000000000000000000000000000000000000000000000 / Target Threshold
pdiff = 0x00000000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF / Target Threshold

由此可以使用Python计算出bdiff的值:

>>> a =  0x00000000FFFF0000000000000000000000000000000000000000000000000000
>>> b =  0x000000000004864c000000000000000000000000000000000000000000000000
>>> print(a/b)
14484.162361225399

因此,得到在比特币客户端中的difficulty值bdiff为14484.162361225399。

为了检验上述结果,可以在比特币核心客户端中使用getblock "000000000003ba27aa200b1cecaad478d2b00432346c3f1f3986da1afd33e506"命令得到该区块的json格式信息:

{
  "hash": "000000000003ba27aa200b1cecaad478d2b00432346c3f1f3986da1afd33e506",
  "confirmations": 360175,
  "strippedsize": 957,
  "size": 957,
  "weight": 3828,
  "height": 100000,
  "version": 1,
  "versionHex": "00000001",
  "merkleroot": "f3e94742aca4b5ef85488dc37c06c3282295ffec960994b2c0d5ac2a25a95766",
  "tx": [
    "8c14f0db3df150123e6f3dbbf30f8b955a8249b62ac1d1ff16284aefa3d06d87", 
    "fff2525b8931402dd09222c50775608f75787bd2b87e56995a7bdd30f79702c4", 
    "6359f0868171b1d194cbee1af2f16ea598ae8fad666d9b012c8ed2b79a236ec4", 
    "e9a66845e05d5abc0ad04ec80f774a7e585c6e8db975962d069a522137b80c1d"
  ],
  "time": 1293623863,
  "mediantime": 1293622620,
  "nonce": 274148111,
  "bits": "1b04864c",
  "difficulty": 14484.1623612254,
  "chainwork": "0000000000000000000000000000000000000000000000000644cb7f5234089e",
  "previousblockhash": "000000000002d01c1fccc21636b607dfd930d31d01c3a62104612a1719011250",
  "nextblockhash": "00000000000080b66c911bd5ba14a74260057311eaeb1982802f7010f1a9f090"
}

最终,可以发现该区块的bits和difficulty字段信息与上面分析计算的相关结果一致。

nBits的大端格式表示法中,其系数最大为0x7fffff,这是因为Target Threshold数据类型是无符号整型,而它继承自有符号数据类,则在实际中Target Threshold系数的最高位有可能是1,这可能会被解析成一个负数。则在挖矿过程中难度值永远无法小于一个负数。因此,为了解决这个问题,比特币核心在生成nBits值时需要首先检查一下生成的nBits是否会被解析为一个负数。如果是,首先在系数开头补8位0,即除以256,然后指数再加上1。这样由nBits转化为Target Threshold过程中转化公式就与普通值相同了,即指数位都是减去3,转化过程上面已经提到。

举个例子说明:

哈希值为00000000d1145790a8694403d4063f323d499e655c83426834d4ce2f8dd4a2ee的区块信息如下:

{
  "hash": "00000000d1145790a8694403d4063f323d499e655c83426834d4ce2f8dd4a2ee",
  "confirmations": 459998,
  "strippedsize": 490,
  "size": 490,
  "weight": 1960,
  "height": 170,
  "version": 1,
  "versionHex": "00000001",
  "merkleroot": "7dac2c5666815c17a3b36427de37bb9d2e2c5ccec3f8633eb91a4205cb4c10ff",
  "tx": [
    "b1fea52486ce0c62bb442b530a3f0132b826c74e473d1f2c220bfa78111c5082", 
    "f4184fc596403b9d638783cf57adfe4c75c605f6356fbc91338530e9831e9e16"
  ],
  "time": 1231731025,
  "mediantime": 1231716245,
  "nonce": 1889418792,
  "bits": "1d00ffff",
  "difficulty": 1,
  "chainwork": "000000000000000000000000000000000000000000000000000000ab00ab00ab",
  "previousblockhash": "000000002a22cfee1f2c846adbd12b3e183d4f97683f85dad08a79780a84bd55",
  "nextblockhash": "00000000c9ec538cab7f38ef9c67a95742f56ab07b0a37c5be6b02808dbfb4e0"
}

发现bdiff值为1,则利用bdiff与Target Threshold关系可以计算出:

Target Threshold = 0x00000000FFFF0000000000000000000000000000000000000000000000000000 / bdiff = 0x00000000FFFF0000000000000000000000000000000000000000000000000000

将Target Threshold值0x00000000FFFF0000000000000000000000000000000000000000000000000000转化为nBits的过程中可以发现其系数为0xffff00,指数为0x1c,这样:

0x00000000FFFF0000000000000000000000000000000000000000000000000000 = 0xffff00 * 256 ^ (0x1c -3)

然而由于系数最高位为1,则如果这样表示的话就可能将Target Threshold解析为负数。因此,我们将系数除以256,指数加上1,得到系数为0x00ffff,指数为0x1d。这样:

0x00000000FFFF0000000000000000000000000000000000000000000000000000 = 0x00ffff * 256 ^ (0x1d -3)

最终,nBits值为0x1d00ffff(大端表示),与json格式信息一致。

上一篇下一篇

猜你喜欢

热点阅读