喜来我所知,确实没有办法对一个调用的事务返回取决于chaincode执行的值。看到这个职位的更多细节https://github.com/IBM-Blockchain/ibm-blockchain-issues/issues/85
但是,你可以做的是发射一个事件从你的chaincode的情况下出现错误的执行或如果一切按计划进行。例如,您可以有:
func (t *SimpleChaincode) publish(stub shim.ChaincodeStubInterface, args []string) ([]byte, error) {
...
//Execution of chaincode finished successfully
tosend := "Tx chaincode finished OK." + stub.GetTxID()
err = stub.SetEvent("evtsender", []byte(tosend))
if err != nil {
return nil, err
}
...
//transactions cannot return errors this way we have to use an event
// return nil, errors.New("No Supplement Found with the given ID")
tosend := "Error, No Supplement Found with the given ID" + suplementId + "." + stub.GetTxID()
err = stub.SetEvent("evtsender", []byte(tosend))
if err != nil {
return nil, err
}
...
在这些事件中,您可以添加事务标识。因此,在您的SDK应用程序(使用与HFC的NodeJS的情况下),你包的调用事务调用的承诺,并解决它取决于发出的事件拒绝。喜欢的东西:
function invokeWithParams(userObj,invReq) {
var txHash="qwe";
return new Promise(function(resolve,reject){
var eh = networkConfig.chain.getEventHub();
// Trigger the invoke transaction
var invokeTx = userObj.invoke(invReq);
// Print the invoke results
invokeTx.on('submitted', function(results) {
// Invoke transaction submitted successfully
console.log(util.format("\nSuccessfully submitted chaincode invoke transaction: request=%j, response=%j", invReq, results));
txHash = results.uuid;
});
invokeTx.on('complete', function(results) {
// Invoke transaction completed successfully
console.log(util.format("\nSuccessfully completed chaincode invoke transaction: request=%j, response=%j", invReq, results));
// resolve(results);
// txHash = results.uuid;
});
invokeTx.on('error', function(err) {
// Invoke transaction submission failed
console.log(util.format("\nFailed to submit chaincode invoke transaction: request=%j, error=%j", invReq, err));
reject(err);
});
//Listen to custom events
var regid = eh.registerChaincodeEvent(invReq.chaincodeID, "evtsender", function(event) {
console.log(util.format("Custom event received, payload: %j\n", event.payload.toString()));
if(event.payload.toString().indexOf("Error") >= 0){
let uuid = event.payload.toString().split(".")[1];
eh.unregisterChaincodeEvent(regid);
if(uuid === txHash){ //resolve promise only when the current transaction has finished
eh.unregisterChaincodeEvent(regid);
reject(event.payload.toString());
}
}
if(event.payload.toString().indexOf("Tx chaincode finished OK") >= 0){
let uuid = event.payload.toString().split(".")[1];
console.log("\nUUID " + uuid);
console.log("\ntxHash " + txHash);
if(uuid === txHash){ //resolve promise only when the current transaction has finished
eh.unregisterChaincodeEvent(regid);
resolve(event.payload.toString());
}
}
});
});
}
无论如何,我知道它远非完美的方法,但它帮助了我,所以我希望它会帮助你:)
什么你的问题? –
如果我通过传递0参数通过REST调用上面的函数,我得到以下(类似)响应:“jsonrpc”:“2.0” “result”:{ “status”:“OK” “message”:“ bf4f2e2c-ed0f-4240-aae5-1dc295515b3f” } - “ID”:4 }'理想的情况下,它应该返回错误响应,而不是OK响应 – JavaD