hyperledger-fabrichyperledgerhyperledger-chaincodehyperledger-fabric-sdk-js

Is it possible to use invokeChaincode method to retrieve data from the chaincode that is on different channel


I'm currently running a project with 2 chaincodes, 2 channels and 2 organizations. I want to install my first chaincode (CC1) on first channel (channel-1) consisting of both org1 and org2. The second chaincode(CC2) will be installed on channel-2 consisting of only org2.

I want to retrieve data from first chaincode and the retrieved data will be used in the second chaincode. Is it possible to retrieve data from a chaincode which is on different channel using invokeChaincode method?

If not possible what are then what are the ways to retrieve data from other chaincodes which is installed on different channel?

I'm using hyperledger fabric version 2.0 and node js to build my chaincode.


Solution

  • You can use the invokeChaincode function to invoke chaincode from a difference channel:

    //get data from channel 1
    const cc1Args = ['arg1', 'arg2'];
    const cc1Res = await ctx.stub.invokeChaincode('CC1', cc1Args, 'channel-1');
    if (cc1Res.status !== 200) {
        throw new Error(cc1Res.message);
    }
    const cc1Asset = JSON.parse(cc1Res.payload.toString('utf8'));
    
    //save data to channel 2
    const cc2Args = [cc1Asset.arg1, cc1Asset.arg2];
    const cc2Res = await ctx.stub.invokeChaincode('CC2', cc2Args, 'channel-2');
    if (cc2Res.status !== 200) {
        throw new Error(cc2Res.message);
    }
    const cc2ResObj = JSON.parse(cc1Res.payload.toString('utf8'));
    
    

    You may also read the document of invokeChaincode: https://hyperledger.github.io/fabric-chaincode-node/master/api/fabric-shim.ChaincodeStub.html#invokeChaincode__anchor