我对select语句中的golang通道是陌生的,因此这里可能缺少明显的内容。
我正在使用hyperledger fabric-sdk-go库[1],该库使用select语句等待对区块链执行的响应。
在这里,用我自己的代码调用执行并等待通道上的响应:
func (mgr *Manager) InvokeTest(value string) (string, error) {
. . .{code excluded here}. . .
fmt.Println("InvokeTest - CHECK 01")
reg, notifier, err := mgr.client.RegisterChaincodeEvent(mgr.ChaincodeID, eventID)
if err != nil {
return "", err
}
defer mgr.client.UnregisterChaincodeEvent(reg)
. . .{code excluded here}. . .
fmt.Println("InvokeTest - CHECK 02")
// Wait for the result of the submission
select {
case ccEvent := <-notifier:
fmt.Printf("Received CC event: %v\n", ccEvent)
case <-time.After(time.Second * 20):
return "", fmt.Errorf("did NOT receive CC event for eventId(%s)", eventID)
}
return string(response.TransactionID), nil
}
这是库中的RegisterChaincodeEvent
函数(我添加了WriteString
用于记录:
func (cc *Client) RegisterChaincodeEvent(chainCodeID string, eventFilter string) (fab.Registration, <-chan *fab.CCEvent, error) {
//-------------------------------
. . .WriteString("----- chclient-RegisterChaincodeEvent - BEGIN -----\r") . . .
//-------------------------------
// Register callback for CE
return cc.eventService.RegisterChaincodeEvent(chainCodeID, eventFilter)
}
...这是eventService.RegisterChaincodeEvent
函数(再次添加WriteString
用于记录日志:
func (s *Service) RegisterChaincodeEvent(ccID, eventFilter string) (fab.Registration, <-chan *fab.CCEvent, error) {
//-------------------------------
. . .WriteString("----- service-RegisterChaincodeEvent - BEGIN -----\r") . . .
//-------------------------------
. . .{code excluded here}. . .
eventch := make(chan *fab.CCEvent, s.eventConsumerBufferSize)
regch := make(chan fab.Registration)
errch := make(chan error)
if err := s.Submit(dispatcher.NewRegisterChaincodeEvent(ccID, eventFilter, eventch, regch, errch)); err != nil {
//-------------------------------
. . .WriteString("service-RegisterChaincodeEvent - CHECK DISP ERR\r") . . .
//-------------------------------
return nil, nil, errors.WithMessage(err, "error registering for chaincode events")
}
select {
case response := <-regch:
//-------------------------------
. . .WriteString("service-RegisterChaincodeEvent - CHECK 01\r") . . .
//-------------------------------
return response, eventch, nil
case err := <-errch:
return nil, nil, err
}
}
...这是终端日志:
InvokeTest - CHECK 01
InvokeTest - CHECK 02
did NOT receive CC event for eventId(eventInvoke)
...以及库函数中的相关日志行:
----- chclient-RegisterChaincodeEvent - BEGIN -----
----- service-RegisterChaincodeEvent - BEGIN -----
service-RegisterChaincodeEvent - CHECK 01
因此,看来该库正在正常运行,并且RegisterChaincodeEvent
函数正在该通道上返回响应。为什么我自己的select语句不接收通道响应?如何调试选择/通道阻止过程以查看其超时原因?