Fabric SDK调用流程
1、文字描述
- 获取FabricSDK
sdk, err := fabsdk.New(config.FromFile(sdkConfig))
- 加入Org、User信息
rcp := sdk.Context(fabsdk.WithOrg(orgName), fabsdk.WithUser(userName))
- 获取指定Channel的Channel Provider
ccp := sdk.ChannelContext(channelID, fabsdk.WithUser(userName))
- 获取该Channel的Channel Client
cc, err := channel.New(ccp)
- 封装channel.Request
var req = &channel.Request{
ChaincodeID: chaincodeID,
Fcn fcn,
Args args,
TransientMap transientMap,
}
- 封装channel.RequestOption
var opts = []channel.RequestOption
opts = append(opts, channel.WithTargetEndpoints(peer...)
opts = append(opts, channel.WithOrdererEndpoint(orderers...))
- 调用链码返回结果
// invoke调用
response, err := cc.Execute(*req, opts...)
// query调用
response, err := cc.Query(*req, opts...)
- 获取该Channel的Event Client
ec, err := event.New(ccp, event.WithBlockEvents())
- 监听区块/链码/交易事件
// 监听区块事件
blockReg, notify, err := ec.RegisterBlockEvent()
// 监听链码事件
chaincodeReg, notify, err := ec.RegisterChaincodeEvent(chaincodeId, ".*")
// 监听交易事件
txReg, notify, err := ec.RegisterTxStatusEvent(txId)
- 获取数据
for {
select {
case event := <- notify:
// todo
}
}
- 取消监听
// 取消监听区块事件
ec.Unregister(blockReg)
// 取消监听链码事件
ec.Unregister(chaincodeReg)
// 取消监听交易事件
ec.Unregister(txReg)
2、图解
来源:oschina
链接:https://my.oschina.net/u/4342388/blog/4367454