這篇文章給大家分享的是有關(guān)Hyperledger Composer架構(gòu)的示例分析的內(nèi)容。小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過(guò)來(lái)看看吧。
創(chuàng)新互聯(lián)公司專(zhuān)注于古交企業(yè)網(wǎng)站建設(shè),響應(yīng)式網(wǎng)站設(shè)計(jì),商城網(wǎng)站開(kāi)發(fā)。古交網(wǎng)站建設(shè)公司,為古交等地區(qū)提供建站服務(wù)。全流程按需求定制開(kāi)發(fā),專(zhuān)業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,創(chuàng)新互聯(lián)公司專(zhuān)業(yè)和態(tài)度為您提供的服務(wù)
首先我們先看一下官方網(wǎng)站放出的一張示意圖:
Composer-Diagram.png
很明顯可以看出,Hyperledger對(duì)這個(gè)項(xiàng)目的定位是Fabric的上層,由Composer創(chuàng)建出Model File(.cto文件), Script File(.js文件), ACL(.acl文件), Query File(.qry文件)等等,進(jìn)行打包成一個(gè)商業(yè)網(wǎng)絡(luò)文件(.bna文件),發(fā)布到Fabric網(wǎng)絡(luò)中。
它的作用很明顯,就是加快Fabric項(xiàng)目的開(kāi)發(fā)和部署的一個(gè)官方工具。在使用前需要先對(duì)商業(yè)模型進(jìn)行快速建模。比如一個(gè)買(mǎi)賣(mài)房屋的商業(yè)模型如下:
Assets(資產(chǎn)): 房屋清單
Participants(參與者): 購(gòu)買(mǎi)人和屋主
Transactions(交易過(guò)程): 完成買(mǎi)賣(mài)房屋,結(jié)算清單
參與者可以訪問(wèn)交易數(shù)據(jù),但是受限于他們的角色。房屋中介可以創(chuàng)建一個(gè)應(yīng)用程序,為買(mǎi)賣(mài)雙方提供一個(gè)簡(jiǎn)單的用戶(hù)接口,看到交易的過(guò)程。商業(yè)網(wǎng)絡(luò)也可以集成已有的庫(kù)存系統(tǒng),完成房屋資產(chǎn)的轉(zhuǎn)移。另外其他相關(guān)的角色都可以注冊(cè)加入到這個(gè)網(wǎng)絡(luò)成為參與者,比如土地局可能會(huì)參與到交易中,完成土地所有權(quán)轉(zhuǎn)移。
Composer是一個(gè)編程模型,包含一種建模語(yǔ)言,以及一組API,用于快速建網(wǎng)和應(yīng)用程序,允許參與者發(fā)起交易,轉(zhuǎn)移資產(chǎn)(Assets)。
Composer提供了兩套組件,一個(gè)基于瀏覽器的UI Playgroud,用于演示和展示本地Fabric網(wǎng)絡(luò)。此外還提供一套開(kāi)發(fā)工具集,為開(kāi)發(fā)者提供便利的開(kāi)發(fā)框架。
Blockchain State Storage
這里就是區(qū)塊鏈的概念了,交易歷史和資產(chǎn)都會(huì)直接保存在區(qū)塊鏈上,用區(qū)塊鏈做存儲(chǔ)。
Connection Profiles
就是一組JSON配置文件,Composer通過(guò)這組被稱(chēng)為Connection Profiles配置文件,定義了應(yīng)該連接到哪個(gè)系統(tǒng)上。Connection Profile通常需要由系統(tǒng)創(chuàng)建者提供,定義了各種網(wǎng)絡(luò)連接參數(shù)。
Assets
這里的資產(chǎn)(Assets)可以指代任何有型的和無(wú)形的資產(chǎn),可以上鏈的一切可交易的商品,都可以作為資產(chǎn)。
Assets必須有唯一標(biāo)識(shí)符,此外,還可以添加一些額外的信息,用于關(guān)聯(lián)其他資產(chǎn)或者參與者等等。
Participants
參與者(Participants)是商業(yè)網(wǎng)絡(luò)的成員,可以擁有資產(chǎn)或發(fā)起交易。參與者也是模型化的,跟資產(chǎn)一樣,必須有唯一的標(biāo)識(shí)符,此外也可以包含其他可選屬性。一個(gè)參與者可以有一個(gè)或多個(gè)身份。
Identities
對(duì)應(yīng)Fabric的PKI認(rèn)證的概念,通過(guò)密鑰確認(rèn)用戶(hù)身份的。
Business Network cards
Business Network cards就是一個(gè)Identitie,一個(gè)connection profile,以及元數(shù)據(jù)的組合,元數(shù)據(jù)包含一個(gè)可選的連接到商業(yè)網(wǎng)絡(luò)名稱(chēng)。Business Network cards簡(jiǎn)化了連接商業(yè)網(wǎng)絡(luò)的過(guò)程。
Transactions
交易,可以抽象為資產(chǎn)轉(zhuǎn)移的過(guò)程。
Queries
查詢(xún)返回的是區(qū)塊鏈當(dāng)中的數(shù)據(jù)。只需要定義好商業(yè)網(wǎng)絡(luò),以及相關(guān)的變量,就可以輕松的利用Composer API從區(qū)塊鏈網(wǎng)絡(luò)中提取所需數(shù)據(jù)。
Events
事件是在商業(yè)網(wǎng)絡(luò)中定義的,就跟定義資產(chǎn)或參與者同樣的方式。定義事件之后,就可以通過(guò)交易處理函數(shù)觸發(fā)。應(yīng)用程序可以通過(guò)composer-client API訂閱這些事件。
Access Control
商業(yè)網(wǎng)絡(luò)可以包含一組訪問(wèn)控制規(guī)則。訪問(wèn)控制規(guī)則允許細(xì)粒度控制什么角色在什么條件下有什么樣的權(quán)限控制什么資產(chǎn)。
Historian registry
historian是專(zhuān)門(mén)用于成功交易記錄的,包含了發(fā)起交易的參與者和身份信息。historian將交易保存為HistorianRecord資產(chǎn),定義在 Composer系統(tǒng)的namespace中。
到這里我們總結(jié)了Composer的一些基礎(chǔ)概念,這些概念將用于Composer的開(kāi)發(fā)過(guò)程中。接下來(lái)進(jìn)一步介紹Composer的大致工作流,用Composer開(kāi)發(fā)的時(shí)候是一個(gè)怎樣的流程。
照例先放出一張官網(wǎng)的示意圖:
ComposerArchitecture.png
可以發(fā)現(xiàn)相比直接使用Fabric,已經(jīng)減少了大量的工作。可以通過(guò)generator-hyperledger-composer生成Angular的應(yīng)用,然后通過(guò)Hyperledger Composer的SDK部署并運(yùn)行在Fabric網(wǎng)絡(luò)環(huán)境中。
整個(gè)Composer由以下組件構(gòu)成:
執(zhí)行環(huán)境
JavaScript SDK
命令行接口
REST Server
LoopBack連接器
Playground Web UI
Yeoman代碼生成器
VSCode和Atom編輯器插件
Hyperledger Composer設(shè)計(jì)支持多種可插拔的運(yùn)行環(huán)境,目前已經(jīng)實(shí)現(xiàn)了三種運(yùn)行環(huán)境:
Hyperledger Fabric v1.1. State存儲(chǔ)在分布式賬本
Web. 直接在Web內(nèi)部執(zhí)行,用于Playground演示。State存儲(chǔ)在瀏覽器的local storage中
內(nèi)嵌環(huán)境。直接在Node.js進(jìn)程內(nèi)部執(zhí)行,主要用于商業(yè)邏輯的單元測(cè)試。State以KV形式存儲(chǔ)在內(nèi)存中。
Connection Profiles用于指明Composer如何連接到一個(gè)執(zhí)行環(huán)境的。每一種執(zhí)行環(huán)境都有不同的配置選項(xiàng)。
JavaScript SDK
這是一組Node.js API,給開(kāi)發(fā)者提供創(chuàng)建應(yīng)用操控和部署B(yǎng)usiness Network。這些API分成兩個(gè)npm模塊:
compser-client: 提交交易請(qǐng)求到business network,以及對(duì)資產(chǎn)和參與者執(zhí)行的CURD操作
composer-admin: 用于管理business network,安裝、啟動(dòng)、升級(jí)等
命令行接口
composer命令行工具提供部署和管理business network的功能
REST Server
Hyperledger Composer REST Server會(huì)自動(dòng)為business network創(chuàng)建一個(gè)Open API(利用Swagger) REST接口。REST Server(基于LoopBack技術(shù))將Composer模型轉(zhuǎn)換為Open API的定義,并且實(shí)現(xiàn)CURD支持。
LoopBack連接器
Hyperledger Composer LoopBack連接器可以被REST Server使用,也可以通過(guò)支持LoopBack的集成工具單獨(dú)使用。當(dāng)然也可以通過(guò)LoopBack工具創(chuàng)建一個(gè)更復(fù)雜的自定義REST API。
Playground Web User Interface
這玩意用于定義和測(cè)試business network的。可以讓商業(yè)分析人在Web上快速導(dǎo)入樣本和商業(yè)邏輯模型。
Yeoman代碼生成器
創(chuàng)建以下工程的腳手架:
Angular web application
Node.js application
business network的腳手架
VSCode和Atom編輯器插件
盡管沒(méi)有直接的IDE支持,但是這個(gè)插件可以替代一些IDE功能。
基本概念介紹完畢之后,讓我們動(dòng)手創(chuàng)建和部署一個(gè)應(yīng)用試試看。
第一步: 創(chuàng)建一個(gè)business network結(jié)構(gòu)
Hyperledger Composer的一個(gè)關(guān)鍵構(gòu)成就是business network definition (BND),BND為區(qū)塊鏈定義了數(shù)據(jù)模型,交易邏輯和訪問(wèn)控制規(guī)則。
最簡(jiǎn)單的方式是直接通過(guò)Yeoman創(chuàng)建一個(gè)腳手架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
在一系列交互式詢(xún)問(wèn)之后,我們就創(chuàng)建了一個(gè)business network應(yīng)用程序。
第二步: 定義一個(gè)business network
一個(gè)business network是由資產(chǎn)、參與者、交易、訪問(wèn)控制規(guī)則,以及可選的時(shí)間和查詢(xún)組成的。在之前創(chuàng)建的腳手架工程中,已經(jīng)有一個(gè)model(.cto)文件了,包含了定義了在business network中存在的所有資產(chǎn)、參與者、交易。這個(gè)工程同樣也包含了一個(gè)訪問(wèn)控制規(guī)則(permissions.acl),一個(gè)包含了交易過(guò)程的函數(shù)腳本(logic.js),package.json包含了business network的元數(shù)據(jù)。
模型化資產(chǎn)、參與者以及交易
模型文件(.cto)是由Hyperledger Composer Modelling Language編寫(xiě)的,我們直接編輯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,指明了一個(gè)交易和參與者之間的關(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); } 添加訪問(wèn)控制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
整個(gè)工程被打包成了.bna文件。
第四步: 部署business network
需要按照安裝Composer的文檔,將docker環(huán)境啟動(dòng)(./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
第五步: 生成一個(gè)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即可訪問(wèn)應(yīng)用。
感謝各位的閱讀!關(guān)于“Hyperledger Composer架構(gòu)的示例分析”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!