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
感谢关注兄弟连区块链教程分享!