Fabric 1.0原始碼分析(44)Tx #RWSet(讀寫集)

尹成發表於2018-05-21
# Fabric 1.0原始碼筆記 之 Tx #RWSet(讀寫集)

## 1、RWSet概述

在背書節點模擬Transaction期間,為交易準備了一個讀寫集合。
Read Set包含模擬Transaction讀取的Key和版本的列表,Write Set包含Key、寫入的新值、以及刪除標記(是否刪除Key)。

RWSet相關程式碼分佈在protos/ledger/rwset、core/ledger/kvledger/txmgmt/rwsetutil目錄下。目錄結構如下:

* protos/ledger/rwset目錄:
    * rwset.pb.go,TxReadWriteSet和NsReadWriteSet結構體定義。
    * kv_rwset.pb.go,KVRWSet、KVRead、KVWrite、Version、RangeQueryInfo結構體定義,以及isRangeQueryInfo_ReadsInfo介面定義。
* core/ledger/kvledger/txmgmt/rwsetutil目錄:
    * rwset_proto_util.go,TxRwSet和NsRwSet結構體及方法。
    * rwset_builder.go,RWSetBuilder結構體及方法。
    * query_results_helper.go,RangeQueryResultsHelper結構體及方法。

## 2、TxReadWriteSet結構體(protos)



TxReadWriteSet結構體:

```go
type TxReadWriteSet_DataModel int32
const (
    TxReadWriteSet_KV TxReadWriteSet_DataModel = 0
)

type TxReadWriteSet struct {
    DataModel TxReadWriteSet_DataModel
    NsRwset []*NsReadWriteSet
}

type NsReadWriteSet struct {
    Namespace string
    Rwset []byte //KVRWSet 序列化
}
//程式碼在protos/ledger/rwset/rwset.pb.go
```

KVRWSet結構體:

```go
type KVRWSet struct {
    Reads []*KVRead
    RangeQueriesInfo []*RangeQueryInfo
    Writes []*KVWrite
}

type KVRead struct {
    Key string
    Version *Version
}

type KVWrite struct {
    Key string
    IsDelete bool
    Value []byte
}

type Version struct {
    BlockNum uint64
    TxNum uint64
}

type RangeQueryInfo struct {
    StartKey string
    EndKey string
    ItrExhausted bool
    ReadsInfo isRangeQueryInfo_ReadsInfo
}
程式碼在protos/ledger/rwset/kvrwset/kv_rwset.pb.go
```

## 3、TxRwSet結構體及方法(core)



```go
type TxRwSet struct {
    NsRwSets []*NsRwSet
}

type NsRwSet struct {
    NameSpace string
    KvRwSet *kvrwset.KVRWSet
}

//按TxRwSet構造TxReadWriteSet並序列化
func (txRwSet *TxRwSet) ToProtoBytes() ([]byte, error)
//[]byte反序列化為TxReadWriteSet,按TxReadWriteSet構造TxRwSet
func (txRwSet *TxRwSet) FromProtoBytes(protoBytes []byte) error
//構造KVRead
func NewKVRead(key string, version *version.Height) *kvrwset.KVRead
//構造version.Height
func NewVersion(protoVersion *kvrwset.Version) *version.Height
//構造Version
func newProtoVersion(height *version.Height) *kvrwset.Version
//構造KVWrite
func newKVWrite(key string, value []byte) *kvrwset.KVWrite
//程式碼在core/ledger/kvledger/txmgmt/rwsetutil/rwset_proto_util.go
```

## 4、RWSetBuilder結構體及方法

RWSetBuilder結構體定義:

```go
type RWSetBuilder struct {
    rwMap map[string]*nsRWs //Namespace
}

type nsRWs struct {
    readMap map[string]*kvrwset.KVRead //KVRead map
    writeMap map[string]*kvrwset.KVWrite //KVWrite map
    rangeQueriesMap map[rangeQueryKey]*kvrwset.RangeQueryInfo //RangeQueryInfo map
    rangeQueriesKeys []rangeQueryKey //rangeQueryKey陣列
}

type rangeQueryKey struct {
    startKey string
    endKey string
    itrExhausted bool //迭代器是否用盡
}
//程式碼在core/ledger/kvledger/txmgmt/rwsetutil/rwset_builder.go
```

涉及方法如下:

```go
//構造nsRWs
func newNsRWs() *nsRWs
//構造RWSetBuilder
func NewRWSetBuilder() *RWSetBuilder
//構造KVRead,並加入nsRWs.readMap
func (rws *RWSetBuilder) AddToReadSet(ns string, key string, version *version.Height)
//構造KVWrite,並加入nsRWs.writeMap
func (rws *RWSetBuilder) AddToWriteSet(ns string, key string, value []byte)
//用RangeQueryInfo構造rangeQueryKey,並將rangeQueryKey和RangeQueryInfo加入nsRWs.rangeQueriesKeys和nsRWs.rangeQueriesMap
func (rws *RWSetBuilder) AddToRangeQuerySet(ns string, rqi *kvrwset.RangeQueryInfo)
從RWSetBuilder構造TxRwSet
func (rws *RWSetBuilder) GetTxReadWriteSet() *TxRwSet
//獲取或建立nsRWs
func (rws *RWSetBuilder) getOrCreateNsRW(ns string) *nsRWs
//程式碼在core/ledger/kvledger/txmgmt/rwsetutil/rwset_builder.go
```

## 5、RangeQueryResultsHelper結構體及方法(**什麼用途呢???**)

### 5.1、merkleTree(默克爾樹)

Merkle Tree,也稱Hash Tree,即儲存hash值的一棵樹。
Merkle樹的葉子是資料塊的hash值,非葉節點是其對應子節點串聯字串的hash。

```go
type MerkleTreeLevel uint32
type Hash []byte
const leafLevel = MerkleTreeLevel(1)

type merkleTree struct {
    tree map[MerkleTreeLevel][]Hash
    maxLevel MerkleTreeLevel //1
    maxDegree uint32 //單層最大個數
}
//程式碼在core/ledger/kvledger/txmgmt/rwsetutil/query_results_helper.go
```

涉及方法如下:

```go
//構造merkleTree
func newMerkleTree(maxDegree uint32) (*merkleTree, error)
//加入雜湊,從第一層開始加,直至當前層雜湊數量沒有超過限額為止
func (m *merkleTree) update(nextLeafLevelHash Hash) error
func (m *merkleTree) done() error
func (m *merkleTree) getSummery() *kvrwset.QueryReadsMerkleSummary //構造QueryReadsMerkleSummary
func (m *merkleTree) getMaxLevel() MerkleTreeLevel //獲取maxLevel
func (m *merkleTree) getMaxLevelHashes() []Hash //maxLevel層雜湊陣列
func (m *merkleTree) isEmpty() bool //maxLevel為1且第1層雜湊陣列為空
func (m *merkleTree) String() string //String
//計算雜湊陣列合並後雜湊
func computeCombinedHash(hashes []Hash) (Hash, error)
//[]Hash轉換為[][]byte
func hashesToBytes(hashes []Hash) [][]byte
//程式碼在core/ledger/kvledger/txmgmt/rwsetutil/query_results_helper.go
```

func (m *merkleTree) update(nextLeafLevelHash Hash) error程式碼如下:

```go
//const leafLevel = MerkleTreeLevel(1)
m.tree[leafLevel] = append(m.tree[leafLevel], nextLeafLevelHash) //先加入第一層
currentLevel := leafLevel
for {
    currentLevelHashes := m.tree[currentLevel]
    //如果當前層數量沒有超過限額,則返回,即Hash加到沒有數量超出限額的層為止
    if uint32(len(currentLevelHashes)) <= m.maxDegree {
        return nil
    }
    //如果當前層數量超過限額
    nextLevelHash, err := computeCombinedHash(currentLevelHashes) //計算當前層合併雜湊
    delete(m.tree, currentLevel) //刪除當前層
    nextLevel := currentLevel + 1 //進入更高一層
    m.tree[nextLevel] = append(m.tree[nextLevel], nextLevelHash) //合併雜湊加入更高一層
    if nextLevel > m.maxLevel { //如果更高一層層高超過原定最大高度,則重置最大高度為更高一層層高
        m.maxLevel = nextLevel
    }
    currentLevel = nextLevel //更新當前層為更高一層
}
//程式碼在core/ledger/kvledger/txmgmt/rwsetutil/query_results_helper.go
```

func (m *merkleTree) done() error程式碼如下:

```go
currentLevel := leafLevel //從第一層開始
var h Hash
var err error
for currentLevel < m.maxLevel { //迴圈至最高層
    currentLevelHashes := m.tree[currentLevel]
    switch len(currentLevelHashes) {
    case 0: //當前層數量為0,進入更高一層
        currentLevel++
        continue
    case 1: //當前層數量為1,獲取當前值
        h = currentLevelHashes[0]
    default: //當前層數量1個,計算合併雜湊
        h, err = computeCombinedHash(currentLevelHashes)
    }
    delete(m.tree, currentLevel) //刪除當前層
    currentLevel++ //進入更高一層
    m.tree[currentLevel] = append(m.tree[currentLevel], h) //前一層合併雜湊加入本層
}

finalHashes := m.tree[m.maxLevel] //最高一層
if uint32(len(finalHashes)) > m.maxDegree { //如果最高一層超出限額
    delete(m.tree, m.maxLevel) //刪除本層
    m.maxLevel++ //進入更高一層
    combinedHash, err := computeCombinedHash(finalHashes) //計算合併雜湊
    m.tree[m.maxLevel] = []Hash{combinedHash} //哈並雜湊加入新的最高層
}
return nil
//程式碼在core/ledger/kvledger/txmgmt/rwsetutil/query_results_helper.go
```

### 5.2 RangeQueryResultsHelper

RangeQueryResultsHelper定義:

```go
type RangeQueryResultsHelper struct {
    pendingResults []*kvrwset.KVRead
    mt *merkleTree //merkleTree
    maxDegree uint32
    hashingEnabled bool
}
//程式碼在core/ledger/kvledger/txmgmt/rwsetutil/query_results_helper.go
```
涉及方法如下:

```go
//構造RangeQueryResultsHelper,如果啟用雜湊將構造merkleTree
func NewRangeQueryResultsHelper(enableHashing bool, maxDegree uint32) (*RangeQueryResultsHelper, error)
//新增KVRead加入pendingResults,如果pendingResults數量超過限額,執行processPendingResults
func (helper *RangeQueryResultsHelper) AddResult(kvRead *kvrwset.KVRead) error
//執行processPendingResults,並執行merkleTree.done()
func (helper *RangeQueryResultsHelper) Done() ([]*kvrwset.KVRead, *kvrwset.QueryReadsMerkleSummary, error)
//獲取QueryReadsMerkleSummary
func (helper *RangeQueryResultsHelper) GetMerkleSummary() *kvrwset.QueryReadsMerkleSummary
//pendingResults序列化並雜湊後,加入merkleTree,並清空pendingResults
func (helper *RangeQueryResultsHelper) processPendingResults() error
//KVRead序列化
func serializeKVReads(kvReads []*kvrwset.KVRead) ([]byte, error)
```

補充 QueryReadsMerkleSummary:

```go
type QueryReadsMerkleSummary struct {
    MaxDegree uint32
    MaxLevel uint32
    MaxLevelHashes [][]byte
}
//程式碼在protos/ledger/rwset/kvrwset/kv_rwset.pb.go
```








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



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

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

尹成學院微信:備註:CSDN








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



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

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

尹成學院微信:備註:CSDN

相關文章