Clone Steven Perry's Project, Build, Deploy, and Start a Business Network
$ cd $HOME/projects/hlf
$ git clone https://github.com/makotogo/developerWorks.git
Use the Node.js package manager (npm) to build the business network.
[kiat@emgsdev iot-perishable-network]$ npm install
Deploy the business network with composer network install
:
[kiat@emgsdev iot-perishable-network]$ composer network install -a dist/iot-perishable-network.bna -c PeerAdmin@hlfv1
✔ Installing business network. This may take a minute...
Successfully installed business network iot-perishable-network, version 0.1.12
Command succeeded
Start the business network with composer network start
:
[kiat@emgsdev iot-perishable-network]$ composer network start --networkName iot-perishable-network --networkVersion 0.1.12 --card PeerAdmin@hlfv1 --networkAdmin admin --networkAdminEnrollSecret adminpw
Starting business network iot-perishable-network at version 0.1.12
Processing these Network Admins:
userName: admin
✔ Starting business network definition. This may take a minute...
Successfully created business network card:
Filename: admin@iot-perishable-network.card
Command succeeded
The composer card import
command tells Hyperledger Composer to import the specified card file so it can be used later to authenticate the user whose credentials are stored in the card. In this case, the card is for the network admin.
[kiat@emgsdev iot-perishable-network]$ composer card import --file admin@iot-perishable-network.card
Successfully imported business network card
Card file: admin@iot-perishable-network.card
Card name: admin@iot-perishable-network
Command succeeded
We have just deployed a business network to our local Hyperledger Fabric.
View the Deployed Business Network
[kiat@emgsdev ~]$ composer network list -c admin@iot-perishable-network
✔ List business network from card admin@iot-perishable-network
models:
- org.hyperledger.composer.system
- org.acme.shipping.perishable
scripts:
- lib/logic.js
registries:
org.acme.shipping.perishable.Contract:
id: org.acme.shipping.perishable.Contract
name: Asset registry for org.acme.shipping.perishable.Contract
registryType: Asset
org.acme.shipping.perishable.Shipment:
id: org.acme.shipping.perishable.Shipment
name: Asset registry for org.acme.shipping.perishable.Shipment
registryType: Asset
org.acme.shipping.perishable.Grower:
id: org.acme.shipping.perishable.Grower
name: Participant registry for org.acme.shipping.perishable.Grower
registryType: Participant
org.acme.shipping.perishable.Importer:
id: org.acme.shipping.perishable.Importer
name: Participant registry for org.acme.shipping.perishable.Importer
registryType: Participant
org.acme.shipping.perishable.Shipper:
id: org.acme.shipping.perishable.Shipper
name: Participant registry for org.acme.shipping.perishable.Shipper
registryType: Participant
Command succeeded
Update the Network
Whenever we make a change during development and we need to update the deployed network:
After running npm install
to rebuild the BNA, enter this command:
$ composer network update -a dist/iot-perishable-network.bna --card admin@iot-perishable-network
Interact with the network via the Composer REST interface
Install the Composer REST interface generator:
[kiat@emgsdev iot-perishable-network]$ npm install -g composer-rest-server
npm WARN deprecated nodemailer@2.7.2: All versions below 4.0.1 of Nodemailer are deprecated. See https://nodemailer.com/status/
npm WARN deprecated socks@1.1.9: If using 2.x branch, please upgrade to at least 2.1.6 to avoid a serious bug with socket data flow and an import issue introduced in 2.1.0
npm WARN deprecated mailcomposer@4.0.1: This project is unmaintained
npm WARN deprecated buildmail@4.0.1: This project is unmaintained
npm WARN deprecated swagger-ui@2.2.10: No longer maintained, please upgrade to swagger-ui@3.
/home/kiat/.nvm/versions/node/v8.12.0/bin/composer-rest-server -> /home/kiat/.nvm/versions/node/v8.12.0/lib/node_modules/composer-rest-server/cli.js
+ composer-rest-server@0.20.4
updated 15 packages in 29.722s
Generate the REST interface:
[kiat@emgsdev iot-perishable-network]$ composer-rest-server -v
v0.20.4
[kiat@emgsdev iot-perishable-network]$ composer-rest-server
? Enter the name of the business network card to use: admin@iot-perishable-netwo
rk
? Specify if you want namespaces in the generated REST API: always use namespace
s
? Specify if you want to use an API key to secure the REST API: No
? Specify if you want to enable authentication for the REST API using Passport:
No
? Specify if you want to enable the explorer test interface: Yes
? Specify a key if you want to enable dynamic logging:
? Specify if you want to enable event publication over WebSockets: No
? Specify if you want to enable TLS security for the REST API: No
To restart the REST server using the same options, issue the following command:
composer-rest-server -c admin@iot-perishable-network -n always -u true
Discovering types from business network definition ...
Discovering the Returning Transactions..
Discovered types from business network definition
Generating schemas for all types in business network definition ...
Generated schemas for all types in business network definition
Adding schemas for all types to Loopback ...
Added schemas for all types to Loopback
Web server listening at: http://localhost:3000
Browse your REST API at http://localhost:3000/explorer
LoopBack Framework
Using the the open source API framework, LoopBack Show archive.org snapshot , on top of the Hyperledger Composer runtime has allowed us to generate a business domain specific REST API based on the deployed business network model!
LoopBack is a framework for creating APIs and connecting them with backend data sources. Built on top of Express, it can take a data model definition and easily generate a fully functional end-to-end REST API that can be called by any client.
LoopBack comes with a built-in client, API Explorer. We’ll use this since it makes it easier to see the results of our work, and so that our example can focus on building the API itself.
Stopping and Restarting the REST Server
To stop the REST server, enter ctrl-C
in the terminal.
To restart the REST server using the same options, issue the following command: composer-rest-server -c admin@iot-perishable-network -n always -u true
Updating the REST Server
After updating a business network definition, the REST server can be updated to generate new APIs reflecting the updates to the business network definition.
To update the REST server, first the REST server must be stopped using ctrl-C
. Then the REST server can be restarted using composer-rest-server
.
Determine Port 3000 is Listening for TCP Connections
In another terminal:
Method 1:
[kiat@emgsdev ~]$ lsof -i TCP:3000
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
node 167135 kiat 14u IPv6 42189389 0t0 TCP *:hbci (LISTEN)
hbci stands for Home Banking Computer Interface and doesn't mean anything here; the reason it is here is because netstat
and similar utilities look up port numbers in a database that maps them to symbolic names (usually, /etc/services).
Method 2:
[kiat@emgsdev ~]$ sudo netstat -anp | grep 3000
tcp6 0 0 :::3000 :::* LISTEN 167135/node
Method 3: using nmap
, if it's installed:
[kiat@emgsdev ~]$ sudo nmap -sT -O localhost
Starting Nmap 7.70 ( https://nmap.org ) at 2018-11-07 09:47 +08
RTTVAR has grown to over 2.3 seconds, decreasing to 2.0
RTTVAR has grown to over 2.3 seconds, decreasing to 2.0
RTTVAR has grown to over 2.3 seconds, decreasing to 2.0
RTTVAR has grown to over 2.3 seconds, decreasing to 2.0
RTTVAR has grown to over 2.3 seconds, decreasing to 2.0
RTTVAR has grown to over 2.3 seconds, decreasing to 2.0
RTTVAR has grown to over 2.3 seconds, decreasing to 2.0
Nmap scan report for localhost (127.0.0.1)
Host is up (0.00025s latency).
Other addresses for localhost (not scanned): ::1
Not shown: 989 closed ports
PORT STATE SERVICE
25/tcp open smtp
80/tcp open http
111/tcp open rpcbind
443/tcp open https
3000/tcp open ppp
3306/tcp open mysql
9000/tcp open cslistener
9001/tcp open tor-orport
9002/tcp open dynamid
9003/tcp open unknown
9010/tcp open sdr
Device type: general purpose
Running: Linux 3.X|4.X
OS CPE: cpe:/o:linux:linux_kernel:3 cpe:/o:linux:linux_kernel:4
OS details: Linux 3.7 - 3.10, Linux 3.8 - 4.14
Network Distance: 0 hops
OS detection performed. Please report any incorrect results at https://nmap.org/submit/ .
Nmap done: 1 IP address (1 host up) scanned in 1.92 seconds
The port list should include 3000/tcp open ppp
.