在 Win7 使用 Docker Image 在 Local Machine 建构 Blockchain Network System

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

在 local machine 建立 blockchain network有2 种方法, 1)使用包含 hyperledger/fabric的 dokcer images或2)直接在本机使用 hyperledger/fabric 源码,为了让问题简单化(编译 source code 可能会失败),先使用储存在docker hub的 docker images,建构 local blockchain netwok,测试原先在 bluemix blockchain service 使用的范例。将blockchain 做成 docker images 是一重要程序,你可以将已开发完毕客制化的 docker images 放在支持 docker 的云端平台提供客户使用;以下范例使用 postman 执行 REST API 开发 local blockchain netwok内之chaincode (智能合约) 。

开发环境:Win7 (64 bit) ,Docker Toolbox, blockchain, postman

1.win7 安装 cygwin (https://cygwin.com/install.html)

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

2. 参考附录 (1) 在 Win7安装 Docker Toolbox (安装最新版)

(Win 10 可直接安装 Docker 请参考附录2 )

3. 桌面点选 (Docker Quickstart Terminal)

$ docker -v

Docker version 1.12.0, build 8eab29e

$ docker-machine ls (Docker Quickstart Terminal接口执行,确认虚拟机default 已安装)

NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS
default virtualbox Running tcp://192.168.99.101:2376 v1.10.3

$ docker-machine env default
SET DOCKER_TLS_VERIFY=1
SET DOCKER_HOST=tcp://192.168.99.100:2376
SET DOCKER_CERT_PATH=C:\Users\user\.docker\machine\machines\default
SET DOCKER_MACHINE_NAME=default
REM Run this command to configure your shell:
REM     FOR /f "tokens=*" %i IN ('docker-machine env default') DO %i
$ FOR /f “tokens=*” %i IN (‘docker-machine env default’) DO %i
$ docker-machine ls (确定虚拟机已经启动)

NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS
default * virtualbox Running tcp://192.168.99.100:2376 v1.10.3

$pwd

#/Users/user

$docker pull hyperledger/fabric-peer:latest
$docker pull hyperledger/fabric-membersrvc:latest
$ docker images

hyperledger/fabric-peer latest af109e2bfebc 7 days a go 1.422 GB
hyperledger/fabric-membersrvc latest 16d2098ab252 7 days a go 1.415 GB

$mkdir docker_blockchain
$cd docker_blockchain
   编辑 docker-compose.yml  (如附录 4)
$docker-compose up (启动 container)

4. 使用 putty (见附录 3)登录虚拟机器(ip:192.168.99.100 username=docker password=tcuser)

$docker ps (container已被启动)

5 进入 fabric-peer container shell (汇入chaincode_example02.go)

$docker exec -it dockerblockchain_vp0_1 bash
root@6ce0d68446e3:/opt/gopath/src/github.com/hyperledger/fabric
#mkdir -p $GOPATH/src/github.com/chaincode_example02/
#cd $GOPATH/src/github.com/chaincode_example02

#curl GET https://raw.githubusercontent.com/hyperledger/fabric/master/examples/chaincode/go/chaincode_example02/chaincode_example02.go > chaincode_example02.go

#ls

chaincode_example02.go

# go build ./ (编译 chaincode_example02.go)
# ls

chaincode_example02 chaincode_example02.go

# export CORE_CHAINCODE_ID_NAME=mycc
#export CORE_PEER_ADDRESS=0.0.0.0:7051
#./chaincode_example02 & (chaincode 启动接收外界送来的交易)

6. 在 win7下载安装 Postman (Postman-win64-4.7.1-Setup.exe)

https://www.getpostman.com/apps
Postman 具有好用的人机接口,藉REST API 与 Blockchain Netwok 沟通

7. 检视已建立的 blockchain network

REST API字段输入:http://192.168.99.100:7050/network/peers
192.168.99.100 (来自虚拟机器default之IP), 7050定义于docker-compose.yml

8. User 至 vp0 (validating peer 0) 注册登录:

REST API字段输入: http://192.168.99.100:7050/registrar
{
“enrollId”: “jim”,
“enrollSecret”: “6avZQLwcUe9b”

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

REST API字段输入: http://192.168.99.100: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
}

10. a 转移 b 10单元

REST API字段输入: http://192.168.99.100: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
}

11. 询问是否a 剩下90

REST API字段输入: http://192.168.99.100: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
}

12. 本例步骤已存至postman_collection.jsonchaincode_example02.json (附錄 6)

当执行至步骤6 import json档后,依序点选项目(peers,login,init,invoke,read)即可执行相对 REST API。

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

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

附录:

1.Win7 安装Docker Toolbox:
https://www.docker.com/products/docker-toolbox
http://knight-.iteye.com/blog/2254627
http://rominirani.com/2015/08/15/docker-toolbox-setup-windows/ https://getcarina.com/docs/troubleshooting/troubleshooting-windows-docker-vm-startup/
http://dockone.io/article/594
2.Win 10 安装Docker
http://blog.miniasp.com/post/2016/08/01/Docker-for-Windows-1-12-Released.aspx
3.putty安装及使用:
http://www.ascc.sinica.edu.tw/putty
4.docker-compose.yml (文字要对齐)
https://1drv.ms/u/s!AkBmzWP0h-Vxgg74u_PQ9l9cRyta
5.Postman使用手册
http://www.jianshu.com/p/13c8017bb5c8
6.chaincode_example02.postman_collection.json网址
https://1drv.ms/u/s!AkBmzWP0h-VxggtvAjN_PPAYw6DZ
7.陈兆麟 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/ibm-watson-iot/blockchain-samples
3.https://github.com/igorbarinov/awesome-blockchain
4.https://www.linkedin.com/pulse/blockchain-smart-contracts-health-booz-allen-hamilton-tori-adams
5.https://bitsonblocks.net/2015/09/09/a-gentle-introduction-to-blockchain-technology/
6.https://www.ibm.com/internet-of-things/iot-news/announcements/private-blockchain/
7.http://blockcan.com/news/topic/21040
8.https://www.ibm.com/internet-of-things/iot-news/announcements/private-blockchain/
9.https://1drv.ms/f/s!AkBmzWP0h-Vxbr7h1uTNsTMcPWI
10.https://1drv.ms/b/s!AkBmzWP0h-VxazOBOsVz8nGMLpM
11.https://1drv.ms/b/s!AkBmzWP0h-VxbLK7FOjH9IL5HHc
12.https://www.youtube.com/watch?v=24JAF9Eu1mk&list=UUOTZe-b_jE_Ovy9K9UhZ4BA&index=74
13.https://www.youtube.com/watch?v=wSmJ-rwUo8I&list=UUOTZe-b_jE_Ovy9K9UhZ4BA&index=73
14.https://www.youtube.com/watch?v=SOsn6MvGH60&list=UUOTZe-b_jE_Ovy9K9UhZ4BA&index=72

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐