Fabric 1.1源代碼分析(3) 系統鏈碼執行過程示例(弟弟篇)
# Fabric 1.1源代碼分析(3) 系統鏈碼執行過程
## 1、系統鏈碼執行過程
* 以peer channel join -b gensis.block命令為例。該命令結果是peer節點加入通道.
這個命令會單獨啟一個進程.在該進程中會構建一個名稱為cscc的鏈碼消息傳到peer節點.
通過grpc調用最終會進到endorser.go中的ProcessProposal函數進行處理。
參考Fabric 1.1源代碼分析(2)http://www.aygfsteel.com/fool/archive/2018/06/12/433277.html
系統鏈碼初始化過程,可以找到../shim/handler.go中
系統鏈碼初始化過程,可以找到../shim/handler.go中
的handleTransaction()函數.最終會調用res := handler.cc.Invoke(stub).這里的
cc就是importsysccs.go文件中systemChaincodes數組中的cscc系統鏈碼的.
Chaincode,其實例是&cscc.PeerConfiger{},實現在cscc/configure.go文件中。每個系統
鏈碼都實現了這個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
}
```
* 至此就可以清晰地看到每一個系統鏈碼都會啟動一對協程,通過chan通信。系統鏈碼消息由
shim/handler.go中的函數處理.并且這里最終調用各自的具體實現的Ivoke方法進行業務處理
## 2、peer channel join命令處理流程圖
* peer channel join命令會調用configure.go中的Excute方法。對應cscc系統鏈碼的處理,
原因如下,以下流程圖大致可以了解cscc都做了些什么



## 3、小結
* 上面的流程圖也不是非常地強細,忽略掉了一些方法。但是有了整個流程的理解,就能理解其
它系統鏈碼的調用過程,需要時直接細讀其實現就好了。從上流程圖中可以看到文件末尾添加區
區塊,leveldb中記錄了區塊號,索引位置信息等等。另外因為系統鏈碼跟一般鏈碼雖然經過
的文件基本一樣,但最終處理方式還是不一樣,一個是協程,一個是grpc.可能參考Fabric 1.1
源代碼分析之 Chaincode(鏈碼)初始化 http://www.aygfsteel.com/fool/archive/2018/06/12/433275.html
posted on 2018-06-13 14:37 傻 瓜 閱讀(1270) 評論(0) 編輯 收藏 所屬分類: 雜項