环境:macOS High Sierra(Version 10.13.3)
参考:Hyperledger Fabric Samples
Fabric官方提供了一个示例项目fabric-samples,本文通过运行该项目来展示fabric的搭建与链码的安装调用。
1. 克隆项目
克隆hyperledger提供的fabric-samples项目
git clone https://github.com/hyperledger/fabric-samples.git
cd fabric-samples
git checkout v1.1.0 #切至1.1版本,以便跟后续安装的fabric版本相匹配
2. 下载fabric相关镜像和二进制文件
执行如下命令,下载fabric相关镜像和二进制文件(版本1.1.0)
curl -sSL https://goo.gl/6wtTN5 | bash -s 1.1.0
注意命令执行完成之后生成的bin目录以及其下的二进制文件,后面步骤中我们需要这些文件来进行fabrc区块链网络搭建。为了方便后续使用,这里我们将bin目录的路径加入到环境变量中
export PATH=$PWD/bin:$PATH
或者将export PATH=<path to download location>/bin:$PAT
加入~/.bash_profile
,使其永久生效。
脚本从docker hub拉去fabric相关镜像并将其标注为‘last’,后面我们会用这些镜像生成的容器来组成fabric区块链网络。
3. 启动fabric网络并完成chaincode测试
fabric-samples项目下有多个示例网络,这里我们参考教程Building Your First Network来运行first-network文件夹下的fabric网络并完成chaincode测试。
- 进入first-network目录
cd first-network
- 生成公私钥和证书信息、创世区块和通道相关信息
./byfn.sh -m generate
公私钥和证书信息基于crypto-config.yaml生成,保存在crypto-config文件夹中。
创世区块和通道相关信息基于configtx.yaml生成,保存在channel-artifacts文件夹中。
./byfn.sh -m generate *[1252c7a][ruby-2.3.0]
Generating certs and genesis block for with channel 'mychannel' and CLI timeout of '10' seconds and CLI delay of '3' seconds
Continue? [Y/n] y
proceeding ...
/Users/lisa/work-station/fabric-samples/first-network/../bin/cryptogen
##########################################################
##### Generate certificates using cryptogen tool #########
##########################################################
+ cryptogen generate --config=./crypto-config.yaml
org1.example.com
org2.example.com
+ res=0
+ set +x
/Users/lisa/work-station/fabric-samples/first-network/../bin/configtxgen
##########################################################
######### Generating Orderer Genesis block ##############
##########################################################
+ configtxgen -profile TwoOrgsOrdererGenesis -outputBlock ./channel-artifacts/genesis.block
2018-03-23 21:03:32.746 CST [common/tools/configtxgen] main -> INFO 001 Loading configuration
2018-03-23 21:03:32.759 CST [msp] getMspConfig -> INFO 002 Loading NodeOUs
2018-03-23 21:03:32.760 CST [msp] getMspConfig -> INFO 003 Loading NodeOUs
2018-03-23 21:03:32.760 CST [common/tools/configtxgen] doOutputBlock -> INFO 004 Generating genesis block
2018-03-23 21:03:32.761 CST [common/tools/configtxgen] doOutputBlock -> INFO 005 Writing genesis block
+ res=0
+ set +x
#################################################################
### Generating channel configuration transaction 'channel.tx' ###
#################################################################
+ configtxgen -profile TwoOrgsChannel -outputCreateChannelTx ./channel-artifacts/channel.tx -channelID mychannel
2018-03-23 21:03:32.783 CST [common/tools/configtxgen] main -> INFO 001 Loading configuration
2018-03-23 21:03:32.795 CST [common/tools/configtxgen] doOutputChannelCreateTx -> INFO 002 Generating new channel configtx
2018-03-23 21:03:32.795 CST [msp] getMspConfig -> INFO 003 Loading NodeOUs
2018-03-23 21:03:32.796 CST [msp] getMspConfig -> INFO 004 Loading NodeOUs
2018-03-23 21:03:32.821 CST [common/tools/configtxgen] doOutputChannelCreateTx -> INFO 005 Writing new channel tx
+ res=0
+ set +x
#################################################################
####### Generating anchor peer update for Org1MSP ##########
#################################################################
+ configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org1MSPanchors.tx -channelID mychannel -asOrg Org1MSP
2018-03-23 21:03:32.841 CST [common/tools/configtxgen] main -> INFO 001 Loading configuration
2018-03-23 21:03:32.851 CST [common/tools/configtxgen] doOutputAnchorPeersUpdate -> INFO 002 Generating anchor peer update
2018-03-23 21:03:32.851 CST [common/tools/configtxgen] doOutputAnchorPeersUpdate -> INFO 003 Writing anchor peer update
+ res=0
+ set +x
#################################################################
####### Generating anchor peer update for Org2MSP ##########
#################################################################
+ configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org2MSPanchors.tx -channelID mychannel -asOrg Org2MSP
2018-03-23 21:03:32.872 CST [common/tools/configtxgen] main -> INFO 001 Loading configuration
2018-03-23 21:03:32.880 CST [common/tools/configtxgen] doOutputAnchorPeersUpdate -> INFO 002 Generating anchor peer update
2018-03-23 21:03:32.880 CST [common/tools/configtxgen] doOutputAnchorPeersUpdate -> INFO 003 Writing anchor peer update
+ res=0
+ set +x
至此,在这一步我们生成了所有网络实体需要的证书和私钥,启动排序服务所需要的“创世区块”和用来配置通道所需要的一系列文件。
- 启动网络
./byfn.sh -m up #默认部署go语言编写的chaincode
或
./byfn.sh -m up -l node #可以通过指定-l node部署node版本的chaincode
看到这样的log时代表fabric网络启动成功。
CORE_PEER_ID=cli
CORE_LOGGING_LEVEL=INFO
CORE_PEER_ADDRESS=peer1.org2.example.com:7051
===================== Querying on peer1.org2 on channel 'mychannel'... =====================
Attempting to Query peer1.org2 ...3 secs
+ peer chaincode query -C mychannel -n mycc -c '{"Args":["query","a"]}'
+ res=0
+ set +x
2018-03-23 13:38:04.649 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 001 Using default escc
2018-03-23 13:38:04.649 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 002 Using default vscc
Query Result: 90
2018-03-23 13:38:15.427 UTC [main] main -> INFO 003 Exiting.....
===================== Query on peer1.org2 on channel 'mychannel' is successful =====================
========= All GOOD, BYFN execution completed ===========
_____ _ _ ____
| ____| | \ | | | _ \
| _| | \| | | | | |
| |___ | |\ | | |_| |
|_____| |_| \_| |____/
该命令执行成功之后会基于docker-compose-cli.yaml启动1Orderer+4Peer+1CLI的Fabric网络容器和相关的chaincode容器。
- chaincode测试
进入cli容器
docker exec -it cli bash
运行以下命令可以查询a账户的余额:
peer chaincode query -C mychannel -n mycc -c '{"Args":["query","a"]}'
可以看到余额是90
运行命令调用chaincode,将a账户的余额转10元给b账户
# be sure to set the -C and -n flags appropriately
peer chaincode invoke -o orderer.example.com:7050 --tls --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C mychannel -n mycc -c '{"Args":["invoke","a","b","10"]}'
返回的结果说明chaincode调用成功,此时a账户余额应为90-10=80,b账户余额应为210+10=120(b账户转账之前余额210)
执行下列命令验证如下
peer chaincode query -C mychannel -n mycc -c '{"Args":["query","a"]}'
peer chaincode query -C mychannel -n mycc -c '{"Args":["query","b"]}'
测试成功!可以关闭fabric网络了
./byfn.sh -m down
接下来就是根据自己的需要确定fabric网络架构(ca,peer,orderer,org个数等),使用go语言或者node编写chaincode,并将chaincode安装到fabric网络中即可。