Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how we can call one chaincode from another chaincode in fabric 1.0 ?? if anyone having example please share

I want to call one chaincode from another chaincode in fabric 1.0 so I have some questions: 1) can we install two chaincode on single peer 2) if we install two chaincode on different peer, how we can call one into another? 3)if anybody having sample example please share.

like image 923
Tejal tandale Avatar asked Mar 19 '18 10:03

Tejal tandale


1 Answers

This is should be pretty straight forward to achieve, here is an example:

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


UPDTAE

As @Gari, correctly stated in the comment:

It's very important to make sure that both chaincodes are installed on each endorsing peer

Consider to read also following material:

  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
like image 96
Artem Barger Avatar answered Sep 19 '22 05:09

Artem Barger