1. 程式人生 > >43.Fabric 1.0原始碼分析(43) Tx(Transaction 交易)

43.Fabric 1.0原始碼分析(43) Tx(Transaction 交易)

Fabric 1.0原始碼筆記 之 Tx(Transaction 交易)

1、Tx概述

Tx,即Transaction,交易或事務。

Tx程式碼分佈目錄結構如下:

  • protos/common/common.pb.go,交易的封裝即Envelope結構體。也包括Payload、Header、ChannelHeader和SignatureHeader。
  • protos/utils目錄,交易相關部分工具函式,包括txutils.go、proputils.go和commonutils.go。
  • core/ledger/kvledger/txmgmt目錄
    • rwsetutil目錄,讀寫集相關結構體及方法。
    • version目錄,version.Height結構體及方法。
    • validator目錄,Validator介面及實現。
    • txmgr目錄,TxMgr介面及實現。

2、交易的封裝Envelope結構體

2.1、Envelope結構體

Envelope直譯為信封,封裝Payload和Signature。

type Envelope struct { //用簽名包裝Payload,以便對資訊做身份驗證
    Payload []byte //Payload序列化
    Signature []byte //Payload header中指定的建立者簽名
}
//程式碼在protos/common/common.pb.go

2.2、Payload相關結構體

Payload直譯為有效載荷。Payload結構體:

type Payload struct {
    Header *Header //Header
    Data []byte //Transaction序列化
}
//程式碼在protos/common/common.pb.go

Header結構體:

type Header struct {
    ChannelHeader   []byte
    SignatureHeader []byte
}
//程式碼在protos/common/common.pb.go

ChannelHeader結構體:

type ChannelHeader struct {
    Type int32
    Version int32
//訊息協議版本 Timestamp *google_protobuf.Timestamp //建立訊息時的本地時間 ChannelId string //訊息繫結的ChannelId TxId string //TxId Epoch uint64 //紀元 Extension []byte //可附加的擴充套件 } //程式碼在protos/common/common.pb.go

補充HeaderType:

type HeaderType int32

const (
    HeaderType_MESSAGE              HeaderType = 0
    HeaderType_CONFIG               HeaderType = 1
    HeaderType_CONFIG_UPDATE        HeaderType = 2
    HeaderType_ENDORSER_TRANSACTION HeaderType = 3
    HeaderType_ORDERER_TRANSACTION  HeaderType = 4
    HeaderType_DELIVER_SEEK_INFO    HeaderType = 5
    HeaderType_CHAINCODE_PACKAGE    HeaderType = 6
)
//程式碼在protos/common/common.pb.go

SignatureHeader結構體:

type SignatureHeader struct {
    Creator []byte //訊息的建立者, 指定為證書鏈
    Nonce []byte //可能只使用一次的任意數字,可用於檢測重播攻擊
}
//程式碼在protos/common/common.pb.go

2.3、Transaction相關結構體

Transaction結構體:

type Transaction struct {
    Actions []*TransactionAction //Payload.Data是個TransactionAction陣列,容納每個交易
}
//程式碼在protos/peer/transaction.pb.go

TransactionAction結構體:

type TransactionAction struct {
    Header []byte
    Payload []byte
}
//程式碼在protos/peer/transaction.pb.go

2.4、ChaincodeActionPayload相關結構體

ChaincodeActionPayload結構體:

type ChaincodeActionPayload struct {
    ChaincodeProposalPayload []byte
    Action *ChaincodeEndorsedAction
}
//程式碼在protos/peer/transaction.pb.go

ChaincodeEndorsedAction結構體:

type ChaincodeEndorsedAction struct {
    ProposalResponsePayload []byte //ProposalResponsePayload序列化
    Endorsements []*Endorsement
}
//程式碼在protos/peer/transaction.pb.go

ProposalResponsePayload結構體:

type ProposalResponsePayload struct {
    ProposalHash []byte
    Extension []byte //ChaincodeAction序列化
}
//程式碼在protos/peer/proposal_response.pb.go

ChaincodeAction結構體:

type ChaincodeAction struct {
    Results []byte //TxRwSet序列化
    Events []byte
    Response *Response
    ChaincodeId *ChaincodeID
}
//程式碼在protos/peer/proposal.pb.go

3、交易驗證程式碼TxValidationFlags

TxValidationFlags是交易驗證程式碼的陣列,在commiter驗證塊時使用。

type TxValidationFlags []uint8

//建立TxValidationFlags陣列
func NewTxValidationFlags(size int) TxValidationFlags
//為指定的交易設定交易驗證程式碼
func (obj TxValidationFlags) SetFlag(txIndex int, flag peer.TxValidationCode) 
//獲取指定交易的交易驗證程式碼
func (obj TxValidationFlags) Flag(txIndex int) peer.TxValidationCode 
//檢查指定的交易是否有效
func (obj TxValidationFlags) IsValid(txIndex int) bool
//檢查指定的交易是否無效
func (obj TxValidationFlags) IsInvalid(txIndex int) bool
//指定交易的交易驗證程式碼與flag比較,相同為true
func (obj TxValidationFlags) IsSetTo(txIndex int, flag peer.TxValidationCode) bool
//程式碼在core/ledger/util/txvalidationflags.go

補充peer.TxValidationCode:

type TxValidationCode int32

const (
    TxValidationCode_VALID                        TxValidationCode = 0
    TxValidationCode_NIL_ENVELOPE                 TxValidationCode = 1
    TxValidationCode_BAD_PAYLOAD                  TxValidationCode = 2
    TxValidationCode_BAD_COMMON_HEADER            TxValidationCode = 3
    TxValidationCode_BAD_CREATOR_SIGNATURE        TxValidationCode = 4
    TxValidationCode_INVALID_ENDORSER_TRANSACTION TxValidationCode = 5
    TxValidationCode_INVALID_CONFIG_TRANSACTION   TxValidationCode = 6
    TxValidationCode_UNSUPPORTED_TX_PAYLOAD       TxValidationCode = 7
    TxValidationCode_BAD_PROPOSAL_TXID            TxValidationCode = 8
    TxValidationCode_DUPLICATE_TXID               TxValidationCode = 9
    TxValidationCode_ENDORSEMENT_POLICY_FAILURE   TxValidationCode = 10
    TxValidationCode_MVCC_READ_CONFLICT           TxValidationCode = 11
    TxValidationCode_PHANTOM_READ_CONFLICT        TxValidationCode = 12
    TxValidationCode_UNKNOWN_TX_TYPE              TxValidationCode = 13
    TxValidationCode_TARGET_CHAIN_NOT_FOUND       TxValidationCode = 14
    TxValidationCode_MARSHAL_TX_ERROR             TxValidationCode = 15
    TxValidationCode_NIL_TXACTION                 TxValidationCode = 16
    TxValidationCode_EXPIRED_CHAINCODE            TxValidationCode = 17
    TxValidationCode_CHAINCODE_VERSION_CONFLICT   TxValidationCode = 18
    TxValidationCode_BAD_HEADER_EXTENSION         TxValidationCode = 19
    TxValidationCode_BAD_CHANNEL_HEADER           TxValidationCode = 20
    TxValidationCode_BAD_RESPONSE_PAYLOAD         TxValidationCode = 21
    TxValidationCode_BAD_RWSET                    TxValidationCode = 22
    TxValidationCode_ILLEGAL_WRITESET             TxValidationCode = 23
    TxValidationCode_INVALID_OTHER_REASON         TxValidationCode = 255
)
//程式碼在protos/peer/transaction.pb.go

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