Sunday, October 11, 2020

Blockchain | Hyperledger Fabric | Package Chaincode - Chaincode LifeCycle

In Hyperledger Fabric chaincode Lifecyle, package of chaincode is very important step so I will show you how to package your chaincode in chaincode LifeCycle.

You need to ensure that you have chaincode development and compiled. You can develop your chaincode in Go Lang, Java, Node, Type Script, Java Script.

In ideal scenario, packaging of chaincode is done by only one organization in network rather that need to be done by each organization. One of the organization can package the chaincode and then send that package to all other organizations.

When you package the chaincode, it create a file with extension .tar.gz.

Now let's see what steps you need to follow for chaincode packaging. 

First you need to ensure that your network is up & running and channel is also created.

Before you package the chaincode, you need to set some variable so you can simply run below commands for that.

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

Now run below command to package your chaincode. You need to ensure that you are under fabric-samples/test-network folder.

peer lifecycle chaincode package car.tar.gz --path ../chaincode/car/go/ --lang golang --label car_1

Here

car.tar.gz is your packaged chaincode

../chaincode/car/go is the path of your compiled chaincode

golang is chaincode language 

car_1 is label of your chaincode package



Blockchain: Hyperledger Fabric 2.2 Setup on Ubuntu

This post explains how to setup Hyperledger Fabric 2.2 on Linux operating system.

Before you install Hyperledger Fabric, you need to do prerequisites setup where you will install

Curl , NodeJs, Git, Python, Go Language, Libtool, Docker CE & Docker Compose

 Now let's see how to install all these one by one.

Curl Installation

 Run below command to install Curl.

 sudo apt-get install curl

 To verify the installation, run below command.

 curl --version

 NodeJs Installation

 Open the terminal window and run below command to download and execute the nodejs file.

 curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash –

 Then run below command.

 sudo apt-get update

 Run below command to start the installation for NodeJs.

 sudo apt-get install nodejs

 Run below command to check if Nodejs is successfully installed or not. This should return the version of NodeJs.

 node --version

 Git Installation

 Open the terminal window and run below command. This will start the installation for Git.

 sudo apt-get install git

Run below command to check if Git is successfully installed or not. This should return the version of Git.

 git --version

 Python Installation

 In the terminal window, run below command to install Python.

 sudo apt-get install python

 Verify the installation by running below command and that should return the version of Python.

 python --version

 Go Language

Step 1: Very first step is to download the installer, for that go to https://golang.org/ site. Click on "Download Go" button and that should take you to next page.

Step 2: Click on Linux tar file which is Linux installer and that should download the installer for you.

Step 3: First untar the downloaded file. Since the file is downloaded in Downloads folder so first you need to go to that folder and then run below command.

 cd Downloads

sudo tar -xvf go1.13.4.linux-amd64.tar.gz

Step 4: You should be able to see newly created "go" folder under Downloads folder, to verify that run "ls" command.

Step 5: Move the go folder from Downloads to /usr/local folder using below command.

 sudo mv go /usr/local

Step 6: Next step is to set few variables and update the PATH variables. There are two ways to do that.

First way is to set is temporary and second way is to set permanent variables so that you need not to do the same steps again and again.

If you want to go with first step then follow below steps.

 export GOROOT=/usr/local/go

 cd $HOME

mkdir gowork

export GOPATH=$HOME/gowork

export PATH=$PATH:$GOROOT/bin

export PATH=$PATH:$GOPATH/bin

 

If you want to go with second step which I recommend then follow below steps.

go to home location and press " ctrl+H" to see the hidden files

open .profile file and add below entries

GOROOT=/usr/local/go

GOPATH=$HOME/gowork

 

Update the PATH variable to append both GOROOT and GOPATH bin folders and your PATH variable should look like below.

 PATH="$HOME/bin:$HOME/.local/bin:/usr/local/go/bin:$HOME/gowork/bin:$PATH"

 Step 7: Validate if golang is successfully installed in your system by running below command. This command should return version of go language installed.

 go version

 Lib Tools Installation 

Install Lib tools using below command.

 sudo apt-get install libltdl-dev

 Docker CE (Community Edition) Installation 

Download and install it using below commands.

wget https://download.docker.com/linux/ubuntu/dists/xenial/pool/stable/amd64/docker-ce_17.06.2~ce- 0~ubuntu_amd64.deb

sudodpkg -i<file>

 Check the version of docker using below command and this should return the version of docker.

docker --version

 

Docker Compose Installation

 Run below commands to setup Docker compose.

sudo apt-get install python-pip

pip --version

sudo pip install docker-compose

 Verify the installation and check the version from below command.

 docker-compose version


One you perform all above prerequisites then you need to perform below steps to complete fabric 2.2 setup.

Hyperledger Fabric Setup

Run below command to download and setup Fabric.

curl -sSL https://bit.ly/2ysbOFE | bash -s

You may encounter below issue when you run above command.

failed to get default registry endpoint from daemon (Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock:

To fix this you need run below command.

sudo chmod 666 /var/run/docker.sock

After running this command, run the command that is mentioned in previous step again and you should be able to download and setup on your system.