How to invoke chaincode in Hyperledger fabric?

  • Every chaincode program must implement the chaincode interface.
  • The init method is called when a chaincode receives an instantiate or upgrade transaction so that the chaincode may perform any necessary initialization, including initialization of application state.
  • The Invoke method is called in response to receiving an Invoke transaction to process transaction proposals.
  • The other interface in the chaincode “shim” APIs is the ChaincodeStubInterface which is used to access and modify the ledger, and to make invocations between chaincodes.

Choosing a Location for the Code

For chaincode written in GO:
You may want to make sure that you have Go Programming Language installed and your system properly configured.

Now, you will want to create a directory for your chaincode application as a child directory of $GOPATH/src/.

To keep things simple, let’s use the following command:

Now, let’s create the source file that we’ll fill in with code:

Build & start the chaincode

You should see the following:

Now, compile your chaincode:

Now run the chaincode:

The chaincode is started with peer and chaincode logs indicating successful registration with the peer. Note that at this stage the chaincode is not associated with any channel. This is done in subsequent steps using the instantiate command.

Use the chaincode

Even though you are in --peer-chaincodedev mode, you still have to install the chaincode so the life-cycle system chaincode can go through its checks normally. This requirement may be removed in future when in --peer-chaincodedev mode.

We’ll leverage the CLI container to drive these calls.

Now issue an invoke to change the value of “a” to “20”.

Finally, query a. We should see a value of 20.

Suffering from Knowledge Quest

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store