兄弟连区块链教程Fabric1.0源代码分析Tx RWSet(读写集)
兄弟连区块链教程Fabric1.0源代码分析Tx RWSet(读写集),2018年下半年,区块链行业正逐渐褪去发展之初的浮躁、回归理性,表面上看相关人才需求与身价似乎正在回落。但事实上,正是初期泡沫的渐退,让人们更多的关注点放在了区块链真正的技术之上。 # 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) 有个图5
1 / 9
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 ```
2 / 9
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) 有个图6
3 / 9
```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
4 / 9
//代码在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
5 / 9