Hyperledger Development Environment On GCP

Pradeep Batchu
6 min readJul 2, 2019

In this article,We will go through steps needed to utilized GCP and create Hyperledger developer environment. Hyperledger does provide detail steps on setting up developer environment. Having enough disk space and memory on the machine important. My workstation is always installed with different versions of various libraries. Having a clean workspace is always easy to learn new technology. I choose GCP Compute engine to install Hyperledger developer environment.

I will not be explaining what was explained in detail in [Hyperledger introduction] (https://hyperledger.github.io/composer/latest/introduction/introduction) . This article is technical.

GCP Environment Setup

We need to create GCP compute engine with 2 CPUs and 7.5 GB memory on Ubuntu 16.04 LTS. I started by searching market place. In this section, we will set up Compute engine and firewalls that are needed for Hyperledger.

  1. Create ubuntu 16 version compute engine
Ubuntu compute engine with 50GB persistent disk and atleast 2 vCPU

2. In Network tags section in VM, Add tag “hyperledger”

3. Once the VM is started, Note internal IP and external IP.

4. Set up firewall rules. Be cautious on source IP range, For my testing i enabled any IP can access and any protocal is allowed.

We will need internal IP for setting up hyperledger fabrics networka and external ip to access composer rest server explorer.

Set up Basic utilities needed (pre-requisites) for Hyperledger

  1. Open compute engine VM SSH and following commands.
sudo apt-get install curlcurl -O https://hyperledger.github.io/composer/v0.19/prereqs-ubuntu.shchmod u+x prereqs-ubuntu.sh./prereqs-ubuntu.shlogout

2. Open SSH again and Check the versions

git --version
batchu_pradeepk@hyperledger-dev-env:~$ git — version
git version 2.22.0
batchu_pradeepk@hyperledger-dev-env:~$ npm — version
6.4.1
batchu_pradeepk@hyperledger-dev-env:~$ python — version
Python 2.7.12
batchu_pradeepk@hyperledger-dev-env:~$ docker version
Client:
Version: 18.09.7
API version: 1.39
Go version: go1.10.8
Git commit: 2d0083d
Built: Thu Jun 27 17:56:17 2019
OS/Arch: linux/amd64
Experimental: false
Server: Docker Engine — Community
Engine:
Version: 18.09.7
API version: 1.39 (minimum version 1.12)
Go version: go1.10.8
Git commit: 2d0083d
Built: Thu Jun 27 17:23:02 2019
OS/Arch: linux/amd64
Experimental: false

3. Install necessary hyperledger needed node packages

// Hyper ledger composer client that is needed to install the 
// network, create network cards etc
npm install -g composer-cli@0.20
// Hyperledger composer generator and yo will help in creating basic // code for code chain
npm install -g generator-hyperledger-composer@0.20
npm install -g yo

4. Hyperledger provided script to download the docker images that are needed for hyperledger fabric. Below is the script to get the docker images and also scripts to start the images

mkdir ~/fabric-dev-servers && cd ~/fabric-dev-servers curl -O https://raw.githubusercontent.com/hyperledger/composer-tools/master/packages/fabric-dev-servers/fabric-dev-servers.tar.gz tar -xvf fabric-dev-servers.tar.gz cd ~/fabric-dev-servers export FABRIC_VERSION=hlfv11 ./downloadFabric.sh

5. You should notice 5 images

downloadFabric should pull 5 images

6. Let’s start the fabric network by running ./startFabric.sh in fabric-dev-servers

7. Create peer admin card by running ./createPeerAdminCard.sh in fabric-dev-servers

batchu_pradeepk@hyperledger-dev-env:~/fabric-dev-servers$ composer card list
The following Business Network Cards are available:

Connection Profile: hlfv1
┌─────────────────┬───────────┬──────────────────┐
│ Card Name │ UserId │ Business Network │
├─────────────────┼───────────┼──────────────────┤
│ PeerAdmin@hlfv1 │ PeerAdmin │ │
└─────────────────┴───────────┴──────────────────┘

Issue composer card list — card <Card Name> to get details a specific card

Command succeeded

Create Fabric network sample

  1. Follow step 1, 2 and 3 from fabric tutorial

Remove logic.js file from test folder

batchu_pradeepk@hyperledger-dev-env:~/fabric-dev-servers/tutorial-network/test$ mv logic.js logic_js_old.backup

batchu_pradeepk@hyperledger-dev-env:~/fabric-dev-servers/tutorial-network$ composer archive create -t dir -n .
Creating Business Network Archive

Looking for package.json of Business Network Definition
Input directory: /home/batchu_pradeepk/fabric-dev-servers/tutorial-network

Found:
Description: sample
Name: tutorial-network
Identifier: tutorial-network@0.0.1

Written Business Network Definition Archive file to
Output file: tutorial-network@0.0.1.bna

Command succeeded

You should notice new file tutorial-network@0.0.1.bna file is created in the tutorial-network folder

2. Run following commands to create initial network and ping to check if network working.

../fabric-dev-servers/stopFabric.sh &&

../fabric-dev-servers/teardownFabric.sh &&

../fabric-dev-servers/teardownAllDocker.sh &&

composer card delete — card PeerAdmin@hlfv1

../fabric-dev-servers/startFabric.sh &&

../fabric-dev-servers/createPeerAdminCard.sh &&

composer network install — card PeerAdmin@hlfv1 — archiveFile tutorial-network@0.0.1.bna &&

composer network start — networkName tutorial-network — networkVersion 0.0.1 — networkAdmin admin — networkAdminEnrollSecret adminpw — card PeerAdmin@hlfv1 — file networkadmin.card &&

composer card delete — card admin@tutorial-network &&

composer card import — file networkadmin.card &&

composer network ping — card admin@tutorial-network

Create Rest Server that interact with hyperledger block chain

  1. Update text localhost in connection.json file which is located under ~/.composer/cards/admin@tutorial-network to internal ip address you got from GCP set up.

2. Copy ~/.composer to ~/composer-repo and update the access rights to 777 for all the files under ~/composer-repo.

cp -r ~/.composer ~/composer-repo
chmod -R 777 ~/composer-repo

If you have the network card any where out of this environment, Get it by ftp or git repos.

3. Create two files in a new docker folder. Dockerfile and startup.sh

startup.sh

cp -r ~/composer-repo ~/.composer
composer-rest-server -c admin@tutorial-network -n never -u true -w true

Dockerfile

FROM hyperledger/composer-rest-server
COPY startup.sh startup.sh
ENTRYPOINT ["/bin/sh","startup.sh"]

4. Build and Run docker

docker build -t test/composer-rest-server .
docker run -d -v ~/composer-repo:/home/composer/composer-repo -p 3000:3000 test/composer-rest-server

Note: If you want to run rest server in separate VM. Make changes to connection.json file under user network card. for example: /home/composer/composer-repo/admin@tutorial-network/connection.json

sed 's/localhost/<<EXTERNAL IP ADDRESS>>/g' connection.json > connectionfix.json
mv connectionfix.json connection.json
docker run -v ~/composer-repo:/home/composer/composer-repo -p 8080:3000 -d test/composer-rest-server

5. Access the explorer by running external ip address you got from GCP set up and access ipaddress:3000/explorer

Debugging

Note in case, if the explorer is not accessable debug by changing docker image.

FROM hyperledger/composer-rest-server
ENTRYPOINT ["/bin/sh"]

Test the settings

batchu_pradeepk@hyperledger-dev-env:~$ docker run -it -v  ~/composer-repo:/home/composer/composer-repo -p 3000:3000 test/composer-rest-server
~ $ cp -r ~/composer-repo ~/.composer
~ $ composer-rest-server
? Enter the name of the business network card to use: admin@tutorial-network
? Specify if you want namespaces in the generated REST API: never use namespaces
? Specify if you want to use an API key to secure the REST API: No
? Specify if you want to enable authentication for the REST API using Passport: No
? Specify if you want to enable the explorer test interface: Yes
? Specify a key if you want to enable dynamic logging:
? Specify if you want to enable event publication over WebSockets: Yes
? Specify if you want to enable TLS security for the REST API: No
To restart the REST server using the same options, issue the following command:
composer-rest-server -c admin@tutorial-network -n never -u true -w true
Discovering types from business network definition ...
Discovering the Returning Transactions..
Discovered types from business network definition
Generating schemas for all types in business network definition ...
Generated schemas for all types in business network definition
Adding schemas for all types to Loopback ...
Added schemas for all types to Loopback
Web server listening at: http://localhost:3000
Browse your REST API at http://localhost:3000/explorer

See if you able to get out as above.

What we have done with this article is to create a hyper ledger developer environment and composer rest server to access hyper ledger network.

--

--