step-by-step-guide-to-install-hyperledger-in-ubuntu.png

Step By Step Guide To Install hyperledger fabric in Ubuntu 16.04 LTS

30/October/2017

    Hyperledeger is a collaborative effort among different industry leaders to frame an open source, Cross-Industry Blockchain aided technologies. The project is led by Linux Foundation and industry leaders like IBM, Intel, Samsung and much more already became part of it. The movement basically aims to develop the distributed ledgers that can support enterprise-level business transactions. The project has developed a number of business Blockchain frameworks like Hyperledger Fabric and Hyperledger Saw toot. Actually, these frameworks provide the development environment for business Blockchain applications.  You can read more about the basics of Hyperledger from our previous blogs.

You can also learn the basic terminologies and components of hyper ledger from here.

Installation of Hyperledger fabric on Ubuntu 16.04 LTS
The Installation has the following prerequisites. Make sure that your system has the following features before installation.  
- The latest version of cURL
- Latest version of Docker & Docker compose (docker v1.12.x or higher)
- Latest version of Go-Lang (Go - Programming Language)
- Python v2.7
- Node JS v6.x (Currently v7.x not supported)

1. Installation of cURL

             $ sudo apt install curl


2. Install the latest version of Docker and Docker compose
 - install docker

             $ sudo apt install docker.io

 - install docker compose

             $ sudo apt install docker-compose

 - create a user group

             $ sudo usermod -a -G docker $USER

    You can check the version of docker and docker compose version by


             $ docker --version
             $ docker-compose --version


 - To check the working of you ‘Docker’ execute the following code.

             $ docker run hello-world

    If the ‘docker’ is installed successfully then the result will be
        Status: Downloaded newer image for hello-world: latest
        Hello from Docker!
        NOTE: If the ‘docker’ displays an error message ‘Cannot connect to the Docker daemon’. Please restart your system and run again. 

3. Install Go Programming language

            $ sudo apt install golang-go

  After the installation executes the following path to set the environment variables.

            $ export GOPATH=$HOME/go
            $ export PATH=$PATH:$GOPATH/bin

4. Install Node.js
    Install node.js version 6.x or greater (version 7.x not supported now)

            $ curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -
            $ sudo apt-get install -y nodejs
            Note: Ensure nodejs version is 6.x
            $ nodejs –v

5. Install the latest version of npm
    Installing Node.js will also install NPM,
    You can upgrade the npm tool with the following command:

            $ sudo npm install npm@3.10.10 -g

6. Install Python 2.7
Ubuntu 16.04 comes with Python package 3.x so install Python Package 2.7
(If already installed then ignore this step- check by typing $ python)

            $ sudo apt-get install python


Now let us go to Hyperledger Installation


Installation takes about an hour since it has to download large files.

1. Install Binaries and Docker Images from docker

        curl -SSL https://goo.gl/6wtTN5 | bash -s 1.1.0
The command downloads and run a ‘bash script’ it will then download the platform-specific executable you need. It then places the files into the repo you created above.

Following files are downloaded.
 - cryptogen
 - configtxgen
 - configtxlator
 - peer

Place them in the bin subdirectory of the current working directory

  export  PATH=<path to download location>/bin:$PATH


Test a Hyperledger Fabric Sample

Now let us look a sample hyper ledger program ‘Fabcar’ to check the installed environment.
‘fabcar’  is a hyper ledger fabric sample which is used for storing the information of the cars like color, model etc as key/value pairs in the ledger. 
Follow the steps to install ‘Fabcar’ on your Hyperledger environment.

1. Remove  containers and chain code images if any, in order to avoid docker conflict


        $ docker rm -f $(docker ps -aq)

    
2. Use the following command to navigate to a particular location in the terminal and download the fabric samples in that location.


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


3. Navigate to the directory


        $ cd fabric-samples

    List the files in the directory using ‘ls’ you will see the following files
        chaincode    invoke.js       network         package.json    query.js           startFabric.sh

4. Run the startFabric.sh script to start the network.
     This script file downloads and extracts the Fabric images from the docker,

        $ ./ startFabric.sh


5. We need to install node SDK for Hyperledger so that we can run the node modules in this example


        $ npm install

Now you created a sample network.
Now let us check the main operational codes in ‘Fabcar’.

1. Querying the ledgers

‘Query’ is a built-in function that is used to read the values of each key (Which is the information) of each or every car according to the permissions of users.
 - We can query the data written in the ledgers as key-value pairs. we can query for a single as well as all the key/value pairs.

    Run the ‘query.js’ script to query the data from the ledger

        $ node query

It will give the following result


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"}}]

These are the data on the ledger that represents details of 10 Cars (CAR0 to CAR9)

Now let us see the request definition in the ‘query.js’ file

 

// queryCar - requires 1 argument, ex: args: ['CAR4'],
// queryAllCars - requires no arguments , ex: args: [''],
const request = {
chaincodeId: options.chaincode_id,
txId: transaction_id,
fcn: 'queryAllCars',
args: ['']
};


‘queryAllCars’ is used when we want to see the details of all cars. If you need the details of a particular car then you can use ‘queryCar’ function (Which is a built-in function).

See the following example which uses the ‘queryCar’ function.

const request = {
chaincodeId: options.chaincode_id,
txId: transaction_id,
fcn: 'queryCars',
args: ['CAR4']
};

 - Now save and run the ‘query.js’ 
        node query.js

The result will be the value of CAR4 Only

{"colour":"black","make":"Tesla","model":"S","owner":"Adriana"}

2. Updating the Ledger

You can update the ledger using the following ‘invok.js’ file

Open the ‘invok.js’ file in any editor and you can see the following request definition in it.

// createCar - requires 5 args, ex: args: ['CAR11', 'Honda', 'Accord', 'Black', 'Tom'],
// changeCarOwner - requires 2 args , ex: args: ['CAR10', 'Barry'],
// send proposal to endorser
var request = {
targets: targets,
chaincodeId: options.chaincode_id,
fcn: 'createCar',
args: ['CAR10', 'Chevy', 'Volt', 'Red', 'Nick'],
chainId: options.channel_id,
txId: tx_id
};
Here the ‘invoke.js’ file requests a function ‘createCar’ which in turn update/create a new key/value pair in the ledger.

The argument list contains the arguments that we need to store in the ledger. Run the following code to update the ledger.

node invoke.js

The following result will be displayed aster successful updating of the value.

The transaction has been committed on peer localhost: 7053

 - Run the query.js file to view the changes 

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"}},
{"Key":"CAR10", "Record": {"colour":"Red","make":"Chevy","model":"Volt","owner":"Barry"}}]


You can see the newly inserted values here. 
If you want to add more car details, change the arguments in ‘invoke.js’ file and run the file again.

Comments

0

Leave a comment