Hyperledger Composer開發(fā)流程

上一篇我們分享了Composer的概念,提到了Composer將一個區(qū)塊鏈應(yīng)用抽象成若干概念,本篇我們將看看Composer如何將這些概念體現(xiàn)到開發(fā)流程的。

Composer開發(fā)體系架構(gòu)

照例先放出一張官網(wǎng)的示意圖:

ComposerArchitecture.png

可以發(fā)現(xiàn)相比直接使用Fabric,已經(jīng)減少了大量的工作??梢酝ㄟ^generator-hyperledger-composer生成Angular的應(yīng)用,然后通過Hyperledger Composer的SDK部署并運(yùn)行在Fabric網(wǎng)絡(luò)環(huán)境中。

整個Composer由以下組件構(gòu)成:

  • 執(zhí)行環(huán)境
  • JavaScript SDK
  • 命令行接口
  • REST Server
  • LoopBack連接器
  • Playground Web UI
  • Yeoman代碼生成器
  • VSCode和Atom編輯器插件

執(zhí)行環(huán)境

Hyperledger Composer設(shè)計(jì)支持多種可插拔的運(yùn)行環(huán)境,目前已經(jīng)實(shí)現(xiàn)了三種運(yùn)行環(huán)境:

  • Hyperledger Fabric v1.1. State存儲在分布式賬本
  • Web. 直接在Web內(nèi)部執(zhí)行,用于Playground演示。State存儲在瀏覽器的local storage中
  • 內(nèi)嵌環(huán)境。直接在Node.js進(jìn)程內(nèi)部執(zhí)行,主要用于商業(yè)邏輯的單元測試。State以KV形式存儲在內(nèi)存中

Connection Profiles

Connection Profiles用于指明Composer如何連接到一個執(zhí)行環(huán)境的。每一種執(zhí)行環(huán)境都有不同的配置選項(xiàng)。

JavaScript SDK

這是一組Node.js API,給開發(fā)者提供創(chuàng)建應(yīng)用操控和部署B(yǎng)usiness Network。這些API分成兩個npm模塊:

  • compser-client: 提交交易請求到business network,以及對資產(chǎn)和參與者執(zhí)行的CURD操作
  • composer-admin: 用于管理business network,安裝、啟動、升級等

命令行接口

composer命令行工具提供部署和管理business network的功能

REST Server

Hyperledger Composer REST Server會自動為business network創(chuàng)建一個Open API(利用Swagger) REST接口。REST Server(基于LoopBack技術(shù))將Composer模型轉(zhuǎn)換為Open API的定義,并且實(shí)現(xiàn)CURD支持。

LoopBack連接器

Hyperledger Composer LoopBack連接器可以被REST Server使用,也可以通過支持LoopBack的集成工具單獨(dú)使用。當(dāng)然也可以通過LoopBack工具創(chuàng)建一個更復(fù)雜的自定義REST API。

Playground Web User Interface

這玩意用于定義和測試business network的??梢宰屔虡I(yè)分析人在Web上快速導(dǎo)入樣本和商業(yè)邏輯模型。

Yeoman代碼生成器

創(chuàng)建以下工程的腳手架:

  • Angular web application
  • Node.js application
  • business network的腳手架

VSCode和Atom編輯器插件

盡管沒有直接的IDE支持,但是這個插件可以替代一些IDE功能。

第一個Composer應(yīng)用

基本概念介紹完畢之后,讓我們動手創(chuàng)建和部署一個應(yīng)用試試看。

第一步: 創(chuàng)建一個business network結(jié)構(gòu)

Hyperledger Composer的一個關(guān)鍵構(gòu)成就是business network definition (BND),BND為區(qū)塊鏈定義了數(shù)據(jù)模型,交易邏輯和訪問控制規(guī)則。

最簡單的方式是直接通過Yeoman創(chuàng)建一個腳手架business network工程:

$ yo hyperledger-composer:businessnetwork
Welcome to the business network generator
? Business network name: tutorial-network
? Description: Here is a hello world example
? Author name:  Feng Yu
? Author email: abcfy2@163.com
? License: Apache-2.0
? Namespace: org.example.mynetwork
? Do you want to generate an empty template network? No: generate a populated sample network
   create package.json
   create README.md
   create models/org.example.mynetwork.cto
   create permissions.acl
   create .eslintrc.yml
   create features/sample.feature
   create features/support/index.js
   create test/logic.js
   create lib/logic.js

在一系列交互式詢問之后,我們就創(chuàng)建了一個business network應(yīng)用程序。

第二步: 定義一個business network

一個business network是由資產(chǎn)、參與者、交易、訪問控制規(guī)則,以及可選的時(shí)間和查詢組成的。在之前創(chuàng)建的腳手架工程中,已經(jīng)有一個model(.cto)文件了,包含了定義了在business network中存在的所有資產(chǎn)、參與者、交易。這個工程同樣也包含了一個訪問控制規(guī)則(permissions.acl),一個包含了交易過程的函數(shù)腳本(logic.js),package.json包含了business network的元數(shù)據(jù)。

模型化資產(chǎn)、參與者以及交易

模型文件(.cto)是由Hyperledger Composer Modelling Language編寫的,我們直接編輯org.example.mynetwork.cto文件:

/**
 * My commodity trading network
 */
namespace org.example.mynetwork
asset Commodity identified by tradingSymbol {
    o String tradingSymbol
    o String description
    o String mainExchange
    o Double quantity
    --> Trader owner
}
participant Trader identified by tradeId {
    o String tradeId
    o String firstName
    o String lastName
}
transaction Trade {
    --> Commodity commodity
    --> Trader newOwner
}

添加JavaScript交易邏輯代碼。model中用transaction聲明的Trade,指明了一個交易和參與者之間的關(guān)系。后面需要定義具體的邏輯實(shí)現(xiàn)。編輯logic.js文件:

/**
 * Track the trade of a commodity from one trader to another
 * @param {org.example.mynetwork.Trade} trade - the trade to be processed
 * @transaction
 */
async function tradeCommodity(trade) {
    trade.commodity.owner = trade.newOwner;
    let assetRegistry = await getAssetRegistry('org.example.mynetwork.Commodity');
    await assetRegistry.update(trade.commodity);
}

添加訪問控制permission.acl:

/**
 * Access control rules for tutorial-network
 */
rule Default {
    description: "Allow all participants access to all resources"
    participant: "ANY"
    operation: ALL
    resource: "org.example.mynetwork.*"
    action: ALLOW
}

rule SystemACL {
  description:  "System ACL to permit all access"
  participant: "ANY"
  operation: ALL
  resource: "org.hyperledger.composer.system.**"
  action: ALLOW
}

第三步: 打包business network

tutorial-network/目錄下執(zhí)行以下命令:

$ composer archive create -t dir -n .
Creating Business Network Archive


Looking for package.json of Business Network Definition
        Input directory: /home/vagrant/tutorial-network

Found:
        Description: Here is a hello world example
        Name: tutorial-network
        Identifier: tutorial-network@0.0.1

Written Business Network Definition Archive file to
        Output file: tutorial-network@0.0.1.bna

Command succeeded

整個工程被打包成了.bna文件。

第四步: 部署business network

需要按照安裝Composer的文檔,將docker環(huán)境啟動(./startFabric.sh),然后部署:

$ composer network install --card PeerAdmin@hlfv1 --archiveFile tutorial-network@0.0.1.bna
? Installing business network. This may take a minute...
Successfully installed business network tutorial-network, version 0.0.1

Command succeeded

之后就可以運(yùn)行了:

composer network start --networkName tutorial-network --networkVersion 0.0.1 --networkAdmin admin --networkAdminEnrollSecret adminpw --card PeerAdmin@hlfv1 --file networkadmin.card
Starting business network tutorial-network at version 0.0.1

Processing these Network Admins:
        userName: admin

? Starting business network definition. This may take a minute...
Successfully created business network card:
        Filename: networkadmin.card

Command succeeded

然后導(dǎo)入網(wǎng)絡(luò)管理員身份作為可用的business network card:

$ composer card import --file networkadmin.card

Successfully imported business network card
        Card file: networkadmin.card
        Card name: admin@tutorial-network

Command succeeded

檢查已部署的網(wǎng)絡(luò)可以用以下命令:

$ composer network ping --card admin@tutorial-network
The connection to the network was successfully tested: tutorial-network
        Business network version: 0.0.1
        Composer runtime version: 0.19.7
        participant: org.hyperledger.composer.system.NetworkAdmin#admin
        identity: org.hyperledger.composer.system.Identity#67624c0918f6ae837d7d3b90e7df8dc305b0cb4e412cc8d4265fbf4f72823600

Command succeeded

第五步: 生成一個REST server

$ composer-rest-server
? Enter the name of the business network card to use: admin@tutorial-network
? Specify if you want namespaces in the generated REST API: never use namespaces
? 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: Yes
? Specify if you want to enable multiple user and identity management using wallets: No
? Specify if you want to enable event publication over WebSockets: Yes
? 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@tutorial-network -n never -a true -w true

Discovering types from business network definition ...
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

第六步: 生成應(yīng)用程序

$ yo hyperledger-composer:angular
Welcome to the Hyperledger Composer Angular project generator
? Do you want to connect to a running Business Network? Yes
? Project name: angular-app
? Description: Hyperledger Composer Angular project
? Author name: Feng Yu
? Author email: abcfy2@163.com
? License: Apache-2.0
? Name of the Business Network card: admin@tutorial-network
? Do you want to generate a new REST API or connect to an existing REST API?  Connect to an existing REST API
? REST server address: http://localhost
? REST server port: 3000
? Should namespaces be used in the generated REST API? Namespaces are not used
Created application!
Completed generation process
   create app.js
   create Dockerfile
   create e2e/app.e2e-spec.ts
   create e2e/app.po.ts
   create e2e/tsconfig.e2e.json
   create e2e/tsconfig.json
   create karma.conf.js
   create manifest.yml
   create package.json
   create protractor.conf.js
   create proxy.conf.js
   create README.md
   create src/app/app-routing.module.ts
   create src/app/app.component.css
   create src/app/app.component.html
   create src/app/app.component.spec.ts
   create src/app/app.component.ts
   create src/app/app.module.ts
   create src/app/asset/images/delete_noun_cc.svg
   create src/app/asset/images/edit_noun_cc.svg
   create src/app/asset/images/failed_noun_cc.svg
   create src/app/asset/images/success_noun_cc.svg
   create src/app/data.service.ts
   create src/app/home/home.component.css
   create src/app/home/home.component.html
   create src/app/home/home.component.ts
   create src/environments/environment.prod.ts
   create src/environments/environment.ts
   create src/favicon.ico
   create src/index.html
   create src/main.ts
   create src/polyfills.ts
   create src/styles.css
   create src/test.ts
   create src/tsconfig.app.json
   create src/tsconfig.json
   create src/tsconfig.spec.json
   create tsconfig.json
   create tslint.json
   create .angular-cli.json
   create .editorconfig
   create .gitignore
   create .dockerignore
   create .cfignore
   create .npmignore
   create src/app/Commodity/Commodity.component.ts
   create src/app/Commodity/Commodity.service.ts
   create src/app/Commodity/Commodity.component.spec.ts
   create src/app/Commodity/Commodity.component.html
   create src/app/Commodity/Commodity.component.css
   create src/app/Trader/Trader.component.ts
   create src/app/Trader/Trader.service.ts
   create src/app/Trader/Trader.component.spec.ts
   create src/app/Trader/Trader.component.html
   create src/app/Trader/Trader.component.css
   create src/app/Trade/Trade.component.ts
   create src/app/Trade/Trade.service.ts
   create src/app/Trade/Trade.component.spec.ts
   create src/app/Trade/Trade.component.html
   create src/app/Trade/Trade.component.css


I'm all done. Running npm install for you to install the required dependencies. If this fails, try running the command yourself.

最后在angular工程下運(yùn)行

npm start

最后用http://localhost:4200即可訪問應(yīng)用。

小結(jié)

本篇文檔大致展示了一下使用composer通過快速腳手架生成工程代碼,快速部署Fabric應(yīng)用。總體來說Composer開發(fā)體驗(yàn)比直接使用Fabric API的體驗(yàn)太好了。在大致了解Fabric的文檔之后,開發(fā)者完全可以通過Composer上手開發(fā)應(yīng)用,而不需要對接非常底層的Fabric。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容