0% found this document useful (0 votes)
129 views

Module 8: Setting Up Hyperledger Fabric Network: Practical Document

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
129 views

Module 8: Setting Up Hyperledger Fabric Network: Practical Document

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Blockchain Certification Training https://www.edureka.

co/blockchain-training

Module 8: Setting up Hyperledger Fabric


Network
Practical Document

© Brain4ce Education Solutions Pvt. Ltd.


Module 8: Setting up Hyperledger Fabric Network https://www.edureka.co/blockchain-training

Practical Document

Step One: Create a Business Network Definition


We will begin by cloning a sample business network. Open up a command prompt and execute the
following command:

▪ git clone https://github.com/hyperledger/composer-sample-networks.git

Make a copy of this directory in your project, called 'my-network’ by using the following command:

▪ cp -r ./composer-sample-networks/packages/basic-sample-network/ ./my-network

Note: This step is already executed in the VM to reduce the time taken:

Step Two: Opening Project File


▪ Open Visual Studio(VS) Code and click on Open Folder.

©Brain4ce Education Solutions Pvt. Ltd Page 1


Module 8: Setting up Hyperledger Fabric Network https://www.edureka.co/blockchain-training

Selecting the Project Folder:

▪ Select the my-network folder and click on OK

Step Three: Update your package.json file


▪ In the left panel, select package.json file under my-network folder

©Brain4ce Education Solutions Pvt. Ltd Page 2


Module 8: Setting up Hyperledger Fabric Network https://www.edureka.co/blockchain-training

• Update the file with the following codes:

"name": "my-network",
"version": "0.1.6",
"description": "My Commodity Trading network",
"networkImage": "https://hyperledger.github.io/composer-sample-
networks/packages/basic-sample-network/networkimage.svg",
"networkImageanimated": "https://hyperledger.github.io/composer-
sample-networks/packages/basic-sample-
network/networkimageanimated.svg",
"scripts": {
"prepublish": "mkdirp ./dist ; composer archive create --
sourceType dir --sourceName . -a ./dist/my-network.bna",
"pretest": "npm run lint",
"lint": "eslint .",
"postlint": "npm run licchk",
"licchk": "license-check",
"postlicchk": "npm run doc",
"doc": "jsdoc --pedantic --recurse -c jsdoc.json",
"test-inner": "mocha -t 0 --recursive && cucumber-js",
"test-cover": "nyc npm run test-inner",
"test": "npm run test-inner"
},

©Brain4ce Education Solutions Pvt. Ltd Page 3


Module 8: Setting up Hyperledger Fabric Network https://www.edureka.co/blockchain-training

Step Four: Update README.md File


▪ Open README.md file and Update the first line as seen bellow:

Step Five: Define your Domain Model


• Open the file models/sample.cto file

• This is the domain model for the business network definition. It defines the structure (schema)
for the assets, transaction and participants in the business network

• Replace the existing code with the following code:

©Brain4ce Education Solutions Pvt. Ltd Page 4


Module 8: Setting up Hyperledger Fabric Network https://www.edureka.co/blockchain-training

/**
* My commodity trading network
*/
namespace org.acme.mynetwork
asset Commodity identified by tradingSymbol {
o String tradingSymbol
o String description
o String mainExchange
o Double quantity
--> Trader owner
}
participant Trader identified by tradeId {
o String tradeId
o String firstName
o String lastName
}
transaction Trade {
--> Commodity commodity
--> Trader newOwner
}

©Brain4ce Education Solutions Pvt. Ltd Page 5


Module 8: Setting up Hyperledger Fabric Network https://www.edureka.co/blockchain-training

Step Six: Write Transaction Processor Functions

1. Open the file lib/sample.js in the left-hand pane


2. Update the sample.js file with the folowing code:
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* Track the trade of a commodity from one trader to another
* @param {org.acme.mynetwork.Trade} trade - the trade to be processed
* @transaction
*/
function tradeCommodity(trade) {
trade.commodity.owner = trade.newOwner;
return getAssetRegistry('org.acme.mynetwork.Commodity')
.then(function (assetRegistry) {
return assetRegistry.update(trade.commodity);
});
}

©Brain4ce Education Solutions Pvt. Ltd Page 6


Module 8: Setting up Hyperledger Fabric Network https://www.edureka.co/blockchain-training

Step Seven: Update your Access Control Rules


▪ The file permissions.acl defines the access control rules for the business network definition.
Replace the entire contents of permissions.acl with the rule below:

/**
* Access control rules for mynetwork
*/
rule Default {
description: "Allow all participants access to all resources"
participant: "ANY"
operation: ALL
resource: "org.acme.mynetwork.*"
action: ALLOW
}

rule SystemACL {
description: "System ACL to permit all access"
participant: "ANY"
operation: ALL
resource: "org.hyperledger.composer.system.**"
action: ALLOW
}

Step Eight: Generate the Business Network Archive


Open a new Terminal and execute the following command:
➢ cd my-network
➢ npm install

©Brain4ce Education Solutions Pvt. Ltd Page 7


Module 8: Setting up Hyperledger Fabric Network https://www.edureka.co/blockchain-training

Step Nine: Write Unit Tests


▪ Open Sample.js present in my-network/test/

▪ Replace the existing code with the following code:

NOTE: Code in page 8 – 10 are continuous code which need to be updated into Sample.js

/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

const AdminConnection = require('composer-admin').AdminConnection;


const BrowserFS = require('browserfs/dist/node/index');
const BusinessNetworkConnection = require('composer-
client').BusinessNetworkConnection;
const BusinessNetworkDefinition = require('composer-
common').BusinessNetworkDefinition;
const path = require('path');

require('chai').should();

const bfs_fs = BrowserFS.BFSRequire('fs');


const NS = 'org.acme.mynetwork';

describe('Commodity Trading', () => {

// let adminConnection;
let businessNetworkConnection;

before(() => {
BrowserFS.initialize(new BrowserFS.FileSystem.InMemory());
const adminConnection = new AdminConnection({ fs: bfs_fs });
return adminConnection.createProfile('defaultProfile', {
type: 'embedded'
})
.then(() => {
return adminConnection.connect('defaultProfile', 'admin',
'adminpw');
})
.then(() => {
return
©Brain4ce Education Solutions Pvt. Ltd Page 8
BusinessNetworkDefinition.fromDirectory(path.resolve(__dirname, '..'));
})
.then((businessNetworkDefinition) => {
return adminConnection.deploy(businessNetworkDefinition);
Module 8: Setting up Hyperledger Fabric Network https://www.edureka.co/blockchain-training

.then(() => {
return adminConnection.connect('defaultProfile',
'admin', 'adminpw');
})
.then(() => {
return
BusinessNetworkDefinition.fromDirectory(path.resolve(__dirname,
'..'));
})
.then((businessNetworkDefinition) => {
return
adminConnection.deploy(businessNetworkDefinition);
})
.then(() => {
businessNetworkConnection = new
BusinessNetworkConnection({ fs: bfs_fs });
return
businessNetworkConnection.connect('defaultProfile', 'my-network',
'admin', 'adminpw');
});
});

describe('#tradeCommodity', () => {

it('should be able to trade a commodity', () => {


const factory =
businessNetworkConnection.getBusinessNetwork().getFactory();

// create the traders


const dan = factory.newResource(NS, 'Trader',
'[email protected]');
dan.firstName = 'Dan';
dan.lastName = 'Selman';

const simon = factory.newResource(NS, 'Trader',


'[email protected]');
simon.firstName = 'Simon';
simon.lastName = 'Stone';

// create the commodity


const commodity = factory.newResource(NS, 'Commodity',
'EMA');
commodity.description = 'Corn';
commodity.mainExchange = 'Euronext';
commodity.quantity = 100;
commodity.owner = factory.newRelationship(NS, 'Trader',
dan.$identifier);

// create the trade transaction


const trade = factory.newTransaction(NS, 'Trade');
trade.newOwner
© B r a i n 4 c e= Efactory.newRelationship(NS,
d u c a t i o n S o l u t i o n s P v t . L'Trader',
td Page 9
simon.$identifier);
trade.commodity = factory.newRelationship(NS,
'Commodity', commodity.$identifier);
Module 8: Setting up Hyperledger Fabric Network https://www.edureka.co/blockchain-training

// create the trade transaction


const trade = factory.newTransaction(NS, 'Trade');
trade.newOwner = factory.newRelationship(NS, 'Trader',
simon.$identifier);
trade.commodity = factory.newRelationship(NS,
'Commodity', commodity.$identifier);

// the owner should of the commodity should be dan

commodity.owner.$identifier.should.equal(dan.$identifier);

// Get the asset registry.


let commodityRegistry;
return businessNetworkConnection.getAssetRegistry(NS +
'.Commodity')
.then((assetRegistry) => {
commodityRegistry = assetRegistry;
// add the commodity to the asset registry.
return commodityRegistry.add(commodity);
})
.then(() => {
return
businessNetworkConnection.getParticipantRegistry(NS + '.Trader');
})
.then((participantRegistry) => {
// add the traders
return participantRegistry.addAll([dan, simon]);
})
.then(() => {
// submit the transaction
return
businessNetworkConnection.submitTransaction(trade);
})
.then(() => {
// re-get the commodity
return
commodityRegistry.get(commodity.$identifier);
})
.then((newCommodity) => {
// the owner of the commodity should now be simon

newCommodity.owner.$identifier.should.equal(simon.$identifier);
});
});
});
});

©Brain4ce Education Solutions Pvt. Ltd Page 10


Module 8: Setting up Hyperledger Fabric Network https://www.edureka.co/blockchain-training

Step Eleven: Cucumber Testing


▪ Open features/sample.feature file in VS code and replace the existing code with the following
code:

#
# Licensed under the Apache License, Version 2 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
Feature: Sample
Background:
Given I have deployed the business network definition ..
And I have added the following participants of type
org.acme.mynetwork.Trader
| tradeId | firstName | lastName |
| [email protected] | Alice | A |
| [email protected] | Bob | B |
And I have added the following assets of type
org.acme.mynetwork.Commodity
| tradingSymbol | description | mainExchange | quantity |
owner |
| 1 | One | London | 1 |
[email protected] |
| 2 | Two | Paris | 2 |
[email protected] |
And I have issued the participant
org.acme.mynetwork.Trader#[email protected] with the identity alice1
And I have issued the participant
org.acme.mynetwork.Trader#[email protected] with the identity bob1
Scenario: Alice can read all of the assets
When I use the identity alice1
Then I should have the following assets of type
org.acme.mynetwork.Commodity
| tradingSymbol | description | mainExchange | quantity |
owner |
| 1 | One | London | 1 |
[email protected] |
| 2 | Two | Paris | 2 |
[email protected] |
Scenario: Bob can read all of the assets
When I use the identity bob1
Then I should have the following assets of type
org.acme.mynetwork.Commodity
© B r a i n 4 c e| Edescription
| tradingSymbol d u c a t i o n S|o mainExchange
l u t i o n s P v t . |L quantity
td Page 11
|
owner |
| 1 | One | London | 1 |
[email protected] |
| 2 | Two | Paris | 2 |
Module 8: Setting up Hyperledger Fabric Network https://www.edureka.co/blockchain-training

Scenario: Bob can read all of the assets


When I use the identity bob1
Then I should have the following assets of type
org.acme.mynetwork.Commodity
| tradingSymbol | description | mainExchange | quantity |
owner |
| 1 | One | London | 1 |
[email protected] |
| 2 | Two | Paris | 2 |
[email protected] |
Scenario: Alice can add assets that she owns
When I use the identity alice1
And I add the following asset of type org.acme.mynetwork.Commodity
| tradingSymbol | description | mainExchange | quantity |
owner |
| 3 | Three | New York | 3 |
[email protected] |
Then I should have the following assets of type
org.acme.mynetwork.Commodity
| tradingSymbol | description | mainExchange | quantity |
owner |
| 3 | Three | New York | 3 |
[email protected] |
Scenario: Bob can add assets that he owns
When I use the identity bob1
And I add the following asset of type org.acme.mynetwork.Commodity
| tradingSymbol | description | mainExchange | quantity |
owner |
| 4 | Four | Rome | 4 |
[email protected] |
Then I should have the following assets of type
org.acme.mynetwork.Commodity
| tradingSymbol | description | mainExchange | quantity |
owner |
| 4 | Four | Rome | 4 |
[email protected] |
Scenario: Alice can update her assets
When I use the identity alice1
And I update the following asset of type
org.acme.mynetwork.Commodity
| tradingSymbol | description | mainExchange | quantity |
owner |
| 1 | One | London | 5 |
[email protected] |
Then I should have the following assets of type
org.acme.mynetwork.Commodity
| tradingSymbol | description | mainExchange | quantity |
owner |
| 1 | One | London | 5 |
[email protected] |
Scenario: Bob can update his assets
When I use©the
B r a identity
i n 4 c e E dbob1
ucation Solutions Pvt. Ltd Page 12
And I update the following asset of type
org.acme.mynetwork.Commodity
| tradingSymbol | description | mainExchange | quantity |
owner |
Module 8: Setting up Hyperledger Fabric Network https://www.edureka.co/blockchain-training

| tradingSymbol | description | mainExchange | quantity | owner


|
| 1 | One | London | 5 |
[email protected] |
Scenario: Bob can update his assets
When I use the identity bob1
And I update the following asset of type
org.acme.mynetwork.Commodity
| tradingSymbol | description | mainExchange | quantity | owner
|
| 2 | Two | Paris | 6 |
[email protected] |
Then I should have the following assets of type
org.acme.mynetwork.Commodity
| tradingSymbol | description | mainExchange | quantity | owner
|
| 2 | Two | Paris | 6 |
[email protected] |
Scenario: Alice can remove her assets
When I use the identity alice1
And I remove the following asset of type
org.acme.mynetwork.Commodity
| tradingSymbol |
| 1 |
Then I should not have the following assets of type
org.acme.mynetwork.Commodity
| tradingSymbol |
| 1 |
Scenario: Bob can remove his assets
When I use the identity bob1
And I remove the following asset of type
org.acme.mynetwork.Commodity
| tradingSymbol |
| 2 |
Then I should not have the following assets of type
org.acme.mynetwork.Commodity
| tradingSymbol |
| 2 |
Scenario: Alice can submit a transaction for her assets
When I use the identity alice1
And I submit the following transaction of type
org.acme.mynetwork.Trade
| commodity | newOwner |
| 1 | [email protected] |
Then I should have the following assets of type
org.acme.mynetwork.Commodity
| tradingSymbol | description | mainExchange | quantity | owner
|
| 1 | One | London | 1 |
[email protected] |
Scenario: Bob can submit a transaction for his assets
When I use©the
B r a identity
i n 4 c e E dbob1
ucation Solutions Pvt. Ltd Page 13
And I submit the following transaction of type
org.acme.mynetwork.Trade
| commodity | newOwner |
| 2 | [email protected] |
Module 8: Setting up Hyperledger Fabric Network https://www.edureka.co/blockchain-training

[email protected] |
Scenario: Bob can submit a transaction for his assets
When I use the identity bob1
And I submit the following transaction of type
org.acme.mynetwork.Trade
| commodity | newOwner |
| 2 | [email protected] |
Then I should have the following assets of type
org.acme.mynetwork.Commodity
| tradingSymbol | description | mainExchange | quantity |
owner |
| 2 | Two | Paris | 2 |
[email protected] |

Step Twelve: Creating BNA file


Ensure you are at the top level project folder (my-network). Generate the BNA file using the following
command:
– mkdir dist
– composer archive create -a dist/my-network.bna --sourceType dir --sourceName
You can also use the command npm run prepublish achieves the same thing.

Step Thirteen: Deploying Business Network to Composer


In Chrome browser, navigate to the online Bluemix Composer Playground.

©Brain4ce Education Solutions Pvt. Ltd Page 14


Module 8: Setting up Hyperledger Fabric Network https://www.edureka.co/blockchain-training

▪ Click on Deploy a new business network

▪ Update the fields with data as seen below and click on Drop here to upload or browse

©Brain4ce Education Solutions Pvt. Ltd Page 15


Module 8: Setting up Hyperledger Fabric Network https://www.edureka.co/blockchain-training

▪ Choose your my-network.bn file from your system

©Brain4ce Education Solutions Pvt. Ltd Page 16


Module 8: Setting up Hyperledger Fabric Network https://www.edureka.co/blockchain-training

• Once the file is uploaded, Click on Deploy.

• Click on Connect now to connect to the deployed Business Network.

©Brain4ce Education Solutions Pvt. Ltd Page 17


Module 8: Setting up Hyperledger Fabric Network https://www.edureka.co/blockchain-training

Step Fourteen: Creating Traders


• Click on Test Option and Ensure you are in Trader Participant plane.
• Add 2 New participants with the codes below:

Participant 1

{
"$class": "org.acme.mynetwork.Trader",
"tradeId": "TRADER1",
"firstName": "James",
"lastName": “Smith”
}

Participant 2
{
"$class": "org.acme.mynetwork.Trader",
"tradeId": "TRADER2",
"firstName": “John",
"lastName": “Snow“
}

©Brain4ce Education Solutions Pvt. Ltd Page 18


Module 8: Setting up Hyperledger Fabric Network https://www.edureka.co/blockchain-training

• Click on Create New once the codes are updated.

Step Fifteen: Creating Commodity


• Switch to Commodities tab under Assets tab and Click on add commodity option.
• Replace the existing code with the code below:

©Brain4ce Education Solutions Pvt. Ltd Page 19


Module 8: Setting up Hyperledger Fabric Network https://www.edureka.co/blockchain-training

Commodity

{
"$class": "org.acme.mynetwork.Commodity",
"tradingSymbol": "ABC",
"description": "Test commodity",
"mainExchange": "Euronext",
"quantity": 72.297,
"owner":
"resource:org.acme.mynetwork.Trader#TRADER1"
}

Step Sixteen: Initiating a Trade

▪ Initiate a trade by selecting Submit Button.

▪ Replace the existing code with the code below:

©Brain4ce Education Solutions Pvt. Ltd Page 20


Module 8: Setting up Hyperledger Fabric Network https://www.edureka.co/blockchain-training

{
"$class": "org.acme.mynetwork.Trade",
"commodity":
"resource:org.acme.mynetwork.Commodity#ABC",
"newOwner":
"resource:org.acme.mynetwork.Trader#TRADER2"
}

Step Seventeen: Validating the Trade


▪ Go to the Transaction history and owners tab to validate that the asset has been
transferred to the new owner as seen below:

▪ As a result of the trade, the owner of the commodity ABC should now be owned TRADER2.

©Brain4ce Education Solutions Pvt. Ltd Page 21


Module 8: Setting up Hyperledger Fabric Network https://www.edureka.co/blockchain-training

Step Eighteen: Deploying Network to Hyperledger Fabric


▪ Switch to the terminal, change directory to the my-network/dist folder containing the my-
network.bna file and execute the following command:

– cd dist

– composer network deploy -a my-network.bna -p hlfv1 -i PeerAdmin -s


randomString

▪ After approximately 30 seconds or so, the business network should have been deployed to your
local Hyperledger Fabric. You should see output as follows:

Note: In case if the business network is not getting deployed, first check if the docker image of fabric is
running. To do that, use the following commands:

– docker ps

If no container image is running, execute the following code in a new terminal:

– cd ~
– cd fabric-tools
– ./startFabric.sh

©Brain4ce Education Solutions Pvt. Ltd Page 22

You might also like