github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/Decentralized-Energy-Composer-master/README.md (about)

     1  *Read this in other languages: [中文](README-cn.md).*
     2  # Decentralized Energy with Hyperledger Composer
     3  
     4  A key application of Blockchain being currently explored is a Decentralized Energy network. The idea stems from a neighborhood where certain Residents are producing energy through Solar panels or other means, and can sell excess energy to Residents needing energy. The transactions would be based on coins in each Resident's account. As per a pre-determined contract and rate, the coins would be debited from the consumer and credited to the producer, for a certain billing period. Each transaction would need to be atomic and added to a Blockchain ledger for trust and verification. The network can include Banks to transact coins for Fiat currency (USD). The network can have Utility Company who can buy or provide energy through the network.
     5  
     6  In this code pattern, we will create such a Blockchain application using Hyperledger Composer. The network consists of Residents, Banks and Utility Companies. Residents can exchange coins for energy among each other.  The application assumes a pre-paid system where transactions occur after the energy is consumed and the values are updated.  The Resident can exchange coins for Fiat money (USD) with Banks on the network.  The Residents can also transact coins for energy with a Utility company on the network.
     7  
     8  This code pattern is for developers looking to start building Blockchain applications with Hyperledger Composer. When the reader has completed this code pattern, they will understand how to:
     9  
    10  * Create business network using Hyperledge Composer and recording transactions on Blockchain ledger
    11  * Deploying the networking to an instance of Hyperledger Fabric
    12  * Building an Angular app to interact with the network through REST API
    13  
    14  
    15  # Architecture Flow
    16  
    17  <p align="center">
    18    <img width="650" height="200" src="images/arch.png">
    19  </p>
    20  
    21  1. The administrator interacts with Decentralized Energy UI comprising of Angular framework
    22  2. The application processes user requests to the network through a REST API.
    23  3. Implements requests to the Blockchain state database on Hyperledger Fabric v1
    24  4. The REST API is used to retrieve the state of the database
    25  5. The Angular framework gets the data through GET calls to the REST API
    26  
    27  # Included Components
    28  
    29  * Hyperledger Composer v0.20
    30  * Hyperledger Fabric v1.2
    31  * Angular Framework
    32  * Loopback
    33  
    34  
    35  # Running the Application
    36  Follow these steps to setup and run this code pattern. The steps are described in detail below.
    37  
    38  ## Prerequisite
    39  - Operating Systems: Ubuntu Linux 14.04 / 16.04 LTS (both 64-bit), or Mac OS 10.12
    40  - [Docker](https://www.docker.com/) (Version 17.03 or higher)
    41  - [npm](https://www.npmjs.com/)  (v5.x)
    42  - [Node](https://nodejs.org/en/) (version 8.9 or higher - note version 9 is not supported)
    43    * to install specific Node version you can use [nvm](https://davidwalsh.name/nvm)
    44  - [Hyperledger Composer](https://hyperledger.github.io/composer/installing/development-tools.html)
    45    * to install composer cli
    46      `npm install -g composer-cli@0.20`
    47    * to install composer-rest-server
    48      `npm install -g composer-rest-server@0.20`
    49    * to install generator-hyperledger-composer
    50      `npm install -g generator-hyperledger-composer@0.20`
    51  
    52  ## Steps
    53  1. [Clone the repo](#1-clone-the-repo)
    54  2. [Setup Fabric](#2-setup-fabric)
    55  3. [Generate the Business Network Archive](#3-generate-the-business-network-archive)
    56  4. [Deploy to Fabric](#4-deploy-to-fabric)
    57  5. [Run Application](#5-run-application)
    58  6. [Create Participants](#6-create-participants)
    59  7. [Execute Transactions](#7-execute-transactions)
    60  
    61  ## 1. Clone the repo
    62  
    63  Clone the `Decentralized-Energy-Composer code` locally. In a terminal, run:
    64  
    65  ```
    66  git clone https://github.com/IBM/Decentralized-Energy-Composer
    67  cd Decentralized-Energy-Composer
    68  ```
    69  
    70  ## 2. Setup Fabric
    71  
    72  These commands will kill and remove all running containers, and should remove all previously created Hyperledger Fabric chaincode images:
    73  
    74  ```none
    75  docker kill $(docker ps -q)
    76  docker rm $(docker ps -aq)
    77  docker rmi $(docker images dev-* -q)
    78  ```
    79  
    80  Set the fabric version to v1.2:
    81  ```
    82  export FABRIC_VERSION=hlfv12
    83  ```
    84  
    85  All the scripts will be in the directory `/fabric-tools`.  Start fabric and create peer admin card:
    86  
    87  ```
    88  cd fabric-dev-servers/
    89  ./downloadFabric.sh
    90  ./startFabric.sh
    91  ./createPeerAdminCard.sh
    92  ```
    93  
    94  ## 3. Generate the Business Network Archive
    95  
    96  Next generate the Business Network Archive (BNA) file from the root directory:
    97  
    98  ```
    99  cd ../
   100  npm install
   101  ```
   102  
   103  The `composer archive create` command in `package.json` has created a file called `decentralized-energy-network@0.1.15.bna`.
   104  
   105  
   106  ## 4. Deploy to Fabric
   107  
   108  Now, we are ready to deploy the business network to Hyperledger Fabric. This requires the Hyperledger Composer chaincode to be installed on the peer, then the business network archive (.bna) must be sent to the peer, and a new participant, identity, and associated card must be created to be the network administrator. Finally, the network administrator business network card must be imported for use, and the network can then be pinged to check it is responding.
   109  
   110  First, install the business network:
   111  
   112  ```
   113  composer network install --card PeerAdmin@hlfv1 --archiveFile decentralized-energy-network@0.1.15.bna
   114  ```
   115  
   116  Start the business network:
   117  
   118  ```
   119  composer network start --networkName decentralized-energy-network --networkVersion 0.1.15 --networkAdmin admin --networkAdminEnrollSecret adminpw --card PeerAdmin@hlfv1 --file networkadmin.card
   120  ```
   121  
   122  Import the network administrator identity as a usable business network card:
   123  ```
   124  composer card import --file networkadmin.card
   125  ```
   126  
   127  Check that the business network has been deployed successfully, run the following command to ping the network:
   128  ```
   129  composer network ping --card admin@decentralized-energy-network
   130  ```
   131  
   132  ## 5. Run Application
   133  
   134  First, go into the `angular-app` folder and install the dependency:
   135  
   136  ```
   137  cd angular-app/
   138  npm install
   139  ```
   140  
   141  To start the application:
   142  ```
   143  npm start
   144  ```
   145  
   146  The application should now be running at:
   147  `http://localhost:4200`
   148  
   149  <div style='border: 2px solid #f00;'>
   150    <img width="800" src="images/app_scrnshot.png">
   151  </div>
   152  </br>
   153  
   154  The REST server to communicate with network is available here:
   155  `http://localhost:3000/explorer/`
   156  
   157  
   158  ## 6. Create Participants
   159  
   160  Once the application opens, create participants and fill in dummy data.  Create Residents, Banks and Utility Companies.
   161  
   162  
   163  ## 7. Execute Transactions
   164  
   165  Execute transactions manually between Residents, Resident and Bank, and Resident and Utility Company.  After executing transactions, ensure the participants account values are updated.
   166  
   167  
   168  At the end of your session, stop fabric:
   169  
   170  ```
   171  cd ~/fabric-tools
   172  ./stopFabric.sh
   173  ./teardownFabric.sh
   174  ```
   175  
   176  ## Extending Code Pattern
   177  
   178  This application demonstrates a basic idea of a decentralized energy network using Blockchain and can be expanded in several ways:
   179  * Adding specific permissions and participant access
   180  * Setting up real time transactions among participants
   181  * Integrating with IoT to read from power meter and distribute energy
   182  
   183  ## Deploy to IBM Cloud
   184  
   185  The blockchain network can be deployed to IBM Cloud. 
   186  You can use the [IBM Blockchain platform](https://console.bluemix.net/catalog/services/blockchain) and start for free under `Starter Membership Plan`.  Follow [these instructions](https://console.bluemix.net/docs/services/blockchain/develop_starter.html#deploying-a-business-network) to deploy the business network to IBM Blockchain platform.
   187  
   188  ## Additional Resources
   189  * [Hyperledger Fabric Docs](http://hyperledger-fabric.readthedocs.io/en/latest/)
   190  * [Hyperledger Composer Docs](https://hyperledger.github.io/composer/latest/introduction/introduction.html)
   191  
   192  ## License
   193  This code pattern is licensed under the Apache Software License, Version 2.  Separate third party code objects invoked within this code pattern are licensed by their respective providers pursuant to their own separate licenses. Contributions are subject to the [Developer Certificate of Origin, Version 1.1 (DCO)](https://developercertificate.org/) and the [Apache Software License, Version 2](https://www.apache.org/licenses/LICENSE-2.0.txt).
   194  
   195  [Apache Software License (ASL) FAQ](https://www.apache.org/foundation/license-faq.html#WhatDoesItMEAN)