本期區塊鏈100講,咱們將瞭解Hyperledger Composer的組件,以及內部設計。而後經過一個示例看Composer如何將一個區塊鏈應用抽象成若干概念並將這些概念體如今開發流程中。css
首先咱們先看一下官方網站放出的一張示意圖:html
Composer-Diagram.png
很明顯能夠看出,Hyperledger對這個項目的定位是Fabric的上層,由Composer建立出Model File(.cto文件), Script File(.js文件), ACL(.acl文件), Query File(.qry文件)等等,進行打包成一個商業網絡文件(.bna文件),發佈到Fabric網絡中。git
它的做用很明顯,就是加快Fabric項目的開發和部署的一個官方工具。在使用前須要先對商業模型進行快速建模。好比一個買賣房屋的商業模型以下:web
Assets(資產): 房屋清單docker
Participants(參與者): 購買人和屋主npm
Transactions(交易過程): 完成買賣房屋,結算清單編程
參與者能夠訪問交易數據,可是受限於他們的角色。房屋中介能夠建立一個應用程序,爲買賣雙方提供一個簡單的用戶接口,看到交易的過程。商業網絡也能夠集成已有的庫存系統,完成房屋資產的轉移。另外其餘相關的角色均可以註冊加入到這個網絡成爲參與者,好比土地局可能會參與到交易中,完成土地全部權轉移。json
Composer是一個編程模型,包含一種建模語言,以及一組API,用於快速建網和應用程序,容許參與者發起交易,轉移資產(Assets)。瀏覽器
Composer提供了兩套組件,一個基於瀏覽器的UI Playgroud,用於演示和展現本地Fabric網絡。此外還提供一套開發工具集,爲開發者提供便利的開發框架。微信
Blockchain State Storage
這裏就是區塊鏈的概念了,交易歷史和資產都會直接保存在區塊鏈上,用區塊鏈作存儲。
Connection Profiles
就是一組JSON配置文件,Composer經過這組被稱爲Connection Profiles配置文件,定義了應該鏈接到哪一個系統上。Connection Profile一般須要由系統建立者提供,定義了各類網絡鏈接參數。
Assets
這裏的資產(Assets)能夠指代任何有型的和無形的資產,能夠上鍊的一切可交易的商品,均可以做爲資產。
Assets必須有惟一標識符,此外,還能夠添加一些額外的信息,用於關聯其餘資產或者參與者等等。
Participants
參與者(Participants)是商業網絡的成員,能夠擁有資產或發起交易。參與者也是模型化的,跟資產同樣,必須有惟一的標識符,此外也能夠包含其餘可選屬性。一個參與者能夠有一個或多個身份。
Identities
對應Fabric的PKI認證的概念,經過密鑰確認用戶身份的。
Business Network cards
Business Network cards就是一個Identitie,一個connection profile,以及元數據的組合,元數據包含一個可選的鏈接到商業網絡名稱。Business Network cards簡化了鏈接商業網絡的過程。
Transactions
交易,能夠抽象爲資產轉移的過程。
Queries
查詢返回的是區塊鏈當中的數據。只須要定義好商業網絡,以及相關的變量,就能夠輕鬆的利用Composer API從區塊鏈網絡中提取所需數據。
Events
事件是在商業網絡中定義的,就跟定義資產或參與者一樣的方式。定義事件以後,就能夠經過交易處理函數觸發。應用程序能夠經過composer-client API訂閱這些事件。
Access Control
商業網絡能夠包含一組訪問控制規則。訪問控制規則容許細粒度控制什麼角色在什麼條件下有什麼樣的權限控制什麼資產。
Historian registry
historian是專門用於成功交易記錄的,包含了發起交易的參與者和身份信息。historian將交易保存爲HistorianRecord資產,定義在 Composer系統的namespace中。
到這裏咱們總結了Composer的一些基礎概念,這些概念將用於Composer的開發過程當中。接下來進一步介紹Composer的大體工做流,用Composer開發的時候是一個怎樣的流程。
照例先放出一張官網的示意圖:
ComposerArchitecture.png
能夠發現相比直接使用Fabric,已經減小了大量的工做。能夠經過generator-hyperledger-composer生成Angular的應用,而後經過Hyperledger Composer的SDK部署並運行在Fabric網絡環境中。
整個Composer由如下組件構成:
執行環境
JavaScript SDK
命令行接口
REST Server
LoopBack鏈接器
Playground Web UI
Yeoman代碼生成器
VSCode和Atom編輯器插件
Hyperledger Composer設計支持多種可插拔的運行環境,目前已經實現了三種運行環境:
Hyperledger Fabric v1.1. State存儲在分佈式帳本
Web. 直接在Web內部執行,用於Playground演示。State存儲在瀏覽器的local storage中
內嵌環境。直接在Node.js進程內部執行,主要用於商業邏輯的單元測試。State以KV形式存儲在內存中。
Connection Profiles用於指明Composer如何鏈接到一個執行環境的。每一種執行環境都有不一樣的配置選項。
JavaScript SDK
這是一組Node.js API,給開發者提供建立應用操控和部署Business Network。這些API分紅兩個npm模塊:
compser-client: 提交交易請求到business network,以及對資產和參與者執行的CURD操做
composer-admin: 用於管理business network,安裝、啓動、升級等
命令行接口
composer命令行工具提供部署和管理business network的功能
REST Server
Hyperledger Composer REST Server會自動爲business network建立一個Open API(利用Swagger) REST接口。REST Server(基於LoopBack技術)將Composer模型轉換爲Open API的定義,而且實現CURD支持。
LoopBack鏈接器
Hyperledger Composer LoopBack鏈接器能夠被REST Server使用,也能夠經過支持LoopBack的集成工具單獨使用。固然也能夠經過LoopBack工具建立一個更復雜的自定義REST API。
Playground Web User Interface
這玩意用於定義和測試business network的。可讓商業分析人在Web上快速導入樣本和商業邏輯模型。
Yeoman代碼生成器
建立如下工程的腳手架:
Angular web application
Node.js application
business network的腳手架
VSCode和Atom編輯器插件
儘管沒有直接的IDE支持,可是這個插件能夠替代一些IDE功能。
基本概念介紹完畢以後,讓咱們動手建立和部署一個應用試試看。
第一步: 建立一個business network結構
Hyperledger Composer的一個關鍵構成就是business network definition (BND),BND爲區塊鏈定義了數據模型,交易邏輯和訪問控制規則。
最簡單的方式是直接經過Yeoman建立一個腳手架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
在一系列交互式詢問以後,咱們就建立了一個business network應用程序。
第二步: 定義一個business network
一個business network是由資產、參與者、交易、訪問控制規則,以及可選的時間和查詢組成的。在以前建立的腳手架工程中,已經有一個model(.cto)文件了,包含了定義了在business network中存在的全部資產、參與者、交易。這個工程一樣也包含了一個訪問控制規則(permissions.acl),一個包含了交易過程的函數腳本(logic.js),package.json包含了business network的元數據。
模型化資產、參與者以及交易
模型文件(.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,指明瞭一個交易和參與者之間的關係。後面須要定義具體的邏輯實現。編輯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/目錄下執行如下命令:
$ 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環境啓動(./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
以後就能夠運行了:
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 而後導入網絡管理員身份做爲可用的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
檢查已部署的網絡能夠用如下命令:
$ 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
第六步: 生成應用程序
$ 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工程下運行
npm start
最後用http://localhost:4200便可訪問應用。
本篇文檔大體展現了一下使用composer經過快速腳手架生成工程代碼,快速部署Fabric應用。整體來講Composer開發體驗比直接使用Fabric API的體驗太好了。在大體瞭解Fabric的文檔以後,開發者徹底能夠經過Composer上手開發應用,而不須要對接很是底層的Fabric。
本文做者:HiBlock區塊鏈技術佈道羣-馮宇Ops
原文發佈於簡書
加微信baobaotalk_com,加入技術佈道羣
線上課程推薦
線下活動推薦