How to install Hyperledger Fabric 2.2 on Ubuntu 20.04 in Google Cloud Platform
By: Jan Rock
Tags: Blockchain, Hyperledger Fabric, Guides And Tutorials, Hyperledger, Blockchain Development

Detailed documentation link: https://hyperledger-fabric.readthedocs.io/en/release-2.2/

Find a working end-to-end example of installation and quick test of Hyperledger Fabric is quite complicated. The only working guide seems to be the mentioned above. However, if you are a beginner, maybe you will get lost in the tons of details. The process is simple and the following guide will take to the stage, when you can start exploring deveploment and deployment of the smart contracts.

First you will need a virtual machine. 2Core/4GB should be ok:

Select the right boot disk (100GB should be enough and Ubuntu 20.04 LTS Minimal). Do not forget to add SSH public key to access the machine ;)

When instance is ready, SSH into that.

$ sudo apt update

$ sudo apt upgrade # dependencies

$ sudo apt-get install vim git wget curl net-tools apt-transport-https ca-certificates curl gnupg-agent software-properties-common

Next you will need a docker instance:

$ 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 docker-ce-cli containerd.io docker-compose $ sudo usermod -aG docker $USER

# or sudo chmod 777 /var/run/docker.sock # test if you can see empty list of containers

$ docker ps -a $ sudo systemctl start docker

$ sudo systemctl enable docker

Docker is up, now Golang. If you prefer Java, search hint in the full documentation.

$ wget https://dl.google.com/go/go1.15.2.linux-amd64.tar.gz $ sudo tar -xvf go1.15.2.linux-amd64.tar.gz # optional

$ rm go1.15.2.linux-amd64.tar.gz $ sudo mv go /usr/local # add it to .bashrc

$ export GOROOT=/usr/local/go $ sudo ln -s /usr/local/go/bin/go /usr/bin/go # final check

$ go version

Finally Hyperledger Fabric installation steps:

# this is the core installation script (I was suspicious where it is going, but you can check it before)

$ sudo curl -sSL https://bit.ly/2ysbOFE | bash -s # paths

$ cd fabric-samples/

export PATH=$PATH:/home/server/fabric-samples/bin

# update ~/.bashrc $ cd test-network/

# create channel (in my case "uni")

$ ./network.sh up createChannel -c uni # to shutdown and clean up docker containers use ./network.sh down # starting a chaincode on the channel

# I'm not sure if this step is necessary

$ ./network.sh deployCC -c uni

All done! Congratulations, you Blockchain server is up and running!

Now, how to test it that it actually works? The demo network containes some assets of two organisations. First we have assigned session to organisation (Org1):



$ export FABRIC_CFG_PATH=$PWD/../config/

$ export CORE_PEER_TLS_ENABLED=true

$ export CORE_PEER_LOCALMSPID="Org1MSP"

$ export CORE_PEER_TLS_ROOTCERT_FILE=${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt

$ export

$ export CORE_PEER_ADDRESS=localhost:7051 # identity Org1$ export FABRIC_CFG_PATH=$PWD/../config/$ export CORE_PEER_TLS_ENABLED=true$ export CORE_PEER_LOCALMSPID="Org1MSP"$ export CORE_PEER_TLS_ROOTCERT_FILE=${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt$ export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org1.example.com/users/Admin@org1.example.com /msp$ export CORE_PEER_ADDRESS=localhost:7051 # initialize the ledger with assets $ peer chaincode invoke -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --tls --cafile ${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C uni -n basic --peerAddresses localhost:7051 --tlsRootCertFiles ${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt --peerAddresses localhost:9051 --tlsRootCertFiles ${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt -c '{"function":"InitLedger","Args":[]}' # response# -> INFO 001 Chaincode invoke successful. result: status:200 # get list of assets

$ peer chaincode query -C uni -n basic -c '{"Args":["GetAllAssets"]}' # response something like this:

[{"ID":"asset1","color":"blue","size":5,"owner":"Tomoko","appraisedValue":300},{"ID":"asset2","color":"red","size":5,"owner":"Brad","appraisedValue":400},{"ID":"asset3","color":"green","size":10,"owner":"Jin Soo","appraisedValue":500},{"ID":"asset4","color":"yellow","size":10,"owner":"Max","appraisedValue":600},{"ID":"asset5","color":"black","size":15,"owner":"Adriana","appraisedValue":700},{"ID":"asset6","color":"white","size":15,"owner":"Michel","appraisedValue":800}] # Chaincodes are invoked when a network member wants to transfer or change an asset on the ledger. Use the following command to change the owner of an asset on the ledger by invoking the asset-transfer (basic) chaincode - from Michael to Christopher $ peer chaincode invoke -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --tls --cafile ${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C uni -n basic --peerAddresses localhost:7051 --tlsRootCertFiles ${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt --peerAddresses localhost:9051 --tlsRootCertFiles ${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt -c '{"function":"TransferAsset","Args":["asset6","Christopher"]}'

# get list of assets

$ peer chaincode query -C uni -n basic -c '{"Args":["GetAllAssets"]}' # response: [{"ID":"asset1","color":"blue","size":5,"owner":"Tomoko","appraisedValue":300},{"ID":"asset2","color":"red","size":5,"owner":"Brad","appraisedValue":400},{"ID":"asset3","color":"green","size":10,"owner":"Jin Soo","appraisedValue":500},{"ID":"asset4","color":"yellow","size":10,"owner":"Max","appraisedValue":600},{"ID":"asset5","color":"black","size":15,"owner":"Adriana","appraisedValue":700},{"ID":"asset6","color":"white","size":15,"owner":"Christopher","appraisedValue":800}] # to switch indentity just apply replace the env. variables:

$ export CORE_PEER_TLS_ENABLED=true

$ export CORE_PEER_LOCALMSPID="Org2MSP"

$ export CORE_PEER_TLS_ROOTCERT_FILE=${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt

$ export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp

$ export CORE_PEER_ADDRESS=localhost:9051 $ peer chaincode query -C uni -n basic -c '{"Args":["ReadAsset","asset6"]}' # response: {"ID":"asset6","color":"white","size":15,"owner":"Christopher","appraisedValue":800}

That’s it! I would like to add more to this topic later when I will have a working demo of Smart Contract in Golang.

Update: hyperledger-ansible-1.0 for GCP ( Source code (tar.gz))