兄弟連區塊鏈教程Fabric1.0源代碼分析Tx(Transaction 交易)二 原

4、交易相關部分工具函數(protos/utils包)

putils更詳細內容,參考:Fabric 1.0源代碼筆記 之 putils(protos/utils工具包)

5、RWSet(讀寫集)

RWSet更詳細內容,參考:Fabric 1.0源代碼筆記 之 Tx #RWSet(讀寫集)

6、version.Height結構體及方法

type Height struct {
    BlockNum uint64 //區塊編號
    TxNum    uint64 //交易編號
}

func NewHeight(blockNum, txNum uint64) *Height //構造Height
func NewHeightFromBytes(b []byte) (*Height, int) //[]byte反序列化構造Height
func (h *Height) ToBytes() []byte //Height序列化
func (h *Height) Compare(h1 *Height) int //比較兩個Height
func AreSame(h1 *Height, h2 *Height) bool //比較兩個Height是否相等
//代碼在core/ledger/kvledger/txmgmt/version/version.go

7、Validator接口及實現(驗證讀寫集)

7.1、Validator接口定義

type Validator interface {
    //驗證和準備批處理
    ValidateAndPrepareBatch(block *common.Block, doMVCCValidation bool) (*statedb.UpdateBatch, error)
}
//代碼在core/ledger/kvledger/txmgmt/validator/validator.go

7.2、Validator接口實現

Validator接口實現,即statebasedval.Validator結構體及方法。Validator結構體定義如下:

type Validator struct {
    db statedb.VersionedDB //statedb
}
//代碼在core/ledger/kvledger/txmgmt/validator/statebasedval/state_based_validator.go

涉及方法如下:

//構造Validator
func NewValidator(db statedb.VersionedDB) *Validator
//驗證背書交易
func (v *Validator) validateEndorserTX(envBytes []byte, doMVCCValidation bool, updates *statedb.UpdateBatch) (*rwsetutil.TxRwSet, peer.TxValidationCode, error)
//驗證和準備批處理,Block中寫集加入批處理
func (v *Validator) ValidateAndPrepareBatch(block *common.Block, doMVCCValidation bool) (*statedb.UpdateBatch, error)
func addWriteSetToBatch(txRWSet *rwsetutil.TxRwSet, txHeight *version.Height, batch *statedb.UpdateBatch)
func (v *Validator) validateTx(txRWSet *rwsetutil.TxRwSet, updates *statedb.UpdateBatch) (peer.TxValidationCode, error)
func (v *Validator) validateReadSet(ns string, kvReads []*kvrwset.KVRead, updates *statedb.UpdateBatch) (bool, error)
func (v *Validator) validateKVRead(ns string, kvRead *kvrwset.KVRead, updates *statedb.UpdateBatch) (bool, error)
func (v *Validator) validateRangeQueries(ns string, rangeQueriesInfo []*kvrwset.RangeQueryInfo, updates *statedb.UpdateBatch) (bool, error)
func (v *Validator) validateRangeQuery(ns string, rangeQueryInfo *kvrwset.RangeQueryInfo, updates *statedb.UpdateBatch) (bool, error)
//代碼在core/ledger/kvledger/txmgmt/validator/statebasedval/state_based_validator.go

func (v Validator) ValidateAndPrepareBatch(block common.Block, doMVCCValidation bool) (*statedb.UpdateBatch, error)代碼如下:

updates := statedb.NewUpdateBatch() //構造批處理
//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 txIndex, envBytes := range block.Data.Data {
    if txsFilter.IsInvalid(txIndex) { //驗證交易是否有效
        continue
    }
    env, err := putils.GetEnvelopeFromBlock(envBytes) //Envelope
    payload, err := putils.GetPayload(env) //Payload
    chdr, err := putils.UnmarshalChannelHeader(payload.Header.ChannelHeader) //ChannelHeader
    txType := common.HeaderType(chdr.Type) //HeaderType

    if txType != common.HeaderType_ENDORSER_TRANSACTION {
            continue
    }
    //驗證背書交易,獲取讀寫集
    txRWSet, txResult, err := v.validateEndorserTX(envBytes, doMVCCValidation, updates)
    txsFilter.SetFlag(txIndex, txResult)
    if txRWSet != nil {
        committingTxHeight := version.NewHeight(block.Header.Number, uint64(txIndex))
        //讀寫集中寫集加入批處理
        addWriteSetToBatch(txRWSet, committingTxHeight, updates)
        txsFilter.SetFlag(txIndex, peer.TxValidationCode_VALID)
    }
}
block.Metadata.Metadata[common.BlockMetadataIndex_TRANSACTIONS_FILTER] = txsFilter
return updates, nil

//代碼在core/ledger/kvledger/txmgmt/validator/statebasedval/state_based_validator.go

8、TxMgr接口及實現(交易管理)

8.1、TxMgr接口定義

type TxMgr interface {
    NewQueryExecutor() (ledger.QueryExecutor, error)
    NewTxSimulator() (ledger.TxSimulator, error)
    ValidateAndPrepare(block *common.Block, doMVCCValidation bool) error
    //返回statedb一致的最高事務的高度
    GetLastSavepoint() (*version.Height, error)
    ShouldRecover(lastAvailableBlock uint64) (bool, uint64, error)
    CommitLostBlock(block *common.Block) error
    Commit() error
    Rollback()
    Shutdown()
}
//代碼在core/ledger/kvledger/txmgmt/txmgr/txmgr.go

8.2、TxMgr接口實現

TxMgr接口實現,即LockBasedTxMgr結構體及方法。LockBasedTxMgr結構體如下:

type LockBasedTxMgr struct {
    db           statedb.VersionedDB //statedb
    validator    validator.Validator //Validator
    batch        *statedb.UpdateBatch //批處理
    currentBlock *common.Block //Block
    commitRWLock sync.RWMutex //鎖
}
//代碼在core/ledger/kvledger/txmgmt/txmgr/lockbasedtxmgr/lockbased_txmgr.go

涉及方法如下:

//構造LockBasedTxMgr
func NewLockBasedTxMgr(db statedb.VersionedDB) *LockBasedTxMgr
//調取txmgr.db.GetLatestSavePoint(),返回statedb一致的最高事務的高度
func (txmgr *LockBasedTxMgr) GetLastSavepoint() (*version.Height, error)
//調取newQueryExecutor(txmgr)
func (txmgr *LockBasedTxMgr) NewQueryExecutor() (ledger.QueryExecutor, error)
func (txmgr *LockBasedTxMgr) NewTxSimulator() (ledger.TxSimulator, error)
//驗證Block,並從Block中獲取寫集加入批處理txmgr.batch
func (txmgr *LockBasedTxMgr) ValidateAndPrepare(block *common.Block, doMVCCValidation bool) error
func (txmgr *LockBasedTxMgr) Shutdown()
//執行txmgr.batch,
func (txmgr *LockBasedTxMgr) Commit() error
func (txmgr *LockBasedTxMgr) Rollback()
func (txmgr *LockBasedTxMgr) ShouldRecover(lastAvailableBlock uint64) (bool, uint64, error)
func (txmgr *LockBasedTxMgr) CommitLostBlock(block *common.Block) error
//代碼在core/ledger/kvledger/txmgmt/txmgr/lockbasedtxmgr/lockbased_txmgr.go

8.3、lockBasedQueryExecutor結構體及方法(實現ledger.QueryExecutor接口)

type lockBasedQueryExecutor struct {
    helper *queryHelper
    id     string
}

func newQueryExecutor(txmgr *LockBasedTxMgr) *lockBasedQueryExecutor 
func (q *lockBasedQueryExecutor) GetState(ns string, key string) ([]byte, error)
func (q *lockBasedQueryExecutor) GetStateMultipleKeys(namespace string, keys []string) ([][]byte, error)
func (q *lockBasedQueryExecutor) GetStateRangeScanIterator(namespace string, startKey string, endKey string) (ledger.ResultsIterator, error)
func (q *lockBasedQueryExecutor) ExecuteQuery(namespace, query string) (ledger.ResultsIterator, error)
func (q *lockBasedQueryExecutor) Done()
//代碼在core/ledger/kvledger/txmgmt/txmgr/lockbasedtxmgr/lockbased_query_executer.go

8.4、queryHelper結構體及方法

queryHelper結構體及方法:

type queryHelper struct {
    txmgr        *LockBasedTxMgr //LockBasedTxMgr
    rwsetBuilder *rwsetutil.RWSetBuilder //讀寫集工具
    itrs         []*resultsItr
    err          error
    doneInvoked  bool //是否調用完成
}

//statedb中獲取versionedValue,並加入rwsetBuilder的nsRWs.readMap中
func (h *queryHelper) getState(ns string, key string) ([]byte, error)
//statedb中獲取多個versionedValue,並加入rwsetBuilder的nsRWs.readMap中
func (h *queryHelper) getStateMultipleKeys(namespace string, keys []string) ([][]byte, error)
//構造resultsItr,並加入queryHelper.itrs
func (h *queryHelper) getStateRangeScanIterator(namespace string, startKey string, endKey string) (commonledger.ResultsIterator, error)
執行查詢並構造queryResultsItr
func (h *queryHelper) executeQuery(namespace, query string) (commonledger.ResultsIterator, error)
//done
func (h *queryHelper) done()
func (h *queryHelper) checkDone()
//代碼在core/ledger/kvledger/txmgmt/txmgr/lockbasedtxmgr/helper.go

resultsItr結構體及方法:

type resultsItr struct {
    ns                      string
    endKey                  string
    dbItr                   statedb.ResultsIterator
    rwSetBuilder            *rwsetutil.RWSetBuilder
    rangeQueryInfo          *kvrwset.RangeQueryInfo
    rangeQueryResultsHelper *rwsetutil.RangeQueryResultsHelper
}

func newResultsItr(ns string, startKey string, endKey string, db statedb.VersionedDB, rwsetBuilder *rwsetutil.RWSetBuilder, enableHashing bool, maxDegree uint32) (*resultsItr, error)
func (itr *resultsItr) Next() (commonledger.QueryResult, error)
func (itr *resultsItr) updateRangeQueryInfo(queryResult statedb.QueryResult)
func (itr *resultsItr) Close()
//代碼在core/ledger/kvledger/txmgmt/txmgr/lockbasedtxmgr/helper.go
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章