二维码

5.4 账本数据 - 数据结构 - 机器学习

1246 人阅读 | 时间:2021年01月15日 01:20
5.4 账本数据 - 数据结构 - 机器学习 #daohang ul li t,.reed .riqi,a.shangg,a.xiatt,a.shangg:hover,a.xiatt:hover,a.shang,a.xiat,a.shang:hover,a.xiat:hover,.reed-pinglun-anniu,span.now-page,#daohangs-around,#caidan-tubiao,#daohangs,#daohangs li,#btnPost{background-color:#D10B04;} .dinglanyou1 h3{border-bottom:3px solid #D10B04;} #dibuer{border-top:2px solid #D10B04;}.cebianlan .rongqi h3{border-bottom:1px solid #D10B04;} #edtSearch{border:1px solid #D10B04;} #daohang .zuo ul li{border-right:1px solid #;} #daohang ul li t a{border-top:1px solid #;border-right:1px solid #D10B04;} #daohang ul li t a:hover{border-right:1px solid #;} #daohang .you ul li a:hover,#daohang .zuo ul li a:hover,.reed-pinglun-anniu:hover{background-color:#;} a:hover,.reed h6 a:hover,#dibuer a:hover,.reed .riqiding,.cebianlan .rongqi li a:hover,#pinglun-liebiao ul.fubens li.depth-1 dl dd span.shu a,#pinglun-liebiao ul.fubens li.depth-1 dl dd span.huifuliuyan a:hover,.reed-biaoti h6 span{color:#D10B04;} .reed .kan a{color:#0A0AF5;}.reed .kan a:hover{color:#D10101;} @media screen and (max-width:1492px){a.shang,a.xiat{background:none;} a.xiat:hover,a.shang:hover{background-color:#f9f9f9;background-image:none;text-decoration:none;}} var _hmt = _hmt || [];(function() { var hm = document.createElement("script"); hm.src = "https://hm.baidu.com/hm.js?b19db5ba3b437a9e8698d2bc8fc64334"; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(hm, s);})(); var _hmt = _hmt || []; (function() { var hm = document.createElement("script"); hm.src = "https://hm.baidu.com/hm.js?b19db5ba3b437a9e8698d2bc8fc64334"; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(hm, s); })(); var _hmt = _hmt || []; (function() { var hm = document.createElement("script"); hm.src = "https://hm.baidu.com/hm.js?2d748c9763cfc72fb7d1ccab29f0770d"; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(hm, s); })(); var _hmt = _hmt || []; (function() { var hm = document.createElement("script"); hm.src = "https://hm.baidu.com/hm.js?f6d451f3f1be23f3abf240c64c469c1b"; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(hm, s); })();

当前位置:首页 » 区块链精品文章 » 正文

(function() { var s = "_" + Math.random().toString(36).slice(2); document.write('
'); (window.slotbydup = window.slotbydup || []).push({ id: "u3646201", container: s }); })();
(function() { var s = "_" + Math.random().toString(36).slice(2); document.write('
'); (window.slotbydup = window.slotbydup || []).push({ id: "u3646162", container: s }); })();

5.4 账本数据

1273 人参与  2018年10月15日 10:46  分类 : 区块链精品文章  评论

账本数据(Ledger)是以二进制文件的形式存储的,每个账本数据存储在不同的目录下。后面的内容都是在已经区分了账本的情况下再对数据进行查询的。基于文件系统的区块存储实现了如下功能接口。

1)账本存储管理。

·提交区块到账本(AddBlock)

·获取区块链信息(GetBlockchainInfo)

·获取区块数据(RetrieveBlocks)

·关闭区块存储(Shutdown)

2)索引管理:跟踪区块和交易保存在哪个文件。

·根据哈希值获取区块(RetrieveBlockByHash)

·根据区块编号获取区块(RetrieveBlockByNumber)

·根据交易编号获取交易(RetrieveTxByID)

·根据区块编号和交易编号获取交易(RetrieveTxByBlockNumTranNum)

·根据交易编号获取区块(RetrieveBlockByTxID)

·根据交易编号获取交易验证码(RetrieveTxValidationCodeByTxID)

账本数据的所有操作都是通过区块文件管理器(blockfileMgr)实现的,定义如下:


type blockfileMgr struct {
   rootDir           string                     // 区块链中区块存储的根目录
   conf              *Conf                      // 配置信息
   db                *leveldbhelper.DBHandle    // 数据库指针
   index             index                      // 区块索引接口
   cpInfo            *checkpointInfo            // 区块检查点信息
   cpInfoCond        *sync.Cond                 // 条件变量
   currentFileWriter *blockfileWriter           // 当前写入区块文件的指针
   bcInfo            atomic.Value               // 区块链信息
}


区块文件管理器实现的功能分为几类。

1)账本数据存储管理。

·确定文件存储在哪个目录;

·确定区块存储在哪个文件。

2)检查点管理:跟踪最新持久化存储的文件。

3)索引管理:跟踪区块和交易保存在哪个文件。

5.4.1 账本数据存储

区块文件管理器创建的文件名以"blockfile_"为前缀,6位数字为后缀,后缀必须是从小到大连续的数字, 中间不能有缺失,比如blockfile_000000、blockfile_000001、blockfile_000002等。默认的区块文件大小上 限为64MB(目前这个大小在代码中是固定的,以后可能会动态调整)。一个账本能保存的最大数据量大概有61TB,这应该能满足目前绝大多数的应用了。

区块文件管理器维护一个当前写入区块文件的指针currentFileWriter,写入区块文件的数据包括两个 部分,一个是区块大小,另一个是区块数据。写入区块文件中的区块大小和区块数据都是经过序列化处理的,序列化过程见技术实现部分。如果写入区块大小和序列 化后,当前区块文件的大小超过设定值,则会写入到下一个区块文件中。创建一个新的区块检查点信息(见区块索引部分),保存到数据库中。

若写入区块文件和保存区块检查点信息的过程出现任何异常,就会根据区块检查点记录的最新区块文件偏移 latestFileChunksize(见区块索引部分),恢复区块文件到写入前的状态。多个区块数据保存到区块文件后,区块文件形成了一个非结构化的 二进制文件,它需要在区块文件外记录索引信息,才能快速地定位到区块。保存区块数据后会建立这个区块的索引,并保存到数据库中,详细的构建区块索引过程在 后面的章节会有介绍。

最后是更新区块链文件管理器维护的区块检查点信息和区块链信息。

5.4.2 账本数据读取

区块文件流(blockfileStream)和区块流(blockStream)是以数据流的方式从区块文件系 统中读取区块的,区块文件流是从单个文件中读取的,区块流可以跨不同的文件,实际的文件读取通过区块文件流来实现。区块流维护了当前的区块文件流指针,还 有当前区块文件的编号等,详细的定义如下:


type blockStream struct {
   rootDir           string            // 区块链中区块存储的根目录
   currentFileNum    int               // 当前读取文件区块的编号
   endFileNum        int               // 结束读取文件区块的编号
   currentFileStream *blockfileStream  // 当前区块文件流指针
}


可以有多个区块流同时读取区块文件以获取区块数据,读取从区块文件currentFileNum的某个偏移开始, 到区块文件endFileNum(包含整个区块文件)之间的所有区块。结束读取文件区块的endFileNum可以设置为小于0的数值,这表示读取后续所 有的区块文件,直到账本数据目录下某个区块文件不存在为止。

跟区块文件存储过程类似,读取区块数据需要先读取区块数据的大小,再读取区块数据本身。区块数据的大小是可变长的 64位数字,最大长度为8个字节。由于可变长数字序列化后最后一个字节的最高位是0,所以能从字节流中区分出区块数据大小和区块数据本身。详细的编码规则 见序列化部分。

区块迭代器(blocksItr)基于区块流,实现了获取区块数据(RetrieveBlocks)的接口。增加的一个业务逻辑是如果需要获取的区块编号还未生成,迭代器会一直等待,直到获取到指定区块或者关闭迭代器。

5.4.3 交易模拟执行

链码是可以并行执行的,执行的过程并不影响当前的状态数据库。实现的方法是在最新账本上生成一个账本数据的模拟 器,模拟执行过程生成的数据会写入模拟器的writeMap中,读取的数据写入到readMap中,最后再根据writeMap和readMap生成 TxRwSet结果。每次链码执行的时候都会生成一个新的模拟器,所以多个链码并行执行并不会相互影响,模拟执行的结果也不会直接影响当前的状态数据库, 生成的TxRwSet在提交交易的时候,只有验证通过以后才会记录到账本中。

来源:我是码农,转载请保留出处和链接!

本文链接:http://www.54manong.com/?id=1067

(function() { var s = "_" + Math.random().toString(36).slice(2); document.write('
'); (window.slotbydup = window.slotbydup || []).push({ id: "u3646208", container: s }); })();
(function() { var s = "_" + Math.random().toString(36).slice(2); document.write('
'); (window.slotbydup = window.slotbydup || []).push({ id: "u3646147", container: s }); })();
window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdPic":"","bdStyle":"0","bdSize":"16"},"share":{},"image":{"viewList":["qzone","tsina","tqq","renren","weixin"],"viewText":"分享到:","viewSize":"16"},"selectShare":{"bdContainerClass":null,"bdSelectMiniList":["qzone","tsina","tqq","renren","weixin"]}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];
区块链是什么  

微信号:qq444848023    QQ号:444848023

加入【我是码农】QQ群:864689844(加群验证:我是码农)

<< 上一篇 下一篇 >>
(function() { var s = "_" + Math.random().toString(36).slice(2); document.write('
'); (window.slotbydup = window.slotbydup || []).push({ id: "u3646186", container: s }); })();
(function() { var s = "_" + Math.random().toString(36).slice(2); document.write('
'); (window.slotbydup = window.slotbydup || []).push({ id: "u3646175", container: s }); })();
搜索

网站分类

标签列表

最近发表

    (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https'){ bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else{ bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })();

全站首页 | 数据结构 | 区块链| 大数据 | 机器学习 | 物联网和云计算 | 面试笔试

var cnzz_protocol = (("https:" == document.location.protocol) ? "https://" : "http://");document.write(unescape("%3Cspan id='cnzz_stat_icon_1276413723'%3E%3C/span%3E%3Cscript src='" + cnzz_protocol + "s23.cnzz.com/z_stat.php%3Fid%3D1276413723%26show%3Dpic1' type='text/javascript'%3E%3C/script%3E"));本站资源大部分来自互联网,版权归原作者所有!

jQuery(document).ready(function($){ /* prepend menu icon */ $('#daohangs-around').prepend('
'); /* toggle nav */ $("#caidan-tubiao").on("click", function(){ $("#daohangs").slideToggle(); $(this).toggleClass("active"); }); });

©著作权归作者所有:来自ZhiKuGroup博客作者没文化的原创作品,如需转载,请注明出处,否则将追究法律责任 来源:ZhiKuGroup博客,欢迎分享。

评论专区
  • 昵 称必填
  • 邮 箱选填
  • 网 址选填
◎已有 0 人评论
搜索
作者介绍
30天热门
×
×
本站会员尊享VIP特权,现在就加入我们吧!登录注册×
»
会员登录
新用户注册
×
会员注册
已有账号登录
×