ledger部分的接口


持续更新中,目前更新到PeerLedgerProvider的实现部分

首先在core/ledger/ledger_interface.go文件中确定了ledger用到的各种接口

PeerLedgerProvider :

给ledger实例提供handle ,代码实现如下

type PeerLedgerProvider interface {
    // Create creates a new ledger with the given genesis block.
    // This function guarantees that the creation of ledger and committing the genesis block would an atomic action
    // The chain id retrieved from the genesis block is treated as a ledger id
    Create(genesisBlock *common.Block) (PeerLedger, error)
    // Open opens an already created ledger
    Open(ledgerID string) (PeerLedger, error)
    // Exists tells whether the ledger with given id exists
    Exists(ledgerID string) (bool, error)
    // List lists the ids of the existing ledgers
    List() ([]string, error)
    // Close closes the PeerLedgerProvider
    Close()
}

方法的实现在core/ledger/kvldger/kv_ledger_provider.go里,在这个文件中还有与blockstore相关的部分。provider与blockstore相关。
provider的结构:
一个DB
三个接口,分别是
1.BlockStoreProvider
2.VersionedDBProvider
3.HistoryDBProvider

新建一个provider,函数实现如下

func NewProvider() (ledger.PeerLedgerProvider, error)

create方法:

从Block中获取chain的id,为chain/block获取Block进行存储,然后commit创始区块

func (provider *Provider) Create(genesisBlock *common.Block) (ledger.PeerLedger, error)

create先从block中获取chaincode的id

ledgerID, err := utils.GetChainIDFromBlock(genesisBlock)

接着创建上面提到的三个provider,并打开相关DB的连接,为chain/block获取存储空间,返回的是一个PeerLdger

ledger, err := provider.openInternal(ledgerID)

过程中创建了kvLedger(Ledger的数据结构实现),代码实现如下:

type kvLedger struct {
    ledgerID   string
    blockStore blkstorage.BlockStore
    txtmgmt    txmgr.TxMgr
    historyDB  historydb.HistoryDB
}

最后:

err := ledger.Commit(genesisBlock)

这句是提交create中传入的genesisBlock。具体实现在kv_ledger.go文件中,commit的过程:
1.是验证块,涉及到的新的接口在core/ledger/txmgmt/txmgr/txmgr.go中的TxMgr(具体细节放到以后看);
2.是向storage提交block(获取存储空间);
3.把block中的交易存入state database(注意这里又用到了另一个commit()方法)
4.根据配置文件的配置,决定是否将交易信息写入history database。(这一步可能没有)
到这commit的流程结束。

open方法:

首先根据ledgerID判断kvledger是否存在,代码如下:

exists, err := provider.idStore.ledgerIDExists(ledgerID)

存在就直接根据ledgerID获取存储空间,代码如下:

return provider.openInternal(ledgerID)

不存在就返回nil,并报错。

openinternal方法

为chain/block获取存储空间

func (provider *Provider) openInternal(ledgerID string) (ledger.PeerLedger, error) 

具体流程如下:
1.与其他provider连接,代码如下:

blockStore, err := provider.blockStoreProvider.OpenBlockStore(ledgerID)
vDB, err := provider.vdbProvider.GetDBHandle(ledgerID)
historyDB, err := provider.historydbProvider.GetDBHandle(ledgerID)

2.新建一个kvledger,代码如下:

l, err := newKVLedger(ledgerID, blockStore, vDB, historyDB)

newKVLedger代码如下:

func newKVLedger(ledgerID string, blockStore blkstorage.BlockStore,versionedDB statedb.VersionedDB, historyDB historydb.HistoryDB) (*kvLedger, error)

流程如下:
2.1.初始化一个transaction manager,代码如下:

var txmgmt txmgr.TxMgr
    txmgmt = lockbasedtxmgr.NewLockBasedTxMgr(versionedDB)

2.2.赋值,代码如下:

l := &kvLedger{ledgerID, blockStore, txmgmt, historyDB}

2.3如果它们与块存储不同步,则可以恢复状态数据库和历史数据库,代码如下:

err := l.recoverDBs()

PeerLedger :

与OrderLedger不同的是维护一个位掩码,用来区分有效交易和无效交易,代码实现如下:

type PeerLedger interface {
    commonledger.Ledger
    // GetTransactionByID retrieves a transaction by id
    GetTransactionByID(txID string) (*peer.ProcessedTransaction, error)
    // GetBlockByHash returns a block given it's hash
    GetBlockByHash(blockHash []byte) (*common.Block, error)
    // GetBlockByTxID returns a block which contains a transaction
    GetBlockByTxID(txID string) (*common.Block, error)
    // GetTxValidationCodeByTxID returns reason code of transaction validation
    GetTxValidationCodeByTxID(txID string) (peer.TxValidationCode, error)
    // NewTxSimulator gives handle to a transaction simulator.
    // A client can obtain more than one 'TxSimulator's for parallel execution.
    // Any snapshoting/synchronization should be performed at the implementation level if required
    NewTxSimulator() (TxSimulator, error)
    // NewQueryExecutor gives handle to a query executor.
    // A client can obtain more than one 'QueryExecutor's for parallel execution.
    // Any synchronization should be performed at the implementation level if required
    NewQueryExecutor() (QueryExecutor, error)
    // NewHistoryQueryExecutor gives handle to a history query executor.
    // A client can obtain more than one 'HistoryQueryExecutor's for parallel execution.
    // Any synchronization should be performed at the implementation level if required
    NewHistoryQueryExecutor() (HistoryQueryExecutor, error)
    //Prune prunes the blocks/transactions that satisfy the given policy
    Prune(policy commonledger.PrunePolicy) error
}

ValidatedLedger :

代表的是去掉所有无效交易的PeerLedger,代码实现如下:

type ValidatedLedger interface {
    commonledger.Ledger
}

QueryExecutor :

字面意思用来执行查询,代码实现如下:

type QueryExecutor interface {
    // GetState gets the value for given namespace and key. For a chaincode, the namespace corresponds to the chaincodeId
    GetState(namespace string, key string) ([]byte, error)
    // GetStateMultipleKeys gets the values for multiple keys in a single call
    GetStateMultipleKeys(namespace string, keys []string) ([][]byte, error)
    // GetStateRangeScanIterator returns an iterator that contains all the key-values between given key ranges.
    // startKey is included in the results and endKey is excluded. An empty startKey refers to the first available key
    // and an empty endKey refers to the last available key. For scanning all the keys, both the startKey and the endKey
    // can be supplied as empty strings. However, a full scan shuold be used judiciously for performance reasons.
    // The returned ResultsIterator contains results of type *KV which is defined in protos/ledger/queryresult.
    GetStateRangeScanIterator(namespace string, startKey string, endKey string) (commonledger.ResultsIterator, error)
    // ExecuteQuery executes the given query and returns an iterator that contains results of type specific to the underlying data store.
    // Only used for state databases that support query
    // For a chaincode, the namespace corresponds to the chaincodeId
    // The returned ResultsIterator contains results of type *KV which is defined in protos/ledger/queryresult.
    ExecuteQuery(namespace, query string) (commonledger.ResultsIterator, error)
    // Done releases resources occupied by the QueryExecutor
    Done()
}

HistoryQueryExecutor:

执行历史查询,代码实现如下:

type HistoryQueryExecutor interface {
    // GetHistoryForKey retrieves the history of values for a key.
    // The returned ResultsIterator contains results of type *KeyModification which is defined in protos/ledger/queryresult.
    GetHistoryForKey(namespace string, key string) (commonledger.ResultsIterator, error)
}

TxSimulator :

模拟了一条交易,该交易的快照是“尽可能是最近状态”,代码如下:

type TxSimulator interface {
    QueryExecutor
    // SetState sets the given value for the given namespace and key. For a chaincode, the namespace corresponds to the chaincodeId
    SetState(namespace string, key string, value []byte) error
    // DeleteState deletes the given namespace and key
    DeleteState(namespace string, key string) error
    // SetMultipleKeys sets the values for multiple keys in a single call
    SetStateMultipleKeys(namespace string, kvs map[string][]byte) error
    // ExecuteUpdate for supporting rich data model (see comments on QueryExecutor above)
    ExecuteUpdate(query string) error
    // GetTxSimulationResults encapsulates the results of the transaction simulation.
    // This should contain enough detail for
    // - The update in the state that would be caused if the transaction is to be committed
    // - The environment in which the transaction is executed so as to be able to decide the validity of the environment
    //   (at a later time on a different peer) during committing the transactions
    // Different ledger implementation (or configurations of a single implementation) may want to represent the above two pieces
    // of information in different way in order to support different data-models or optimize the information representations.
    GetTxSimulationResults() ([]byte, error)
}

以上接口涉及到的其他文件中的接口:

在common/ledger/ledger_interface.go中:

Ledger接口:

Ledger捕捉“PeerLedger”,“OrdererLedger”和“ValidatedLedger”中常见的方法。代码实现如下:

type Ledger interface {
    // GetBlockchainInfo returns basic info about blockchain
    GetBlockchainInfo() (*common.BlockchainInfo, error)
    // GetBlockByNumber returns block at a given height
    // blockNumber of  math.MaxUint64 will return last block
    GetBlockByNumber(blockNumber uint64) (*common.Block, error)
    // GetBlocksIterator returns an iterator that starts from `startBlockNumber`(inclusive).
    // The iterator is a blocking iterator i.e., it blocks till the next block gets available in the ledger
    // ResultsIterator contains type BlockHolder
    GetBlocksIterator(startBlockNumber uint64) (ResultsIterator, error)
    // Close closes the ledger
    Close()
    // Commit adds a new block
    Commit(block *common.Block) error
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 206,311评论 6 481
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 88,339评论 2 382
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 152,671评论 0 342
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 55,252评论 1 279
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 64,253评论 5 371
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,031评论 1 285
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,340评论 3 399
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,973评论 0 259
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 43,466评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,937评论 2 323
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,039评论 1 333
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,701评论 4 323
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,254评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,259评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,485评论 1 262
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,497评论 2 354
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,786评论 2 345