我试图在我的链码中使用自动收录器根据某些条件定期更新链码状态:
func (t *SimpleChaincode) Invoke(stub *shim.ChaincodeStub, function string, args []string) ([]byte, error) {
ticker := time.NewTicker(time.Millisecond * 10000)
go func() {
for t := range ticker.C {
fmt.Println("Tick at", t)
a = a+5
err:= stub.PutState("a", []byte(strconv.Itoa(a)))
fmt.Println(err.Error())
}
}()
return nil, nil
}
我使用链接代码REST api发送调用事务以进行调用:
POST http://<ip>:<port>/chaincode
{
"jsonrpc": "2.0",
"method": "invoke",
"params": {
"type": 1,
"chaincodeID":{
"name":"c7b3c82f1170423115dcfc2524189f96f156b30961e0a0e84426c425c22f3b4e8b6ecbf477b76e014bfce74b996dee476a2470cbddc14d390617192f00c22c38"
},
"ctorMsg": {
"function":"invoke",
"args":[]
},
"secureContext": "tom"
},
"id": 1
}
但PutState失败了以下日志:
2016/05/20 13:44:04 [8bcbe40e]Inside putstate, isTransaction = false
Tick at 2016-05-20 13:44:04.609079034 +0000 UTC
Cannot put state in query context
Tick at 2016-05-20 13:44:14.609093012 +0000 UTC
Cannot put state in query context
2016/05/20 13:44:14 [8bcbe40e]Inside putstate, isTransaction = false
Tick at 2016-05-20 13:44:24.609070317 +0000 UTC
Cannot put state in query context
为什么 isTransaction = false ,为什么将此上下文视为查询上下文?
答案 0 :(得分:4)
原始调用事务已完成并返回。来自goroutine的PutState继续在事务上下文之外(实际上变成&#34;事务&#34;由链代码本身启动)。这是不允许的。与分类帐的所有互动都成为外部交易的一部分。