Building a SeaShell Application using HyperLedger
This blog will help you to understand the concept of Hyperledger which is the Blockchain for Business. I have highlighted the terms used specifically in Blockchain for your quick reference.
I would like to thank Mr Arun Rajpurohit Sir from DLithe (www.dlithe.com) for organizing intership on Blockchain Technology and my mentor Mr Anubhav Chaturvedi Sir for all the things he taught us.
Hyperledger
It is an open source collaborative effort created to advance cross-industry blockchain technologies. It is a global collaboration, hosted by The Linux Foundation, including leaders in finance, banking, Internet of Things, supply chains, manufacturing, and Technology
UseCases
Hyperledger Fabric is a modular blockchain framework that acts as a foundation for developing blockchain-based products, solutions, and applications using plug-and-play components that are aimed for use within private enterprises.
Why Hperledger?
Businesses can’t use public blockchains due to the following reasons: Privacy issues: Public blockchains like Bitcoin are permissionless. Anyone can join it, including anonymous and pseudonymous users
Ethereum Vs BlockChain
The most essential distinction between Hyperledger and Ethereum is the intent they are designed for. Ethereum runs the Smart Contracts on the EVM for applications that are attributed to being decentralized and are for mass consumption. On the other hand, Hyperledger leverages blockchain technology for business.
Docker is a tool designed to make it easier to create, deploy, and run applications by using containers. Containers allow a developer to package up an application with all of the parts it needs, such as libraries and other dependencies, and ship it all out as one package.
Case Study :
We will be describing how tuna fishing and sea shells can be improved, starting from the source, fisherman Sarah, and the process by which her tuna/seashells ends up at Miriam’s restaurant. In between, we’ll have other parties involved, such as the regulator who verify the validity of the data and the sustainability of the tuna catches.
We will be using Hyperledger Fabric’s framework to keep track of each part of this process.
Hyperledger Fabric Elements
- Channels are data partitioning mechanisms that allow transaction visibility for stakeholders only. Each channel is an independent chain of transaction blocks containing only transactions for that particular channel.
- Chaincode /(Smart Contract) | It encapsulates both the asset definitions and the business logic (or transactions) for modifying those assets. Transaction invocations result in changes to the ledger.
- Ledger contains the current world state of the network and a chain of transaction invocations. A shared, permissioned ledger is an append-only system of records and serves as a single source of truth.
- Network is the collection of data processing peers that form a blockchain network. The network is responsible for maintaining a consistently replicated ledger.
- Ordering Service is a collection of nodes that orders transactions into a block.
- World State reflects the current data about all the assets in the network. This data is stored in a database for efficient access. Current supported databases are LevelDB and CouchDB.
- Membership Service Provider (MSP) manages identity and permissioned access for clients and peers.
I will be trying to recreate the application done in the below blog:
Prequisites that need to be fulfilled to ensure that our system is prepared for the technical requirements.
- Install cURL.
- Install Node.js and npm package manager.
- Install Go Language.
- Install Docker and Docker Compose.
- Curl
Type the following command in your terminal and enter your password:
$ sudo apt install curl
To check, run the following command in your terminal/command line:
$ curl -V
2. Docker
To check your docker installation:
docker --version && docker-compose --version
3. Node.js and NPM
Download | Node.js
Node.js® is a JavaScript runtime built on Chrome's V8 JavaScript engine.
nodejs.org
Verify the versions of both Node.js and npm, and make sure the Node.js version you are installing is greater than v6.9 (do not use v7), and the npm version is greater than 3.x:
node — version && npm — version
4. Go is an open source programming language that makes it easy to build simple, reliable, and efficient software.
Visit https://golang.org/dl/ and make note of the latest stable release (v1.8 or later):
$ cd ~
$ sudo curl -O https://storage.googleapis.com/golang/go1.9.2.darwin-amd64.pkg
Note: Switch out the black portion of the URL with the correct filename.
Open the downloaded file to run the installation package:
$ open go1.9.2.darwin-amd64.pkg
After completing the installation, go will have been installed at /usr/local/bin/go. You can check by running:
which gogo version
Context of our Case Study
Please refer this for better understanding of our application context.
Installing Fabric Components
Determine a location on your machine where you want to place the Hyperledger Fabric samples applications repository and open that in a terminal window. Then, execute the following commands:
git clone -b master https://github.com/hyperledger/fabric-samples.gitcd fabric-samplesgit checkout {TAG}
Execute the following command from within the directory into which you will extract the platform-specific binaries:
curl -sSL https://goo.gl/6wtTN5 | bash -s 1.1.0## comment: <full URL>
https://raw.githubusercontent.com/hyperledger/fabric/master/scripts/bootstrap.sh
Make sure you have given sufficient permission to your docker socket.
sudo systemctl restart docker
sudo chmod 666 /var/run/docker.sock
docker ps -a
As you can see ,it retrieves four platform-specific binaries:
cryptogen
,configtxgen
,configtxlator
,peer
orderer
andfabric-ca-client
and places them in the bin
sub-directory of the current working directory.
Test:
docker images
We will use Docker images to bootstrap our first Hyperledger Fabric network. It will also launch a container to run a scripted execution that will join peers to a channel, deploy, and instantiate the chaincode, and execute transactions against the chaincode.
cd first-network./byfn.sh -m generate
A brief description will appear, along with a Y/N command line prompt. Respond with a Y <Enter> to continue.
This step generates all of the certificates and keys for all our various network entities, including the genesis block used to bootstrap the ordering service and a collection of configuration transactions required to create a channel.
Next, you can start the network with the following command:
./byfn.sh -m up
Another command line will appear, reply with Y <Enter> to continue.
Finally, let’s test bringing down this network.
Within the same terminal, do Control+C to exit the current execution.
Then, run the following command:
./byfn.sh -m down
Another command line will appear, reply with Y <Enter> to continue.
Hyperledger Fabric Chaincode
TunaFish/Seashell Scenario using Hyperledger Fabric
Fabric Node.js SDK
In this exercise, we will be using the Node.js SDK to interact with the network, and, therefore, the ledger. The Hyperledger Fabric Client SDK makes it easy to use APIs to interact with a Hyperledger Fabric blockchain
SeaShell Application
StakeHolders
- Sarah is the fisherman who sustainably and legally catches tuna.
- Regulators verify that the tuna has been legally and sustainably caught.
- Miriam is a restaurant owner who will serve as the end user, in this situation.
- Carl is another restaurant owner fisherman Sarah can sell tuna to.
Chain Code : Invoke Method
Invoke method is the one which gets called when a transaction is proposed by a client application. Within this method, we have three different types of transactions — recordTuna, queryTuna, and changeTunaHolder.
Sarah, the fisherman, will invoke the recordTuna when she catches each tuna/seashells.
changeTunaHolder can be invoked by Miriam, the restaurateur, when she confirms receiving and passing on a particular tuna fish/seashells as it passes through the supply chain. queryTuna can be invoked by Miriam, the restaurateur, to view the state of a particular tuna/seashells.
Regulators will invoke queryTuna and queryAllTuna based on their need to verify and check for sustainability of the supply chain.
Chaincode Methods :
The queryTuna method would be used by a fisherman, regulator, or restaurateur to view the record of one particular tuna. It takes one argument — the key for the tuna in question.
The initLedger method will add test data to our network.
The recordTuna method is the method a fisherman like Sarah would use to record each of her tuna catches. This method takes in five arguments (attributes to be saved in the ledger).
The queryAllTuna method allows for assessing all the records; in this case, all the Tuna records added to the ledger. This method does not take any arguments. It will return a JSON string containing the results.
As the tuna fish is passed to different parties in the supply chain, the data in the world state can be updated with who has possession. The changeTunaHolder method takes in 2 arguments, tuna id and new holder name.
You can refer the below for all the codes explained above:
- Clone the Repository
git clone https://github.com/hyperledger/education.gitcd education/LFS171x/fabric-material/tuna-app
Once cloned, go through the file structure and check all the files present under the education folder.
Make sure you have Docker running on your machine before you run the next command.
Also, make sure that you have completed the Installing Hyperledger Fabric (sdk).
First, remove any pre-existing containers, as it may conflict with commands:
docker rm -f $(docker ps -aq)
2. Start the Fabric Network
Next, let’s start the Hyperledger Fabric network with the following command:
./startFabric.sh
3. Register Admin
Cool. Now that the network is up. Let’s registerAdmin.
node registerAdmin.js
Step 4: RegisterUser
Now that the network and admin are set, let’s register an User.
node registerUser.jsif you get an error, Delete the key directory & Register Admin again.rm ~/.hfc-key-store/*node registerUser.jsnode registerAdmin.jsnode registerUser.js
Step 5. Run the Server
This step is the final step to run the application. For that we gotta setup the nodejs server, call the needed packages, instantiate the app and listen on a particular port.
$ node server.jsWe should get the below:Live on port: 8000
YASS!!..our application is live on port 8000
Output:
1.Query All SeaShell Catches<All the data we used in the chaincode to initLedger is pulled and shown here>
2. Change the Owner of SeaShell
Similarly you can query a transaction based on Catch Id and also add a new SeaShell Record. Try it!!
With this we successfully Complete our Hyperledger Task!!