本文的目的是深入解析bitshares区块链的每一个区块具体数据结构是什么样的,每个区块下面包括一个或多个transaction,这些transaction的具体数据结构又是什么样的,当对区块结构及transaction结构有了清晰认识后,就可以从整体上认识链的操作原理。
【区块结构】
<p dir="auto">块结构 变量定义(类型&变量名) 类型
<p dir="auto"><span><img src="https://images.hive.blog/768x0/http://imglf5.nosdn.127.net/img/dUhZdTNwa3RzelVTUlFxS3lvQ3l3K2NzU200NDNXZWR0OUhzalQrcWh1aEt0U0xwUHc0blVnPT0.png?imageView&thumbnail=1680x0&quality=96&stripmeta=0" srcset="https://images.hive.blog/768x0/http://imglf5.nosdn.127.net/img/dUhZdTNwa3RzelVTUlFxS3lvQ3l3K2NzU200NDNXZWR0OUhzalQrcWh1aEt0U0xwUHc0blVnPT0.png?imageView&thumbnail=1680x0&quality=96&stripmeta=0 1x, https://images.hive.blog/1536x0/http://imglf5.nosdn.127.net/img/dUhZdTNwa3RzelVTUlFxS3lvQ3l3K2NzU200NDNXZWR0OUhzalQrcWh1aEt0U0xwUHc0blVnPT0.png?imageView&thumbnail=1680x0&quality=96&stripmeta=0 2x" />
<p dir="auto">下面详解一下各个变量:<br />
Block_id_type:这里的fc::ripemd160本质上是一个长度为5的uint32_t数组,存储了一个160位的哈希值,这个哈希值是链上前一个区块的哈希,当前区块根据这个哈希和前一个区块进行关联。<br />
fc::time_point_sec:这个变量存储的是出块时间,具体定义见fc库相关代码,time_point_sec是对时间戳的封装。<br />
witness_id_type:graphene区块链上每一个区块都是由21个见证人节点轮流生成的,这个witness_id_type存储的是生成这个区块的那个见证人id。<br />
checksum_type:该区块中所有transaction生成的默克尔树。<br />
extensions_type:用于将来扩展的数据结构,当将来对区块进行扩展时,此处可以存储一个或多个static_variant类型的变量,static_variant在fc库中,是一个模板类,支持将二进制数据流序列化对应的模板类对象,具体实现本文档不作详细说明。<br />
signature_type:见证人签名,当区块被见证人产生出来后,该见证人对此区块进行签名操作,相关签名存在此处。<br />
vector<processed_transaction>:这是一个容器,可以存储多个processed_transaction,processed_transaction是执行过的transaction,只有执行成功的transaction,才能被写入到区块之中。
<pre><code>【transaction结构】
<p dir="auto"><span><img src="https://images.hive.blog/768x0/http://imglf4.nosdn.127.net/img/dUhZdTNwa3RzelVTUlFxS3lvQ3l3NkdhalNOcTR2M0U3eEFKcG1OK3djaHRydjViU0NBSmhnPT0.png?imageView&thumbnail=1680x0&quality=96&stripmeta=0" srcset="https://images.hive.blog/768x0/http://imglf4.nosdn.127.net/img/dUhZdTNwa3RzelVTUlFxS3lvQ3l3NkdhalNOcTR2M0U3eEFKcG1OK3djaHRydjViU0NBSmhnPT0.png?imageView&thumbnail=1680x0&quality=96&stripmeta=0 1x, https://images.hive.blog/1536x0/http://imglf4.nosdn.127.net/img/dUhZdTNwa3RzelVTUlFxS3lvQ3l3NkdhalNOcTR2M0U3eEFKcG1OK3djaHRydjViU0NBSmhnPT0.png?imageView&thumbnail=1680x0&quality=96&stripmeta=0 2x" />
<p dir="auto">一个transaction包括如下两个部分:基础部分和签名部分。基础部分,即transaction,存储了一个transaction的基本信息。签名部分,即signed_transaction,包含了见证人对这个transaction的签名,最终生成部分,包括了这个transaction的执行结果。<br />
ref_block_num:一个transaction包含在一个区块中,这里存了该transaction所在区块的编号信息。<br />
ref_block_prefix:这个变量存储了transaction所在区块的基本信息。<br />
expiration:transaction的超时时间<br />
operations:一个transaction所包含的操作,operation的具体定义在operations.hpp中,是一个fc::static_variant模板类,static_variant可以根据模板类的类型,将二进制数据进行还原操作,序列化为对象实体,如下图所示:
<p dir="auto"><span><img src="https://images.hive.blog/768x0/http://imglf6.nosdn.127.net/img/dUhZdTNwa3RzelZWYUVJYjlFM3JpY1orZmN2Ylp4b21HV2JoOHA4TS9RbS9XYmxCOVhYVUlRPT0.png?imageView&thumbnail=500x0&quality=96&stripmeta=0" srcset="https://images.hive.blog/768x0/http://imglf6.nosdn.127.net/img/dUhZdTNwa3RzelZWYUVJYjlFM3JpY1orZmN2Ylp4b21HV2JoOHA4TS9RbS9XYmxCOVhYVUlRPT0.png?imageView&thumbnail=500x0&quality=96&stripmeta=0 1x, https://images.hive.blog/1536x0/http://imglf6.nosdn.127.net/img/dUhZdTNwa3RzelZWYUVJYjlFM3JpY1orZmN2Ylp4b21HV2JoOHA4TS9RbS9XYmxCOVhYVUlRPT0.png?imageView&thumbnail=500x0&quality=96&stripmeta=0 2x" />
<p dir="auto">在bitshares里面有如上多种类型的operation,用户相关的operation例如acount_create_operation、account_update_operation,转账操作的operation是transfer_operation,而每一种operation的数据和长度不一样,因此需要一个static_variant类进行统一转化操作。operation中存储了一个transaction的关键业务数据,例如transfer_operation中存储了转账操作的来源账户、目标账户、转账金额、转账备注等。见证人结点接收到这个transaction后,根据其中的operation类型,执行相应的操作,并从operation中提取出关键数据,进行处理。<br />
extensions:扩展部分。<br />
signatures:见证人节点对transaction的签名。<br />
operation_results:operation的操作结果,也是static_variant类型的数据。
<pre><code>【总结】
<p dir="auto">bitshares的一个区块包含多个processed_transaction,processed_transaction是执行成功的操作,包括创建账户、创建资产、转账、发布限价单等。
Congratulations @lpbottle! You have completed some achievement on Steemit and have been rewarded with new badge(s) :
<p dir="auto"><a href="http://steemitboard.com/@lpbottle" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link"><img src="https://images.hive.blog/768x0/https://steemitimages.com/70x80/http://steemitboard.com/notifications/firstvote.png" srcset="https://images.hive.blog/768x0/https://steemitimages.com/70x80/http://steemitboard.com/notifications/firstvote.png 1x, https://images.hive.blog/1536x0/https://steemitimages.com/70x80/http://steemitboard.com/notifications/firstvote.png 2x" /> You made your First Vote <p dir="auto">Click on any badge to view your own Board of Honor on SteemitBoard.<br /> For more information about SteemitBoard, click <a href="https://steemit.com/@steemitboard" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">here <p dir="auto">If you no longer want to receive notifications, reply to this comment with the word <code>STOP <blockquote> <p dir="auto">Upvote this notification to help all Steemit users. Learn why <a href="https://steemit.com/steemitboard/@steemitboard/http-i-cubeupload-com-7ciqeo-png" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">here!