开源 Bitcoin P2P电子货币系统背后的技术(五)

未知来源 阅读 28 2014-2-19 02:11
分享至
微信扫一扫,打开网页后点击屏幕右上角分享按钮

挖矿与块(Blocks)

接下来讲的是什么是挖矿,以及交易单在P2P网络上的存放,其实这个过程也是挖矿的过程。

所有的交易单是以Block的形式在Bitcoin P2P网络上存放的。每一个Block包含了最近所有的有效交易单。Block中最为重要数据为:

最近交易单集合

Nonce随机数

前一个块的散列值

在造币的Node总在侦听网络,接收信息,然后各自将接收到的新交易打包到一个新block(CBlock Class in main.h)。在块交易单集合中的第一个交易单总是“造币单”:给创建该块的人以新的货币,节点只需要校验货币金额是否合法即可:形成一个合法的新块得到的回报是初始金额为50BTC,每产生210,000块的时候这个金额减半(大约每隔4年会发生一次)。产生合法的新块是不容易的,需要通过无数次的计算尝试得到,另外如果大家都在计算同一个新块,那么还存在竞争关系,这个时候只有最困难(Difficulty)的那个块(指整个块链的困难度值加起来最大的)才会被网络认可。另外如果块中的某笔交易size大于该块中交易单的平均size,那么该笔交易单会被收取一笔小的交易费用。

块的数据结构

块的数据结构可以分为块头(block header)和块体(block body)

块头(Block Header)

块头的数据是用于校验块、产生块ID(256位散列值),这个块ID也是决定块是否合法的数字。计算块ID的公式如下:

BlockID = SHA256(SHA256(Block_Header))

块头(Block Header)的内容如下:

ver: 版本号(4字节)

prev_block: 前一个块的256位散列值

mrkl_root: 所有交易单的256位散列

time: 时间戳(4字节)

Bits: 一个紧凑格式的4字节的数字,它表示一个256位的当前目标(Target)数值,这也表示困难度(Difficulty),最后产生块的ID值必须不大于目标值才能被接受(如果有多人同时产生同一个块,那么最困难的将被接受)。这个数值会每隔2016个block(网络大约每小时创建6个块,创建2016块大约2周)调整一次,当产生前一个2016块的时间大于2周的时候,难度值将会被调低,当小于两周的时候难度值将被调高(关于Target和Difficulty详见后述)。

nonce: 32位随机数。节点在产生Block的时候,会增量尝试改变nonce的值,直到产生的块的散列值符合bits的要求

计算目标(Target)和困难度(Difficulty)

计算目标(Target)

计算目标(Target)是一个256位的数值,块的散列值ID必须小于或等于该目标数值,放可被接受。块头中的BITS是一个用紧凑的4字节格式表示的目标(Target)数值,它表示一个256位的当前目标数值,方式如下:

//假设BITS为:

BITS = 0x1B0406CC

//那么target的数值则是:

Target = 0x0406CC * 2^(8*(0x1B-3))= 0x00000000000406CC000000000000000000000000000000000000000000000000

当前网络的目标值为:Current target。

困难度(Difficulty)

困难度(Difficulty)表示该块ID的计算难度,值越大表示越困难,1表示最容易。

1困难度(Difficulty)的Target被定义为:

0x1D00FFFF(BITS)

展开后:

0x00FFFF * 2^(8*(0x1D - 3)) = 0x00000000FFFF0000000000000000000000000000000000000000000000000000

因此困难度的计算公式为:1 困难度(Target) / 当前目标

我们可以在这里查看当前网络的困难度:Current difficulty(BitCoin的 getDifficulty 接口输出)。也可以通过这个网站查看困难度值的变化情况图:Graphs。

块体(Block Body)

Block Body 实际上就是交易单的集合。

你可以在这个网站上看到当前网络所有的交易单(Blocks): https://blockexplorer.com/

一个具体的块的示例:

{

//hash: 自身ID的sha256散列值

"hash":"0000000000001470ed62a9d73c1c74a4b3754e18db0abb057be5ad5ffed659bb",

"ver":1,

"prev_block":"000000000000158a4051185f339d59227b776ac6bfe6120fa18293b5c08644a8",

"mrkl_root":"c5341062635c427b79a730dca8d4a5eb6a482fa21f82cc37c84ae01ea5945fc3",

"time":1307565484,

"bits":438145839,

"nonce":2323396371,

//交易单数量

"n_tx":6,

"size":2193,

"tx":[

{

"hash":"67dbdbc6c4a49bc3364d34aebe76b6f470ced66eb731f9d099d0da6c58e466cf",

"ver":1,

"vin_sz":1,

"vout_sz":1,

"lock_time":0,

"size":135,

"in":[

{

"prev_out":{

"hash":"0000000000000000000000000000000000000000000000000000000000000000",

"n":4294967295

},

"coinbase":"042f931d1a022301"

}

],

"out":[

{

"value":"50.04000000",

"scriptPubKey":"049db428c19890bb7706bebdcf69a93c192daef8a73eb7035e2600468b4e1f83d4c2134640f6c619e5521c2050ebc9be9e9c502fdab655a570236418e5c2e95137 OP_CHECKSIG"

}

]

},

{

"hash":"c96eb8f874d105e24121cdc5918440c85af27b797013679796fcc26e40938ad2",

"ver":1,

"vin_sz":1,

"vout_sz":2,

"lock_time":0,

"size":258,

"in":[

{

"prev_out":{

"hash":"c8caedb7edb2665b8543916a1e6b1dee2bf1733f75b4604c423db08efa570d65",

"n":0

},

"scriptSig":"3045022100b47c10ebd2413c575510563363fe9cad7f0fdcbefca9dd219bd345bcfeff98a1022049d7037c0be3493650cb2a410a655394678e75a28b8f4cd587644813ccee383801

041f5853d9d43b81af0999a4add012e6580db5edc7dabf99bc2aff1a01b1296e7004b73b10a9e2d3ebe0d7377c545aacc56e24aff1c9b130e0721332315e414f72"

}

],

"out":[

{

"value":"3.98000000",

"scriptPubKey":"OP_DUP OP_HASH160 22718315d786b8f64510be88ad590760de676a59 OP_EQUALVERIFY OP_CHECKSIG"

},

{

"value":"0.01000000",

"scriptPubKey":"OP_DUP OP_HASH160 d52804c9bc12e7a7186e53b41784dcd2afed9a92 OP_EQUALVERIFY OP_CHECKSIG"

}

]

},

{

"hash":"cd74421dd3d32346bdbef02cde4bc0ed19b5cc15621f761b465a869b2f51287c",

"ver":1,

"vin_sz":2,

"vout_sz":2,

"lock_time":0,

"size":437,

"in":[

{

"prev_out":{

"hash":"e38b71a89071be2820ce95841da03ba8bac77d536d9ac5fe5347136e9bbca104",

"n":1

},

"scriptSig":"3045022100ed5c3626adf84a6d364d7b58fb182ee3bd35758a883ebe2e642e83f4103e91cd022063f61a1d89c1a30f713c2ca277c08c1c4ceb585babef82301f738fdc2312329d01

047b4277bb4c23988986c44552156130d4155a1020ec411ca4a07904191d9054de705ec2b79a46155a0d59e8a39b5d450ac73d485b082789b6dad55d5e2230f597"

},

{

"prev_out":{

"hash":"a6fc46a5a4a6b202c644cc4854feb2d8cf7befcf695ad810346c8dfb4cc18138",

"n":1

},

"scriptSig":"304402207de219304bfca0e2d93524a2547767ce5044501bb0d9107e04d35cf3d644136d02201244a1ce47c2f3aab6c205e75e433371efb01dad5073d316bf68488ea1ad064301

04d74d74c0f2bd415a94090c83bbe10b4949c7ee8f3bb957352d02fb50f3e1ddc5ab79a28b1228eaf2986339a3735e32869019d4894f34a46645288a68900ef6a5"

}

],

"out":[

{

"value":"0.01000000",

"scriptPubKey":"OP_DUP OP_HASH160 e95281e139fc37bb2aabd84028835c6513a6ac99 OP_EQUALVERIFY OP_CHECKSIG"

},

{

"value":"2.01000000",

"scriptPubKey":"OP_DUP OP_HASH160 234c6c8644008de240d90753904789868100786c OP_EQUALVERIFY OP_CHECKSIG"

}

]

},

{

"hash":"9d4f130995dbc1ddc5346c109992c0779aa44122195eff51dc00109afc5cad7d",

"ver":1,

"vin_sz":1,

"vout_sz":2,

"lock_time":0,

"size":258,

"in":[

{

"prev_out":{

"hash":"d84cd74d4b470f96379fa9737c43be544e4e57f33e83fc8bf26f232ec5ea97b5",

"n":0

},

"scriptSig":"3045022100d8f3bbb19b4d2593649b4f6ac0074a772ff78fce1fb60e7957c7d99d2627931802205ad4774f58af0b52e37f87592bea7bae84c883c9230e722962f2e52e7eb061e201

04732060c3f57ccc0703e05f86f442e45617b446ebca7e9f6c96ca1ec754b9d8a9f2bd727a15c7821cbca0498c2b7f60081873a450f29c5937e9593407166dc71b"

}

],

"out":[

{

"value":"30.26538895",

"scriptPubKey":"OP_DUP OP_HASH160 7fa1d78819360231c6bc1b23ff644e9124da075f OP_EQUALVERIFY OP_CHECKSIG"

},

{

"value":"4.76000000",

"scriptPubKey":"OP_DUP OP_HASH160 847f4729a1464d7a2103796cca32022fe0481d58 OP_EQUALVERIFY OP_CHECKSIG"

}

]

},

{

"hash":"68050dc3810310e1e5e9eb3464b1581a353de523a6f26d8b978370bd63880eb7",

"ver":1,

"vin_sz":4,

"vout_sz":1,

"lock_time":0,

"size":766,

"in":[

{

"prev_out":{

"hash":"07a39559553efb329af1538e0fc8a037f20048a83b2278a5041975054e425c3e",

"n":17

},

"scriptSig":"3046022100c3ce1ee6c222d74ff9c97d5729e5c6702c3cabdb61ea0fb3ae8632eae36f03fb0221009cea5a9f94fe14e4673fd87f75750e1d151f6e846c371d8e4af72b5d213bb5c001

04ee9e80055248ed804ca6724b2d784c91eec04abdc4ec00b14dc01e56c90513a85453026acf90f1cb1bdd3e3ca07035be40d0b3e3ac6076b5b12bd3234ac8c20b"

},

{

"prev_out":{

"hash":"07f95bfdd09e98bf7ce0543e8fdd82f9e8532449ab7167f69aa3d1f030399b96",

"n":0

},

"scriptSig":"3044022011f922460d01986255a6f5e5ce88586bb3a9a32edc836e420d2347dae9df12e1022018d9d4e48dea2b821e01d3253dfaa03af801d01e79ab4eb3ef48f22f84f196aa01

04398f321ea6140c6ad51fea724f7c13e1df6c43afb8e4f6f9ab2385b6e18d8eae52516a0b3487512a3dbeb36109d3fcb6391fbc156518c96f57467f97dc55d0a3"

},

{

"prev_out":{

"hash":"6c93900990b712fbf2a386ba53bc3aad8f5bcdda73b855a24628d7eae80cf3f9",

"n":0

},

"scriptSig":"3046022100b688feeb1012f913b6366b5b9bd5313238d00b61b48a0e3956c33d1dffc9a1f2022100ed159808e8a732e646ba5af7ed5a0d896c7db0a10757aeccc9c58a67ec42ec7501

047e79adbe50ae668041d2b08702c12de83fdaecb866b261755cbba27fe344c6fc67f3fc2593edc0bb053b0fde62c4d42d152b6525c10bde365d80a8d2d8d77a85"

},

{

"prev_out":{

"hash":"ed416acb1373bcce28ec63e6e600e0da7f8892b557591bc0386210c56e8c034f",

"n":1

},

"scriptSig":"3046022100c1ffdf383d108a9b1e8e33ed3ef8bd6b368b1ed0584ac187eb0897c90422fc5b022100f7bfb8f0b17c3b121943622a4d04a8a818391c68028b6a63d9cd2536d9f3714501

04ee9e80055248ed804ca6724b2d784c91eec04abdc4ec00b14dc01e56c90513a85453026acf90f1cb1bdd3e3ca07035be40d0b3e3ac6076b5b12bd3234ac8c20b"

}

],

"out":[

{

"value":"1.69000000",

"scriptPubKey":"OP_DUP OP_HASH160 71b9ea593ba56d1159493cb2b37a03f368fb6c40 OP_EQUALVERIFY OP_CHECKSIG"

}

]

},

{

"hash":"4e6e47f0fa7a559219be309db20c5c17296956304cf47dc8c52910c24b505a92",

"ver":1,

"vin_sz":1,

"vout_sz":2,

"lock_time":0,

"size":258,

"in":[

{

"prev_out":{

"hash":"897f67e6ec0b5a64b511c2052d05f0e2437811260041bb0915f2c40879025215",

"n":1

},

"scriptSig":"304502200dbe53f77f947b12dfabb737e0862c1f2d37515f6293b06c08710ccdb69419d6022100b379ab597d8d17034f1654f20e68c72930050342884743869b6421ee7eaad83801

04bdd996cd29ca3d34604306de8983d1fadf978ae43c12c1e832ce803ae57241cd7762a40dc1e5f6176ca3b0caac622c82e271b59c66fb1f8fa8225930a8807edf"

}

],

"out":[

{

"value":"0.04000000",

"scriptPubKey":"OP_DUP OP_HASH160 50e44bb02555c7084e08cda95eb43a9c621fa0ac OP_EQUALVERIFY OP_CHECKSIG"

},

{

"value":"0.03000000",

"scriptPubKey":"OP_DUP OP_HASH160 ecd8f97222481950eed4f98db82455635ccb86b4 OP_EQUALVERIFY OP_CHECKSIG"

}

]

}

],

"mrkl_tree":[

"67dbdbc6c4a49bc3364d34aebe76b6f470ced66eb731f9d099d0da6c58e466cf",

"c96eb8f874d105e24121cdc5918440c85af27b797013679796fcc26e40938ad2",

"cd74421dd3d32346bdbef02cde4bc0ed19b5cc15621f761b465a869b2f51287c",

"9d4f130995dbc1ddc5346c109992c0779aa44122195eff51dc00109afc5cad7d",

"68050dc3810310e1e5e9eb3464b1581a353de523a6f26d8b978370bd63880eb7",

"4e6e47f0fa7a559219be309db20c5c17296956304cf47dc8c52910c24b505a92",

"f4462bd30c529d154dc1b35d8f68961441e09ffaceaa25da17eb1814101f8ad5",

"1961263895b63d4760f81eda7ae94848eb68e2170b79a224ba704a3c21fd6feb",

"0427a2dbd54fa6ec034d704336fa255f6c139e6d7b9bf803714cfbc9dcc61b85",

"42441420d09c91336e69bae9f7eb39b1c6d90562028de3094bea5f675a25a082",

"4af79ccb4bf857df3e75a50106b15ef35aa73a209efa5089f2f2193ff70ab93d",

"c5341062635c427b79a730dca8d4a5eb6a482fa21f82cc37c84ae01ea5945fc3"

]

}

转自http://www.showmuch.com/a/20110618/123447.html

作者:李雪愚

btcfans公众号

微信扫描关注公众号,及时掌握新动向

来自互联网
免责声明:
2.本文版权归属原作所有,仅代表作者本人观点,不代表比特范的观点或立场
2.本文版权归属原作所有,仅代表作者本人观点,不代表比特范的观点或立场
上一篇:开源 Bitcoin P2P电子货币系统背后的技术(四) 下一篇:开源Bitcoin P2P电子货币系统内幕(完)—— 问题与展望

相关资讯