【问题标题】:How to call a chaincode from another chaincode deployed on two different organization peers connected using a single channel in hyperledger-fabric?如何从部署在使用超级账本结构中的单个通道连接的两个不同组织对等点上的另一个链码调用链码?
【发布时间】:2018-05-12 07:17:36
【问题描述】:

我编写了一个链码 1(部署在 ORG1 的一个对等节点上),它接受用户的详细信息并将其存储到分类帐中。现在我想编写一个chaincode2(部署在ORG2的对等体上),它从chaincode1中获取一些数据进行计算。此链代码 2 应由链代码 1 调用,并具有计算所需的特定详细信息。 我怎样才能实现这个东西,我应该在哪里测试它?

【问题讨论】:

    标签: block hyperledger-fabric blockchain hyperledger


    【解决方案1】:

    首先,有几个先决条件,例如:

    1. 如果您想让chaincode1 调用chaincode2,您需要将两个chaincode 安装在同一个peer 中,并且
    2. 需要确保这两个通道的对等部分

    接下来,您需要利用以下 API:

    // InvokeChaincode locally calls the specified chaincode `Invoke` using the
    // same transaction context; that is, chaincode calling chaincode doesn't
    // create a new transaction message.
    // If the called chaincode is on the same channel, it simply adds the called
    // chaincode read set and write set to the calling transaction.
    // If the called chaincode is on a different channel,
    // only the Response is returned to the calling chaincode; any PutState calls
    // from the called chaincode will not have any effect on the ledger; that is,
    // the called chaincode on a different channel will not have its read set
    // and write set applied to the transaction. Only the calling chaincode's
    // read set and write set will be applied to the transaction. Effectively
    // the called chaincode on a different channel is a `Query`, which does not
    // participate in state validation checks in subsequent commit phase.
    // If `channel` is empty, the caller's channel is assumed.
    InvokeChaincode(chaincodeName string, args [][]byte, channel string) pb.Response
    

    这是一个例子:

    chainCodeArgs := util.ToChaincodeArgs("arg1", "arg2")
    response := stub.InvokeChaincode("chaincodeName", chainCodeArgs, "channelID")
    
    if response.Status != shim.OK {
        return shim.Error(response.Message)
    }
    

    【讨论】:

    • 但在我的情况下,两个链码都安装在通过单通道连接的不同组织的两个不同对等节点上
    • 如果被调用的链码未安装在对等体上,则不能从另一个链码调用。
    • ToChaincodeArgs 函数在 nodejs shim 中是否可用?找不到:S
    【解决方案2】:

    这是可用于从另一个链码调用链码的函数

    func (stub *TestAPIStub) InvokeChaincode(chaincode1 string, args [][]byte, channel string) pb.Response {
       return pb.Response{}
    }
    

    您可以参考this document 了解智能合约如何调用或“链码”调用另一个智能合约。

    【讨论】:

      猜你喜欢
      • 2018-08-05
      • 1970-01-01
      • 2020-02-21
      • 1970-01-01
      • 1970-01-01
      • 2018-09-30
      • 2018-12-14
      • 2018-10-19
      • 1970-01-01
      相关资源
      最近更新 更多