如何使用 invokeChaincode() API

How to use invokeChaincode() API

我想在同一个通道中调用另一个链代码,但我不知道如何使用 InvokeChainCode API。我试图测试它,但出现了一些错误。

INVOKE_CHAINCODE failed: transaction ID:: cannot retrieve package for chaincode dealercc/1.0, error open /var/hyperledger/production/chaincodes/dealercc.1.0: no such file or directory"

而且,我在网上搜索但没有找到,但我从 https://hyperledger-fabric.readthedocs.io/en/release-1.4/developapps/chaincodenamespace.html#considerations

找到了这个

For chaincode to chaincode interactions using the invokeChaincode() API, both chaincodes must be installed on the same peer.

不过,我不太清楚这是什么意思。他想让我在使用 ICC API 的节点上安装所有链代码吗?如果他这样做,我该怎么办?我试了但出错了,说已经安装了一些东西。

要从另一个链代码调用一个链代码,您需要确保 两个链代码都安装在每个背书节点上,之后你可以执行链代码到链代码调用。

链码到链码的调用非常简单,这里有一个例子:

// Invoke
func (am *accountManagement) Invoke(stub shim.ChaincodeStubInterface) 
peer.Response {
actionName, params := stub.GetFunctionAndParameters()

if actionName == "callAnotherCC" {
    chainCodeArgs := util.ToChaincodeArgs("anotherCCFunc", "paramA")
    response := stub.InvokeChaincode("anotherCCName", chainCodeArgs, "channelName")

    if response.Status != shim.OK {
       return shim.Error(response.Message)
    }
    return shim.Success(nil)
}

// NOTE: This is an example, hence assuming only valid call is to call another chaincode
return shim.Error(fmt.Sprintf("[ERROR] No <%s> action defined", actionName))
}

这个例子是以前shim库的实现,你可以根据最新的shim库更新这个