Module 8: Setting Up Hyperledger Fabric Network: Practical Document
Module 8: Setting Up Hyperledger Fabric Network: Practical Document
co/blockchain-training
Practical Document
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:
"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"
},
• 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
/**
* 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
}
/**
* 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);
});
}
/**
* 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
}
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';
require('chai').should();
// 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', () => {
commodity.owner.$identifier.should.equal(dan.$identifier);
newCommodity.owner.$identifier.should.equal(simon.$identifier);
});
});
});
});
#
# 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
[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] |
▪ Update the fields with data as seen below and click on Drop here to upload or browse
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“
}
Commodity
{
"$class": "org.acme.mynetwork.Commodity",
"tradingSymbol": "ABC",
"description": "Test commodity",
"mainExchange": "Euronext",
"quantity": 72.297,
"owner":
"resource:org.acme.mynetwork.Trader#TRADER1"
}
{
"$class": "org.acme.mynetwork.Trade",
"commodity":
"resource:org.acme.mynetwork.Commodity#ABC",
"newOwner":
"resource:org.acme.mynetwork.Trader#TRADER2"
}
▪ As a result of the trade, the owner of the commodity ABC should now be owned TRADER2.
– cd dist
▪ 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
– cd ~
– cd fabric-tools
– ./startFabric.sh