您现在的位置是:首页 > 文章详情

区块链教程Fabric1.0源代码分析ledgerID数据库-兄弟连区块链教程

日期:2018-10-31点击:325

1、idStore概述

  • Fabric支持创建多个Ledger,不同Ledger以ledgerID区分。
  • 多个ledgerID及其创世区块存储在idStore数据库中,idStore数据库基于leveldb实现。
  • idStore默认使用路径:/var/hyperledger/production/ledgersData/ledgerProvider/。
  • idStore库中特殊key "underConstructionLedgerKey",用于标志最新在建的ledgerID,ledgerID创建成功后或失败时该标志将清除,另外此标志也用于异常时按ledgerID恢复数据。
  • idStore相关代码集中在core/ledger/kvledger/kv_ledger_provider.go。

2、idStore结构体定义

leveldbhelper更详细内容,参考:Fabric 1.0源代码笔记 之 LevelDB(KV数据库)

type idStore struct {     db *leveldbhelper.DB } //代码在core/ledger/kvledger/kv_ledger_provider.go

3、idStore方法定义

func openIDStore(path string) *idStore //按path创建并打开leveldb数据库 func (s *idStore) setUnderConstructionFlag(ledgerID string) error //设置ledgerID在建标志,将key为"underConstructionLedgerKey",value为ledgerID写入库 func (s *idStore) unsetUnderConstructionFlag() error //取消ledgerID在建标志(确认构建失败时),删除key"underConstructionLedgerKey" func (s *idStore) getUnderConstructionFlag() (string, error) //获取ledgerID在建标志(按ledgerID恢复时),按key"underConstructionLedgerKey",取ledgerID func (s *idStore) createLedgerID(ledgerID string, gb *common.Block) error //创建LedgerID,即以ledgerID为key,将创世区块写入库 func (s *idStore) ledgerIDExists(ledgerID string) (bool, error) //查找ledgerID是否存在,即查库中key为ledgerID是否存在 func (s *idStore) getAllLedgerIds() ([]string, error) //获取ledgerID列表 func (s *idStore) close() //关闭idStore leveldb数据库 func (s *idStore) encodeLedgerKey(ledgerID string) []byte //为ledgerID添加前缀即"l" func (s *idStore) decodeLedgerID(key []byte) string //解除ledgerID前缀 //代码在core/ledger/kvledger/kv_ledger_provider.go

func (s idStore) createLedgerID(ledgerID string, gb common.Block) error代码如下:
将ledgerID和Block入库,并清除ledgerID在建标志。

func (s *idStore) createLedgerID(ledgerID string, gb *common.Block) error {     key := s.encodeLedgerKey(ledgerID) //为ledgerID添加前缀即"l"     var val []byte     var err error     if val, err = proto.Marshal(gb); err != nil { //Block序列化         return err     }     if val, err = s.db.Get(key); err != nil {         return err     }     if val != nil {         return ErrLedgerIDExists //ledgerID已存在     }     batch := &leveldb.Batch{}     batch.Put(key, val) //ledgerID和Block入库     batch.Delete(underConstructionLedgerKey) //清除ledgerID在建标志     return s.db.WriteBatch(batch, true) //提交执行 } //代码在core/ledger/kvledger/kv_ledger_provider.go

感谢关注兄弟连区块链教程分享!

原文链接:https://yq.aliyun.com/articles/662541
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章