Fabric 1.1源代碼分析之 系統鏈碼初始化過程(哥哥篇)
# Fabric 1.1源代碼分析之 Fabric 1.1源代碼分析 系統鏈碼初始化過程
* 鏈碼這一塊的代碼非常的繞。基本上就是一個大循環。限于水平或者其它原因,差露可能難免,各位看官包涵則個...
* 鏈碼這一塊的代碼非常的繞。基本上就是一個大循環。限于水平或者其它原因,差露可能難免,各位看官包涵則個...
## 1、系統鏈碼
* 系統鏈碼跟智能合約鏈碼涉及到的文件差不多,流程也差不多。只是智能合約是grpc,系統鏈碼是chan實現調用.
LSCC Lifecycle system chaincode,處理生命周期請求。我理解的生命周期請求應該指的是一個chaincode的安裝,實例化,升級,
卸載等對其生命周期起關鍵作用的一系列操作請求。
CSCC Configuration system chaincode,處理在peer程序端的channel配置。
QSCC Query system chaincode,提供賬本查詢接口,如獲取塊和交易信息。
ESCC Endorsement system chaincode,通過對交易申請的應答信息進行簽名,來提供背書功能。
VSCC Validation system chaincode,處理交易校驗,包括檢查背書策略和版本在并發時的控制。
## 2、系統鏈碼注冊
* 在/core/chaincode/shim/interfaces_stable.go中實現了下面的接口
```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
}
```
* 在core/scc/sysccapi.go中定義了SystemChaincode結構體,其中定義了 Chaincode接口變量
```go
type SystemChaincode struct {
//Unique name of the system chaincode
Name string
//Path to the system chaincode; currently not used
Path string
//InitArgs initialization arguments to startup the system chaincode
InitArgs [][]byte
// Chaincode is the actual chaincode object
Chaincode shim.Chaincode
// InvokableExternal keeps track of whether
// this system chaincode can be invoked
// through a proposal sent to this peer
InvokableExternal bool
// InvokableCC2CC keeps track of whether
// this system chaincode can be invoked
// by way of a chaincode-to-chaincode
// invocation
InvokableCC2CC bool
// Enabled a convenient switch to enable/disable system chaincode without
// having to remove entry from importsysccs.go
Enabled bool
}
```
* 在 core/scc/importsysccs.go文件中對系統鏈碼進行了初始化,并且每個Chainoce指定了具體實現
在
```go
//see systemchaincode_test.go for an example using "sample_syscc"
var systemChaincodes = []*SystemChaincode{
{
Enabled: true,
Name: "cscc",
Path: "github.com/hyperledger/fabric/core/scc/cscc",
InitArgs: [][]byte{[]byte("")},
Chaincode: &cscc.PeerConfiger{},
InvokableExternal: true, // cscc is invoked to join a channel
},
{
Enabled: true,
Name: "lscc",
Path: "github.com/hyperledger/fabric/core/scc/lscc",
InitArgs: [][]byte{[]byte("")},
Chaincode: lscc.NewLifeCycleSysCC(),
InvokableExternal: true, // lscc is invoked to deploy new chaincodes
InvokableCC2CC: true, // lscc can be invoked by other chaincodes
},
{
Enabled: true,
Name: "escc",
Path: "github.com/hyperledger/fabric/core/scc/escc",
InitArgs: [][]byte{[]byte("")},
Chaincode: &escc.EndorserOneValidSignature{},
},
{
Enabled: true,
Name: "vscc",
Path: "github.com/hyperledger/fabric/core/scc/vscc",
InitArgs: [][]byte{[]byte("")},
Chaincode: &vscc.ValidatorOneValidSignature{},
},
{
Enabled: true,
Name: "qscc",
Path: "github.com/hyperledger/fabric/core/chaincode/qscc",
InitArgs: [][]byte{[]byte("")},
Chaincode: &qscc.LedgerQuerier{},
InvokableExternal: true, // qscc can be invoked to retrieve blocks
InvokableCC2CC: true, // qscc can be invoked to retrieve blocks also by a cc
},
}
```
* 注冊流程圖



## 3、系統鏈碼初始化
* 系統注冊完成后會對鏈碼初始化.跟一般chaincode稍有不同的是chaincode在合約里通過grpc與peer節點交互。
而系統鏈碼則是在協程里通過chan 實現交互.下面代碼創建兩個 peerRcvCCSend := make(chan *pb.ChaincodeMessage)
ccRcvPeerSend := make(chan *pb.ChaincodeMessage) ,是客戶端和服務端共同的參數
```go
func (ipc *inprocContainer) launchInProc(ctxt context.Context, id string, args []string, env []string, ccSupport ccintf.CCSupport) error {
peerRcvCCSend := make(chan *pb.ChaincodeMessage)
ccRcvPeerSend := make(chan *pb.ChaincodeMessage)
var err error
ccchan := make(chan struct{}, 1)
ccsupportchan := make(chan struct{}, 1)
//啟動客戶端處理
go func() {
defer close(ccchan)
inprocLogger.Debugf("chaincode started for %s", id)
if args == nil {
args = ipc.args
}
if env == nil {
env = ipc.env
}
err := _shimStartInProc(env, args, ipc.chaincode, ccRcvPeerSend, peerRcvCCSend)
if err != nil {
err = fmt.Errorf("chaincode-support ended with err: %s", err)
_inprocLoggerErrorf("%s", err)
}
inprocLogger.Debugf("chaincode ended with for %s with err: %s", id, err)
}()
//啟動服務端處理
go func() {
defer close(ccsupportchan)
inprocStream := newInProcStream(peerRcvCCSend, ccRcvPeerSend)
inprocLogger.Debugf("chaincode-support started for %s", id)
err := ccSupport.HandleChaincodeStream(ctxt, inprocStream)
if err != nil {
err = fmt.Errorf("chaincode ended with err: %s", err)
_inprocLoggerErrorf("%s", err)
}
inprocLogger.Debugf("chaincode-support ended with for %s with err: %s", id, err)
}()
select {
case <-ccchan:
close(peerRcvCCSend)
inprocLogger.Debugf("chaincode %s quit", id)
case <-ccsupportchan:
close(ccRcvPeerSend)
inprocLogger.Debugf("chaincode support %s quit", id)
case <-ipc.stopChan:
close(ccRcvPeerSend)
close(peerRcvCCSend)
inprocLogger.Debugf("chaincode %s stopped", id)
}
return err
}
```
* 初始化流程圖


## 4、系統鏈碼的執行
...
posted on 2018-06-12 15:00 傻 瓜 閱讀(1023) 評論(0) 編輯 收藏 所屬分類: 雜項