拆解fabric-sample/first-network/byfn.sh——执行demo看效果
byfn是fabric-sample下的一个快速入门的demo。
byfn全名意为:build your first network,就是用来构建一个你自己的第一个fabric超级账本网络。
这个demo属于入门级demo,也是一个快速帮助初学者了解超级账本的启动流程的一个最好的例子。
目录结构
使用tree命令查看目录结构,由于层级太多,这里只看两层
tree -L 2 fabric-sample
目录结构如下:
fabric-samples/
├── balance-transfer
│ ├── app
│ ├── app.js
│ ├── artifacts
│ ├── config.js
│ ├── config.json
│ ├── package.json
│ ├── README.md
│ ├── runApp.sh
│ ├── testAPIs.sh
│ └── typescript
├── basic-network
│ ├── config
│ ├── configtx.yaml
│ ├── crypto-config
│ ├── crypto-config.yaml
│ ├── docker-compose.yml
│ ├── generate.sh
│ ├── init.sh
│ ├── README.md
│ ├── start.sh
│ ├── stop.sh
│ └── teardown.sh
├── chaincode
│ ├── abac
│ ├── chaincode_example02
│ ├── fabcar
│ ├── marbles02
│ ├── marbles02_private
│ └── sacc
├── chaincode-docker-devmode
│ ├── docker-compose-simple.yaml
│ ├── msp
│ ├── myc.tx
│ ├── orderer.block
│ ├── README.rst
│ └── script.sh
├── CODE_OF_CONDUCT.md
├── CONTRIBUTING.md
├── fabcar
│ ├── enrollAdmin.js
│ ├── invoke.js
│ ├── package.json
│ ├── query.js
│ ├── registerUser.js
│ └── startFabric.sh
├── fabric-ca
│ ├── bootstrap.sh
│ ├── build-images.sh
│ ├── makeDocker.sh
│ ├── README.md
│ ├── scripts
│ ├── start.sh
│ └── stop.sh
├── first-network
│ ├── base
│ ├── byfn.sh
│ ├── channel-artifacts
│ ├── configtx.yaml
│ ├── crypto-config
│ ├── crypto-config.yaml
│ ├── docker-compose-cli.yaml
│ ├── docker-compose-couch-org3.yaml
│ ├── docker-compose-couch.yaml
│ ├── docker-compose-e2e-template.yaml
│ ├── docker-compose-e2e.yaml
│ ├── docker-compose-org3.yaml
│ ├── eyfn.sh
│ ├── org3-artifacts
│ ├── README.md
│ └── scripts
├── high-throughput
│ ├── chaincode
│ ├── README.md
│ └── scripts
├── Jenkinsfile
├── LICENSE
├── MAINTAINERS.md
├── README.md
└── scripts
├── bootstrap.sh
└── Jenkins_Scripts
demo包含的示例功能
- 搭建一个包含1个排序服务节点、4个peer节点的超级账本网络;
- 在(1)基础上,使用couchdb数据库的超级账本;
- 在(1)基础上,使用upgrade进行链码(智能合约)升级;
- 在(1)基础上,继续增加peer节点;
执行过程
-
环境依赖
- docker-ce、docker-compose
- golang>=1.10(这里使用的1.11)
- fabric可执行文件:configtxgen、configtxlator、cryptogen、orderer、peer(可执行文件可以从官网下载,这里采用的是源码编译的方式make release生成)
-
环境检查(启动之前最好清除上一次未清除的运行容器)
- docker stop $(docker ps -a -q)
- docker rm $(docker ps -a -q)
-
启动
- hyperledger/fabric-samples/first-network
- ./byfn.sh up
大功告成(附上几个日志关键点)
____ _____ _ ____ _____
/ ___| |_ _| / \ | _ \ |_ _|
\___ \ | | / _ \ | |_) | | |
___) | | | / ___ \ | _ < | |
|____/ |_| /_/ \_\ |_| \_\ |_|
Build your first network (BYFN) end-to-end test
...
Channel 'mychannel' created
...
peer0.org1 joined channel 'mychannel'
...
peer1.org1 joined channel 'mychannel'
...
peer0.org2 joined channel 'mychannel'
...
peer1.org2 joined channel 'mychannel'
...
Anchor peers updated for org 'Org1MSP' on channel 'mychannel'
...
Anchor peers updated for org 'Org2MSP' on channel 'mychannel'
...
Chaincode is installed on peer0.org1
...
Chaincode is installed on peer0.org2
...
Chaincode is instantiated on peer0.org2 on channel 'mychannel'
...
Query successful on peer0.org1 on channel 'mychannel'
...
Invoke transaction successful on peer0.org1 peer0.org2 on channel 'mychannel'
...
Chaincode is installed on peer1.org2
...
Query successful on peer1.org2 on channel 'mychannel'
...
All GOOD, BYFN execution completed
_____ _ _ ____
| ____| | \ | | | _ \
| _| | \| | | | | |
| |___ | |\ | | |_| |
|_____| |_| \_| |____/
主要流程
通过日志可以提取到byfn启动过程中经历的主要流程如下:
- Channel 'mychannel' created
- peer0.org1 joined channel 'mychannel'
- peer1.org1 joined channel 'mychannel'
- peer0.org2 joined channel 'mychannel'
- peer1.org2 joined channel 'mychannel'
- Anchor peers updated for org 'Org1MSP' on channel 'mychannel'
- Anchor peers updated for org 'Org2MSP' on channel 'mychannel'
- Chaincode is installed on peer0.org1
- Chaincode is installed on peer0.org2
- Chaincode is instantiated on peer0.org2 on channel 'mychannel'
- Query successful on peer0.org1 on channel 'mychannel'
- Chaincode is installed on peer1.org2
- Query successful on peer1.org2 on channel 'mychannel'
其中前10步属于fabric超级账本的启动过程,后面3步属于demo验证过程。
这个时候我们看一下docker容器运行的情况,如下:
docker ps --format "{{.ID}}: {{.Names}}: {{.Ports}}"
7fbd587c2fed: dev-peer1.org2.example.com-mycc-1.0:
8ccb7d4907bc: dev-peer0.org1.example.com-mycc-1.0:
2265d858df3a: dev-peer0.org2.example.com-mycc-1.0:
010e86a766e8: cli:
13e39a9f823b: peer0.org1.example.com: 0.0.0.0:7051->7051/tcp, 0.0.0.0:7053->7053/tcp
aea27eac63e3: peer1.org2.example.com: 0.0.0.0:10051->7051/tcp, 0.0.0.0:10053->7053/tcp
52e301ba519b: peer0.org2.example.com: 0.0.0.0:9051->7051/tcp, 0.0.0.0:9053->7053/tcp
23796a609cde: peer1.org1.example.com: 0.0.0.0:8051->7051/tcp, 0.0.0.0:8053->7053/tcp
0c8cc5f2a4dd: orderer.example.com: 0.0.0.0:7050->7050/tcp
可以发现启动了4个peer节点各自监听7051端口,1个orderer节点监听7050端口,1个cli命令行客户端,还有3个链码容器。
Note:
参照官网解释,链码容器只有在init或者执行事务操作时才会启动
Furthermore, a chaincode container is not started for a peer until an init or traditional transaction - read/write - is performed against that chaincode (e.g. query for the value of “a”).
命令DIY
除了上面使用脚本来看demo外,我们还可以基于启动的demo进行自己实操一把
使用命令在byfn网络下进行账户查询、转账操作
- 进入cli容器
docker exec -it cli /bin/bash
- 查询A账户的余额
peer chaincode query -C mychannel -n mycc -c '{"Args":["query","a"]}'
- 执行转账操作(a->b)
peer chaincode invoke -C mychannel -n mycc -c '{"Args":["invoke","a","b","10"]}'
其他操作
- 查看当前账本的链码
@ubuntu:~/first-network$ docker exec -it cli peer chaincode list --installed
Get installed chaincodes on peer:
Name: mycc, Version: 1.0, Path: github.com/chaincode/chaincode_example02/go/, Id: xxx
- 查看当前账本的通道
@ubuntu:~/first-network$ docker exec -it cli peer channel list
2018-09-04 05:57:52.794 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized
Channels peers has joined:
mychannel
- 获取该channel的最新区块信息
@ubuntu:~/first-network$ docker exec -it cli peer channel fetch newest -c mychannel -o orderer.example.com
2018-09-04 06:35:56.336 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized
2018-09-04 06:35:56.338 UTC [cli/common] readBlock -> INFO 002 Received block: 4
- 获取当前channel的信息
@ubuntu:~/first-network$ docker exec -it cli peer channel getinfo -c mychannel
2018-09-04 06:38:40.767 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized
Blockchain info: {"height":5,"currentBlockHash":"x5E2Cy8tUTuRAZCylxvHLSDP988dsllAxn8F9S2QIkw=","previousBlockHash":"jIE/NJv3PpJXSwrvCspd1jpbUMFXf2/QGwwRLL+dn84="}
- 更多可利用peer -help查看系统帮助,也可以查看官方介绍
谢谢。