How to deploy chaincode in Hyperledger Fabric?

Davor Kljajic
5 min readJan 24, 2019

--

What is chaincode ?

The chaincode is a program that handles business logic agreed to by members of the network, so it may be considered as a “smart contract. Chaincode runs in a secured Docker container isolated from the endorsing peer process.
Chaincode initializes and manages ledger state through transactions submitted by applications.

It is installed and instantiated through an SDK or CLI onto a network of Hyperledger Fabric peer nodes, enabling interaction with that network’s shared ledger.

It can be written in Go, node.js, or Java that implements a prescribed interface.

In this article, I will show how to deploy chaincode through CLI via script(.sh) in the docker container also named cli.

Type of chaincodes

IBM offers two different perspectives on chaincode.

Chaincode for Operators

Blockchain network operator who is responsible for managing a blockchain network, and who would leverage the Hyperledger Fabric API to install, instantiate, and upgrade chaincode, but would likely not be involved in the development of a chaincode application.

Chaincode for Developers

Developing a blockchain application/solution.

Chaincode phases with cli container

The real question is why to use this way of deploying chaincode?

First of all compatibility, all Hyperledger components are deployed via docker containers, the second is the speed of deploying. In CLI deploying without cli container, install, instantiate and invoke must be executed on each peer separately with lots of parameters. You can see in official documentation in Hyperledger Fabric.

So in our case, we decide in this way.

  1. create folder chaincode
  2. create package.json & install node_modules
  3. mounting the folder chaincode in cli container
  4. Install chaincode
  5. Instantiate
  6. Invoke chaincode

Before we start all component of the network are running. If you have some problems or if you are new in Hyperledger Fabric you can check here.

In our case, we will create chaincode in the root of the project. Create package.json with fabric-shim that provides the chaincode interface, a lower level API for implementing “Smart Contracts.

package.json

folder structure after 2 phases

2.folder_structure

mounting the folder chaincode in cli container

In this case, we will use cli container to deploy chaincode in one organization, if you have multiple organization in your network you can use for each organization specific cli container.

Two things are important. Mounting the right local folder of chaincode and certificates.

cli:# name of container, change name for multiple org
container_name: cli
image: hyperledger/fabric-tools:$IMAGE_TAGtty: truestdin_open: trueenvironment:- GOPATH=/opt/gopath- CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock#- CORE_LOGGING_LEVEL=DEBUG- CORE_LOGGING_LEVEL=INFO- CORE_PEER_ID=cli# peer that handles install and instantaite chaincode in channel- CORE_PEER_ADDRESS=peer0.org1.example.com:7051
# MSP directory of all org certificates ##important
- CORE_PEER_LOCALMSPID=Org1MSP
#MSP directory of peer ##important
CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp
working_dir: /opt/gopath/src/github.com/hyperledger/fabric/peercommand: /bin/bashvolumes:- /var/run/:/host/var/run/
#local path of chaincode ## important
- ./../chaincode/:/opt/gopath/src/github.com/chaincode- ./crypto-config:/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/- ./scripts:/opt/gopath/src/github.com/hyperledger/fabric/peer/scripts/
## local path of peer certificates
- ./channel-artifacts:/opt/gopath/src/github.com/hyperledger/fabric/peer/channel-artifactsdepends_on:- orderer.example.com- peer0.org1.example.com- peer1.org1.example.comnetworks:- our_network

The most mistakes are made in this env. :

CORE_PEER_MSPCONFIGPATH,

CORE_PEER_ADDRESS

CORE_PEER_LOCALMSPID

In our company, we create a script that handles install, instantiate and invoke chaincode. I put some comments in the script for better understanding.

#!/bin/bash
#
# Copyright IBM Corp All Rights Reserved
#
# SPDX-License-Identifier: Apache-2.0
#
# Exit on first error, print all commands.
set -e
# don't rewrite paths for Windows Git Bash users
export MSYS_NO_PATHCONV=1
starttime=$(date +%s)
# Be carefully default is golang, number 1 is parametar :)
LANGUAGE=${1:-"node"}
# version of chaincode
CC_VERSION=${2:-"v0"}
# path of cli container
CC_SRC_PATH=/opt/gopath/src/github.com/chat/node# clean the keystore
rm -rf ./hfc-key-store
# start cli container (managment of chaincode)
# cli for each company, change container name in this case
docker-compose -f ./docker-compose.yml up -d cli # install chaincode on org1 peer0
docker exec -e "CORE_PEER_LOCALMSPID=Org1MSP" -e "CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp" cli peer chaincode install -n chat -v "$CC_VERSION" -p "$CC_SRC_PATH" -l "$LANGUAGE"
# instantiate chaincode org1, only once in channeldocker exec -e "CORE_PEER_LOCALMSPID=Org1MSP" -e "CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp" cli peer chaincode instantiate -o orderer.example.com:7050 -C mychannel -n chat -l "$LANGUAGE" -v "$CC_VERSION" -c '{"Args":["init"]}' -P "OR ('Org1MSP.member','Org2MSP.member')"
#sleep 10
# invoke chaincode org1
#docker exec -e "CORE_PEER_LOCALMSPID=Org1MSP" -e "CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp" cli peer chaincode invoke -o orderer.example.com:7050 -C mychannel -n chat -c '{"function":"createFunc","Args":[""]}'

This script must be executable, you can do with command “sudo chmod 777 nameOfscript.sh”.

logs

#INSTALL
2019–01–23 21:06:34.671 UTC [chaincodeCmd] install -> INFO 005 Installed remotely response:<status:200 payload:”OK” >

Instantiate is a method that creates new docker container with name od peer, chaincode name and chaincode version. To check logs you can go in the newly created container.

Invoke method is used for invoking transactions in chaincode.

I will share some tip for you, you can mount the folder scripts as we here and put above script in that folder. After that execute cli container with “comand docker exec -it cli bash” , in the scipts folder and execute. Also you can exports env variables of other organizations and do the same thing. You will have install,instantiate and invoke chaincode in multiple organizations.

Conclusion

In this article, we deploy Node.js chaincode in the Hyperledger Fabric via cli docker container. The same script with a change of commands and some params, you can use for upgrading the chaincode on the newest version. In the next article, i will write about a simple chaincode application that manages some sort of “assets”, also about main methods in chaincode.

You can check here official Hyperledger documentation for the network.

Work with us!

Beyondi offers high-quality services: Web Development and Design, Mobile Development and Design, Embedded Solutions, Blockchain Solutions, Digital Marketing, SEO, Growth and Team Augmentation? You can find more about our services on our website.

--

--