IBM/BlockchainPublicRegulationFabric-Food

Name: BlockchainPublicRegulationFabric-Food

Owner: International Business Machines

Description: A Public Regulation Fabric example in the Food Industry

Created: 2017-08-21 20:22:44.0

Updated: 2018-05-24 00:30:30.0

Pushed: 2018-04-23 21:39:43.0

Homepage: https://developer.ibm.com/code/patterns/implement-fda-food-supplier-verification-program-on-hyperledger-composer/

Size: 2701

Language: JavaScript

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

BlockchainPublicRegulationFabric-Food

In this journey, we capture the regulatory compliance logic for the Food Supplier Verification Program in a smart contract deployed on a business network.

This business network defines:

Participants: Supplier Importer Retailer Regulator

Assets: ProductListingContract

Transactions: createProductListing transferListing checkProducts updateExemptedList

Initially, the supplier will transfer the food products to an importer who verifies whether the supplier, country, and food type all match with the correct identifiers. At port of entry, the supplier is checked against a list of known suppliers in a database (managed by the regulator). If the supplier is of type exempt, then the products are then transferred to the retailer. If the supplier is of type non-exempt, then the products are checked against a list of known food products in the database (managed by the regulator). If the food is exempt product then transfer it to the retailer. If the food is a non-exempt product, the importer must conduct a harms analysis (either independently, or using a third-party). The supplier provides the harms analysis report to the regulator. The regulator reviews compliance attestation and transfers the products to the retailer.

The createProductListing function is called when an createProductListing transaction is submitted. The logic allows a supplier to create a ProductListingContract asset.

The transferListing function is called when a transferListing transaction is submitted by the owner of ProductListingContract. It is submitted either by Supplier to transfer ProductListingContract to Importer or by Importer to transfer ProductListingContract to Retailer when the exempt check for the products is completed.

The checkProducts function is called when a checkProducts transaction is submitted by the Supplier to perform the exempt check for the products present in the ProductListingContract. The status of ProductListingContract contract will change to CHECKCOMPLETED if all all the products are exempted else the status will change to HAZARDANALYSISCHECKREQ. HAZARDANALYSISCHECKREQ means the Supplier needs to provide Hazard Analysis report for the products. After submitting the report Supplier performs the checkProducts transaction to complete the exempt check for the products.

The updateExemptedList function is called when a updateExemptedList transaction is submitted by the Regulator to update the list of exempted Orgs ids and Product ids.

Audience level : Intermediate Developers

If you have an IBM cloud lite account, you can also use the Starter Plan for 30 days to do this pattern.

Included Components
Included technologies
Application Workflow Diagram

Application Workflow

Steps
  1. Generate the Business Network Archive (BNA)
  2. Deploy the Business Network Archive using Composer Playground
  3. Deploy the Business Network Archive on Hyperledger Composer running locally
1. Generate the Business Network Archive (BNA)

To check that the structure of the files is valid, you can now generate a Business Network Archive (BNA) file for your business network definition. The BNA file is the deployable unit – a file that can be deployed to the Composer runtime for execution.

Use the following command to generate the network archive:

install

You should see the following output:

ting Business Network Archive

ing for package.json of Business Network Definition
Input directory: /Users/ishan/Documents/git-demo/BlockchainPublicRegulationFabric-Food

d:
Description: Sample food supplier verification network
Name: food-supply
Identifier: food-supply@0.0.1

ten Business Network Definition Archive file to
Output file: ./dist/food-supply.bna

and succeeded

The composer archive create command has created a file called food-supply.bna in the dist folder.

You can test the business network definition against the embedded runtime that stores the state of 'the blockchain' in-memory in a Node.js process. From your project working directory, open the file test/foodTest.js and run the following command:

test

You should see the following output :

od-supply@0.0.1 test /Users/ishan/Documents/demo/BlockchainPublicRegulationFabric-Food
cha --recursive

omposer.food.supply
? Transfer ProductListing to Importer (67ms)
? Exempt Check for ProductListing (98ms)
? Transfer ProductListing to Retailer (95ms)

passing (2s)
2. Deploy the Business Network Archive using Composer Playground

Use Ctrl-click (or the equivalent action for your system) to open the food-supply.bna file in a separate tab. Click Download to download this file to your own device.

Open Composer Playground. If you have previously used Playground, be sure to clear your browser local storage by running localStorage.clear() in your browser Console, or to use the option presented to you by Composer Playground.

Next, click the Deploy a new business network button.

Deploy new network 1

and drop the food-supply.bna file (downloaded above) in the Drop here to upload or browse area.

Deploy new network 2

Finally, click Deploy to deploy the BNA.

You can also setup Composer Playground locally.

Once imported, click Connect now on the admin card. You should see the following:

Composer Playground

To test your Business Network Definition, first click on the Test tab:

In the Supplier participant registry, create a new participant. Make sure you click on the Supplier tab on the far left-hand side first and click on Create New Participant button.

Create participant

Enter the following information to create the supplier.


class": "composer.food.supply.Supplier",
upplierId": "supplierA",
ountryId": "UK",
rgId": "ACME"

Similarly create retailer, regulator and importer participants by selecting the respective tabs and provide the information as follows:


class": "composer.food.supply.Retailer",
etailerId": "retailerA",
roducts": []



class": "composer.food.supply.Regulator",
egulatorId": "regulatorA",
ocation": "SF",
xemptedOrgIds": ["ACME","XYZ CORP"],
xemptedProductIds": ["prodA","prodB"]



class": "composer.food.supply.Importer",
mporterId": "importerA"

Now we are ready to add Access Control. Do this by first clicking the admin tab followed by ID Registry to issue new IDs to the participants and to add these IDs to the wallet. Please follow the instructions as shown in the images below:

Admin Tab

Click on Issue New ID button to create new IDs.

Generate New Id

Repeat the above step to also create IDs for the importer, regulator and retailer. Once you completed the creation of the four IDs, select the Supplier id from the list and click Use now.

Select ID

Next, click on the test tab to perform createProductListing and transferListing transactions. Click the Submit Transaction button and select the createProductListing transaction from the dropdown to create a product listing for the list of products. The products array element contains information about the productid and quantity separated by ,.


class": "composer.food.supply.createProductListing",
roducts": ["prodA,5","prodB,2"],
ser": "resource:composer.food.supply.Supplier#supplierA"

After executing the transaction successfully, a productListing will be created in ProductListingContract registry.

It is worth copying the ID of the ProductListingContract to your clipboard as you need to provide this ID in the next couple of steps as well.

Product Listing

Similarly, submit a transferListing transaction to transfer the productListing to the Importer.

ProductListingContractID is the ID of the ProductListingContract copied from the ProductListingContract registry.


class": "composer.food.supply.transferListing",
wnerType": "supplier",
ewOwner": "resource:composer.food.supply.Importer#importerA",
roductListing": "resource:composer.food.supply.ProductListingContract#<ProductListingContractID>"

Now importerA will be the owner of ProductListingContract and the status will be EXEMPTCHECKREQ. Also, the productListing will be removed from the Supplier view. Now select the Importer ID from the ID Registry and submit a checkProducts transaction to perform the exempt check for the products.


class": "composer.food.supply.checkProducts",
egulator": "resource:composer.food.supply.Regulator#regulatorA",
roductListing": "resource:composer.food.supply.ProductListingContract#<ProductListingContractID>"

A successful execution of the transaction will change the status of productListing to CHECKCOMPLETED. Now perform a transferListing transaction to transfer the products to retailer.


class": "composer.food.supply.transferListing",
wnerType": "importer",
ewOwner": "resource:composer.food.supply.Retailer#retailerA",
roductListing": "resource:composer.food.supply.ProductListingContract#<ProductListingContractID>"

The transaction will the change the owner of ProductListingContract and update the list of products in Retailer registry. Select the Retailer id from the ID Registry and view the updated registries.

Product Listing

Retailer Registry

You can also use the default System user to perform all the actions as we have a rule in permissions.acl to permit all access System user.

3. Deploy the Business Network Archive on Hyperledger Composer running locally

Please start the local Fabric using the instructions. Now change directory to the dist folder containing food-supply.bna file and type:

ist
oser network install --card PeerAdmin@hlfv1 --archiveFile food-supply.bna
oser network start --networkName food-supply --networkVersion 0.0.1 --networkAdmin admin --networkAdminEnrollSecret adminpw --card PeerAdmin@hlfv1 --file networkadmin.card
oser card import --file networkadmin.card

You can verify that the network has been deployed by typing:

oser network ping --card admin@food-supply

You should see the the output as follows:

connection to the network was successfully tested: food-supply
Business network version: 0.0.1
Composer runtime version: 0.19.4
participant: org.hyperledger.composer.system.NetworkAdmin#admin
identity: org.hyperledger.composer.system.Identity#c1d5b4919bb775fef75f407050c7fa292ce79d7f978703e974cb8f19404d100c

and succeeded

To create the REST API we need to launch the composer-rest-server and tell it how to connect to our deployed business network. Now launch the server by changing directory to the BlockchainPublicRegulationFabric-Food folder and type:

.
oser-rest-server

Answer the questions posed at startup. These allow the composer-rest-server to connect to Hyperledger Fabric and configure how the REST API is generated.

Test REST API

If the composer-rest-server started successfully you should see these two lines are output:

server listening at: http://localhost:3000
se your REST API at http://localhost:3000/explorer

Open a web browser and navigate to http://localhost:3000/explorer

You should see the LoopBack API Explorer, allowing you to inspect and test the generated REST API. Follow the instructions to test Business Network Definition as mentioned above in the composer section.

Additional Resources
License

Apache 2.0


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.