hyperledger fabric 结构分析(二)


hyperledger fabric 结构分析(二)_第1张图片

接着上图分析,经过Consensus Commit流程生成批数据后,是如何送入到ChainCode呢?我们还是以Invoke命令分析。
1)在consensus的helper中调用chaincode的ExecuteTransactions 进入transaction处理流程
func (h *Helper) ExecTxs(id interface{}, txs []*pb.Transaction) ([]byte, error) {
succeededTxs, res, ccevents, txerrs, err := chaincode.ExecuteTransactions(context.Background(), chaincode.DefaultChain, txs)
}

2)该函数在core/chaincode 中处理,将命令封装成ChainCode识别的格式。其中的chain对象则是访问ChainCode对应的ChainCodeSupport,这样就说明访问ChainCode的接口类是ChainCodeSupportServer

func Execute(ctxt context.Context, chain *ChaincodeSupport, t *pb.Transaction) ([]byte, *pb.ChaincodeEvent, error) {
ccMsg, err = createTransactionMessage(t.Txid, cMsg)
resp, err := chain.Execute(ctxt, chaincode, ccMsg, timeout, t)
}

3)该函数在ChainCodeSupport文件中,首先检测ChainCode是否建立成功、能否正常运行。其中chrte.handler的得来是比较复杂的,见下描述

func (chaincodeSupport *ChaincodeSupport) Execute(ctxt context.Context, chaincode string, msg *pb.ChaincodeMessage, timeout time.Duration, tx *pb.Transaction) (*pb.ChaincodeMessage, error) {
chrte, ok := chaincodeSupport.chaincodeHasBeenLaunched(chaincode)
chrte.handler.sendExecuteMessage(msg, tx)
}

3.1)在创建ChainCodeSupport的时候registerChaincodeSupport 调用 NewChaincodeSupport 实例化ChainCodeSupport(start.go),服务器的Name:

protos.ChaincodeSupport
	ccStartupTimeout := time.Duration(tOut) * time.Millisecond

	ccSrv := chaincode.NewChaincodeSupport(chainname, peer.GetPeerEndpoint, userRunsCC,
		ccStartupTimeout, secHelper)

	//Now that chaincode is initialized, register all system chaincodes.
	system_chaincode.RegisterSysCCs()

	pb.RegisterChaincodeSupportServer(grpcServer, ccSrv)

var _ChaincodeSupport_serviceDesc = grpc.ServiceDesc{
       ServiceName: "protos.ChaincodeSupport",
       HandlerType: (*ChaincodeSupportServer)(nil),
       Methods:     []grpc.MethodDesc{},
       Streams: []grpc.StreamDesc{
              {
                     StreamName:    "Register",
                     Handler:       _ChaincodeSupport_Register_Handler,
                     ServerStreams: true,
                     ClientStreams: true,
              },
       },
}

3.2)ChainCode 调用 err := shim.Start(new(SimpleChaincode)) 接入到ChainCodeSupportServer

err := shim.Start(new(SimpleChaincode))

3.3)连接ChainCodeSupprotServer同时调用Register函数

func Start(cc Chaincode) error {
chaincodeSupportClient := pb.NewChaincodeSupportClient(clientConn)
stream, err := chaincodeSupportClient.Register(context.Background())
err = chatWithPeer(chaincodename, stream, cc)
}

3.4)与此同时ChainCodeSupportServer会根据Client调用注册函数创建该Stream的Handler处理句柄,创建消息响应循环,等待Client发送命令.(注意该handler就是我们关心的

handler.sendExecuteMessage)
func HandleChaincodeStream(chaincodeSupport *ChaincodeSupport, ctxt context.Context, stream ccintf.ChaincodeStream) error {
	deadline, ok := ctxt.Deadline()
	chaincodeLogger.Debugf("Current context deadline = %s, ok = %v", deadline, ok)
	handler := newChaincodeSupportHandler(chaincodeSupport, stream)
	return handler.processStream()
}
 
  

3.5) 客户端shim/Chaincode发送RegisterMessage

handler.serialSend(&pb.ChaincodeMessage{Type: pb.ChaincodeMessage_REGISTER, Payload: payload})

3.6)对于Server而言,我们刚刚创建了handler又有ProcessStream消息响应循环,这样RegisterMessage就交到了ProcessStream手里,ProcessStream根据消息类型执行命令分发
调用beforeRegisterEvent函数。

func (handler *Handler) beforeRegisterEvent(e *fsm.Event, state string) {
err = handler.chaincodeSupport.registerHandler(handler)
}

3.7)同理在client端(shim/chaincode)也建立响应的消息响应循环。


4)到目前为止还没有完,我们将invoke命令送给了Client的委托模块Shim进行处理。Shim模块根据来访事件类型送入指定处理函数
func (handler *Handler) enterTransactionState(e *fsm.Event) {
	msg, ok := e.Args[0].(*pb.ChaincodeMessage)
	if !ok {
		e.Cancel(fmt.Errorf("Received unexpected message type"))
		return
	}
	chaincodeLogger.Debugf("[%s]Received %s, invoking transaction on chaincode(Src:%s, Dst:%s)", shorttxid(msg.Txid), msg.Type.String(), e.Src, e.Dst)
	if msg.Type.String() == pb.ChaincodeMessage_TRANSACTION.String() {
		// Call the chaincode's Run function to invoke transaction
		handler.handleTransaction(msg)
	}
}
5)调用ChainCode的invoke函数
func (handler *Handler) handleTransaction(msg *pb.ChaincodeMessage) {
res, err := handler.cc.Invoke(stub, function, params)
}

以上分析涉及两个过程 1) consensus结束后如何将命令送入ChainCode 2)ChainCodeSupportServer与ChainCode如何建立通信关系。

画一个 ChainCodeSupportServer与ChainCode如何建立通信关系 图:
	
	hyperledger fabric 结构分析(二)_第2张图片


 
  
 
  
 
  
 
  

你可能感兴趣的:(fabric源码)