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

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

原文地址:http://blog.51cto.com/14041296/2311775

时间: 2024-08-25 03:46:25

兄弟连区块链教程Fabric1.0源代码分析ledgerID数据的相关文章

区块链教程Fabric1.0源代码分析Peer peer根命令入口及加载子命令二

区块链教程Fabric1.0源代码分析Peer peer根命令入口及加载子命令二.flogging,即:fabric logging,为Fabric基于第三方包go-logging封装的日志包,go-logging使用方法参考:github.com/op/go-logging如下代码为flogging包的初始化函数: func init() { ????logger = logging.MustGetLogger(pkgLogID) //创建仅在flogging包内代码使用的logging.Lo

区块链教程Fabric1.0源代码分析Ledger historydb历史数据库

1.historydb概述 historydb,用于存储所有块读写集中写集的内容.代码分布在core/ledger/kvledger/history/historydb目录下,目录结构如下: historydb.go,定义核心接口HistoryDBProvider和HistoryDB. histmgr_helper.go,historydb工具函数. historyleveldb目录,historydb基于leveldb的实现.???? historyleveldb.go,HistoryDBPr

兄弟连区块链教程open-ethereum-pool矿池源码分析unlocker模块

兄弟连区块链教程open-ethereum-pool以太坊矿池源码分析unlocker模块open-ethereum-pool以太坊矿池-unlocker模块 unlocker模块配置 json"unlocker": {????"enabled": false,????"poolFee": 1.0,????"poolFeeAddress": "",????"donate": true,?

区块链教程以太源码分析accounts账户管理分析

区块链教程以太源码分析accounts账户管理分析. 数据结构分析 ETH的账户管理定义在accounts/manager.go中,其数据结构为: // Manager is an overarching account manager that can communicate with various // backends for signing transactions. type Manager struct { backends map[reflect.Type][]Backend /

兄弟连区块链教程open-ethereum-pool矿池源码分析环境安装

安装Geth //安装parity cd /tmp/ wget d1h4xl4cr1h0mo.cloudfront.net/v1.8.11/x86_64-unknown-linux-gnu/parity_1.8.11_ubuntu_amd64.deb dpkg -i parity_1.8.11_ubuntu_amd64.deb //安装screen apt-get update apt-get -y install screen //启动parity screen parity --base-p

兄弟连区块链教程btcpool矿池源码分析JobMaker模块解析

核心机制总结 同时监听kafka KAFKA_TOPIC_RAWGBT和KAFKA_TOPIC_NMC_AUXBLOCK,以支持混合挖矿 接收的Gbt消息,如果与本地时间延迟超过60秒将丢弃,如果延迟超过3秒将打印log 可用的Gbt消息,将以gbtTime+isEmptyBlock+height来构造key写入本地Map,另gbtHash也会写入本地队列 本地gbtHash队列仅保存最近20条,本地gbtMap中Gbt消息有效期:非空Gbt有效期90秒,空Gbt有效期15秒,过期将清除??*

兄弟连区块链教程btcpool矿池源码分析BlockMaker模块解析

btcpool矿池-BlockMaker模块解析 核心机制总结 blkmaker可以连多个bitcoind节点 blkmaker监听和接收4类消息:RAWGBT.STRATUM_JOB.SOLVED_SHARE和NMC_SOLVED_SHARE 监听RAWGBT目的为获取gbtHash/交易列表,用于构建Block,gbtHash和vtxs写入rawGbtMap_??* rawGbtMap_保存最近100条gbtHash/vtxs对 监听STRATUMJOB目的为获取jobId/gbtHash,

兄弟连区块链教程open-ethereum-pool矿池源码分析policy模块

open-ethereum-pooleth矿池-policy模块 PolicyServer定义 type PolicyServer struct { sync.RWMutex statsMu sync.Mutex config *Config stats map[string]*Stats banChannel chan string startedAt int64 grace int64 timeout int64 blacklist []string whitelist []string s

兄弟连区块链教程open-ethereum-pool矿池源码分析payouts模块

open-ethereum-pooleth矿池-payouts模块 PayoutsProcessor定义 type PayoutsProcessor struct { config *PayoutsConfig backend *storage.RedisClient rpc *rpc.RPCClient halt bool lastFail error } GetPendingPayments原理 func (r *RedisClient) GetPendingPayments() []*Pe