如何在Fabric 1.0中从一个链码调用另一个链码?如果有示例,请分享。

7

我希望在Fabric 1.0中从一个链码调用另一个链码。因此我有一些问题: 1)我们可以在单个节点上安装两个链码吗? 2)如果我们在不同的节点上安装两个链码,如何调用其中一个到另一个? 3)如果有任何示例,请分享。

1个回答

13

这应该很容易实现,这里有一个例子:

// 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))
}


更新

正如@Gari在评论中正确指出的:

确保每个背书对等方都安装了两个链码非常重要。

还可以考虑阅读以下材料:

  1. https://github.com/asararatnakar/fabric_v1_Chaincode_instructions/blob/master/call-chaincode-to-chaincode-nondefault-chain.md
  2. https://jira.hyperledger.org/browse/FAB-1788
  3. http://hyperledger-fabric.readthedocs.io/en/release-1.0/chaincode.html

@ArtemBarger 我可以调用其他通道的链码吗? - Akshay Sood
只有当同行在两个频道中都参与并安装了第二个链码时,@AkshaySood 才能执行操作。 - Artem Barger

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接