Fabric 1.0原始碼分析(21)Ledger #historydb(歷史資料庫)

尹成發表於2018-05-20
# Fabric 1.0原始碼筆記 之 Ledger #historydb(歷史資料庫)

## 1、historydb概述

historydb,用於儲存所有塊讀寫集中寫集的內容。
程式碼分佈在core/ledger/kvledger/history/historydb目錄下,目錄結構如下:

* historydb.go,定義核心介面HistoryDBProvider和HistoryDB。
* histmgr_helper.go,historydb工具函式。
* historyleveldb目錄,historydb基於leveldb的實現。
    * historyleveldb.go,HistoryDBProvider和HistoryDB介面實現,即historyleveldb.HistoryDBProvider和historyleveldb.historyDB結構體及方法。
    * historyleveldb_query_executer.go,定義LevelHistoryDBQueryExecutor和historyScanner結構體及方法。

## 2、核心介面定義

HistoryDBProvider介面定義:

```go
type HistoryDBProvider interface {
    GetDBHandle(id string) (HistoryDB, error) //獲取HistoryDB
    Close() //關閉所有HistoryDB
}
//程式碼在core/ledger/kvledger/history/historydb/historydb.go
```

HistoryDB介面定義:

```go
type HistoryDB interface {
    //構造 LevelHistoryDBQueryExecutor
    NewHistoryQueryExecutor(blockStore blkstorage.BlockStore) (ledger.HistoryQueryExecutor, error)
    //提交Block入historyDB
    Commit(block *common.Block) error
    //獲取savePointKey,即version.Height
    GetLastSavepoint() (*version.Height, error)
    //是否應該恢復,比較lastAvailableBlock和Savepoint
    ShouldRecover(lastAvailableBlock uint64) (bool, uint64, error)
    //提交丟失的塊
    CommitLostBlock(block *common.Block) error
}
//程式碼在core/ledger/kvledger/history/historydb/historydb.go
```

補充ledger.HistoryQueryExecutor介面定義:執行歷史記錄查詢。

```go
type HistoryQueryExecutor interface {
    GetHistoryForKey(namespace string, key string) (commonledger.ResultsIterator, error) //按key查歷史記錄
}
//程式碼在core/ledger/ledger_interface.go
```

## 3、historydb工具函式

```go
//構造複合HistoryKey,ns 0x00 key 0x00 blocknum trannum
func ConstructCompositeHistoryKey(ns string, key string, blocknum uint64, trannum uint64) []byte
//構造部分複合HistoryKey,ns 0x00 key 0x00 0xff
func ConstructPartialCompositeHistoryKey(ns string, key string, endkey bool) []byte
//按分隔符separator,分割bytesToSplit
func SplitCompositeHistoryKey(bytesToSplit []byte, separator []byte) ([]byte, []byte)
//程式碼在core/ledger/kvledger/history/historydb/histmgr_helper.go
```

## 4、HistoryDB介面實現

HistoryDB介面實現,即historyleveldb.historyDB結構體及方法。historyDB結構體定義如下:

```go
type historyDB struct {
    db *leveldbhelper.DBHandle //leveldb
    dbName string //dbName
}
//程式碼在core/ledger/kvledger/history/historydb/historyleveldb/historyleveldb.go
```

涉及方法如下:

```go
//構造historyDB
func newHistoryDB(db *leveldbhelper.DBHandle, dbName string) *historyDB
//do nothing
func (historyDB *historyDB) Open() error
//do nothing
func (historyDB *historyDB) Close()
//提交Block入historyDB,將讀寫集中寫集入庫,並更新savePointKey
func (historyDB *historyDB) Commit(block *common.Block) error
//構造 LevelHistoryDBQueryExecutor
func (historyDB *historyDB) NewHistoryQueryExecutor(blockStore blkstorage.BlockStore) (ledger.HistoryQueryExecutor, error)
獲取savePointKey,即version.Height
func (historyDB *historyDB) GetLastSavepoint() (*version.Height, error)
//是否應該恢復,比較lastAvailableBlock和Savepoint
func (historyDB *historyDB) ShouldRecover(lastAvailableBlock uint64) (bool, uint64, error)
//提交丟失的塊,即呼叫historyDB.Commit(block)
func (historyDB *historyDB) CommitLostBlock(block *common.Block) error
//程式碼在core/ledger/kvledger/history/historydb/historyleveldb/historyleveldb.go
```

func (historyDB *historyDB) Commit(block *common.Block) error程式碼如下:

```go
blockNo := block.Header.Number //區塊編號
var tranNo uint64 //交易編號,初始化值為0
dbBatch := leveldbhelper.NewUpdateBatch() //leveldb批量更新

//交易驗證程式碼,type TxValidationFlags []uint8
//交易篩選器
txsFilter := util.TxValidationFlags(block.Metadata.Metadata[common.BlockMetadataIndex_TRANSACTIONS_FILTER])
if len(txsFilter) == 0 {
    txsFilter = util.NewTxValidationFlags(len(block.Data.Data))
    block.Metadata.Metadata[common.BlockMetadataIndex_TRANSACTIONS_FILTER] = txsFilter
}
for _, envBytes := range block.Data.Data {
    if txsFilter.IsInvalid(int(tranNo)) { //檢查指定的交易是否有效
        tranNo++
        continue
    }
    //[]byte反序列化為Envelope
    env, err := putils.GetEnvelopeFromBlock(envBytes)
    payload, err := putils.GetPayload(env) //e.Payload反序列化為Payload
    //[]byte反序列化為ChannelHeader
    chdr, err := putils.UnmarshalChannelHeader(payload.Header.ChannelHeader)

    if common.HeaderType(chdr.Type) == common.HeaderType_ENDORSER_TRANSACTION { //背書交易,type HeaderType int32
        respPayload, err := putils.GetActionFromEnvelope(envBytes) //獲取ChaincodeAction
        txRWSet := &rwsetutil.TxRwSet{}
        err = txRWSet.FromProtoBytes(respPayload.Results) //[]byte反序列化後構造NsRwSet,加入txRWSet.NsRwSets
        for _, nsRWSet := range txRWSet.NsRwSets {
            ns := nsRWSet.NameSpace
            for _, kvWrite := range nsRWSet.KvRwSet.Writes {
                writeKey := kvWrite.Key
                //txRWSet中寫集入庫
                compositeHistoryKey := historydb.ConstructCompositeHistoryKey(ns, writeKey, blockNo, tranNo)
                dbBatch.Put(compositeHistoryKey, emptyValue)
            }
        }
    } else {
        logger.Debugf("Skipping transaction [%d] since it is not an endorsement transaction\n", tranNo)
    }
    tranNo++
}

height := version.NewHeight(blockNo, tranNo)
dbBatch.Put(savePointKey, height.ToBytes())
err := historyDB.db.WriteBatch(dbBatch, true)
//程式碼在core/ledger/kvledger/history/historydb/historyleveldb/historyleveldb.go
```

Tx(Transaction 交易)相關更詳細內容,參考:[Fabric 1.0原始碼筆記 之 Tx(Transaction 交易)](../tx/README.md)

## 5、HistoryDBProvider介面實現

HistoryDBProvider介面實現,即historyleveldb.HistoryDBProvider結構體和方法。

```go
type HistoryDBProvider struct {
    dbProvider *leveldbhelper.Provider
}

//構造HistoryDBProvider
func NewHistoryDBProvider() *HistoryDBProvider
//獲取HistoryDB
func (provider *HistoryDBProvider) GetDBHandle(dbName string) (historydb.HistoryDB, error)
//關閉所有HistoryDB控制程式碼,調取provider.dbProvider.Close()
func (provider *HistoryDBProvider) Close()
//程式碼在core/ledger/kvledger/history/historydb/historyleveldb/historyleveldb.go
```

## 6、LevelHistoryDBQueryExecutor和historyScanner結構體及方法

LevelHistoryDBQueryExecutor結構體及方法:實現ledger.HistoryQueryExecutor介面。

```go
type LevelHistoryDBQueryExecutor struct {
    historyDB *historyDB
    blockStore blkstorage.BlockStore //用於傳遞給historyScanner
}
//按key查historyDB,呼叫q.historyDB.db.GetIterator(compositeStartKey, compositeEndKey)
func (q *LevelHistoryDBQueryExecutor) GetHistoryForKey(namespace string, key string) (commonledger.ResultsIterator, error)
//程式碼在core/ledger/kvledger/history/historydb/historyleveldb/historyleveldb_query_executer.go
```

historyScanner結構體及方法:實現ledger.ResultsIterator介面。

```go
type historyScanner struct {
    compositePartialKey []byte //ns 0x00 key 0x00
    namespace string
    key string
    dbItr iterator.Iterator //leveldb迭代器
    blockStore blkstorage.BlockStore
}

//構造historyScanner
func newHistoryScanner(compositePartialKey []byte, namespace string, key string, dbItr iterator.Iterator, blockStore blkstorage.BlockStore) *historyScanner
//按迭代器中key取blockNum和tranNum,再按blockNum和tranNum從blockStore中取Envelope,然後從Envelope的txRWSet.NsRwSets中按key查詢並構造queryresult.KeyModification
func (scanner *historyScanner) Next() (commonledger.QueryResult, error)
func (scanner *historyScanner) Close() //scanner.dbItr.Release()
從Envelope的txRWSet.NsRwSets中按key查詢並構造queryresult.KeyModification
func getKeyModificationFromTran(tranEnvelope *common.Envelope, namespace string, key string) (commonledger.QueryResult, error)
//程式碼在core/ledger/kvledger/history/historydb/historyleveldb/historyleveldb_query_executer.go
```

補充queryresult.KeyModification:

```go
type KeyModification struct {
    TxId string //交易ID,ChannelHeader.TxId
    Value []byte //讀寫集中Value,KVWrite.Value
    Timestamp *google_protobuf.Timestamp //ChannelHeader.Timestamp
    IsDelete bool //KVWrite.IsDelete
}
//程式碼在protos/ledger/queryresult/kv_query_result.pb.go
```






網址:http://www.qukuailianxueyuan.io/



欲領取造幣技術與全套虛擬機器資料

區塊鏈技術交流QQ群:756146052  備註:CSDN

尹成學院微信:備註:CSDN





網址:http://www.qukuailianxueyuan.io/



欲領取造幣技術與全套虛擬機器資料

區塊鏈技術交流QQ群:756146052  備註:CSDN

尹成學院微信:備註:CSDN

相關文章