This part serves a next step after completing part 1 of the guide. In part 1, we discussed the different components that are needed to bootstrap a simple network. We formed one organization, created a channel, and joined that organization to that channel. In this part, we are going to write a chaincode (smart contract), install and instantiate it, and interact with it. We are going to use the repository that we developed in the previous guide. The repository contains both the network and chaincode as well as useful scripts to automate crucial tasks. Chaincode (smart contract) is a piece of code that is used to provide access to the state of the ledger in a channel. Chaincode is the only way to gain access to the ledger and the ledger cannot be accessed outside of that chaincode. Chaincode can also be used to implement access control permissions as well as complex business logic while updating or reading the data in the ledger. We are going to use Typescript and the fabric-node-chaincode-utils to write chaincode. If you have experience writing smart contracts using Solidity in Ethereum or other public blockchain, you might notice similarities. We have a chaincode class named FirstChaincode which has two functions that are available to be executed from outside the ledger. initLedger function adds car entries into the ledger. The second function queryAllCars returns all the car objects that have been initialized. We use the chaincode utils developed by the TheLedger. You will find the chaincode Node.js SDK to be quite different if you look at the official chaincode samples developed by Hyperledger Fabric. These utilities are intended to make writing chaincode easier. We are going to start the network that we built in the previous article, create and join channels, install and instantiate the chaincode, and finally call functions on the chaincode. Go to the network directory and run the following commands. This command will start all the docker containers. A new channel mychannel is created and Org1 is joined to that channel. There are three steps to installing chaincode. Go to the chaincode/node directory. The following commands will install all the dependencies and make a new build out of source code to be installed on peers. This command will install the chaincode on the peer. This command will instantiate the chaincode on the peer. Now, that we have the network deployed and chaincode installed and instantiated. We will interact with the chaincode. This command will make use of the invoke command to execute initLedgerfunction which will populate the ledger with entries. You should see the following message. The command below will run the queryAllCars function get all the cars that have been put onto the ledger. If you see an output like this, your invoke function was successful. We created a simple chaincode which adds entries in the ledger and queries them. We might get into exploring the Node.js SDK that enables us to build client applications for Hyperledger Fabric network. If you want to get your hands on the code, you can get it here. Interested in starting your own blockchain project, but don’t know how? Get in touch at https://xord.solutions/contactOverview
What is chaincode?
Prerequisites to writing chaincode
npm install typescript -g
mkdir chaincode/node
cd chaincode/node
tsc --init
npm install
Writing chaincode
Building the network
Starting the network
docker-compose -f docker-compose.yml up -d
Creating and joining the channel
docker exec -e "CORE_PEER_LOCALMSPID=Org1MSP" -e "CORE_PEER_MSPCONFIGPATH=/etc/hyperledger/msp/users/[email protected]/msp" peer0.org1.example.com peer channel create -o orderer.example.com:7050 -c mychannel -f /etc/hyperledger/configtx/channel.txdocker exec -e "CORE_PEER_LOCALMSPID=Org1MSP" -e "CORE_PEER_MSPCONFIGPATH=/etc/hyperledger/msp/users/[email protected]/msp" peer0.org1.example.com peer channel join -b mychannel.block
Installing the chaincode
Building
yarn
yarn run clean
yarn run build
Installing
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/[email protected]/msp" cli peer chaincode install -n "firstchaincode" -v "1.0" -p "/opt/gopath/src/github.com/firstchaincode/node" -l "node"
Instantiating
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/[email protected]/msp" cli peer chaincode instantiate -o orderer.example.com:7050 -C mychannel -n "firstchaincode" -l "node" -v "1.0" -c '{"function":"init","Args":["'1.0'"]}'
Executing a transaction
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/[email protected]/msp" cli peer chaincode invoke -o orderer.example.com:7050 -C mychannel -n "firstchaincode" -c '{"function":"initLedger","Args":[""]}'
2019-03-14 08:56:24.608 UTC [chaincodeCmd] chaincodeInvokeOrQuery -> INFO 04f Chaincode invoke successful. result: status:200
Querying the chaincode
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/[email protected]/msp" cli peer chaincode query -o orderer.example.com:7050 -C mychannel -n "firstchaincode" -c '{"function":"queryAllCars","Args":[""]}'
[{"color":"black","docType":"car","id":"1","make":"Honda","model":"Civic"},{"color":"blue","docType":"car","id":"2","make":"Toyota","model":"Prius"},{"color":"red","docType":"car","id":"3","make":"Suzuki","model":"Ciaz"}]
Conclusion
Get in touch