Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call a chaincode from another chaincode deployed on two different organization peers connected using a single channel in hyperledger-fabric?

I wrote a chaincode1(deployed on one peer of ORG1) which accepts the details from the user and store it into the ledger. Now I want to write a chaincode2(deployed on a peer of ORG2) which takes some data from the chaincode1 for computation. This chaincode2 should be called by chaincode1 with specific details required for computation. How can i achieve this thing and where should I test it?

like image 213
Kshitiz Sharma Avatar asked Jan 25 '26 12:01

Kshitiz Sharma


1 Answers

To start with, there are few preconditions such as:

  1. If you'd like to have chaincode1 to invoke chaincode2, you need both chaincodes to be installed the same peer and
  2. Need to make sure this peer part of both channels

Next, you need to leverage following 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

Here is an example:

chainCodeArgs := util.ToChaincodeArgs("arg1", "arg2")
response := stub.InvokeChaincode("chaincodeName", chainCodeArgs, "channelID")

if response.Status != shim.OK {
    return shim.Error(response.Message)
}
like image 151
Artem Barger Avatar answered Jan 27 '26 16:01

Artem Barger



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!