本文共 2227 字,大约阅读时间需要 7 分钟。
leveldbhelper更详细内容,参考:Fabric 1.0源代码笔记 之 LevelDB(KV数据库)
type idStore struct { db *leveldbhelper.DB}//代码在core/ledger/kvledger/kv_ledger_provider.go
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",取ledgerIDfunc (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
感谢关注兄弟连区块链教程分享!
转载地址:http://oblzl.baihongyu.com/