# Fabric 1.1源代碼分析(3) 系統(tǒng)鏈碼執(zhí)行過程
## 1、系統(tǒng)鏈碼執(zhí)行過程
* 以peer channel join -b gensis.block命令為例。該命令結(jié)果是peer節(jié)點(diǎn)加入通道.
這個命令會單獨(dú)啟一個進(jìn)程.在該進(jìn)程中會構(gòu)建一個名稱為cscc的鏈碼消息傳到peer節(jié)點(diǎn).
通過grpc調(diào)用最終會進(jìn)到endorser.go中的ProcessProposal函數(shù)進(jìn)行處理。
參考Fabric 1.1源代碼分析(2)http://www.aygfsteel.com/fool/archive/2018/06/12/433277.html
系統(tǒng)鏈碼初始化過程,可以找到../shim/handler.go中
的handleTransaction()函數(shù).最終會調(diào)用res := handler.cc.Invoke(stub).這里的
cc就是importsysccs.go文件中systemChaincodes數(shù)組中的cscc系統(tǒng)鏈碼的.
Chaincode,其實(shí)例是&cscc.PeerConfiger{},實(shí)現(xiàn)在cscc/configure.go文件中。每個系統(tǒng)
鏈碼都實(shí)現(xiàn)了這個Chaincode接口()
```go
type Chaincode interface {
// Init is called during Instantiate transaction after the chaincode container
// has been established for the first time, allowing the chaincode to
// initialize its internal data
Init(stub ChaincodeStubInterface) pb.Response
// Invoke is called to update or query the ledger in a proposal transaction.
// Updated state variables are not committed to the ledger until the
// transaction is committed.
Invoke(stub ChaincodeStubInterface) pb.Response
}
```
* 至此就可以清晰地看到每一個系統(tǒng)鏈碼都會啟動一對協(xié)程,通過chan通信。系統(tǒng)鏈碼消息由
shim/handler.go中的函數(shù)處理.并且這里最終調(diào)用各自的具體實(shí)現(xiàn)的Ivoke方法進(jìn)行業(yè)務(wù)處理
## 2、peer channel join命令處理流程圖
* peer channel join命令會調(diào)用configure.go中的Excute方法。對應(yīng)cscc系統(tǒng)鏈碼的處理,
原因如下,以下流程圖大致可以了解cscc都做了些什么


## 3、小結(jié)
* 上面的流程圖也不是非常地強(qiáng)細(xì),忽略掉了一些方法。但是有了整個流程的理解,就能理解其
它系統(tǒng)鏈碼的調(diào)用過程,需要時直接細(xì)讀其實(shí)現(xiàn)就好了。從上流程圖中可以看到文件末尾添加區(qū)
區(qū)塊,leveldb中記錄了區(qū)塊號,索引位置信息等等。另外因為系統(tǒng)鏈碼跟一般鏈碼雖然經(jīng)過
的文件基本一樣,但最終處理方式還是不一樣,一個是協(xié)程,一個是grpc.可能參考Fabric 1.1
源代碼分析之 Chaincode(鏈碼)初始化 http://www.aygfsteel.com/fool/archive/2018/06/12/433275.html