2019独角兽企业重金招聘Python工程师标准>>>
HyperLedger Fabric 实战入门
先实践后理论
第一步 初始化开发环境
参考官方文档
http://hyperledger-fabric.readthedocs.io/en/release-1.2/
注意: 本博客只为了记录个人对HyperLedger Fabric的学习,所以省略go,docker,docker-compose,nodejs,python,maven,jdk等基本软件的安装和使用,详细使用请参考相关文档
系统信息:
Linux ubuntu 4.15.0-29-generic
go version go1.10.1 linux/amd64
Docker version 18.06.0-ce-dev
docker-compose version 1.21.1
node-v8.11.3
npm 5.6.0
java version "1.8.0_161"
Apache Maven 3.5.3
1 基础环境配置加速器
docker加速器 docker-cn
nnodejs加速器 淘宝NPM镜像
golang国内镜像 Go中文网
环境变量设置
#jdk
export JAVA_HOME=/opt/jdk1.8.0_161
export CLASSPATH=${JAVA_HOME}/lib:${JAVA_HOME}/jre/lib
export PATH=${JAVA_HOME}/bin:${PATH}
#maven
export MAVEN_HOME=/opt/apache-maven-3.5.3
export PATH=${MAVEN_HOME}/bin:${PATH}
#
#全局命令
export SHELL_HOME=/opt/shell
export PATH=${SHELL_HOME}:${PATH}
#nodejs
export NODE_HOME=/opt/node-v8.11.3-linux-x64
export NODE_PATH=${NODE_HOME}/lib/node_modules
export PATH=${NODE_HOME}/bin:${PATH}
alias cnpm="npm --registry=https://registry.npm.taobao.org --cache=${HOME}/.npm/.cache/cnpm --disturl=https://npm.taobao.org/dist --userconfig=${HOME}/.cnpmrc"
#go
export GOPATH=${HOME}/go
export PATH=${GOPATH}/bin:${PATH}
#fabric
export PATH=${HOME}/fabric-samples/bin:${PATH}
*注意:*具体配置请配置成自己的路径
2 快速安装
为了快速构建,不用该太多脚本路径,请保持在$HOME目录下
cd ~/
curl -sSL https://raw.githubusercontent.com/hyperledger/fabric/release-1.3/scripts/bootstrap.sh -o bootstrap.sh
chmod +x bootstrap.sh
./bootstrap.sh
如果github下载速度慢:
sed -i 's#https://github.com/hyperledger/fabric-samples.git#[email protected]:fansinZhao/fabric-samples.git#g' bootstrap.sh
如果其他资源下载安装包慢,请访问 fabric离线包
耐心等待下载,下载成功后显示如下
===> List out hyperledger docker images
hyperledger/fabric-ca 1.2.0 66cc132bd09c 4 weeks ago 252MB
hyperledger/fabric-ca latest 66cc132bd09c 4 weeks ago 252MB
hyperledger/fabric-tools 1.2.0 379602873003 4 weeks ago 1.51GB
hyperledger/fabric-tools latest 379602873003 4 weeks ago 1.51GB
hyperledger/fabric-ccenv 1.2.0 6acf31e2d9a4 4 weeks ago 1.43GB
hyperledger/fabric-ccenv latest 6acf31e2d9a4 4 weeks ago 1.43GB
hyperledger/fabric-orderer 1.2.0 4baf7789a8ec 4 weeks ago 152MB
hyperledger/fabric-orderer latest 4baf7789a8ec 4 weeks ago 152MB
hyperledger/fabric-peer 1.2.0 82c262e65984 4 weeks ago 159MB
hyperledger/fabric-peer latest 82c262e65984 4 weeks ago 159MB
hyperledger/fabric-zookeeper 0.4.10 2b51158f3898 5 weeks ago 1.44GB
hyperledger/fabric-zookeeper latest 2b51158f3898 5 weeks ago 1.44GB
hyperledger/fabric-kafka 0.4.10 936aef6db0e6 5 weeks ago 1.45GB
hyperledger/fabric-kafka latest 936aef6db0e6 5 weeks ago 1.45GB
hyperledger/fabric-couchdb 0.4.10 3092eca241fc 5 weeks ago 1.61GB
hyperledger/fabric-couchdb latest 3092eca241fc 5 weeks ago 1.61GB
执行完需要刷新一下环境变量
source /etc/profile
或者注销重启
3 官方例子1 "helloword"
cd ~/fabric-samples/first-network
./byfn.sh generate
./byfn.sh up
一切顺利的话,显示下面的内容
===================== Query successful on peer1.org2 on channel 'mychannel' =====================
========= All GOOD, BYFN execution completed ===========
关闭的话
./byfn.sh down
_____ _ _ ____
| ____| | \ | | | _ \
| _| | \| | | | | |
| |___ | |\ | | |_| |
|_____| |_| \_| |____/
4 官方例子2 IBM的marble
cd ~/fabric-samples/fabcar
./startFabric.sh
npm install
npm audit fix
node enrollAdmin.js
node registerUser.js
node invoke.js
node query.js
正常执行完毕后,会在目录$HOME/fabric-samples/fabcar/hfc-key-store
下有文件存在.类似下面
zhaofeng[@ubuntu](https://my.oschina.net/u/555627):~/fabric-samples/fabcar$ tree hfc-key-store/
hfc-key-store/
├── 51da36ca8deb87c1cefa30ae686c5addb67cff53739b859c63ba708581cb6640-priv
├── 51da36ca8deb87c1cefa30ae686c5addb67cff53739b859c63ba708581cb6640-pub
├── 95d4a6c61e4b503269b5d525e2c876c22cf0038bfa8b3396f3d5f8a5c53055a8-priv
├── 95d4a6c61e4b503269b5d525e2c876c22cf0038bfa8b3396f3d5f8a5c53055a8-pub
├── admin
└── user1
0 directories, 6 files
使用docker查看镜像
zhaofeng[@ubuntu](https://my.oschina.net/u/555627):~/fabric-samples/fabcar$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f2aa240d4298 dev-peer0.org1.example.com-marbles-v4-f704f1411b52539123b5920f0c3d59dc4c88aa255a82cd002be690a5a6a8150e "chaincode -peer.add…" 8 hours ago Up 8 hours dev-peer0.org1.example.com-marbles-v4
127aac75b9bb dev-peer0.org1.example.com-fabcar-1.0-5c906e402ed29f20260ae42283216aa75549c571e2e380f3615826365d8269ba "chaincode -peer.add…" 8 hours ago Up 8 hours dev-peer0.org1.example.com-fabcar-1.0
d9d7172e9043 hyperledger/fabric-tools "/bin/bash" 8 hours ago Up 8 hours cli
04bcf025450c hyperledger/fabric-peer "peer node start" 8 hours ago Up 8 hours 0.0.0.0:7051->7051/tcp, 0.0.0.0:7053->7053/tcp peer0.org1.example.com
2ddca7b27e38 hyperledger/fabric-ca "sh -c 'fabric-ca-se…" 8 hours ago Up 8 hours 0.0.0.0:7054->7054/tcp ca.example.com
5454667897c4 hyperledger/fabric-couchdb "tini -- /docker-ent…" 8 hours ago Up 8 hours 4369/tcp, 9100/tcp, 0.0.0.0:5984->5984/tcp couchdb
0a23230a291e hyperledger/fabric-orderer "orderer" 8 hours ago Up 8 hours 0.0.0.0:7050->7050/tcp orderer.example.com
注意:npm install
执行报权限错误,使用npm install --unsafe-perm=true
到此步骤,fabric local环境已经初始化.下面安装IBM marbles
cd ~
git clone [email protected]:IBM-Blockchain/marbles.git --depth 1
修改配置文件marbles/config/connection_profile_local.json
"client": {
"organization": "Org1MSP",
"credentialStore": {
"path": "/$HOME/.hfc-key-store"
}
}
将$HOME/fabric-samples/fabcar/hfc-key-store复制一份到$HOME/.hfc-key-store,这一步是解决一个bug
cp -r $HOME/fabric-samples/fabcar/hfc-key-store $HOME/.hfc-key-store
修改配置文件marbles/config/marbles_local.json
"use_events": false,
不然系统会报错. 修改完配置文件,就可以执行安装代码了
cd marbles/
npm install
npm audit fix
cd scripts/
node install_chaincode.js
node instantiate_chaincode.js
npm install gulp -g
npm install
npm audit fix
gulp marbles_local
正常启动后访问http://localhost:3001/home. 其他细节参考官方文档
5 官方源码安装
下载主线代码
cd ~
git clone https://github.com/hyperledger/fabric.git
或者 cd ~ git clone https://gitee.com/fansinZhao/fabric.git
下载完毕后 cd fabric git checkout -b v1.2.0 cp -r ../fabric $GOPATH/src/github.com/hyperledger/fabric sudo ./network_setup.sh up
正常显示下面内容
2018-08-15 08:41:55.473 UTC [bccsp] GetDefault -> WARN 02d Before using BCCSP, please call InitFactories(). Falling back to bootBCCSP.
2018-08-15 08:41:55.473 UTC [msp] Sign -> DEBU 02e Idemix identity &{Org3MSP x: "\004\0249R\035\017$$\251n \023\352\365\250j\201\354\316\255\200\035\325E\001r\347\257\311\347\2230"
y: "z\014\025O\\\301\233\231\3773\342\372]TO\327\003Xer4\221\267\"VI}\203\345\2260W"
} is signing
Error: error endorsing query: rpc error: code = Unknown desc = Failed evaluating policy on signed data during check policy on channel [mychannel] with policy [/Channel/Application/Writers]: [Failed to reach implicit threshold of 1 sub-policies, required 1 remaining] - proposal response:
===================== Query successful on peer1.org3 on channel 'mychannel' =====================
===================== All GOOD, End-2-End execution completed =====================
_____ _ _ ____ _____ ____ _____
| ____| | \ | | | _ \ | ____| |___ \ | ____|
| _| | \| | | | | | _____ | _| __) | | _|
| |___ | |\ | | |_| | |_____| | |___ / __/ | |___
|_____| |_| \_| |____/ |_____| |_____| |_____|
关闭执行
sudo ./network_setup.sh down
*注意:*使用sudo,不然order会启动异常