HyperLedger Fabcar 學習筆記(基於超級帳本編寫第一個應用)

HyperLedger Fabcar 學習筆記(基於超級帳本編寫第一個應用)

翻譯自:http://hyperledger-fabric.rea...html

編寫第一個應用程序

咱們須要實現以下三個步驟node

  1. 首先設置一個開發環境
  2. 學習一些簡單的咱們app將會用到的智能合約的參數
  3. 開發一個app可以更新查詢一個帳本

設置開發環境

  1. 首先確保必備的fabric image等已經安裝成功,若是沒有安裝,請參考(https://hyperledger-fabric.re...
  2. 下載fabric-samples,而且安裝必備的運行工具。(參考地址:https://hyperledger-fabric.re...
  3. 上述步驟若是不想看英文的同窗,能夠參考(搭建第一個超級帳本網絡的方法)https://segmentfault.com/a/11...
  4. 若是按照創建超級帳本網絡的教程運行,可能會啓動着一個網絡,會對接下來的流程有影響,須要關閉網絡。

    ./byfn.sh -m downgit

  5. 進入到fabric-sample/fabcar目錄下:這是一個關於car(汽車)交易的appgithub

    cd fabric-samples/fabcar  && ls
    顯示結果
    enrollAdmin.js     invoke.js       package.json    query.js        registerUser.js startFabric.sh
  6. 殺死當前運行的全部docker 鏡像服務

    docker rm -f $(docker ps -aq)golang

  7. 清除全部緩存網絡
    docker network prune
  8. 若是已經運行過此教程,還須要執行以下操做,刪除chaincode鏡像,若是第一次運行是不會有的
    docker rmi dev-peer0.org1.example.com-fabcar-1.0-5c906e402ed29f20260ae42283216aa75549c571e2e380f3615826365d8269ba

安裝客戶端並登陸網絡

  1. 首先要確保fabric-ca-client和fabric-client是否已經準備好了,這兩個module是Node.js訪問超級帳本網絡的SDKdocker

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

    • 此時會在fabcar目錄下顯示node_module目錄以及一個package.json的文件。此部分若是不理解能夠學習node.js相關知識。
  2. 經過命令建立一個超級帳本網絡

    ./startFabric.sh #運行golang編寫的chaincodejson

  • 說明:此腳本會建立建立一個超級帳本網絡,包括ca-server peer order等節點,安裝並實例化了chaincode。
    ./startFabric.sh node#運行node.js編寫的chaincode

App如何使用網絡

建立Admin用戶

  1. 首先應用的admin用戶應該向ca-server發送一個證書登記請求,接受一個對於這個user的登記證書(eCert),後續咱們會根據使用這個admin註冊和認證一個新的user。命令以下:

    node enrollAdmin.jssegmentfault

    命令執行成功會建立一個hfc-key-store目錄,目錄中存放admin的身份標示,一對公私密鑰。promise

建立普通User用戶

  1. 建立一個普通用戶user1,這個用戶用來查詢和更新帳本。admin用戶身份用來建立user1用戶。執行以下命令:

    node registerUser.js

查詢帳本信息

  1. 執行查詢命令查詢當前汽車信息

    node query.js

    • 會顯示所有的CAR信息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. 咱們能夠嘗試修改query.js後再次運行腳本

    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-store
    Successfully loaded user1 from persistence
    Query has completed, checking results
    Response is  {"colour":"purple","make":"Peugeot","model":"205","owner":"Michel"}
  3. 咱們經過查看fabric-sample/chaincode/fabcar 查看智能合約代碼,咱們的app就是調用了智能合約中支持的函數,經過rpc方式。支持函數以下:initLedger用於建立初始化的10個CAR信息

    initLedger, queryCar, queryAllCars, createCar, and changeCarOwner.
  4. APP執行流程圖

更新帳本信息

  1. 更新帳本信息在APP側和查詢類似,就是經過智能合約提供的接口更新數據。當前提供的功能包括建立CAR,以及修改CAR的owner屬性。咱們能夠經過修改invoke.js代碼實現對不一樣接口的調用。

    var request = {
            //targets: let default to the peer assigned to the client
            chaincodeId: 'fabcar',
            fcn: 'createCar',
            args: ['CAR12', 'Honda', 'Accord', 'Black', 'Tom'],
            chainId: 'mychannel',
            txId: tx_id
        };
    執行結果:
    lydeiMac:fabcar ly$ node invoke.js 
    Store path:/Users/ly/go/src/github.com/hyperledger/fabric-samples/fabcar/hfc-key-store
    Successfully loaded user1 from persistence
    Assigning transaction_id:  a5b684603b1f2a0296851409cecb143c3109220014182721165ef8fe5c326b2e
    Transaction proposal was good
    Successfully sent Proposal and received ProposalResponse: Status - 200, message - "OK"
    The transaction has been committed on peer localhost:7053
    Send transaction promise and event listener promise have completed
    Successfully sent transaction to the orderer.
    Successfully committed the change to the ledger by the peer
  2. 建立後查詢全部CAR信息,能夠看到增長了CAR12信息

    [{"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. 合約執行流程如圖:

- 先經過客戶端提交交易
- 經過背書節點檢查後,客戶端獲得背書節點響應
- 客戶端將背書節點響應結果提交給order排序節點
- order節點建立區塊後,廣播給全部的peer節點更新帳本
相關文章
相關標籤/搜索