# Fabric 1.1源代碼分析(3) 系統(tǒng)鏈碼執(zhí)行過(guò)程
## 1、系統(tǒng)鏈碼執(zhí)行過(guò)程
* 以peer channel join -b gensis.block命令為例。該命令結(jié)果是peer節(jié)點(diǎn)加入通道.
這個(gè)命令會(huì)單獨(dú)啟一個(gè)進(jìn)程.在該進(jìn)程中會(huì)構(gòu)建一個(gè)名稱為cscc的鏈碼消息傳到peer節(jié)點(diǎn).
通過(guò)grpc調(diào)用最終會(huì)進(jìn)到endorser.go中的ProcessProposal函數(shù)進(jìn)行處理。
參考Fabric 1.1源代碼分析(2)http://m.tkk7.com/fool/archive/2018/06/12/433277.html
系統(tǒng)鏈碼初始化過(guò)程,可以找到../shim/handler.go中
的handleTransaction()函數(shù).最終會(huì)調(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文件中。每個(gè)系統(tǒng)
鏈碼都實(shí)現(xiàn)了這個(gè)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
}
```
* 至此就可以清晰地看到每一個(gè)系統(tǒng)鏈碼都會(huì)啟動(dòng)一對(duì)協(xié)程,通過(guò)chan通信。系統(tǒng)鏈碼消息由
shim/handler.go中的函數(shù)處理.并且這里最終調(diào)用各自的具體實(shí)現(xiàn)的Ivoke方法進(jìn)行業(yè)務(wù)處理
## 2、peer channel join命令處理流程圖
* peer channel join命令會(huì)調(diào)用configure.go中的Excute方法。對(duì)應(yīng)cscc系統(tǒng)鏈碼的處理,
原因如下,以下流程圖大致可以了解cscc都做了些什么


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