Adventures in Blockchain

Daz Wilkin
Nov 4, 2017 · 4 min read

I’m spending time in November improving my knowledge of Blockchain technologies (and growing a moustache for Movember). I started in my comfort zone by deploying Hyperledger Fabric to a Google Cloud Platform (GCP) VM and spun through the ‘fabcar’ sample app.

Deploying Fabric and running ‘fabcar’ is straightforward so this post mostly serves as a foundational post and may be of interest if you’d like to run Fabric on GCP.

Hyperledger Fabric

I’m running everything on an Ubuntu 16.04 VM on GCP. If you don’t have a GCP account, you can get started for free here. I assume you’re using a Linux machine with Google’s Cloud SDK installed to create the VM:

BILLING=[[YOUR-BILLING-ID]]
PROJECT=[[YOUR-PROJECT-ID]]
ZONE=[[YOUR-PREFERRED-ZONE]] // us-west1-c
INSTANCE=hyperledger-fabric-01gcloud alpha projects create $PROJECTgcloud alpha billing projects link $PROJECT --account-id=$BILLINGgcloud services enable compute.googleapis.com --project=$PROJECTgcloud compute instances create ${INSTANCE} \
--project=${PROJECT} \
--zone=${ZONE} \
--machine-type=custom-2-8192 \
--image=ubuntu-1604-xenial-v20171011 \
--image-project=ubuntu-os-cloud \
--boot-disk-size=50 \
--boot-disk-type=pd-standard \
--boot-disk-device-name=${INSTANCE}
gcloud compute ssh ${INSTANCE} \
--zone=${ZONE} \
--project=${PROJECT}

From the ssh session on the ‘hyperledger-fabric-01’, we need to install multiple pre-requisite components.

https://hyperledger-fabric.readthedocs.io/en/release/prereqs.html

Here we go!

Docker-CE

sudo apt-get updatesudo apt-get install \
linux-image-extra-$(uname -r) \
linux-image-extra-virtual
sudo apt-get updatesudo apt-get install \
apt-transport-https \
ca-certificates \
curl \
software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -sudo add-apt-repository \
"deb [arch=amd64] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) \
stable"
sudo apt-get update
sudo apt-get install docker-ce
sudo usermod -aG docker $USER
sudo systemctl enable docker
docker --version

docker-compose

sudo curl -L https://github.com/docker/compose/releases/download/1.16.1/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-composesudo chmod +x /usr/local/bin/docker-composedocker-compose --version

Golang

VERSION=1.9.1
OS=linux
ARCH=amd64
sudo curl \
--location https://golang.org/dl/go$VERSION.$OS-$ARCH.tar.gz \
--output go$VERSION.$OS-$ARCH.tar.gz
sudo tar -C /usr/local -xzf go$VERSION.$OS-$ARCH.tar.gzexport PATH=$PATH:/usr/local/go/bingo version

Node.js

curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -
sudo apt-get install -y nodejs
nodejs --version
v6.11.5
npm --version
3.10.10

Downgrade Python (on Ubuntu)

sudo apt-get install pythonpython --version
Python 2.7.12

Fabric Samples: fabcar

git clone https://github.com/hyperledger/fabric-samples.gitcd fabric-samples/fabcar

Trick: Hyperledger hosts Fabric containers on dockerhub. Because these are built for various runtime architectures, there’s no singular “latest” container. The trick is to identify and download the most recent versions of the containers, they’re versioned in lock-step which is convenient, and then tag these with “latest”. Here’s a script for Linux X86_64:

TAG=x86_64-1.0.3
for IMAGE in orderer couchdb peer ca tools
do
docker pull hyperledger/fabric-${IMAGE}:${TAG} && \
docker tag hyperledger/fabric-${IMAGE}:${TAG} hyperledger/fabric-${IMAGE}:latest
done

Once this completes:

docker images \
--format="{{ .Tag }}\t{{ .Repository }}" \
| grep hyperledger \
| sort
latest hyperledger/fabric-ca
latest hyperledger/fabric-couchdb
latest hyperledger/fabric-orderer
latest hyperledger/fabric-peer
latest hyperledger/fabric-tools
x86_64-1.0.3 hyperledger/fabric-ca
x86_64-1.0.3 hyperledger/fabric-ccenv
x86_64-1.0.3 hyperledger/fabric-couchdb
x86_64-1.0.3 hyperledger/fabric-orderer
x86_64-1.0.3 hyperledger/fabric-peer
x86_64-1.0.3 hyperledger/fabric-tools

Then run the following command to run the Fabric test cluster:

./startFabric.sh

You should see a stream of logs ending:

2017-11-04 00:03:42.880 UTC [chaincodeCmd] chaincodeInvokeOrQuery -> INFO 00a Chaincode invoke successful. result: status:200 
2017-11-04 00:03:42.880 UTC [main] main -> INFO 00b Exiting.....
Total execution time : 33 secs ...

and, you should be able to:

docker ps --format="{{ .ID }}\t{{ .Names }}"bdcad9115060    dev-peer0.org1.example.com-fabcar-1.0
e3a219fd5c50 cli
160318483edd peer0.org1.example.com
5caa3f261c05 couchdb
f1b93255fa5d ca.example.com
385d784e0581 orderer.example.com

Alright! There’s one more pre-req step:

sudo apt-get install build-essentialnpm install

and now you should be able to:

node query.jsCreate a client and set the wallet location
Set wallet path, and associate user PeerAdmin with application
Check user is enrolled, and set a query URL in the network
Make query
Assigning transaction_id: 2b7d262eca16c68523fe61331f08114110f6c6b4d6f1d4725063fcb00d8da4d7
returned from query
Query result count = 1
Response is [
{"Key":"CAR0", "Record":{"colour":"blue","make":"Toyota","model":"Prius","owner":"Tomoko"}},{"Key":"CAR1", "Record":{"colour":"red","make":"Ford","model":"Mustang","owner":"Brad"}},{"Key":"CAR2", "Record":{"colour":"green","make":"Hyundai","model":"Tucson","owner":"Jin Soo"}},
{"Key":"CAR3", "Record":{"colour":"yellow","make":"Volkswagen","model":"Passat","owner":"Max"}},{"Key":"CAR4", "Record":{"colour":"black","make":"Tesla","model":"S","owner":"Adriana"}},{"Key":"CAR5", "Record":{"colour":"purple","make":"Peugeot","model":"205","owner":"Michel"}},{"Key":"CAR6", "Record":{"colour":"white","make":"Chery","model":"S22L","owner":"Aarav"}},{"Key":"CAR7", "Record":{"colour":"violet","make":"Fiat","model":"Punto","owner":"Pari"}},{"Key":"CAR8", "Record":{"colour":"indigo","make":"Tata","model":"Nano","owner":"Valeria"}},{"Key":"CAR9", "Record":{"colour":"brown","make":"Holden","model":"Barina","owner":"Shotaro"}}
]

I won’t replicate the Fabric tutorial here, instead:

https://hyperledger-fabric.readthedocs.io/en/latest/write_first_app.html

Conclusion

Pre-req’s aside, it’s straightforward to get a Fabric test cluster running and to use the ‘fabcar’ sample. Fabric is written in Golang and supports Golang and Java for contracts and, in this case, Node.js as a client.

Tear-down

To shutdown the Fabric cluster:

cd ../basic-network
docker-compose --file ./docker-compose.yml down
Stopping cli ... done
Stopping peer0.org1.example.com ... done
Stopping orderer.example.com ... done
Stopping couchdb ... done
Stopping ca.example.com ... done
Removing cli ... done
Removing peer0.org1.example.com ... done
Removing orderer.example.com ... done
Removing couchdb ... done
Removing ca.example.com ... done
Removing network net_basic

You may “exit” from all the ssh sessions.

You should be returned to your host session. The one from which you ran the gcloud commands. You may either stop the hyperledger-fabric-01 VM:

gcloud compute instances stop ${INSTANCE} \
--zone=${ZONE} \
--project=${PROJECT}

Or (permanently) delete it:

gcloud compute instances delete $(INSTANCE} \
--zone=${ZONE} \
--project=${PROJECT}
--quiet

Or simply delete the entire GCP project:

gcloud alpha projects delete ${PROJECT} --quiet

That’s it!

Google Cloud Platform - Community

A collection of technical articles published or curated by Google Cloud Platform Developer Advocates. The views expressed are those of the authors and don't necessarily reflect those of Google.

 by the author.

Daz Wilkin

Written by

Google Cloud Platform - Community

A collection of technical articles published or curated by Google Cloud Platform Developer Advocates. The views expressed are those of the authors and don't necessarily reflect those of Google.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade