Hyperledger Composer Demystified!
What is Blockchain, and what it means to IBM?
“Blockchain is a shared, immutable ledger for recording the history of transactions. It fosters a new generation of transactional applications that establish trust, accountability and transparency.”
In 3 simple words, we can define blockchain as “Decentralised Immutable Assets”
Hyperledger Composer is based on Hyperledger Fabric. Hyperledger fabric is an open source business blockchain created by Linux Foundation. IBM has massively contributed to the Hyperledger project and has accomplished various project based on Hyperledger Fabric. Unlike Ethereum and other Blockchain which are based on the concept of cryptocurrency, IBM has taken a step forward to evolve in an area of private and permission-based business blockchain, where participants know each other. Hyperledger Fabric blockchain is based on “Practical Byzantine Fault Tolerant” protocol. Unlike Ethereum’s PoW (Proof or Work) protocol, Hyperledger Fabric PBFT protocol doesn’t have a concept of mining which makes it much well performant than Ethereum in terms of computation but it lacks scalability which is a trade-off.
Building your first Blockchain, creating a mini student grade portal “BlockchainUDC”
What is Hyperledger Fabric ?
Hyperledger Composer simplifies application development on top of the Hyperledger Fabric blockchain infrastructure.
Prerequisites
- Install Docker
- Install VSCode, and VSCode Hyperledger Composer Plugin
- Install required npm node packages
npm install -g composer-cli
npm install -g generator-hyperledger-composer
npm install -g composer-rest-server
npm install -g yo
3. Setting and running Hyperledger Fabric
mkdir fabric-tools
cd fabric-tools
curl -O https://raw.githubusercontent.com/hyperledger/composer- tools/master/packages/fabric-dev-servers/fabric-dev-servers.zip
unzip fabric-dev-servers.zip
./downloadFabric.sh
./startFabric.sh
./createPeerAdminCard.sh
Instantiating your Business Blockchain with Composer
- Create a new project directory
mkdir app
cd app
2. Instantiate a Business network by yo npm plugin
yo hyperledger-composer:businessnetwork
> Business network name: my-network
> Description: A simple blockchain network
> Author name: saif
> Author email: s4saif.121@email.com
> License: Apache-2.0
> Namespace: org.acme.mynetwork
3. Navigate to business network folder and install the dependencies
cd my-network
npm install
- You define your models which can be Participant, Asset, Event, and Transaction in .cto file which over here is org.acme.mynetwork.cto
- You define your blockchain logic of smart contract in logic.js under lib folder
4. Coding your model file
- Line 8–12: You create a participant that will participate in the blockchain which in this case is the professor. You define your class member with annotation ‘o’ and relationship with ‘ →’
- Line 15–19: You define your asset which is the grade of the student called assetGrade. This asset has studentID and grade.
- Line 22–25: You create a smart contract to change student’s final grade
5. Coding your logic.js
- Here you code the functionality of your smart contract which is your business logic.
- Line 7 : Declare transaction which is your smart contract instantiated in your model file
- Line 10–18: Prototypes your transaction function which is your smart contract. Line 11 saves the new grade in your asset model. Line 13 calls the assetregistry method that manages asset stored in the blockchain. Line 14–17 is a callback promise if the contract is successfully accessible with the correct namespace defined. Line 17 updates the asset in the blockchain which is your new grade
Creating the .bna file
Once everything is set let’s create a .bna file which defines your compiled logic of your blockchain
mkdir dist
composer archive create --sourceType dir --sourceName . -a ./dist/network.bna
Test your Blockchain application on composer playground
What is composer playground?
Hyperledger Composer Playground! In this web sandbox, you can deploy, edit and test business network definitions
Go to https://composer-playground.mybluemix.net to use composer playground
- Create Import/Replace button and select Drop Here to Upload or Browse
2. Choose your .bna created from previous command
3. Go to test directory to actually test your business blockchain application
4. Create a new participant, in this case it is professor
5. Create a empty asset which is the student grade
6. Invoke the smart contract to change the grade by clicking on Submit Transaction Button
7. The smart contract will be invoked and the asset will change.
8. You will have history of all transaction made through smart contract in All Transaction button
Deploying the .bna file on the Hyperledger Fabric Blockchain
> composer network install --card PeerAdmin@hlfv1 --archiveFile <business-network-archive>composer network start --networkName <business-network-name> --networkVersion <business-network-version> --networkAdmin <admin-name> --networkAdminEnrollSecret <enroll-secret> --card <peer-admin-card> --file <admin-card-file-name>
> composer network start --card PeerAdmin@hlfv1 -A admin -S adminpw -a dist/network.bna -f networkadmin.card
> composer card import -f networkadmin.card
Interfacing you blockchain with Restfull CRUD Apis
> composer-rest-server
Explore your Restful Apis created by loopback on http://localhost:3000 :)
Now you can create a proper front end application in Angular/React/Vuejs or which even framework of your choice by leveraging these rest apis.
Read more:
- Follow me for more: https://www.engineerability.com
Conclusion
This tutorial has only covered basic just to get started with Hyperledger Composer based on Hyperledger Fabric by using Hyperledger Composer. We successfully created a end-to-end blockchain that allow the professor to store the grade of the student. By working on top of this tutorial you create a blockchain replica of blackboard. With blockchain the possibilities are endless.
If this was a informative blog, clap if you liked it :) A lot of effort has been put writing this blog.