Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

Name – Saurabh Umbarkar

Roll no - 60
PRN no - 2043110151
Branch - CSE
Subject - Block Chain and Digital Currency

Experiment - 6

Aim - Write a program to create a Business Network using


Hyperledger.

Theory -
Installing the development environment
Follow these instructions to obtain the Hyperledger Composer
development tools (primarily used to create Business
Networks) and stand up a Hyperledger Fabric (primarily used
to run/deploy your Business Networks locally). Note that the
Business Networks you create can also be deployed to
Hyperledger Fabric runtimes in other environments e.g. on a
cloud platform.

Before you begin

Make sure you have installed the required pre-requisites,


following the instructions in Installing pre-requisites.

These instructions assume that you've not installed the tools


and used them before. If this is not the case, you might want
to check that your previous setup is completely destroyed
before you start following this guide. To learn how to do this,
skip to the Appendix.
Installing components
Step 1: Install the CLI tools

There are a few useful CLI tools for Composer developers.


The most important one is composer-cli, which contains all
the essential operations, so we'll install that first. Next, we'll
also pick up generator-hyperledger-composer,
composer-rest-server and Yeoman plus the
generator-hyperledger-composer. Those last 3 are not
core parts of the development environment, but they'll be
useful if you're following the tutorials or developing
applications that interact with your Business Network, so we'll
get them installed now.

Note that you should not use su or sudo for the following
npm commands.

1. Essential CLI tools:


2. npm install -g [email protected]
3. Utility for running a REST Server on your machine to
expose your business networks as RESTful APIs:

npm install -g [email protected]

4.
5. Useful utility for generating application assets:
npm install -g [email protected]

6.
7. Yeoman is a tool for generating applications, which

npm install -g yo

8.

Step 2: Install Playground

If you've already tried Composer online, you'll have seen the


browser app "Playground". You can run this locally on your
development machine too, giving you a UI for viewing and
demonstrating your business networks.
1. Browser app for simple editing and testing Business
Networks:
npm install -g [email protected]

2.
Step 3: Set up your IDE

Whilst the browser app can be used to work on your Business


Network code, most users will prefer to work in an IDE. Our
favorite is VSCode, because a Composer extension is
available.

1. Install VSCode from this URL:


https://code.visualstudio.com/download
2. Open VSCode, go to Extensions, then search for and
install the Hyperledger Composer extension from the
Marketplace.

Step 4: Install Hyperledger Fabric

This step gives you a local Hyperledger Fabric runtime to


deploy your business networks to.
1. In a directory of your choice (we will assume
~/fabric-dev-servers), get the .tar.gz file that
contains the tools to install Hyperledger Fabric:
mkdir ~/fabric-dev-servers && cd ~/fabric-dev-servers

curl -O
https://raw.githubusercontent.com/hyperledger/composer-tools/master/p
ackages/fabric-dev-servers/fabric-dev-servers.tar.gz
tar -xvf fabric-dev-servers.tar.gz

preceding snippet.
3. Use the scripts you just downloaded and extracted to
download a local Hyperledger Fabric v1.1 runtime:
cd ~/fabric-dev-servers
export FABRIC_VERSION=hlfv11
./downloadFabric.sh
4. Controlling your dev environment
Starting and stopping Hyperledger Fabric

You control your runtime using a set of scripts which you'll find

The first time you start up a new runtime, you'll need to run
the start script, then generate a PeerAdmin card:

cd ~/fabric-dev-servers
export FABRIC_VERSION=hlfv11
./startFabric.sh
./createPeerAdminCard.sh

You can start and stop your runtime using

At the end of your development session, you run


~/fabric-dev-servers/stopFabric.sh and then
~/fabric-dev-servers/teardownFabric.sh. Note that
if you've run the teardown script, the next time you start the
runtime, you'll need to create a new PeerAdmin card just like
you did on first time startup.

Start the web app ("Playground")


To start the web app, run:

composer-playground

It will typically open your browser automatically, at the


following address: http://localhost:8080/login

You should see the PeerAdmin@hlfv1 Card you created


with the createPeerAdminCard script on your "My
Business Networks" screen in the web app: if you don't see
this, you may not have correctly started up your runtime!

Appendix: destroy a previous setup


If you've previously used an older version of Hyperledger
Composer and are now setting up a new install, you may
want to kill and remove all previous Docker containers, which
you can do with these commands:
docker kill $(docker ps -q)
docker rm $(docker ps -aq)

docker rmi $(docker images dev-* -q)


Code -
/**
* Business Network Definition
*/

/**
* Define the namespace for the business network
*/
namespace org.example.biznet

/**
* Asset definition
*/
asset Product identified by productId {
o String productId
o String name
o Double price
}

/**
* Participant definition
*/
participant Trader identified by tradeId {
o String tradeId
o String firstName
o String lastName
}
/**
* Transaction definition
*/
transaction Trade {
--> Trader trader
--> Product product
}

/**
* Contract definition
*/
contract TradeContract {
/**
* Transaction logic
* @param {org.example.biznet.Trade} trade - the trade
to be processed
* @transaction
*/
function tradeProduct(trade) {
trade.product.owner = trade.trader;
return getAssetRegistry('org.example.biznet.Product')
.then(function(productRegistry) {
return productRegistry.update(trade.product);
});
}
}
Output -

You might also like