Blockchain Application on Hyperledger Fabric
In this blockchain application, the blockchain will store the state of the system, in addition to the immutable record of transactions that created that state. A client application will be used to send transactions to the blockchain. The smart contracts (Chaincode) will encode some (if not all) of the business logic.
FlashBack
Distributed ledger technology generally consists of three basic components:
-
A data model that captures the current state of the ledger ( a time-stamped series of transactions cryptographically secured)
-
A language of transactions that changes the ledger state [Smart Contracts / Chain code]
-
A protocol used to build consensus among participants around which transactions will be accepted, and in what order, by the ledger.
We are using Hyperledger Fabric, one of the 5 frameworks available within the Hyperledger universe to explore a way to bring some transparency to the Tuna Fish supply chain.

Fun Part : BlockChain Application
Step 1: Clone the repository
git clone https://github.com/hyperledger/education.git
cd education/LFS171x/fabric-material/tuna-app
Make sure you have Docker running on your machine before you run the next command. If you do not have Docker installed, return to Chapter 4, Technical Requirements.
Also, make sure that you have completed the Installing Hyperledger Fabric section in this chapter before moving on to this application section, as you will likely experience errors.
First, remove any pre-existing containers, as it may conflict with commands in this tutorial:
docker rm -f $(docker ps -aq)
Once you executed the git clone it would have created this folder structure within your system
.png)
.png)
.png)
.png)
.png)
Now open the fabric-material in your favorite text editor. (I’m using Sublime)


.png)
Step 2: Start Fabric Network
Let’s break down what’s happening when you run this command in your terminal.
-
Existing Network Artifacts are removed from Docker and just one organisation with a peer & couch DB is created along with an orderer.
As you can see in the code, the startfabric.Sh internally calls start.sh within basic-network folder.
./startFabric.sh
.png)
Now, if you traverse to start.sh , you can see how the docker container is cleared and re-populated. Also, a channel is created and a peer is added
.png)
A typical output would look like this:the channel creation along with few dummy records are created

Step 3: RegisterAdmin

node registerAdmin.js
Cool. Now that the network is up. Let’s registerAdmin.
You should get this output, basically an Admin is created with the required keys. YOu can open up registerAdmin.js to see the code. Basically, withe the Fabric CA client we check if an Admin is already registered, if not it enrolls with the CA client. Basic parameters such as enrollmentID, Secret are set. Then the user is created with FabricClient with membership ID, along with the public private key pair.
​
​
.png)
You should be seeing similar output:

Step 4: RegisterUser
$ node registerUser.js
Now that the network and admin are set, let’s register an User.
root@DESKTOP-UMC4FSE:tuna-app $ node registerUser.js
​
Store path:/Users/LENOVO/.hfc-key-store
Successfully loaded admin from persistence
Failed to register: Error: fabric-ca request register failed with errors [[{"code":20,"message":"Authorization failure"}]]
Authorization failures may be caused by having admin credentials from a previous CA instance.
Try again after deleting the contents of the store directory /Users/LENOVO/.hfc-key-store
Delete the key directory & Register Admin again.
root@DESKTOP-UMC4FSE:tuna-app $ rm ~/.hfc-key-store/*
​
root@DESKTOP-UMC4FSE:tuna-app $ node registerUser.js
Store path:/Users/LENOVO/.hfc-key-store
​
Failed to register: Error: Failed to get admin.... run registerAdmin.js
root@DESKTOP-UMC4FSE:tuna-app $ node registerAdmin.js
Store path:/Users/LENOVO/.hfc-key-store
Successfully enrolled admin user "admin"
Assigned the admin user to the fabric client ::{"name":"admin","mspid":"Org1MSP","roles":null,"affiliation":"","enrollmentSecret":"","enrollment":{"signingIdentity":"174630b086dc3d165c65b139fc2bee92bfe6f0749bf93672b28b823784349ebf","identity":{"certificate":"-----BEGIN CERTIFICATE-----\nMIICAjCCAaigAwIBAgIUVA8TjKrtWvmuTkghyW5fGylwvvIwCgYIKoZIzj0EAwIw\nczELMAkGA1UEBhMCVVMxEzARBgNVBAgTCkNhbGlmb3JuaWExFjAUBgNVBAcTDVNh\nbiBGcmFuY2lzY28xGTAXBgNVBAoTEG9yZzEuZXhhbXBsZS5jb20xHDAaBgNVBAMT\nE2NhLm9yZzEuZXhhbXBsZS5jb20wHhcNMTgwNTEzMTcxMzAwWhcNMTkwNTEzMTcx\nODAwWjAhMQ8wDQYDVQQLEwZjbGllbnQxDjAMBgNVBAMTBWFkbWluMFkwEwYHKoZI\nzj0CAQYIKoZIzj0DAQcDQgAEnfxwodId3gRqKsdwxv6zVzuVeRakg4u9MvPf3eGE\nnkGrS9YZ0MW+QVUnbfeS1xS0jpSUUGGqWT9TKMgnunKe6aNsMGowDgYDVR0PAQH/\nBAQDAgeAMAwGA1UdEwEB/wQCMAAwHQYDVR0OBBYEFPPzep8ev4FHiQBTbZu3ud7Z\nG8u3MCsGA1UdIwQkMCKAIEI5qg3NdtruuLoM2nAYUdFFBNMarRst3dusalc2Xkl8\nMAoGCCqGSM49BAMCA0gAMEUCIQDcbAn2549BIfGjCE4vmyF38LY0F5JhFbuGcQC9\nFAK9awIgWV1GODLV/OhPnR42yNa+fdwkry/8IgWU27PcFN15oFQ=\n-----END CERTIFICATE-----\n"}}}
Let’s try and register user again.
root@DESKTOP-UMC4FSE:tuna-app $ node registerUser.js
Store path:/Users/LENOVO/.hfc-key-store
Successfully loaded admin from persistence
Successfully registered user1 - secret:oDSXCnPBMrUC
Successfully enrolled member user "user1"
User1 was successfully registered and enrolled and is ready to intreact with the fabric network
If you open up registerUser.js, you can see that we register user with the CA server and then enroll.
.png)
You should see the following output:


Step 5: Run the server
$ node server.js
Now, start the server (server.js) and then try accessing client at the servehost_address:8000. Make sure port 8000 is accessible from the host. The user can interact with the Web application that enables users to query and update a ledger. Under the hood, the application using the SDK sends the endorsed proposal (automatically) to the Solo ordering service where the order is packaged into a block then broadcasted to all the peers on the network
Try accessing the Web application client using the browser http://IPaddress:8000/:
Voila..our applcation is live
.png)

Query All, Query Tuna
.png)
Create tuna record, Change tuna holder
The UI has a button under 4 headings.

Query All Tuna Catches
triggers — queryAllTuna.js
Query Specific Tuna
triggers — queryTuna.js
Create Tuna Record
triggers — recordTuna.js
Change Tuna Holder
triggers — changeTunaHolder.js
All these js files are inturn defined within the chaincode tuna-chaincode.go
.png)
Output
1.Query All Tuna (All the data we used in the chaincode to initLedger is pulled and shown here)
.png)
2. Query Tuna (I am retrieving record 9 below)

3. Log a Tuna Catch (Adding Joyboy as new holder)

.jpg)
4. Change the Owner of Tuna Fish (From Joyboy to Menez)


Thats all Folks
Congratulation .... we have come to an end of of Hyperledger fabric tutorial ... and successfuly developed our Blockchain Application on Hyperledger Fabric.......


Special thanks to my mentor Anubhav Chaturvedi and Dlithe team providing me such a strong foundation in an industry that can be confusing,I learned something new from you every day. You are a fantastic mentor that is worthy of emulation. You deserve a big thank you from me