如何从在hyperledger-fabric中使用单个通道连接的两个不同组织对等点上部署的另一个链代码调用链代码?

时间:2018-05-11 11:41:17

标签: block hyperledger-fabric blockchain hyperledger

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

2 个答案:

答案 0 :(得分:2)

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

  1. 如果你想让chaincode1调用chaincode2,你需要将两个链代码安装在同一个对等体上并且
  2. 需要确保两个频道的同行部分
  3. 接下来,您需要利用以下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)
    }
    

答案 1 :(得分:0)

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

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

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