Hyperledger Fabric V1.4.6(一)手动搭建Fabric网络(基于first-network)
如果还不具备Hyperledger Fabric的完整环境,请参考:Hyperledger Fabric(一)V2.0基础环境搭建(同样适用于V1.4版本,注意更换Fabric二进制文件及镜像的版本号,见本文档底部)。当前Fabric最新版本为V2.0 bate测试版,若想搭建最新版本Fabric网络,请参考:Hyperledger Fabric V2.0(五)手动搭建Fabric网络。请注意目前V2.0版本仍处于测试阶段,经个人尝试,发现java链码部署一直报错,尝试解决未果,因此在2.0正式版未被公布之前,建议部署V1.4+版本。在具备基础环境后,便可进行Fabric网络的部署工作。可以参照官网文档关于first-network教程的部分,使用提供的脚本文件快速启动区块链网络。但为了搭建自定义的Fabric网络,需要掌握脚本所执行的具体操作。
first-network教程中byfn.sh脚本使用说明如下:
Usage:
byfn.sh <mode> [-c <channel name>] [-t <timeout>] [-d <delay>] [-f <docker-compose-file>] [-s <dbtype>] [-l <language>] [-o <consensus-type>] [-i <imagetag>] [-a] [-n] [-v]
<mode> - one of 'up', 'down', 'restart', 'generate' or 'upgrade'
- 'up' - bring up the network with docker-compose up
- 'down' - clear the network with docker-compose down
- 'restart' - restart the network
- 'generate' - generate required certificates and genesis block
- 'upgrade' - upgrade the network from version 1.3.x to 1.4.0
-c <channel name> - channel name to use (defaults to "mychannel")
-t <timeout> - CLI timeout duration in seconds (defaults to 10)
-d <delay> - delay duration in seconds (defaults to 3)
-f <docker-compose-file> - specify which docker-compose file use (defaults to docker-compose-cli.yaml)
-s <dbtype> - the database backend to use: goleveldb (default) or couchdb
-l <language> - the chaincode language: golang (default) or node
-o <consensus-type> - the consensus-type of the ordering service: solo (default), kafka, or etcdraft
-i <imagetag> - the tag to be used to launch the network (defaults to "latest")
-a - launch certificate authorities (no certificate authorities are launched by default)
-n - do not deploy chaincode (abstore chaincode is deployed by default)
-v - verbose mode
byfn.sh -h (print this message)
Typically, one would first generate the required certificates and
genesis block, then bring up the network. e.g.:
byfn.sh generate -c mychannel
byfn.sh up -c mychannel -s couchdb
byfn.sh up -c mychannel -s couchdb -i 1.4.0
byfn.sh up -l node
byfn.sh down -c mychannel
byfn.sh upgrade -c mychannel
Taking all defaults:
byfn.sh generate
byfn.sh up
byfn.sh down
使用脚本启动网络的操作如下:
① 进入first-network 目录
cd fabric-samples/first-network
② 启动网络(该过程包括:启动容器、创建通道、加入通道、更新组织锚节点、打包链码、安装链码、实例化链码、调用链码)
./byfn.sh up -l node -o etcdraft -s couchdb #安装node语言版本的chaincode_example02的链码,使用Raft排序服务而不是solo,启用couchdb数据库(支持富查询)
# ./byfn.sh up -n -a -o etcdraft -s couchdb #不安装chaincode_example02的链码,启用CA服务,启用Raft排序服务而不是solo,启用couchdb数据库(支持富查询)
等效的手动部署Fabric网络的方式如下(启用CAs,使用couchdb):
Ⅰ 使用cryptogen工具生成证书及密钥,放置于crypto-config目录下
cryptogen generate --config=./crypto-config.yaml
Ⅱ 使用configtxgen工具生成4个配置组件(genesis.block,channel.tx,Org1MSPanchors.tx,Org2MSPanchors.tx),放置于 channel-artifacts目录下
① export FABRIC_CFG_PATH=$PWD
② configtxgen -profile TwoOrgsOrdererGenesis -channelID byfn-sys-channel -outputBlock ./channel-artifacts/genesis.block #生成系统创世区块,默认使用solo排序,channelID是系统通道的名字
# configtxgen -profile SampleMultiNodeEtcdRaft -channelID byfn-sys-channel -outputBlock ./channel-artifacts/genesis.block #使用Raft排序服务
③ export CHANNEL_NAME=mychannel #通过环境变量设置通道名称
configtxgen -profile TwoOrgsChannel -outputCreateChannelTx ./channel-artifacts/channel.tx -channelID $CHANNEL_NAME #创建 通道配置 事务channel.tx
④ configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org1MSPanchors.tx -channelID $CHANNEL_NAME -asOrg Org1MSP #创建 更新组织Org1在该通道上的锚节点 的事务Org1MSPanchors.tx
configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org2MSPanchors.tx -channelID $CHANNEL_NAME -asOrg Org2MSP #创建 更新组织Org2在该通道上的锚节点 的事务Org2MSPanchors.tx
Ⅲ 创建通道,将peer节点加入通道,并更新组织的锚节点
① docker-compose -f docker-compose-cli.yaml up -d #不启用CA,不启动couchDB
#docker-compose -f docker-compose-cli.yaml -f docker-compose-couch.yaml up -d 启用couchdb
② docker exec -it cli bash #进入CLI容器
配置环境变量,以使CLI容器作为peer0.org1.example.com节点执行命令,后续很多操作需要不断地切换这组环境变量,以作为不同的peer节点执行相关操作,节点peer0.org1.example.com的环境变量已默认配置在cli容器中,peer0.org1.example.com的环境变量如下,
CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp CORE_PEER_ADDRESS=peer0.org1.example.com:7051 CORE_PEER_LOCALMSPID="Org1MSP" CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt
为了便于查看,在此也先给出节点peer0.org2.example.com的环境变量:
CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp
CORE_PEER_ADDRESS=peer0.org2.example.com:9051
CORE_PEER_LOCALMSPID="Org2MSP"
CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt
③ 将通道配置事务发送给order节点以创建通道
export CHANNEL_NAME=mychannel #以环境变量形式设置通道名称,供以下命令使用
peer channel create -o orderer.example.com:7050 -c $CHANNEL_NAME -f ./channel-artifacts/channel.tx --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
④ 切换环境变量,多次执行以下命令,将所有节点加入通道
peer channel join -b mychannel.block
⑤ 切换环境变量,多次执行以下命令,更新组织锚节点
peer channel update -o orderer.example.com:7050 -c $CHANNEL_NAME -f ./channel-artifacts/${CORE_PEER_LOCALMSPID}anchors.tx --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
Ⅲ 安装、实例化链码(实例化时,node及java耗时较长,执行init
或者其他交易才能导致链码容器的启动),以chaincode_example02链码为例
① 切换环境变量,多次执行以下命令,在不同节点安装链码
peer chaincode install -n mycc -v 1.0 -p github.com/chaincode/chaincode_example02/go/ #Golang
peer chaincode install -n mycc -v 1.0 -l node -p /opt/gopath/src/github.com/chaincode/chaincode_example02/node/ #Node.js
peer chaincode install -n mycc -v 1.0 -l java -p /opt/gopath/src/github.com/chaincode/chaincode_example02/java/ #Java
② 切换环境变量,多次执行以下命令,在不同节点实例化链码
peer chaincode instantiate -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 $CHANNEL_NAME -n mycc -v 1.0 -c '{"Args":["init","a", "100", "b","200"]}' -P "AND ('Org1MSP.peer','Org2MSP.peer')" #Golang
peer chaincode instantiate -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 $CHANNEL_NAME -n mycc -l node -v 1.0 -c '{"Args":["init","a", "100", "b","200"]}' -P "AND ('Org1MSP.peer','Org2MSP.peer')" #Node.js
peer chaincode instantiate -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 $CHANNEL_NAME -n mycc -l java -v 1.0 -c '{"Args":["init","a", "100", "b","200"]}' -P "AND ('Org1MSP.peer','Org2MSP.peer')" #Java
③ 查询键‘a’在状态数据库的Value
peer chaincode query -C $CHANNEL_NAME -n mycc -c '{"Args":["query","a"]}'
④ 调用‘invoke’函数,a向b转账10
peer chaincode invoke -o orderer.example.com:7050 --tls true --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 $CHANNEL_NAME -n mycc --peerAddresses peer0.org1.example.com:7051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt --peerAddresses peer0.org2.example.com:9051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt -c '{"Args":["invoke","a","b","10"]}'
Fabric的默认状态数据库为levelDB,levelDB仅支持通过KEY来查询数据,而couchdb支持使用富查询,即可根据Value的某个属性进行查询,marbles02案例演示了链码中如何使用富查询,在此对marbles02案例的链码进行测试(使用docker compose网络容器时,添加上couchdb对应的yaml文件,其余步骤与上述完全相同)
docker exec -it cli bash #进入CLI容器
export CHANNEL_NAME=mychannel #通道名称
① 安装、实例化链码(在此通过-l node安装并实例化node语言版本链码,默认为go且go版本链码-p只需指定相对路径github.com/chaincode/marbles02/go)(背书策略在此指定为"OR ('Org1MSP.peer','Org2MSP.peer')",因此只在peer0.org1.example.com上安装并实例化链码即可)
peer chaincode install -n marbles -l node -v 1.0 -p /opt/gopath/src/github.com/chaincode/marbles02/node
peer chaincode instantiate -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 $CHANNEL_NAME -n marbles -l node -v 1.0 -c '{"Args":["init"]}' -P "OR ('Org1MSP.peer','Org2MSP.peer')"
② 创建一些弹珠并转移它们(可在浏览器中输入http://localhost:5984/_utils查看couchdb数据库状态)
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 $CHANNEL_NAME -n marbles -c '{"Args":["initMarble","marble1","blue","35","tom"]}' 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 $CHANNEL_NAME -n marbles -c '{"Args":["initMarble","marble2","red","50","tom"]}' 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 $CHANNEL_NAME -n marbles -c '{"Args":["initMarble","marble3","blue","70","tom"]}' 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 $CHANNEL_NAME -n marbles -c '{"Args":["transferMarble","marble2","jerry"]}' 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 $CHANNEL_NAME -n marbles -c '{"Args":["transferMarblesBasedOnColor","blue","jerry"]}' 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 $CHANNEL_NAME -n marbles -c '{"Args":["delete","marble1"]}'
③ 查询
peer chaincode query -C $CHANNEL_NAME -n marbles -c '{"Args":["readMarble","marble2"]}' #通过名字查询marble2弹珠
peer chaincode query -C $CHANNEL_NAME -n marbles -c '{"Args":["getHistoryForMarble","marble1"]}' #检索特定弹珠的历史
peer chaincode query -C $CHANNEL_NAME -n marbles -c '{"Args":["queryMarblesByOwner","jerry"]}' #还可以对数据内容执行富查询,例如按所有者查询弹珠
其他:
① 链码升级:修改版本号后,在节点上再次安装链码:
peer chaincode install -n elecData -l node -v 1.1 -p /opt/gopath/src/github.com/chaincode/always/ElecData
升级命令和实例化命令基本相同,只需将instantiate改为upgrade
peer chaincode upgrade -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 $CHANNEL_NAME -n elecData -l node -v 1.1 -c '{"Args":["instantiate"]}' -P "OR ('Org1MSP.peer','Org2MSP.peer')"
② 查看日志:
docker logs -f cli #通过cli容器日志查看网络中的事务信息
docker logs 链码容器 #查看链码日志命令格式
③ FabricV1.4官方JS链码网址
https://hyperledger.github.io/fabric-chaincode-node/release-1.4/api/
https://github.com/hyperledger/fabric-chaincode-node/tree/v1.4.5
https://hyperledger.github.io/fabric-chaincode-node/
④ Fabric官方JS SDK1.4网址
https://github.com/hyperledger/fabric-sdk-node
https://hyperledger.github.io/fabric-sdk-node/release-1.4/index.html
⑤ fabric-samples地址:https://github.com/hyperledger/fabric-samples
⑥ V1.4二进制文件及镜像安装脚本命令:curl -sSL http://bit.ly/2ysbOFE | bash -s -- 1.4.6 1.4.6 0.4.18
单纯的curl -sSL http://bit.ly/2ysbOFE | bash -s总是默认安装当前最新版本,因此需要指定1.4的版本号
posted on 2020-03-26 03:53 always-kaixuan 阅读(790) 评论(0) 编辑 收藏 举报