首页 文章 精选 留言 我的

精选列表

搜索[双层思维链],共10000篇文章
优秀的个人博客,低调大师

区块链教程Fabric1.0源代码分析Ledger statedb(状态数据库)-兄弟连区块链

Fabric 1.0源代码笔记 之 Ledger #statedb(状态数据库) 1、statedb概述 statedb,或VersionedDB,即状态数据库,存储了交易(transaction)日志中所有键的最新值,也称世界状态(world state)。可选择基于leveldb或cauchdb实现。 statedb,代码分布在core/ledger/kvledger/txmgmt/statedb目录下,目录结构如下: statedb.go,定义了核心接口VersionedDBProvider、VersionedDB、ResultsIterator和QueryResult,以及UpdateBatch和nsIterator结构体及方法。 util.go,包括工具函数EncodeValue和DecodeValue的实现。 stateleveldb目录,VersionedDBProvider和VersionedDB接口的leveldb版本实现,即stateleveldb.VersionedDBProvider和stateleveldb.versionedDB结构体及方法。 statecouchdb目录,VersionedDBProvider和VersionedDB接口的couchdb版本实现,即statecouchdb.VersionedDBProvider和statecouchdb.VersionedDB结构体及方法。 2、核心接口定义 VersionedDBProvider接口定义: type VersionedDBProvider interface { GetDBHandle(id string) (VersionedDB, error) //获取VersionedDB句柄 Close() //关闭所有 VersionedDB 实例 } //代码在core/ledger/kvledger/txmgmt/statedb/statedb.go VersionedDB接口定义: type VersionedDB interface { //获取给定命名空间和键的值 GetState(namespace string, key string) (*VersionedValue, error) //在单个调用中获取多个键的值 GetStateMultipleKeys(namespace string, keys []string) ([]*VersionedValue, error) //返回一个迭代器, 其中包含给定键范围之间的所有键值(包括startKey,不包括endKey) GetStateRangeScanIterator(namespace string, startKey string, endKey string) (ResultsIterator, error) //执行给定的查询并返回迭代器 ExecuteQuery(namespace, query string) (ResultsIterator, error) //批处理应用 ApplyUpdates(batch *UpdateBatch, height *version.Height) error //返回statedb一致的最高事务的高度 GetLatestSavePoint() (*version.Height, error) //测试数据库是否支持这个key(leveldb支持任何字节, 而couchdb只支持utf-8字符串) ValidateKey(key string) error //打开db Open() error //关闭db Close() } //代码在core/ledger/kvledger/txmgmt/statedb/statedb.go ResultsIterator和QueryResult接口定义: type ResultsIterator interface { Next() (QueryResult, error) Close() } type QueryResult interface{} //代码在core/ledger/kvledger/txmgmt/statedb/statedb.go 补充CompositeKey、VersionedValue和VersionedKV结构体: type CompositeKey struct { Namespace string //命名空间 Key string //Key } type VersionedValue struct { Value []byte //Value Version *version.Height //版本 } type VersionedKV struct { CompositeKey //嵌入CompositeKey VersionedValue //嵌入VersionedValue } //代码在core/ledger/kvledger/txmgmt/statedb/statedb.go nsUpdates结构体及方法: type nsUpdates struct { m map[string]*VersionedValue //string为Key } func newNsUpdates() *nsUpdates//构造nsUpdates //代码在core/ledger/kvledger/txmgmt/statedb/statedb.go UpdateBatch结构体及方法: type UpdateBatch struct { updates map[string]*nsUpdates //string为Namespace } //构造UpdateBatch func NewUpdateBatch() *UpdateBatch //按namespace和key获取Value func (batch *UpdateBatch) Get(ns string, key string) *VersionedValue //按namespace和key添加Value func (batch *UpdateBatch) Put(ns string, key string, value []byte, version *version.Height) //按namespace和key删除Value,即置为nil func (batch *UpdateBatch) Delete(ns string, key string, version *version.Height) //按namespace和key查找是否存在 func (batch *UpdateBatch) Exists(ns string, key string) bool //获取更新的namespace列表 func (batch *UpdateBatch) GetUpdatedNamespaces() []string //按namespace获取nsUpdates func (batch *UpdateBatch) GetUpdates(ns string) map[string]*VersionedValue //构造nsIterator func (batch *UpdateBatch) GetRangeScanIterator(ns string, startKey string, endKey string) ResultsIterator //按namespace获取或创建nsUpdates func (batch *UpdateBatch) getOrCreateNsUpdates(ns string) *nsUpdates //代码在core/ledger/kvledger/txmgmt/statedb/statedb.go nsIterator结构体及方法: type nsIterator struct { ns string //namespace nsUpdates *nsUpdates //batch.updates[ns] sortedKeys []string //nsUpdates.m中key排序 nextIndex int //startKey lastIndex int //endKey } //构造nsIterator func newNsIterator(ns string, startKey string, endKey string, batch *UpdateBatch) *nsIterator func (itr *nsIterator) Next() (QueryResult, error) //按itr.nextIndex获取VersionedKV func (itr *nsIterator) Close() // do nothing //代码在core/ledger/kvledger/txmgmt/statedb/statedb.go 3、statedb基于leveldb实现 3.1、VersionedDB接口实现 VersionedDB接口实现,即versionedDB结构体,定义如下: type versionedDB struct { db *leveldbhelper.DBHandle //leveldb dbName string //dbName } //代码在core/ledger/kvledger/txmgmt/statedb/stateleveldb/stateleveldb.go 涉及方法如下: //构造versionedDB func newVersionedDB(db *leveldbhelper.DBHandle, dbName string) *versionedDB func (vdb *versionedDB) Open() error // do nothing func (vdb *versionedDB) Close() // do nothing func (vdb *versionedDB) ValidateKey(key string) error // do nothing //按namespace和key获取Value func (vdb *versionedDB) GetState(namespace string, key string) (*statedb.VersionedValue, error) //在单个调用中获取多个键的值 func (vdb *versionedDB) GetStateMultipleKeys(namespace string, keys []string) ([]*statedb.VersionedValue, error) //返回一个迭代器, 其中包含给定键范围之间的所有键值(包括startKey,不包括endKey) func (vdb *versionedDB) GetStateRangeScanIterator(namespace string, startKey string, endKey string) (statedb.ResultsIterator, error) //leveldb不支持ExecuteQuery方法 func (vdb *versionedDB) ExecuteQuery(namespace, query string) (statedb.ResultsIterator, error) //批处理应用 func (vdb *versionedDB) ApplyUpdates(batch *statedb.UpdateBatch, height *version.Height) error //返回statedb一致的最高事务的高度 func (vdb *versionedDB) GetLatestSavePoint() (*version.Height, error) //拼接ns和key,ns []byte{0x00} key func constructCompositeKey(ns string, key string) []byte //分割ns和key,分割符[]byte{0x00} func splitCompositeKey(compositeKey []byte) (string, string) //代码在core/ledger/kvledger/txmgmt/statedb/stateleveldb/stateleveldb.go func (vdb versionedDB) ApplyUpdates(batch statedb.UpdateBatch, height *version.Height) error代码如下: dbBatch := leveldbhelper.NewUpdateBatch() namespaces := batch.GetUpdatedNamespaces() //获取更新的namespace列表 for _, ns := range namespaces { updates := batch.GetUpdates(ns) //按namespace获取nsUpdates for k, vv := range updates { compositeKey := constructCompositeKey(ns, k) //拼接ns和key if vv.Value == nil { dbBatch.Delete(compositeKey) } else { dbBatch.Put(compositeKey, statedb.EncodeValue(vv.Value, vv.Version)) } } } //statedb一致的最高事务的高度 dbBatch.Put(savePointKey, height.ToBytes()) //var savePointKey = []byte{0x00} err := vdb.db.WriteBatch(dbBatch, true) //代码在core/ledger/kvledger/txmgmt/statedb/stateleveldb/stateleveldb.go 3.2、ResultsIterator接口实现 ResultsIterator接口实现,即kvScanner结构体及方法。 type kvScanner struct { namespace string dbItr iterator.Iterator } //构造kvScanner func newKVScanner(namespace string, dbItr iterator.Iterator) *kvScanner //迭代获取statedb.VersionedKV func (scanner *kvScanner) Next() (statedb.QueryResult, error) func (scanner *kvScanner) Close() //释放迭代器 //代码在core/ledger/kvledger/txmgmt/statedb/stateleveldb/stateleveldb.go 3.3、VersionedDBProvider接口实现 VersionedDBProvider接口实现,即VersionedDBProvider结构体及方法。 type VersionedDBProvider struct { dbProvider *leveldbhelper.Provider } func NewVersionedDBProvider() *VersionedDBProvider //构造VersionedDBProvider //获取statedb.VersionedDB func (provider *VersionedDBProvider) GetDBHandle(dbName string) (statedb.VersionedDB, error) func (provider *VersionedDBProvider) Close() //关闭statedb.VersionedDB //代码在core/ledger/kvledger/txmgmt/statedb/stateleveldb/stateleveldb.go 4、statedb基于cauchdb实现 暂略,待补充。

优秀的个人博客,低调大师

从高通英特尔的服务器SoC之争,看能源问题如何改变芯片厂商的思维方式

通过设计高性能的服务器SoC,英特尔成功地垄断了数据中心市场,而且它在短期内丧失大的市场份额的可能性相当小。不过,如果我们从一个长远的时间来看,比如到2030年或2040年,就像半导体行业协会(SIA)在2015年九月份的一份报告中所揭示的那样,我们意识到,现在这种芯片设计方式将会发生剧烈的变化。仅以性能为指标来设计SoC,即使是在最先进的工艺节点上,甚或是在时机成熟时继续下探工艺节点,也是不可持续的一种方式。 如果您不相信我的话,看一看下面这个图吧:如果我们当今设计计算机系统的方式不发生改变的话,计算机系统消耗的总能量将会在2037年时超过全球生产的能量总和。 首先,我们需要承认,报告的作者并没有把调查限定在各种不同的SoC设计方法上,而且评估了尚未到来的非硅材料芯片、3D设计带来的影响、接近物理极限时的运算操作等因素。就我个人而言,我建议在所知的领域内做一个调研:硅器件、SoC设计技术和Si制造工艺。 在过去的十五年中,我们看到了两种不同类型的芯片制造商,它们面向两种完全不同的市场开发SoC,都取得了巨大的成功。其中一类由英特尔或思科领衔,它们面向数据中心或网络通信设计SoC,总是在追求更高的性能(计算能力或者带宽),而不计功率消耗,假设当今的能源还可以支持芯片正常运行。 另一组则是由高通或者苹果领衔,他们为电池供电的移动系统开发应用处理器SoC。这一类公司学会了使用诸如在芯片层级上部署时钟门控或者电源孤岛、以及在系统层面上使用功率管理单元等设计技术,在保持功率消耗尽可能低的情况下,也能提供最高的CPUGPUDSP性能。当然,我们不能忘记高通们在制造工艺领域的合作伙伴-台积电、三星或格罗方德,他们站在系统的高度开发一些低功耗的工艺,同样需要谨记在心的还包括提供低功耗版本的基础IP的IP供应商。 我们注意到一件有趣的事情,英特尔近年来频繁试图渗透到移动市场,却屡次折戟沙场。是因为英特尔专注于纯粹的性能这种“公司文化”阻止它支持正确的工艺(低功耗)呢,还是英特尔公司的设计人员自己不愿意采用和曾经使用了几十年之久、成功打造过CPU SoC的技术迥然不同的新技术呢? 无论是哪种原因,可能是公司文化或短期的营销策略一起导致了英特尔现下的处境。现在,数据处理、计算和网络正在每个应用类别中以几何级数式增长。摘自SIA报告的下图,清晰地揭示了数据正在以级数形式增长。如果你观察一下数据的三个主要需求源-多媒体、消费者IoT、工业IoT,业界的共识是它还会继续保持增长。事实上,IoT和工业IoT的更大规模部署才刚刚开始。如果你认为这个图还没有涵盖所有数据需求,并希望纳入那些数据量的话,毫无疑问,数据增长的速度只会有增无减。如果,我们不在中期内采取行动的话,计算行业肯定会在2035年或2040年前后遇到大问题。 时至当下,数据中心由服务器机器堆积而成,它需要通过一个巨大的空调系统进行散热。现在,数据中心的电费非常高昂,而且超过一半都用在了制冷系统上。再看看服务器芯片,他们需要高效的封装进行功率耗散,还需要加入散热片。换句话说,数据中心运行的每一步都会因芯片散发的高热烧掉很大一笔钱。 如果我们希望管理数据中心的公司们(谷歌、亚马逊等)能彻底改变,采用对功耗敏感的架构,不要指望他们会出于无私的利他主义而这样做。一个可行的解决方案需要能降低拥有成本,也就是说总体成本要降低。我们能否定义一种服务器架构,可以以低得多的功率消耗提供同样的性能(MIPS、延迟、带宽),从而大幅度降低电费支出呢?我不知道,但是需要马上启动这种研究,我是指研究一种可以在接下来的三到五年内实施的方案,而不是期待神奇材料突然出现而替代硅材料。如果你看得够远的话,2037年已经不远了,想想1995年是不是刚刚过去? 本文转自d1net(转载)

资源下载

更多资源
Mario

Mario

马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。

腾讯云软件源

腾讯云软件源

为解决软件依赖安装时官方源访问速度慢的问题,腾讯云为一些软件搭建了缓存服务。您可以通过使用腾讯云软件源站来提升依赖包的安装速度。为了方便用户自由搭建服务架构,目前腾讯云软件源站支持公网访问和内网访问。

Rocky Linux

Rocky Linux

Rocky Linux(中文名:洛基)是由Gregory Kurtzer于2020年12月发起的企业级Linux发行版,作为CentOS稳定版停止维护后与RHEL(Red Hat Enterprise Linux)完全兼容的开源替代方案,由社区拥有并管理,支持x86_64、aarch64等架构。其通过重新编译RHEL源代码提供长期稳定性,采用模块化包装和SELinux安全架构,默认包含GNOME桌面环境及XFS文件系统,支持十年生命周期更新。

Sublime Text

Sublime Text

Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。

用户登录
用户注册