本教程基本上是对Marbles
项目的翻译过程. 若是英文比较好的话,建议根据官方操做说明,一步步进行环境部署。固然你也能够参考本教程在本身的主机上部署该项目。html
Linux Foundation
项目。 您可能想查阅如下操做说明来稍微了解一下 Hyperledger Fabric
各类版本的 marbles
同时存在。 本版本兼容 Hyperledger Fabric v1.1x
。 你能够经过检出别的分支来获取别的版本的 marble
,这里演示使用的是ae4e37d
分支node
请你们集中注意力,这个应用程序将演示如何利用 Hyperledger Fabric
在许多弹珠全部者之间转移弹珠。 咱们将在 Node.js
中使用一些 GoLang
代码完成此任务。 该应用程序的后端将是在咱们的区块链网络中运行的 GoLang
代码。 从如今开始,这些 GoLang
代码将称为“链代码”或“cc”。 该链代码自己会建立一颗弹珠,将它存储到链代码状态中。 该链代码自己能够将数据做为字符串存储在键/值对设置中。 所以,咱们将字符串化 JSON
对象,以便存储更复杂的结构.react
弹珠的属性包括:linux
咱们将建立一个用户界面,它能够设置这些值并将它们存储在区块链的帐本中。 弹珠其实是一个键值对。 键为弹珠 ID,值为一个包含(上面列出的)弹珠属性的 JSON
字符串。 与 cc
的交互是经过对网络上的一个对等节点使用 gRPC
协议来完成的。 gRPC
协议的细节由一个名为 Hyperledger Fabric Client SDK 的 SDK 处理。 请查看下图了解拓扑结构细节。
git
这里使用的是本地的 Hyperledger Fabric 网络来部署项目,若是想使用 IBM Cloud IBM Blockchain 服务来部署该项目,请参考前言中给的官方文档.github
ubuntu16.04
若是您经过本人的上一篇博客基于ubuntu16.04快速构建Hyperledger Fabric网络已经搭建好了一个 Hyperledger Fabric 网络,那么这里只须要安装 Node.js 的环境并验证环境是否正确便可,若是您没有在本地搭建 Hyperledger Fabric 网络,建议您经过上述博客先在本地构建好网络环境.docker
通常来讲 linux 系统都是自带 Git ,若是系统里没有装,可使用以下命令来进行安装npm
$ sudo apt-get install git
安装完成后验证一下json
$ git --version git version 2.7.4
Go安装安装了一组Go CLI工具,这些工具在编写连接代码时很是有用。例如,该 go build 命令容许您在尝试将其部署到网络以前检查链代码是否实际编译.gulp
$ go version go version go1.10 linux/amd64 $ echo $GOPATH /home/ubuntu/go
这里的 ubuntu是个人用户名,表示个人 GOPATH 目录是个人主目录下的 go 文件夹,固然你的 GOPATH 不须要匹配上面的那个。它只是很重要的,但你必须把这个变量设置为文件系统上的有效目录.
首先能够先使用 node -v
和 npm -v
命令来验证系统中是否有 Node.js 环境,若是没有安装则须要使用以下命令进行安装:
$ sudo apt-get install nodejs $ sudo apt install nodejs-legacy $ sudo apt install npm
安装完成以后使用 node -v
和 npm -v
命令来查看版本信息:
$ node -v v4.2.6 $ npm -v 3.5.2
遗憾的是经过这种方式安装的 Node.js 版本都比较低,并且并不符合咱们项目的环境要求(官网文档中出现的版本为:node:v6.10.1;npm:3.10.10)
,为了不因软件版本不一样而引发的问题,咱们还须要对 Node 以及 npm 的版本进行升级操做
$ npm install -g nrm
$ nrm ls npm ---- https://registry.npmjs.org/ cnpm --- http://r.cnpmjs.org/ taobao - https://registry.npm.taobao.org/ nj ----- https://registry.nodejitsu.com/ rednpm - http://registry.mirror.cqupt.edu.cn/ npmMirror https://skimdb.npmjs.com/registry/ edunpm - http://registry.enpmjs.org/
$ nrm use taobao Registry has been set to: https://registry.npm.taobao.org/
$ npm install -g n
$ n 6.10.1
$ node -v v6.10.1
$ npm install -g npm@3.10.10
$ npm -v 3.10.10
至此,Node.js的环境就算是搭建完成了
官方文档中提供了三种选择,一种是不想对链码进行修改的,下面操做能够没必要执行.而想要本身修改链码的并且想使用最新版本 Fabric 的能够切换到最新的分支,虽说该项目兼容 Hyperledger Fabric v1.1x
,可是出于避免出现未知的错误,建议将分支切换到文档中使用的版本ae4e37d
.切换步骤命令以下
$ cd $GOPATH/src/github.com/hyperledger/fabric $ git checkout ae4e37d
若是按照个人上篇博客配置的,这里的 $GOPATH
既用户主目录下的 go 文件夹,
$ git branch (HEAD detached at ae4e37d) release-1.1
显示已经切换到ae4e37d
分支,当前最新发布版本为1.1
. 固然,你若是想知道ae4e37d
分支的具体信息,能够经过以下命令查看:
$ git log -p commit ae4e37dbafe74997534ab317dec5c3f4f53b6a84 Author: Gari Singh <gari.r.singh@gmail.com> Date: Mon Aug 7 17:50:39 2017 -0400 FAB-5652 Prepare fabric for 1.0.2 release -base version = 1.0.2 -prev version = 1.0.1 -is_release = false Change-Id: Ibce2a81193b09015eef896391b0e8166d40e7102 Signed-off-by: Gari Singh <gari.r.singh@gmail.com> diff --git a/Makefile b/Makefile index d1febaa..ffe51f3 100755 --- a/Makefile +++ b/Makefile @@ -36,9 +36,9 @@ # - unit-test-clean - cleans unit test state (particularly from docker) PROJECT_NAME = hyperledger/fabric
经过上面的命令输出结果能够看到,该分支是基于1.0.2
版本的.切换到该分支后,还须要验证结构安装
$ cd $GOPATH/src/github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02 $ go build --tags nopkcs11 ./
它应该返回没有错误/警告。您还应该看到在该目录中建立了可执行文件。
请注意,nopkcs11标签很重要。PKCS 11是您不太可能在您的系统上使用的公钥加密标准。 请记住在开发/构建链码时使用此标志。
对编写链码 IDE 的选择官方文档推荐了两个 Visual Studio Code 和 Atom,具体的 IDE 开发环境配置能够在网上搜索.
这里是构建一个本地的Hyperledger网络,而后测试该网络步骤过程.
咱们须要将 Marbles 下载到本地系统。 让咱们使用 Git 经过克隆此存储库来完成该任务。 即便您计划将 Marbles 托管在 IBM Cloud 中,也须要执行这一步,运行如下命令便可
$ cd ~ $ git clone https://github.com/IBM-Blockchain/marbles.git --depth 1 $ cd marbles
注意:我这里将 Marbles 克隆到了用户主目录下,你能够选择任意合适的目录
咱们将使用 Hyperledger Fabric 例子运行本地网络。他们的代码具备 Fabric 网络的设置以及连接代码示例。咱们只会使用网络设置部分。
使用如下命令下载它们的节点示例:
$ git clone https://github.com/hyperledger/fabric-samples.git $ cd fabric-samples
若是以前没有下载各类结构组织的 Docker 镜像,那么可使用下面的命令进行下载
$ curl -sSL https://raw.githubusercontent.com/hyperledger/fabric/release-1.1/scripts/bootstrap-1.1.0-preview.sh -o setup_script.sh $ sudo bash setup_script.sh
请务必经过运行如下命令或将其粘贴到您的.profile文件中,将这些二进制文件添加到PATH变量中
$ export PATH=$PWD/bin:$PATH
$ vim ~/.profile
打开后在最后一行插入插入export PATH=/home/ubuntu/fabric-samples/bin:$PATH
,这里能够先使用 pwd
命令来获取您本地fabric-samples
的目录,而后将上面命令中的 $PWD
换成该目录便可,最后使用 :wq
保存退出,执行下面命令刷新一下
$ source ~/.profile
接下来,咱们须要启动Fabric。运行下面的脚原本让全部的事情都发生
$ cd ./fabcar $ sudo ./startFabric.sh
一两分钟后,命令提示符将返回,运行结果以下图所示
如今运行该命令 docker ps
查看当前正在运行的Docker
容器。您应该看到相似于如下内容的内容:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 01cdf948b39c dev-peer0.org1.example.com-fabcar-1.0 "chaincode -peer.add…" 2 minutes ago Up 2 minutes dev-peer0.org1.example.com-fabcar-1.0 2f79bac1371e hyperledger/fabric-tools "/bin/bash" 3 minutes ago Up 3 minutes cli 648da0074a8d hyperledger/fabric-peer "peer node start" 3 minutes ago Up 3 minutes 0.0.0.0:7051->7051/tcp, 0.0.0.0:7053->7053/tcp peer0.org1.example.com 868e0f78f80e hyperledger/fabric-ca "sh -c 'fabric-ca-se…" 3 minutes ago Up 3 minutes 0.0.0.0:7054->7054/tcp ca.example.com 4c385bb6aa9d hyperledger/fabric-couchdb "tini -- /docker-ent…" 3 minutes ago Up 3 minutes 4369/tcp, 9100/tcp, 0.0.0.0:5984->5984/tcp couchdb 4b9a2b2b0718 hyperledger/fabric-orderer "orderer" 3 minutes ago Up 3 minutes 0.0.0.0:7050->7050/tcp orderer.example.com
若是你没有看到所有6个容器在运行,那么有些问题是错误的。在继续以前,您须要排除故障。我建议进入一个已中止的容器的日志sudo docker logs peer0
(替换名称为w / e的peer0已中止)。
若是您看到containerID already exists
正在运行的Docker
工具 - 组成,那么您须要删除现有的容器。该命令将删除全部容器docker rm -f $(docker ps -aq)
很好,就快要完成了!如今,咱们须要运行咱们的 Marbles 链码。 请记住,链码是一个关键组件,它最终会在帐本上建立咱们的 Marbles 事务。 该链码是须要安装在对等节点上,而后在一个通道上实例化的 GoLang 代码。 已为您编写好该代码! 咱们只须要运行它
咱们须要一些弹珠依赖来运行安装/实例化脚本。经过返回 Marbles 目录的根目录并输入这些命令来安装弹珠 npm 依赖关系:
$ cd ~/marbles $ npm install
重要的是安装没有错误返回(警告是好的)。若是你有 npm 安装错误,在继续以前你必须解决并修复这些错误
这是一个很是重要步骤! 安装和实例化操做须要管理员证书和私钥。若是找不到这些文件,您将没法运行任何操做。
$ cd ../fabric-samples/fabcar
$ node enrollAdmin.js Store path:/home/ubuntu/fabric-samples/fabcar/hfc-key-store Successfully enrolled admin user "admin" Assigned the admin user to the fabric client ::{"name":"admin","mspid":"Org1MSP","roles":null,"affiliation":"","enrollmentSecret":"","enrollment":{"signingIdentity":"9b6f84a7672908c0629d9b3ad0bf23437d624089061e937af0b0476ec6dec81d","identity":{"certificate":"-----BEGIN CERTIFICATE-----\nMIIB8DCCAZegAwIBAgIUeQVhK98LQFSz5Dz0bt3bB9Baom8wCgYIKoZIzj0EAwIw\nczELMAkGA1UEBhMCVVMxEzARBgNVBAgTCkNhbGlmb3JuaWExFjAUBgNVBAcTDVNh\nbiBGcmFuY2lzY28xGTAXBgNVBAoTEG9yZzEuZXhhbXBsZS5jb20xHDAaBgNVBAMT\nE2NhLm9yZzEuZXhhbXBsZS5jb20wHhcNMTgwNTE1MTA1ODAwWhcNMTkwNTE1MTA1\nODAwWjAQMQ4wDAYDVQQDEwVhZG1pbjBZMBMGByqGSM49AgEGCCqGSM49AwEHA0IA\nBPlS00VDvBQpsmMFUGnNzEAQd7lgpTNgEDpzJGk4/xfBuechE8cfNH6WuibJtXxh\nsEQ4uLAlDcOAP1nfXq9oEtWjbDBqMA4GA1UdDwEB/wQEAwIHgDAMBgNVHRMBAf8E\nAjAAMB0GA1UdDgQWBBShJWerMoKEE2u+dn08UBkGs4tWzjArBgNVHSMEJDAigCBC\nOaoNzXba7ri6DNpwGFHRRQTTGq0bLd3brGpXNl5JfDAKBggqhkjOPQQDAgNHADBE\nAiAmqy0J0M1aZlvuv6cDK8GjeMTMjN0V5dZIW/uBv+whtAIgCMbyQRtE+PDwsoSS\nG40hZ4UOoNS2tvIXHRglMMHvKjs=\n-----END CERTIFICATE-----\n"}}}
$ node registerUser.js Store path:/home/ubuntu/fabric-samples/fabcar/hfc-key-store Successfully loaded admin from persistence Successfully registered user1 - secret:PfPGkGQmNgfw Successfully enrolled member user "user1" User1 was successfully registered and enrolled and is ready to intreact with the fabric network
fabric-samples/fabcar/hfc-key-store
<marbles root>/config/connection_profile_local.json
。fabric-samples
目录的位置以及密钥存储数据所在的位置来更改这些值。一旦路径有效,您能够继续。cd ~/marbles
并继续执行下面的安装连接代码说明。完成以后,咱们须要将链代码放到 peer 节点的文件系统中。记住 chaincode 定义了什么弹珠(资产)是咱们系统交易的业务逻辑。你能够在这个目录中找到弹珠链码<marbles root>/chaincode/src/
。这个目录下的文件就是链码文件。
咱们将使用位于scripts文件夹中的脚本install_chaincode.js
。它会读取咱们的弹珠配置文件和链接配置文件数据。您能够经过编辑install_chaincode.js
文件来更改本项目链代码ID或版本。若是您想编辑这些文件并想要更多关于其内容的信息,请打开下面的配置和链接配置文件自述文件。若是您对默认设置没有问题,那么只需将这些文件单独保存并运行下面的命令便可。
$ cd ./scripts $ node install_chaincode.js ...... .......... #这里省略了许多输出信息 .......... ...... --------------------------------------- info: Now we install --------------------------------------- debug: [fcw] Installing Chaincode debug: [fcw] Sending install req targets=[grpc.http2.keepalive_time=300, grpc.keepalive_time_ms=300000, grpc.http2.keepalive_timeout=35, grpc.keepalive_timeout_ms=3500, grpc.max_receive_message_length=-1, grpc.max_send_message_length=-1, grpc.primary_user_agent=grpc-node/1.10.1, _url=grpc://localhost:7051, addr=localhost:7051, , _request_timeout=90000, , _name=null], chaincodePath=marbles, chaincodeId=marbles, chaincodeVersion=v4 info: [packager/Golang.js]: packaging GOLANG from marbles debug: [fcw] Successfully obtained transaction endorsement --------------------------------------- info: Install done. Errors: nope ---------------------------------------
出现上述输出结果,说明链码安装成功
接下来咱们须要实例化链码。这会让您的 channel(通道)
启动弹珠链码mychannel
。一旦完成,咱们准备使用区块链网络来记录咱们的系统(Marbels)活动。使用下面的命令完成实例化:
$ node instantiate_chaincode.js ...... .......... #这里省略了许多输出信息 .......... ...... --------------------------------------- info: Now we instantiate --------------------------------------- debug: [fcw] Instantiating Chaincode peer_urls=[grpc://localhost:7051], channel_id=mychannel, chaincode_id=marbles, chaincode_version=v4, cc_args=[12345], ssl-target-name-override=null, pem=null, grpc.http2.keepalive_time=300, grpc.keepalive_time_ms=300000, grpc.http2.keepalive_timeout=35, grpc.keepalive_timeout_ms=3500 debug: [fcw] Sending instantiate req targets=[grpc.http2.keepalive_time=300, grpc.keepalive_time_ms=300000, grpc.http2.keepalive_timeout=35, grpc.keepalive_timeout_ms=3500, grpc.max_receive_message_length=-1, grpc.max_send_message_length=-1, grpc.primary_user_agent=grpc-node/1.10.1, _url=grpc://localhost:7051, addr=localhost:7051, , _request_timeout=90000, , _name=null], chaincodeId=marbles, chaincodeVersion=v4, fcn=init, args=[12345], 0=214, 1=155, 2=127, 3=34, 4=197, 5=82, 6=208, 7=191, 8=141, 9=140, 10=57, 11=113, 12=46, 13=90, 14=76, 15=231, 16=170, 17=118, 18=197, 19=137, 20=186, 21=212, 22=64, 23=33, _transaction_id=d550ed194a2d798f2a6c2924c0302fdc6323fba2835e128f3dc541f1b6754525 debug: [fcw] Successfully obtained transaction endorsement debug: [fcw] Successfully ordered instantiate endorsement. --------------------------------------- info: Instantiate done. Errors: nope ---------------------------------------
出现上述输出结果,说明实例化链码成功
经过上述操做,咱们全部的环境都已经配置完成了,接下来就是运行本项目
打开命令提示符/终端并导航到 Marbles 目录,并执行下面的几个命令:
$ cd ~/marbles $ sudo npm install gulp -g $ sudo npm install
安装依赖成功后,而且没有错误返回(警告是好的).若是你有 npm 安装错误,在继续以前你必须解决并修复这些错误
使用以下命令运行项目:
$ gulp marbles_local ...... .......... #这里省略了许多输出信息 .......... ...... ----------------------------------- Server Up - localhost:3001 ----------------------------------- Welcome aboard: United Marbles Channel: mychannel Org: Org1MSP CA: fabric-ca Orderer: fabric-orderer Peer: fabric-peer-org1 Chaincode ID: marbles Chaincode Version: v4 ------------------------------------------ Websocket Up ------------------------------------------ info: [fcw] Going to enroll peer_urls=[grpc://localhost:7051], channel_id=mychannel, uuid=marblesDockerComposeNetworkmychannelOrg1MSPfabricpeerorg1, ca_url=http://localhost:7054, orderer_url=grpc://localhost:7050, enroll_id=admin, enroll_secret=adminpw, msp_id=Org1MSP, kvs_path=/home/ubuntu/.hfc-key-store info: [fcw] Successfully loaded enrollment from persistence debug: added peer grpc://localhost:7051 debug: [fcw] Successfully got enrollment marblesDockerComposeNetworkmychannelOrg1MSPfabricpeerorg1 info: Success enrolling admin debug: Checking if chaincode is already instantiated or not 1 info: Checking for chaincode... debug: [fcw] Querying Chaincode: read() debug: [fcw] Sending query req: chaincodeId=marbles, fcn=read, args=[selftest], txId=null debug: [fcw] Peer Query Response - len: 5 type: number debug: [fcw] Successful query transaction. ----------------------------- Chaincode found on channel "mychannel" ----------------------------- info: Checking chaincode and ui compatibility... debug: [fcw] Querying Chaincode: read() debug: [fcw] Sending query req: chaincodeId=marbles, fcn=read, args=[marbles_ui], txId=null warn: [fcw] warning - query resp is not json, might be okay: string 4.0.1 debug: [fcw] Successful query transaction. info: Chaincode version is good info: Checking ledger for marble owners listed in the config file info: Fetching EVERYTHING... debug: [fcw] Querying Chaincode: read_everything() debug: [fcw] Sending query req: chaincodeId=marbles, fcn=read_everything, args=[], txId=null debug: [fcw] Peer Query Response - len: 30 type: object debug: [fcw] Successful query transaction. debug: This company has not registered marble owners info: We need to make marble owners - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - info: Detected that we have NOT launched successfully yet debug: Open your browser to http://localhost:3001 and login as "admin" to initiate startup - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
出现上述输出信息,则表示项目启动成功,如今你能够在浏览器中输入 http://localhost:3001
来访问本项目,而且您不须要输入密码或更改预先填写的用户名admin
.
注意: 本人在使用gulp marbles_local
命令启动项目的过程当中出现了以下图的错误,有可能你在运行时也会出现这个错误:
查看 Issues:208能够发现有人已经遇到过这种问题, 阅读后发现这个bug
是由fabric-sdk-node
https://jira.hyperledger.org/browse/FAB-2593 引发的, 须要将hfc-key-store
目录复制到您的主目录$HOME / .hfc-key-store
,而后从新配置connection_profile_local.json
的client.credentialStore.path
. 若是你也遇到了这个问题,能够参考以下步骤.
hfc-key-store
目录复制到您的主目录$HOME / .hfc-key-store
:$ cd ~/fabric-samples/fabcar $ cp -r hfc-key-store ~/.hfc-key-store
connection_profile_local.json
的client.credentialStore.path
:$ cd ~/marbles/config $ vim connection_profile_local.json
在文件中定位到下面的片断:
"client": { "organization": "Org1MSP", "credentialStore": { "path": "/$HOME/.hfc-key-store" } },
将path
改成上面的路径(/$HOME/.hfc-key-store
)便可.
$ cd ~/marbles $ gulp marbles_local
若是这样,还不能运行,你能够在 issues 里找找看有没有相同的错误, 若是有解答过程,能够按照解答的过程,本身试着解决这些问题.
开始
点击选择右边的按钮Guided
, 经过这种方式便可以了解 Fabric 又能自定义一些设置
第一步:检查链接配置数据
第一步是检查你的链接配置JSON文件。 检查的文件是:marbles/config/marbles_local.json
和`marbles/config/connection_profile_local.json
接下来,咱们尝试将您注册为贵公司的管理员。此步骤与您的证书颁发机构(CA)联系并从您的链接配置文件中提供了enrollID
和enrollSecret
如今咱们须要在您的channel(通道)
上找到链码。检查或修改您的链接配置文件里配置的链码名为弹珠的通道mychannel
。
做为一个弹珠贸易公司,您能够携带新的弹珠业主。这些弹珠业主表明您的用户群。
这一步将建立弹珠用户而且每一个用户拥有3个弹珠。
进行下一步前,请点击Create
进行建立
第五步:配置完成,点击Enter
进入系统
进入系统后,你能够按照本教程开头,或者下面的Gif
动画演示的那样为一个用户建立弹珠资产,或者将一个弹珠资产转移给另外一个用户;也能够删除这个弹珠资产.