A Containerised Development Environment for Hyperledger Fabric

0
3875

This article is a complete primer on how to set up the Hyperledger Fabric development environment on a local machine using Vagrant on an Ubuntu image. It also covers how to deploy Fabric by using Docker Compose to launch Docker containers.

The Hyperledger Fabric development environment uses either Vagrant running an Ubuntu image (which in turn launches Docker containers), or simply deploys the Fabric network with Docker Compose (which also launches Docker containers). This model allows developers to leverage their favourite OS/editors and execute the system in a controlled environment that is consistent across the development team.

Before we begin, if you haven’t already done so, you may wish to check that you have all the prerequisites listed below installed on the platform(s) on which you’ll be developing blockchain applications and operating Hyperledger Fabric.

  • Download the latest version of the cURL tool if it is not already installed or if you get errors running the curl commands from the documentation.
  • Install Docker and Docker Compose.
  • Install Vagrant if you wish to set up a development environment with it.
  • Hyperledger Fabric uses the Go programming language for many of its components. For development, Go version 1.11.x is required.
  • If you wish to develop applications for Hyperledger Fabric leveraging its SDK, you need supporting packages for SDK installed; e.g., for Node.js, you will need to have version 8.9.x of Node.js installed.
  • If you are developing on Windows 7, you will want to work within the Docker Quickstart terminal, which uses Git Bash and provides a better alternative to the built-in Windows shell.
  • On Windows 10, you should use the native Docker distribution and you may use the Windows PowerShell.

Setting up Git
Before running any Git clone commands, run the following:

git config --global core.autocrlf false
git config --global core.longpaths true

You can check the setting of these parameters with the following commands:

git config --get core.autocrlf
git config --get core.longpaths

These need to be false and true, respectively.

Set your GOPATH
Make sure you have properly set up your host’s GOPATH environment variable. This allows for building within the host, the VM and containers using the Fabric tools container.

Cloning the peer project
Create a fork of the Fabric repository using the GitHub Web interface. Next, clone your fork in the appropriate location, as follows:

cd $GOPATH/src
mkdir -p github.com/hyperledger
cd github.com/hyperledger
git clone https://github.com/<username>/fabric.git

Bootstrapping the VM using Vagrant
Now you’re ready to launch Vagrant, by using the following command:

cd $GOPATH/src/github.com/hyperledger/fabric/devenv
vagrant up

Go get yourself some coffee… this will take a few minutes. Once complete, you should be able to ssh into the Vagrant VM just created.

vagrant ssh

Starting your containerised deployment of the Fabric network
First, clone the Fabric samples repository (git clone https://github.com/hyperledger/fabric-samples).
If you don’t have Fabric installed in your environment, install it by using the following command:

cd fabric-samples/scripts
./fabric-preload.sh

Then proceed to start a basic Fabric network, as follows:

cd fabric-samples/basic-network
./start.sh

Where should the chaincode go?
A lot of developers mount the chaincode directly from the development folder — that’s a terrible idea. What you should do is develop chaincode in your GOPATH under the vendor-name github.com/yourproject/and-here-goes-your-chaincode-files. You can also write a script, which will copy your chaincode under GOPATH and run a go build command to spit out the go-errors before running it in your Fabric tools.

Here’s a simple script to copy your chaincode:

#!/bin/bash
echo “Copying your chaincode”
rm -rf $GOPATH/src/github.com/project-name/go & mkdir -p $GOPATH/src/github.com/project-name/go
cp -Rf chaincode/\* $GOPATH/src/github.com/project-name/go/

If you want to build the code, just add the following command:

#!/bin/bash
cd $GOPATH/src/github.com/project-name/go/
echo “Building chaincode”
go build.

Installing the chaincode
Now it’s time to launch our Armageddon. We have our network running, so let’s install the chaincode using the bash script we wrote, as follows:

#!/bin/bash
# install-chaincode.sh

LANGUAGE=golang
CC_SRC_PATH=github.com/project-name/go
CC_VERSION=1.0
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 project-name -v $CC_VERSION -p “$CC_SRC_PATH” -l “$LANGUAGE”

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 instantiate -o orderer.example.com:7050 -C mychannel -n project-name -l “$LANGUAGE” -v $CC_VERSION -c ‘{“Args”:\\\[“”\\\]}’ -P “OR (‘Org1MSP.member’)”

That’s all! In case you are wondering why a bash script has to be written for every small process, you can also write a single bash master script to control everything with logical conditions.

Updating the chaincode
This is where developers waste time – by updating the code, building it and deploying it again by updating the chaincode-version. Doing it in our style, let’s write a script. I have the script added to one of my personal repos and the link is provided in References.
That’s it. Run your queries and see the magic of your blockchain network with Hyperledger Fabric.

LEAVE A REPLY

Please enter your comment!
Please enter your name here