Header Ads

Building Blockchain with Hyperledger : Developer's Guide (Part 1)


In our previous post about blockchain, we described the basics and terminologies of blockchain technology and its success stories in various fields including supply chain management, prediction markets, identity management stock trading, governance, etc.

As a quick recap of the blockchain, IBM is collaborating with other organizations with the aim of developing an open source implementation of the blockchain. The business use case is hosted by The Linux Foundation and is called the Hyperledger Fabric.


Now that you are aware of the fundamentals, we will go deep with the implementation of blockchain and will walk you through the fundamentals of 'Building Your Own BlockChain' ( using Hyperledger Fabric v1.0 ).

Setup a Blockchain Network

This is the first step where you start from, which will help you setup a blockchain network and allow you to develop, test and deploy the code.

There are two ways which can be followed in setting up the network which includes the Hyperledger Fabric code base on GitHub or IBM blockchain service on Bluemix.

1) Using Hyperledger Fabric code base on GitHub


Before you start the installation make sure the prerequisites are in place which includes
  1. Installation of cURL tool
  2. Docker and Docker Compose which is a platform on which Hyperledger Fabric will be developed and operated.
  3. Node.js Runtime and NPM: In order to develop applications for Hyperledger Fabric the version 6.9x of Node.js must be installed.
After the prerequisites are satisfied, binaries and docker images must be installed along with Hyperledger fabric samples. There are many tutorials available on the web which would act as a great help for building the first blockchain application.

Further, a fully annotated script (byfn.sh) is present which leverages Docker images to bootstrap the network. This network is comprised of 4 peers each representing two different organizations and order note. A script will run to launch a container which will join peers to channel, deploy and instantiate chain-code. This will then drive execution of transactions against deployed chain-code.
Below is the helptext for byfn.sh script :

./byfn.sh -h

Usage:

  byfn.sh -m up|down|restart|generate [-c <channel name>] [-t <timeout>]  
  byfn.sh -h|--help (print this message)  
   -m <mode> - one of 'up', 'down', 'restart' or 'generate'  
    - 'up' - bring up the network with docker-compose up  
    - 'down' - clear the network with docker-compose down  
    - 'restart' - restart the network  
    - 'generate' - generate required certificates and genesis block  
   -c <channel name> - config name to use (defaults to "mychannel")  
   -t <timeout> - CLI timeout duration in microseconds (defaults to 10000)  

Typically, one would first generate the required certificates and genesis block, then bring up the network. e.g.:

  byfn.sh -m generate -c <channelname>  
  byfn.sh -m up -c <channelname>  

Post this, network artifacts should be generated with following command :

 ./byfn.sh -m generate  

Further to which you should respond in “y” for every prompt. This will generate all certificates and keys for various network entities. Post this, the below command will bring up the network:

 ./byfn.sh -m up  

Once again, respond in “y” for every prompt.

For more details, you can learn about bootstrap mechanics and underlying tools to build a fully functional Hyperledger Network.

2) Using the Blockchain service on Bluemix


Bluemix supports several network plans, the Starter Network Plan and High-Security Business Network Plan both of which are built on Hyperledger Fabric.

The High-Security Business Network plan is open to the new subscriber and same services can be signed up on Blockchain service page in Bluemix.

Deploying Chaincode

Now that the network is setup and running, you are ready to deploy chaincode which enables the interaction with the network’s shared ledger. The chaincode function in the ledger reads and writes values to the ledger, whenever a transaction is invoked on the network. Chaincode can be written in languages of GoLang or Java which can be installed and instantiated through SDK or CLI onto the network of peer nodes. In order to build an effective chaincode, let's go through the following steps and setup development pipeline.
  1. Sample code can be downloaded from http://gopkg.in/ibm-blockchain/learn-chaincode.v1/finished OR http://gopkg.in/ibm-blockchain/learn-chaincode.v2/finished. Changes can be made to this code post which can then be compile on your machine.
  2. The updates are pushed to GitHub.
  3. Deploy your updated chaincode to your local Hyperledger network using the fabric REST API.
  4. Test your chaincode using the fabric REST API.
  5. Repeat.

Write and Deploy Client Apps

Now that network is built and chaincode is registered, the client application can be built which can invoke the business rules defined by your chaincode. This implementation can be eased by use of Hyperledger Fabric Client SDK. The API’s provided by the SDK allows applications to interact with Hyperledger Fabric blockchain.

HFC SDK leverages Node.js applications which perform the following tasks on the network:
  1. Users who have authenticated to the web application can be dynamically registered and enrolled with the web application
  2. All transactions are anonymous, confidential and unlinkable without auditor’s authority and can be submitted to the blockchain network.
  3. A simple key value store interface can be implemented. The private keys and certificates can be stored in any location such as an off-blockchain database.
There are two ways by which application developers can be carried out.

1) Application Development by Writing client apps using Bluemix


A custom business solution can be built by using marbles02 example and the corresponding javascript app. The application communication flow for marbles02 can be seen in below image.


The code can be deployed by following the below steps:

a) Download Marbles to your system, open command prompt and browse to your working directory with the following command

 git clone https://github.com/IBM-Blockchain/marbles.git --depth 1  
 git checkout v3.0  

b) Create a blockchain network with Bluemix IBM Blockchain Service or locally hosted Hyperledger Fabric Network

c) Install and instantiate chaincode with IBM blockchain service or SDK locally.

d) Host marbles locally or on IBM Bluemix.

e) Navigate to http://localhost:3001 or Bluemix www route and test the application by clicking on '+' icon.


This will create your new marble application. Source code for marbles and its prerequisites can be found on the git-hub.

2) Writing client apps using DockerHub images


Alternatively, DockerHub images can be used to build a client application. This project is published by using two separate NPM packages :
  1. - fabric-client: This is used to install, instantiate, submit transactions and make queries against HFN.
  2. - fabric-ca-client: This component will allow application to enroll peers and application users to submit trusted identities on HFN
The project can be cloned and launched by using following commands and install dependencies to perform various tasks :
  1. npm install - installs various dependencies.
  2. gulp watch - sets up watch that updates fabric-ca-client’s shared dependencies from fabric-client/lib and updates installed fabric-client.
  3. Gulp-doc generates API docs to review the content
  4. Npm- test - runs the test that do not require additional setup.
Further docker images can be setup in local machine.

Now that a custom application is built, stay tuned to get more information on blockchain and about the subsequent steps. In our next post, we will see how you can manage and monitor the developed blockchain networks and apps, help build the Hyperledger Fabric by contributing code and get help and support from Bluemix, Hyperledger Fabric code base and general blockchain forums.

Have an idea to implement blockchain ? Share the same with us in comments below.