喜來我所知,確實沒有辦法對一個調用的事務返回取決於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