在 Win7 使用 hyperledger fabric source code建构 Local Blockchain Network System

在 Win7 使用 hyperledger/fabric source code建构 Local Blockchain Network System

作者:陳兆麟 Email:chenlin2@ms9.hinet.net

当 IBM 在 Bluemix开发 Blockchain Service 时因无法面面俱到,所以将 4 万行源码 (hyperledger/fabric) 贡献给 Linux Foundation;它希望开发者根据不同的应用开发客制化的Blockchain,并以 Docker Images 的形式安装在 Bluemix 云端平台供客户使用;同时 IBM 也扩充了Bluemix 的其它服务 (例如 IoT platform service), 使其能支持 Blockchain,如此便能吸引开发者愿意将客制化Blockchain 部署在 Bluemix。阳春型的 Blockchain 能做的事有限,如果开发者要将 Blockchain转至其它云端平台,可能要考虑开发其它配套 (例如物联网的支持) 的成本;而其它云端大厂也可以 hyperledger/fabric为基础开发自己的Blockchain应用系统,虽然方便但也成就了IBM成为 Blockchain 规格的制定者。要建立客制化的Blockchain Service,就必须先在local machine以hyperledger/fabric 源码建立 local blockchain network,系统开发完毕后再以docker images 的形式安装在云端平台供客户使用,以下范例即在 Win7建立 local blockchain network 并测试其基本功能。

开发环境: Win7 (64 bit, RAM >4G) , vagrant , blockchain, postman,golang

1. win7 安装 cygwin

(https://cygwin.com/install.html)

将安装之执行文件目录 (C:\cygwin64\bin) 设定在控制台系统 path 变量

win7 安装 virtualbox (VirtualBox-5.1.6-110634-Win.exe)

(https://www.virtualbox.org/)

win7 安装 git

(https://git-scm.com/downloads)

win7 安装 vagrant

(https://releases.hashicorp.com/vagrant/1.8.5/vagrant_1.8.5.msi)

2. 建立 github账号

登入github 建立账号 (https://github.com/ 纪录 username 及 password),clone hyperledger/fabric 至你的 repository
浏览器入以下网址:
 https://github.com/hyperledger/fabric/blob/master/docs/Gerrit/gerrit.md#Working-with-a-local-clone-of-the-repository  
(点选 fork )

3. 确认 fabric 已 clone 至你的 repository

4. 将 fabric clone至Win7 local machine

进入Win7 command shell:
$mkdir d:\GoProjects
$set GOPATH=D:\GoProjects
$mkdir d:\GoProjects\src
$ cd d:\GoProjects\src
$mkdir -p github.com\hyperledger
$cd github.com/hyperledger
$git clone https://github.com/chenlin2/fabric
$cd d:\GoProjects\src\github.com\hyperledger\fabric\devenv
$vagrant up (第一次在 virtual box 产生虚拟机器 hyperledger需要一段时间)

5. 登录至hyperledger 虚拟机器

$vagrant ssh (若失败,参考附录 3)
vagrant@hyperledger-devenv:v0.1.0-57bbb61:echo $GOPATH

/opt/gopath

($GOPATH/src/github.com/hyperledger/fabric) 挂载点将挂载 win 7 local machine目录 (d:\GoProjects\src\github.com\hyperledger\fabric) 可用 touch 指令新增 empty file (touch abcd) 测试是否挂载成功

6. 建立安全机制 (在 hyperledger虚拟机器 command shell)

#cd $GOPATH/src/github.com/hyperledger/fabric
#make membersrvc && membersrvc &
#export CORE_SECURITY_ENABLED=true
#export CORE_SECURITY_PRIVACY=true
# peer -v (检验 peer 是否建立)
若 peer 不存在 #make peer
#peer node start --peer-chaincodedev & (建立 blockchain network node)

7. 编译范例 chaincode_example02

#cd $GOPATH/src/github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02
#go build (输出 chaincode_example02)
#ls
 chaincode_example02  chaincode_example02.go
#export CORE_CHAINCODE_ID_NAME=mycc
#export CORE_PEER_ADDRESS=0.0.0.0:7051
#./chaincode_example02 &

8. 在 win7下载安装 Postman (Postman-win64-4.7.1-Setup.exe) https://www.getpostman.com/apps

Postman 具有图形接口,藉REST API 与刚建立的 Blockchain Netwok 沟通

9. 检视 blockchain network

REST API字段输入:http://localhost:7050/network/peers
7050定义于core.yaml

10. User 至 vp (validating peer) 注册登录:

REST API字段输入: http://localhost:7050/registrar
 {
   "enrollId": "jim",
   "enrollSecret": "6avZQLwcUe9b"
 }

11. 初始设定 a=100 b=200

REST API字段输入: http://localhost:7050/chcincode
执行init function args:[“a”, “100”, “b”, “200”]
 {
   "jsonrpc": "2.0",
   "method": "deploy",
   "params": {
    "type": 1,
    "chaincodeID":{
        "name": "mycc"
    },
    "ctorMsg": {
         "function":"init",
         "args":["a", "100", "b", "200"]
    },
    "secureContext": "jim"
 },
 "id": 1
}

12. a 转移 b 10单元

REST API字段输入: http://localhost:7050/chcincode
执行 invoke function args:[“a”, “b”, “10”]
{
  "jsonrpc": "2.0",
  "method": "invoke",
  "params": {
      "type": 1,
      "chaincodeID":{
          "name":"mycc"
      },
      "ctorMsg": {
         "function":"invoke",
         "args":["a", "b", "10"]
      },
      "secureContext": "jim"
  },
  "id": 3
}

13. 询问是否a=90

REST API字段输入: http://localhost:7050/chcincode
执行 function:query, args:[“a”]
{
  "jsonrpc": "2.0",
  "method": "query",
  "params": {
      "type": 1,
      "chaincodeID":{
          "name":"mycc"
      },
      "ctorMsg": {
         "function":"query",
         "args":["a"]
      },
      "secureContext": "jim"
  },
  "id": 5
}

14. 本例步骤已存至local_blockchain_example02.postman_collection.json (如 附录 2),当执行至步骤6 import此json檔后点选各项(peers, login, init,invoke,read) 即可执行。

15. 模仿本例步骤可将先前使用 bluemix blockchain service 的范例:

http://www.remkohde.com/2016/07/04/running-your-own-blockchain/
转换至 local blockchain network 环境执行。

附录:

1.Postman使用手册
 http://www.jianshu.com/p/13c8017bb5c8
2.local_blockchain_example02.postman_collection.json网址
https://1drv.ms/u/s!AkBmzWP0h-Vxgg9c8mHDB4iJzH6D
3.cygwin 增加 ssh 指令
http://superuser.com/questions/868604/ssh-client-not-found-on-cygwin64
4.陈兆麟 facebook
https://zh-cn.facebook.com/chaolin.chen.18

參考文獻:

1.https://github.com/hyperledger/fabric/blob/master/docs/Setup/Chaincode-setup.md
2.https://github.com/hyperledger/fabric/blob/master/docs/dev-setup/devenv.md
3.https://github.com/hyperledger/fabric/blob/master/docs/Setup/Chaincode-setup.md
4.https://segmentfault.com/a/1190000002645737
5.https://github.com/hyperledger
6.http://marmelab.com/blog/2016/04/28/blockchain-for-web-developers-the-theory.html
7.http://marmelab.com/blog/2016/05/20/blockchain-for-web-developers-in-practice.html
8.http://marmelab.com/blog/2016/06/14/blockchain-for-web-developers-the-truth.html
9.https://www.hyperledger.org/
10.http://blockchaindev.org/
11.http://hyperledger-fabric.readthedocs.io/en/latest/Setup/Network-setup/
12.http://www.qkldx.net/topic/213/hyperledger-fabric-chaincode-%E5%AD%A6%E4%B9%A01-%E5%BC%80%E5%8F%91%E8%B0%83%E8%AF%95
13.https://1drv.ms/f/s!AkBmzWP0h-Vxbr7h1uTNsTMcPWI
14.https://1drv.ms/b/s!AkBmzWP0h-VxazOBOsVz8nGMLpM
15.https://1drv.ms/b/s!AkBmzWP0h-VxbLK7FOjH9IL5HHc
16.https://www.youtube.com/watch?v=24JAF9Eu1mk&list=UUOTZe-b_jE_Ovy9K9UhZ4BA&index=74
17.https://www.youtube.com/watch?v=wSmJ-rwUo8I&list=UUOTZe-b_jE_Ovy9K9UhZ4BA&index=73
18.https://www.youtube.com/watch?v=SOsn6MvGH60&list=UUOTZe-b_jE_Ovy9K9UhZ4BA&index=72

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值