写点什么

长安链源码分析之交易过程分析(1)

作者:
  • 2022-10-25
    湖南
  • 本文字数:1041 字

    阅读完需:约 3 分钟

本文已参与「开源摘星计划」,欢迎正在阅读的你加入。活动链接:https://github.com/weopenprojects/WeOpen-Star


在 api_service.go 中发现了合约调用滴地方,根据交易类型来处理合约


// SendRequest - deal received TxRequest//处理收到交易请求func (s *ApiService) SendRequest(ctx context.Context, req *commonPb.TxRequest) (*commonPb.TxResponse, error) {	s.log.DebugDynamic(func() string {		return fmt.Sprintf("SendRequest[%s],payload:%#v,\n----signer:%v\n----endorsers:%+v",			req.Payload.TxId, req.Payload, req.Sender, req.Endorsers)	})  //调用合约处理	resp := s.invoke(&commonPb.Transaction{		Payload:   req.Payload,		Sender:    req.Sender,		Endorsers: req.Endorsers,		Result:    nil}, protocol.RPC)
// audit log format: ip:port|orgId|chainId|TxType|TxId|Timestamp|ContractName|Method|retCode|retCodeMsg|retMsg s.logBrief.Infof("|%s|%s|%s|%s|%s|%d|%s|%s|%d|%s|%s", GetClientAddr(ctx), req.Sender.Signer.OrgId, req.Payload.ChainId, req.Payload.TxType, req.Payload.TxId, req.Payload.Timestamp, req.Payload.ContractName, req.Payload.Method, resp.Code, resp.Code, resp.Message)
return resp, nil}
复制代码


// invoke contract according to TxType//根据交易类型调用合约,这里的txsource是  rpcfunc (s *ApiService) invoke(tx *commonPb.Transaction, source protocol.TxSource) *commonPb.TxResponse {	var (		errCode commonErr.ErrCode		errMsg  string		resp    = &commonPb.TxResponse{}	)
//如果不是系统链 if tx.Payload.ChainId != SYSTEM_CHAIN { //先验证交易 errCode, errMsg = s.validate(tx) if errCode != commonErr.ERR_CODE_OK { resp.Code = commonPb.TxStatusCode_INTERNAL_ERROR resp.Message = errMsg resp.TxId = tx.Payload.TxId return resp } } //判断交易类型 switch tx.Payload.TxType { //查询合约 case commonPb.TxType_QUERY_CONTRACT: return s.dealQuery(tx, source) //调用合约 case commonPb.TxType_INVOKE_CONTRACT: return s.dealTransact(tx, source) //归档 case commonPb.TxType_ARCHIVE: return s.doArchive(tx) default: return &commonPb.TxResponse{ Code: commonPb.TxStatusCode_INTERNAL_ERROR, Message: commonErr.ERR_CODE_TXTYPE.String(), } }}
复制代码


用户头像

关注

还未添加个人签名 2018-05-04 加入

还未添加个人简介

评论

发布
暂无评论
长安链源码分析之交易过程分析(1)_李_InfoQ写作社区