Hyperledger Development with in 10 days — Day 3


For the last two days, we have seen various capabilities of Hyberledger fabric. Today we are going to get into setup the blockchain app using Hyperledger Fabric.
The main focus is set the mindset and to build confidence on a setup of the Hyperledger Fabric system.
“We are not deep diving into the core technical configuration now. We will use the existing code base to start the network, create a peer, create a channel, deploy smart contract, smart contract code container, ordering node, Nodejs application SDK to connect with blockchain.”
We are going to pull the latest code for sample app from Github, startup scripts, configuration and testing. End of this article we could sense the fully functional blockchain app.
Prerequisites
echo $GOPATH
/Users/xxx/go
else use this command.
export GOPATH=$HOME/go
export PATH=$PATH:$GOPATH/bin

Install Binaries and Docker Images

Installers development is in progress, so we are going to use the Fabric docker images.
Steps to download the binaries and docker images
>>open terminal
mkdir FabricBinaries
cd FabricBinaries
--> run the below curl command
curl -sSL https://goo.gl/eYdRbX | bash
It retrieves four platform specific binaries in that directory with bin sub directory.
  • crptogen
  • configtxgen
  • configtxlator
  • peer
You need to add that bin to the PATH, using
export PATH=/bin:$PATH
Finally, the script will download the Hyperledger Fabric docker images from Docker Hub into your local Docker registry and tag them as ‘latest’.
using "docker images" in the terminal, we can see the list of docker images downloaded.

Hyperledger Fabric SDK’s

SDK’s are important for the clients, to connect with Fabric network. To deploy the smart contract, to do function invocation in the smart contract. All the connectivity between Fabric network and client application done through SDK’s.
There are two types of SDK’s,
  • NodeJS
  • Java

Hyperledger Fabric CA

Fabric also provides the Certificate Authority service. It is used to generate the certificates, private and public keys.
Because each fabric node or client needs to be configured with private and public keys. So that all parties involved in the fabric network are known identities.
Any Certificate Authority generates ECDSA certificates can be used to provision the nodes in the network.

Developing the First App

We are going to use,
Fabric” + “NodeJs
Steps involved in running the first app
  1. Starting the test hyperledger network using the docker images downloaded.
  2. Writing the smart contract(identify which params we are going to update or retrieve), then deploy it.
  3. Develop a app or script using Nodejs SDK to query and update data to the blockchain.
Starting the test Hyperledger Network
  • Make sure all the prerequisites installed.
>> open terminal, another new directory(don't confuse with binaries)
mkdir hyperledger
cd hyperledger
git clone https://github.com/hyperledger/fabric-samples.git
cd fabric-samples/fabcar
In this, we can see fabcar is another sub directory, this is the application we are going to deploy to the network.
>> give "ls" command, you will see the below scripts.
chaincode    invoke.js       network         package.json    query.js        startFabric.sh
Now use the startFabric.sh script to launch the network.
./startFabric.sh
The following command downloads and extracts the Hyperledger Fabric Docker images, so it will take a few minutes to complete.
Here you can see steps happen in the start script,
  • launches a peer node, ordering node, Certificate Authority and CLI container
  • creates a channel and joins the peer to the channel
  • installs smart contract (i.e. chaincode) onto the peer’s file system and instantiates said chaincode on the channel; instantiate starts a chaincode container
  • calls the initLedger function to populate the channel ledger with 10 unique cars
Issue a docker ps command to reveal the processes started by the startFabric.sh script.
fabric readdocs
So far we have created the test network, deployed the smart contract. And blockchain is pre populated with 10 data. You can see all happens in the start script console.
Now are going to use the NodeJs SDK to retrieve and update data to the blockchain.
credit: fabric read docs
  1. Querying the ledger:
Inside the fabcar directory, run the following command.
npm install
You can see the query.js in that directory. if you run the below command
node query.js
The result will be like,
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"}}]
If we see the query.js, we can see the configuration to connect the fabric network from the sdk
var options = {
      wallet_path : path.join(__dirname, './network/creds'),
      user_id: 'PeerAdmin',
      channel_id: 'mychannel',
      chaincode_id: 'fabcar',
      network_url: 'grpc://localhost:7051',
Also also below, we can see the query constructor.
// 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: ['']
We can see, in a corresponding chaincode usingchaincode_id(fabcar) we call the function queryAllCars.
In the same directory of fabcar, we can see the another directory with chaincode, chaincode/fabcar/fabcar.go is the deployed smart contract(chain code) to the network. There the function for queryAllCars is written.
func (s *SmartContract) queryAllCars(APIstub shim.ChaincodeStubInterface) sc.Response {

     startKey := "CAR0"
     endKey := "CAR999"

     resultsIterator, err := APIstub.GetStateByRange(startKey, endKey)
2. Querying the particular car in the blockchain, in the same query do the changs to the request object.
const request = {
      chaincodeId: options.chaincode_id,
      txId: transaction_id,
      fcn: 'queryCar',
      args: ['CAR4']
save the program and call
node query.js
You should see the following:
{"colour":"black","make":"Tesla","model":"S","owner":"Adriana"}
3. Updating the ledger.
Now open the “invoke.js” file. Which is used to insert and update data to the blockchain using nodejs SDK.
To insert data, change the request object to
var request = {
    targets: targets,
    chaincodeId: options.chaincode_id,
    fcn: 'createCar',
    args: ['CAR10', 'Chevy', 'Volt', 'Red', 'Nick'],
    chainId: options.channel_id,
    txId: tx_id
Save it and run the program:
node invoke.js
output will be,
The transaction has been committed on peer localhost:7053
Response is  {"colour":"Red","make":"Chevy","model":"Volt","owner":"Nick"}
To update the data, change the request object in “invoke.js” to
var request = {
   targets: targets,
   chaincodeId: options.chaincode_id,
   fcn: 'changeCarOwner',
   args: ['CAR10', 'Barry'],
   chainId: options.channel_id,
   txId: tx_id
Save it and run the program:
node invoke.js
Output is,
Response is  {"colour":"Red","make":"Chevy","model":"Volt","owner":"Barry"}
In this part we have successfully run a test network, query and update data on the blockchain. Next section we can see the details of forming the network and smart contract code.


Please hit that clap 

Comments

Popular posts from this blog

Script For Login, Logout and View Using PHP, MySQL and Bootstrap

Real-Time Web Interface to MQTT using Socket.io and Node.js

Customize radio buttons and checkboxes with CSS sprites