Hyperledger fabric chaincode: function calling another function from within chaincode?












0















I have a scenario where I need to call a some other(say query) function from a chaincode function (say update). Does hyperledger fabric provide any interface for this.
Eg:



   ...
async query(stub, args) {
}
async update(stub, args) {
if(condition) {
call query();
}
}
...


I have tried the answer on following post but it did't work.
how to invoke chaincode function from itself to record sub transactions.
Though by using invokeChaincode() I am able to call function from another chaincode.



Thanks in advance.



Chaincode code:



    let Chaincode = class {

async Init(stub) {
return shim.success();
}

async Invoke(stub) {

let ret = stub.getFunctionAndParameters();

console.info(ret);

let method = this[ret.fcn];

if (!method) {
throw new Error('Received unknown function ' + ret.fcn + ' invocation');
}
try {
let payload = await method(stub, ret.params);
return shim.success(payload);
} catch (err) {
return shim.error(err);
}
}

async init(stub, args) {

if (args.length != 1) {
throw new Error('Invalid args. Expects no args');
}
}

async query(stub, args) {
...
}

async dummy(stub, args) {
return Buffer.from('Hello');
}


async update(stub, args) {
...
let resp = await dummy(); // gives error
//let resp = await stub.invokeChaincode('cc2', ['dummy'] ); // working
console.log(resp)
...
}
};
shim.start(new Chaincode());









share|improve this question





























    0















    I have a scenario where I need to call a some other(say query) function from a chaincode function (say update). Does hyperledger fabric provide any interface for this.
    Eg:



       ...
    async query(stub, args) {
    }
    async update(stub, args) {
    if(condition) {
    call query();
    }
    }
    ...


    I have tried the answer on following post but it did't work.
    how to invoke chaincode function from itself to record sub transactions.
    Though by using invokeChaincode() I am able to call function from another chaincode.



    Thanks in advance.



    Chaincode code:



        let Chaincode = class {

    async Init(stub) {
    return shim.success();
    }

    async Invoke(stub) {

    let ret = stub.getFunctionAndParameters();

    console.info(ret);

    let method = this[ret.fcn];

    if (!method) {
    throw new Error('Received unknown function ' + ret.fcn + ' invocation');
    }
    try {
    let payload = await method(stub, ret.params);
    return shim.success(payload);
    } catch (err) {
    return shim.error(err);
    }
    }

    async init(stub, args) {

    if (args.length != 1) {
    throw new Error('Invalid args. Expects no args');
    }
    }

    async query(stub, args) {
    ...
    }

    async dummy(stub, args) {
    return Buffer.from('Hello');
    }


    async update(stub, args) {
    ...
    let resp = await dummy(); // gives error
    //let resp = await stub.invokeChaincode('cc2', ['dummy'] ); // working
    console.log(resp)
    ...
    }
    };
    shim.start(new Chaincode());









    share|improve this question



























      0












      0








      0








      I have a scenario where I need to call a some other(say query) function from a chaincode function (say update). Does hyperledger fabric provide any interface for this.
      Eg:



         ...
      async query(stub, args) {
      }
      async update(stub, args) {
      if(condition) {
      call query();
      }
      }
      ...


      I have tried the answer on following post but it did't work.
      how to invoke chaincode function from itself to record sub transactions.
      Though by using invokeChaincode() I am able to call function from another chaincode.



      Thanks in advance.



      Chaincode code:



          let Chaincode = class {

      async Init(stub) {
      return shim.success();
      }

      async Invoke(stub) {

      let ret = stub.getFunctionAndParameters();

      console.info(ret);

      let method = this[ret.fcn];

      if (!method) {
      throw new Error('Received unknown function ' + ret.fcn + ' invocation');
      }
      try {
      let payload = await method(stub, ret.params);
      return shim.success(payload);
      } catch (err) {
      return shim.error(err);
      }
      }

      async init(stub, args) {

      if (args.length != 1) {
      throw new Error('Invalid args. Expects no args');
      }
      }

      async query(stub, args) {
      ...
      }

      async dummy(stub, args) {
      return Buffer.from('Hello');
      }


      async update(stub, args) {
      ...
      let resp = await dummy(); // gives error
      //let resp = await stub.invokeChaincode('cc2', ['dummy'] ); // working
      console.log(resp)
      ...
      }
      };
      shim.start(new Chaincode());









      share|improve this question
















      I have a scenario where I need to call a some other(say query) function from a chaincode function (say update). Does hyperledger fabric provide any interface for this.
      Eg:



         ...
      async query(stub, args) {
      }
      async update(stub, args) {
      if(condition) {
      call query();
      }
      }
      ...


      I have tried the answer on following post but it did't work.
      how to invoke chaincode function from itself to record sub transactions.
      Though by using invokeChaincode() I am able to call function from another chaincode.



      Thanks in advance.



      Chaincode code:



          let Chaincode = class {

      async Init(stub) {
      return shim.success();
      }

      async Invoke(stub) {

      let ret = stub.getFunctionAndParameters();

      console.info(ret);

      let method = this[ret.fcn];

      if (!method) {
      throw new Error('Received unknown function ' + ret.fcn + ' invocation');
      }
      try {
      let payload = await method(stub, ret.params);
      return shim.success(payload);
      } catch (err) {
      return shim.error(err);
      }
      }

      async init(stub, args) {

      if (args.length != 1) {
      throw new Error('Invalid args. Expects no args');
      }
      }

      async query(stub, args) {
      ...
      }

      async dummy(stub, args) {
      return Buffer.from('Hello');
      }


      async update(stub, args) {
      ...
      let resp = await dummy(); // gives error
      //let resp = await stub.invokeChaincode('cc2', ['dummy'] ); // working
      console.log(resp)
      ...
      }
      };
      shim.start(new Chaincode());






      hyperledger-fabric hyperledger hyperledger-fabric-sdk-js






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 9 at 11:57







      SandeepR

















      asked Jan 3 at 6:45









      SandeepRSandeepR

      715




      715
























          2 Answers
          2






          active

          oldest

          votes


















          0














          As a alternative approach I am installing and instantiating same chaincode with two names say cc1 and cc2 on same channel say ch1. Then I am using:



           invokeChaicode('cc2', ['function', 'arg1', arg2]);


          from chaincode one (cc1).



          But the problem is I need to install and instantiate same chaincode twice with different names on same channel.



          Any other insights are welcome.






          share|improve this answer


























          • Why not just call the second function by name directly from the first function?

            – Gari Singh
            Jan 6 at 10:27











          • OK will try that. I never thought this way. :) Sometimes we miss simpler scenarios.

            – SandeepR
            Jan 7 at 4:21













          • I declared a dummy function as: async dummy(stub, args) { return Buffer.from('hello'); } and called it from another function (say update) like: async update(stub, args) { console.log(await dummy()); } and got the following error: Error: Error endorsing invoke: rpc error: code = Unknown desc = error executing chaincode: transaction returned with failure: ReferenceError: dummy is not defined - <nil>

            – SandeepR
            Jan 7 at 5:38













          • would you mind posting a larger sample of your chaincode?

            – Gari Singh
            Jan 7 at 12:33











          • @GariSingh appended the larger code sample to question.

            – SandeepR
            Jan 9 at 11:58



















          0














          Sometimes we should think it more simple.Well, the answer is: call it directly.
          For example:
          we got two functions A and B



          func (s *SmartContract) A(APIstub shim.ChaincodeStubInterface, args string) sc.Response {}


          func (s *SmartContract) B(APIstub shim.ChaincodeStubInterface, args string) sc.Response {}


          if we want to call function A within function B,just do it as



          func (s *SmartContract) B(APIstub shim.ChaincodeStubInterface, args string) sc.Response {

          s.A(APIstub,args)

          }





          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54017510%2fhyperledger-fabric-chaincode-function-calling-another-function-from-within-chai%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            As a alternative approach I am installing and instantiating same chaincode with two names say cc1 and cc2 on same channel say ch1. Then I am using:



             invokeChaicode('cc2', ['function', 'arg1', arg2]);


            from chaincode one (cc1).



            But the problem is I need to install and instantiate same chaincode twice with different names on same channel.



            Any other insights are welcome.






            share|improve this answer


























            • Why not just call the second function by name directly from the first function?

              – Gari Singh
              Jan 6 at 10:27











            • OK will try that. I never thought this way. :) Sometimes we miss simpler scenarios.

              – SandeepR
              Jan 7 at 4:21













            • I declared a dummy function as: async dummy(stub, args) { return Buffer.from('hello'); } and called it from another function (say update) like: async update(stub, args) { console.log(await dummy()); } and got the following error: Error: Error endorsing invoke: rpc error: code = Unknown desc = error executing chaincode: transaction returned with failure: ReferenceError: dummy is not defined - <nil>

              – SandeepR
              Jan 7 at 5:38













            • would you mind posting a larger sample of your chaincode?

              – Gari Singh
              Jan 7 at 12:33











            • @GariSingh appended the larger code sample to question.

              – SandeepR
              Jan 9 at 11:58
















            0














            As a alternative approach I am installing and instantiating same chaincode with two names say cc1 and cc2 on same channel say ch1. Then I am using:



             invokeChaicode('cc2', ['function', 'arg1', arg2]);


            from chaincode one (cc1).



            But the problem is I need to install and instantiate same chaincode twice with different names on same channel.



            Any other insights are welcome.






            share|improve this answer


























            • Why not just call the second function by name directly from the first function?

              – Gari Singh
              Jan 6 at 10:27











            • OK will try that. I never thought this way. :) Sometimes we miss simpler scenarios.

              – SandeepR
              Jan 7 at 4:21













            • I declared a dummy function as: async dummy(stub, args) { return Buffer.from('hello'); } and called it from another function (say update) like: async update(stub, args) { console.log(await dummy()); } and got the following error: Error: Error endorsing invoke: rpc error: code = Unknown desc = error executing chaincode: transaction returned with failure: ReferenceError: dummy is not defined - <nil>

              – SandeepR
              Jan 7 at 5:38













            • would you mind posting a larger sample of your chaincode?

              – Gari Singh
              Jan 7 at 12:33











            • @GariSingh appended the larger code sample to question.

              – SandeepR
              Jan 9 at 11:58














            0












            0








            0







            As a alternative approach I am installing and instantiating same chaincode with two names say cc1 and cc2 on same channel say ch1. Then I am using:



             invokeChaicode('cc2', ['function', 'arg1', arg2]);


            from chaincode one (cc1).



            But the problem is I need to install and instantiate same chaincode twice with different names on same channel.



            Any other insights are welcome.






            share|improve this answer















            As a alternative approach I am installing and instantiating same chaincode with two names say cc1 and cc2 on same channel say ch1. Then I am using:



             invokeChaicode('cc2', ['function', 'arg1', arg2]);


            from chaincode one (cc1).



            But the problem is I need to install and instantiate same chaincode twice with different names on same channel.



            Any other insights are welcome.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Jan 4 at 10:08

























            answered Jan 3 at 9:13









            SandeepRSandeepR

            715




            715













            • Why not just call the second function by name directly from the first function?

              – Gari Singh
              Jan 6 at 10:27











            • OK will try that. I never thought this way. :) Sometimes we miss simpler scenarios.

              – SandeepR
              Jan 7 at 4:21













            • I declared a dummy function as: async dummy(stub, args) { return Buffer.from('hello'); } and called it from another function (say update) like: async update(stub, args) { console.log(await dummy()); } and got the following error: Error: Error endorsing invoke: rpc error: code = Unknown desc = error executing chaincode: transaction returned with failure: ReferenceError: dummy is not defined - <nil>

              – SandeepR
              Jan 7 at 5:38













            • would you mind posting a larger sample of your chaincode?

              – Gari Singh
              Jan 7 at 12:33











            • @GariSingh appended the larger code sample to question.

              – SandeepR
              Jan 9 at 11:58



















            • Why not just call the second function by name directly from the first function?

              – Gari Singh
              Jan 6 at 10:27











            • OK will try that. I never thought this way. :) Sometimes we miss simpler scenarios.

              – SandeepR
              Jan 7 at 4:21













            • I declared a dummy function as: async dummy(stub, args) { return Buffer.from('hello'); } and called it from another function (say update) like: async update(stub, args) { console.log(await dummy()); } and got the following error: Error: Error endorsing invoke: rpc error: code = Unknown desc = error executing chaincode: transaction returned with failure: ReferenceError: dummy is not defined - <nil>

              – SandeepR
              Jan 7 at 5:38













            • would you mind posting a larger sample of your chaincode?

              – Gari Singh
              Jan 7 at 12:33











            • @GariSingh appended the larger code sample to question.

              – SandeepR
              Jan 9 at 11:58

















            Why not just call the second function by name directly from the first function?

            – Gari Singh
            Jan 6 at 10:27





            Why not just call the second function by name directly from the first function?

            – Gari Singh
            Jan 6 at 10:27













            OK will try that. I never thought this way. :) Sometimes we miss simpler scenarios.

            – SandeepR
            Jan 7 at 4:21







            OK will try that. I never thought this way. :) Sometimes we miss simpler scenarios.

            – SandeepR
            Jan 7 at 4:21















            I declared a dummy function as: async dummy(stub, args) { return Buffer.from('hello'); } and called it from another function (say update) like: async update(stub, args) { console.log(await dummy()); } and got the following error: Error: Error endorsing invoke: rpc error: code = Unknown desc = error executing chaincode: transaction returned with failure: ReferenceError: dummy is not defined - <nil>

            – SandeepR
            Jan 7 at 5:38







            I declared a dummy function as: async dummy(stub, args) { return Buffer.from('hello'); } and called it from another function (say update) like: async update(stub, args) { console.log(await dummy()); } and got the following error: Error: Error endorsing invoke: rpc error: code = Unknown desc = error executing chaincode: transaction returned with failure: ReferenceError: dummy is not defined - <nil>

            – SandeepR
            Jan 7 at 5:38















            would you mind posting a larger sample of your chaincode?

            – Gari Singh
            Jan 7 at 12:33





            would you mind posting a larger sample of your chaincode?

            – Gari Singh
            Jan 7 at 12:33













            @GariSingh appended the larger code sample to question.

            – SandeepR
            Jan 9 at 11:58





            @GariSingh appended the larger code sample to question.

            – SandeepR
            Jan 9 at 11:58













            0














            Sometimes we should think it more simple.Well, the answer is: call it directly.
            For example:
            we got two functions A and B



            func (s *SmartContract) A(APIstub shim.ChaincodeStubInterface, args string) sc.Response {}


            func (s *SmartContract) B(APIstub shim.ChaincodeStubInterface, args string) sc.Response {}


            if we want to call function A within function B,just do it as



            func (s *SmartContract) B(APIstub shim.ChaincodeStubInterface, args string) sc.Response {

            s.A(APIstub,args)

            }





            share|improve this answer




























              0














              Sometimes we should think it more simple.Well, the answer is: call it directly.
              For example:
              we got two functions A and B



              func (s *SmartContract) A(APIstub shim.ChaincodeStubInterface, args string) sc.Response {}


              func (s *SmartContract) B(APIstub shim.ChaincodeStubInterface, args string) sc.Response {}


              if we want to call function A within function B,just do it as



              func (s *SmartContract) B(APIstub shim.ChaincodeStubInterface, args string) sc.Response {

              s.A(APIstub,args)

              }





              share|improve this answer


























                0












                0








                0







                Sometimes we should think it more simple.Well, the answer is: call it directly.
                For example:
                we got two functions A and B



                func (s *SmartContract) A(APIstub shim.ChaincodeStubInterface, args string) sc.Response {}


                func (s *SmartContract) B(APIstub shim.ChaincodeStubInterface, args string) sc.Response {}


                if we want to call function A within function B,just do it as



                func (s *SmartContract) B(APIstub shim.ChaincodeStubInterface, args string) sc.Response {

                s.A(APIstub,args)

                }





                share|improve this answer













                Sometimes we should think it more simple.Well, the answer is: call it directly.
                For example:
                we got two functions A and B



                func (s *SmartContract) A(APIstub shim.ChaincodeStubInterface, args string) sc.Response {}


                func (s *SmartContract) B(APIstub shim.ChaincodeStubInterface, args string) sc.Response {}


                if we want to call function A within function B,just do it as



                func (s *SmartContract) B(APIstub shim.ChaincodeStubInterface, args string) sc.Response {

                s.A(APIstub,args)

                }






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Mar 19 at 15:15









                chuancey yangchuancey yang

                11




                11






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54017510%2fhyperledger-fabric-chaincode-function-calling-another-function-from-within-chai%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Mossoró

                    Error while reading .h5 file using the rhdf5 package in R

                    Pushsharp Apns notification error: 'InvalidToken'