HyperLedger Fabcar Study notes (writing the first app based on the Super Ledger)

Source: Internet
Author: User
Tags docker ps
This is a creation in Article, where the information may have evolved or changed.

HyperLedger Fabcar Study notes (writing the first app based on the Super Ledger)

Translated from: Http://hyperledger-fabric.rea ...

Writing the first application

We need to implement the following three steps

    1. First set up a development environment
    2. Learn some simple parameters of the smart contract that our app will use
    3. Develop an app to update queries for a ledger

Setting up the development environment

    1. First make sure that the necessary fabric image has been installed successfully, if not installed, please refer to (https://hyperledger-fabric.re ... )
    2. Download Fabric-samples, and install the necessary running tools. (Refer to Address: https://hyperledger-fabric.re ... )
    3. The above steps if you do not want to see the English students, you can refer to (build the first super-Ledger network method) Https://segmentfault.com/a/11 ...
    4. If you follow the tutorial on creating a Super Ledger network, you might start a network that will have an impact on the next process and need to shut down the network.

      ./byfn.sh -m down

    5. Go to the Fabric-sample/fabcar directory: This is an app for car trading

      cd fabric-samples/fabcar  && ls显示结果enrollAdmin.js     invoke.js       package.json    query.js        registerUser.js startFabric.sh
    6. Kill all the Docker image services currently running

      docker rm -f $(docker ps -aq)

    7. Clear all Cache networks
      docker network prune
    8. If you have already run this tutorial, you will need to do the following to remove the Chaincode image if the first run is not available
      docker rmi dev-peer0.org1.example.com-fabcar-1.0-5c906e402ed29f20260ae42283216aa75549c571e2e380f3615826365d8269ba

Install the client and log on to the network

    1. The first thing to make sure that fabric-ca-client and fabric-client are ready is that the two module is the SDK for node. js to access the Super Ledger network

      npm install fabric-client
      npm install fabric-ca-client

      • The Node_module directory and a Package.json file are displayed in the Fabcar directory. If you don't understand this section, you can learn about node. js.
    2. Create a Super ledger network with commands

      ./startFabric.sh#运行golang编写的chaincode

    • Description: This script creates a network of super ledgers, including nodes such as Ca-server peer order, and installs and instantiates the Chaincode.
      ./startFabric.sh nodeChaincode written by #运行node. js

How the app uses the web

Create Admin user

    1. The first app Admin user should send a certificate enrollment request to Ca-server, accept a registration certificate for this user (Ecert), follow up we will use this admin to register and authenticate a new user. The command is as follows:

      node enrollAdmin.js

      The successful execution of the command creates a Hfc-key-store directory in which the admin's identity is stored, and a pair of public and private keys are available.

Create a regular user

    1. Create an ordinary user user1 that the user uses to query and update the ledger. The Admin user identity is used to create User1 users. Execute the following command:

      node registerUser.js

Query Ledger information

  1. Execute query command to query current car information

    node query.js

      • All car information is displayed CAR0-CAR9
      [{"Key": "CAR0", "Record": {"colour": "Blue", "Make": "Toyota", "model": "Prius", "owner": "Tomoko"}},{"key": "CAR1", "record": {"colour": "Red", "make": "Ford", "model": "Mustang", "owner": "Brad"}},{"Key": "CAR2", "record": {" Colour ":" Green "," make ":" Hyundai "," model ":" Tucson "," owner ":" Jin soo "}},{" Key ":" CAR3 "," Record ": {" colour ":" Yellow " , "Make": "Volkswagen", "model": "Passat", "owner": "Max"}},{"Key": "CAR4", "Record": {"colour": "Black", "make": "Tesla", "Model": "S", "owner": "Adriana"}},{"Key": "CAR5", "Record": {"colour": "Purple", "Make": "Peugeot", "Model": "205", " Owner ":" Michel "}},{" Key ":" CAR6 "," Record ": {" colour ":" White "," Make ":" Chery "," model ":" s22l "," owner ":" Aarav "}},{" Key ":" CAR7 "," record ": {" colour ":" Violet "," Make ":" Fiat "," model ":" Punto "," owner ":" Pari "}},{" Key ":" CAR8 "," Record ": {"Colour": "Indigo", "Make": "Tata", "model": "Nano", "owner": "Valeria"}},{"Key": "CAR9", "Record": {"colour": "Brown", " Make ":" Holden "," model ":" Barina "," owner ":" Shotaro "}}]  
  2. We can try to modify query.js and run the script again

    const request = {//targets : --- letting this default to the peers assigned to the channel    chaincodeId: 'fabcar',    fcn: 'queryCar',    //此处修改为查询单个CAR信息    args: ['CAR5']        //通过修改参数查询指定的CAR信息};显示结果如下Store path:/Users/ly/go/src/github.com/hyperledger/fabric-samples/fabcar/hfc-key-storeSuccessfully loaded user1 from persistenceQuery has completed, checking resultsResponse is  {"colour":"purple","make":"Peugeot","model":"205","owner":"Michel"}
  3. We view the smart contract code by looking at Fabric-sample/chaincode/fabcar, and our app calls the functions supported in the smart contract via RPC. The support functions are as follows: Initledger used to create 10 car information for initialization

    initLedger, queryCar, queryAllCars, createCar, and changeCarOwner.
  4. App Execution flowchart

Update ledger information

  1. Updating the ledger information is similar to the query on the app side, which is to update the data via the interface provided by the smart contract. Currently available features include the creation of car and the modification of the owner property of car. We can implement calls to different interfaces by modifying the Invoke.js code.

    var request = {        //targets:let default to the peer assigned to the CLIENT&N Bsp;       chaincodeid: ' Fabcar ',         FCN: ' Createcar ',         args: [' CAR12 ', ' Honda ', ' Accord ', ' Black ', ' Tom '],        chainid: ' MyChannel ',         txId:tx_id    }; execution Result: Lydeimac:fabcar ly$ node Invoke.js Store path:/users/ly/go/src /github.com/hyperledger/fabric-samples/fabcar/hfc-key-storesuccessfully loaded user1 from persistenceassigning Transaction_id:a5b684603b1f2a0296851409cecb143c3109220014182721165ef8fe5c326b2etransaction proposal was Goodsuccessfully sent proposal and received proposalresponse:status-200, message-"OK" The transaction has been Committ Ed on peer Localhost:7053send transaction promise and Event listener promise has compLetedsuccessfully sent transaction to the Orderer. Successfully committed the ledger by the peer
  2. Query all car information after creation, you can see the addition of CAR12 information

    [{"Key": "CAR0", "Record": {"colour": "Blue", "Make": "Toyota", "model": "Prius", "owner": "Tomoko"}},{"key": "CAR1", " Record ": {" colour ":" Red "," make ":" Ford "," model ":" Mustang "," owner ":" Brad "}},{" Key ":" CAR12 "," Record ": {" colour ":" Black "," Make ":" Honda "," model ":" Accord "," owner ":" Tom "}},{" Key ":" CAR2 "," Record ": {" colour ":" Green "," Make ":" Hyundai "," model ":" Tucson "," owner ":" Jin soo "}},{" Key ":" CAR3 "," Record ": {" colour ":" Yellow "," make ":" Volkswagen "," Model ":" Passat "," owner ":" Max "}},{" Key ":" CAR4 "," Record ": {" colour ":" Black "," make ":" Tesla "," model ":" S "," Owner ":" Adriana "}},{" Key ":" CAR5 "," Record ": {" colour ":" Purple "," Make ":" Peugeot "," Model ":" 205 "," owner ":" Michel "}},{" key ": "CAR6", "record": {"colour": "White", "Make": "Chery", "model": "s22l", "owner": "Aarav"}},{"Key": "CAR7", "record": {" Colour ":" Violet "," Make ":" Fiat "," model ":" Punto "," owner ":" Pari "}},{" Key ":" CAR8 "," Record ": {" colour ":" Indigo "," Make ":" Tata "," model ":" Nano "," owner ":" Valeria "}},{" Key ":" CAR9 "," Record ": {" colour ":" Brown "," Make ":" Holden "," Model ":" Barina "," owner":" Shotaro "}}] 
  3. Contract Execution Process

- 先通过客户端提交交易- 通过背书节点检查后,客户端得到背书节点响应- 客户端将背书节点响应结果提交给order排序节点- order节点创建区块后,广播给所有的peer节点更新账本
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.