Hyperledger Fabric By Example: Part 6
Wrapping up the series by exploring chaincode.

This article is part of a series starting with Hyperledger Fabric By Example: Part 1.
Chaincode
Now that we have a distributed ledger, we can begin to do something interesting; using chaincode.
A smart contract is code — invoked by a client application external to the blockchain network — that manages access and modifications to a set of key-value pairs in the World State. In Hyperledger Fabric, smart contracts are referred to as chaincode. Smart contract chaincode is installed onto peer nodes and instantiated to one or more channels.
— Hyperledger Fabric — Glossary
The following example closely follows the concepts described in the Hyperledger Fabric documentation (read before continuing).
- Applications and Smart Contract chaincode
- Installing a smart contract
- Instantiating a smart contract
- Endorsement policy
In this example, we are going to install a Node.js chaincode, aka, smart contract, named mycc onto peer0.org1.example.com and peer0.org2.example.com. We then instantiate it on the channel mychannel.

Using the CLI container:
docker exec -it cli bash
We execute the following command to install the chaincode onto peer0.org1.example.com:
peer chaincode install -n mycc -v 1.0 -l node -p /opt/gopath/src/github.com/chaincode/chaincode_example02/node/
and as before we set environment variables and execute on peer0.org2.example.com:
CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp CORE_PEER_ADDRESS=peer0.org2.example.com:7051 CORE_PEER_LOCALMSPID="Org2MSP" CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt peer chaincode install -n mycc -v 1.0 -l node -p /opt/gopath/src/github.com/chaincode/chaincode_example02/node/
We execute the following command to instantiate the chaincode on the channel.
peer chaincode instantiate -o orderer.example.com:7050 --tls --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C mychannel -n mycc -l node -v 1.0 -c '{"Args":["init","a", "100", "b","200"]}' -P "AND ('Org1MSP.peer','Org2MSP.peer')"
note: The instantiation of the Node.js chaincode will take roughly a minute. The command is not hanging; rather it is installing the fabric-shim layer as the image is being compiled.
Let us take a peek at the chaincode itself. By examining the CLI container configuration we can see the code is the following:
./chaincode/chaincode_example02/node/package.json
./chaincode/chaincode_example02/node/chaincode_example02.js
Observations:
- To be honest, it is not overly clear what this code is doing. The Hyperledger Fabric Writing Your First Application has a better example and explanation of writing chaincode
Finally, we execute the chaincode (in this case the query method) with the following command:
peer chaincode query -C mychannel -n mycc -c '{"Args":["query","a"]}'
It will log the number 100 to the screen.
Wrap Up
While I found the Hyperledger Fabric concepts to be super intriguing, the implementation was a little rough; a lot of blind copy and pasting of configuration. Guess it is a new project that will likely improve rapidly over time.
That being said, we did accomplish our goal of reverse engineering the Build Your First Network Example tutorial.

