Mallikarjuna J S
Mallikarjuna J S

Reputation: 130

Can a chaincode interact with other chaincode?

I am trying to write an application with hyper-ledger and I was looking for a way to communicate with chain-code from another chain-code. Is that possible in hyper-ledger?

I know its possible in etherum to communicate with other smart contracts but is the same possible in hyperledger. I could not find any related links related to same. Any suggestions on how to approach this will be really helpful.

I checked out Writing Your First Application, but I could not find the suitable explanation for same.

Upvotes: 2

Views: 1060

Answers (1)

Artem Barger
Artem Barger

Reputation: 41232

Chaincode could interact with other chaincode by means of leveraging existing API of ChaincodeStubInterface, e.g.:

// 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 of how you can use it:

response := stub.InvokeChaincode(chaincodeName, chainCodeArgs, channelName)

Of course peer should have rights and chaincode installed.

Upvotes: 5

Related Questions