Hyperledger Fabric学习笔记——startFabric.sh

fabcar案例中的startFabric.sh调用了first-network.sh脚本,建立了一个区块链网络,并且将chaincode目录中的fabcar链码安装到节点上并在通道中实例化。本文将以JS版本链码为例,手动安装并实例化链码。

startFabric.sh首先调用byfn.sh,./byfn.sh up -a -n -s couchdb,也可以手动启动first-network网络,具体见另一篇博客。

1、清理环境

./byfn.sh down
docker rm -f $(docker ps -aq)
docker rmi -f $(docker images | grep fabcar | awk '{print $3}')
rm -rf ./hfc-key-store

2、运行first-network案例网络

可以参照之前的博客手动运行,这里方便起见直接使用提供的脚本运行。

./byfn.sh up -a -n -s couchdb

3、安装链码依赖(JS版链码为例)

cd chaincode/fabcar/javascript
npm install

4、在peer0.org1.example.com上打包链码

官方提供的脚本中为了方便起见,将peer命令后面都设置了提交到orderer的参数,实际有些操作并不需要,故后面可能部分和官方脚本略有不同。注意--path指定链码的存储路径,docker-compose-cli.yaml文件中有卷的映射关系./..chaincode/:/opt/gopath/src/github.com/hyperledger/fabric-samples/chaincode,因此主机的fabric-samples/chaincode目录对应着peer容器的/opt/gopath/src/github.com/hyperledger/fabric-samples/chaincode目录。

#进入cli容器终端
docker exec -it cli bash
#导入peer0.org1.example.com环境变量
CORE_PEER_LOCALMSPID=Org1MSP
CORE_PEER_ADDRESS=peer0.org1.example.com:7051
CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/[email protected]/msp
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.org1.example.com上打包链码
peer lifecycle chaincode package fabcar.tar.gz --path /opt/gopath/src/github.com/hyperledger/fabric-samples/chaincode/fabcar/javascript --lang node --label fabcarv1

5、在不同节点安装链码

这里要注意一下,因为我们是在所有的节点上都安装相同的链码,因此只打包一次,通过更改环境变量安装到4个peer节点上,同样后面也只设置一次链码ID。

peer lifecycle chaincode install fabcar.tar.gz

CORE_PEER_LOCALMSPID=Org1MSP
CORE_PEER_ADDRESS=peer1.org1.example.com:8051
CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/[email protected]/msp
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

peer lifecycle chaincode install fabcar.tar.gz


CORE_PEER_LOCALMSPID=Org2MSP
CORE_PEER_ADDRESS=peer0.org2.example.com:9051
CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/[email protected]/msp
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

peer lifecycle chaincode install fabcar.tar.gz

CORE_PEER_LOCALMSPID=Org2MSP
CORE_PEER_ADDRESS=peer1.org2.example.com:10051
CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/[email protected]/msp
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

peer lifecycle chaincode install fabcar.tar.gz

6、查询已安装的链码,将链码ID设置为环境变量

链码ID在不同主机上生成的是不一样的,因此根据查询结果自行设置。

peer lifecycle chaincode queryinstalled
CC_PACKAGE_ID=fabcarv1:2967ce2aca225ca89f2049b7549957e841cc0621c4d0aa41adcba66bbae36e0c

7、各组织批准链码定义

切换peer节点的环境变量,多次执行下面的命令,所有组织各自在其组织层面批准链码(只需要每个组织一个peer执行即可)。

CORE_PEER_LOCALMSPID=Org1MSP
CORE_PEER_ADDRESS=peer0.org1.example.com:7051
CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/[email protected]/msp
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

peer --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 --orderer=orderer.example.com:7050 lifecycle chaincode approveformyorg --package-id $CC_PACKAGE_ID --channelID mychannel --name fabcar --version 1.0 --signature-policy "AND('Org1MSP.member','Org2MSP.member')" --sequence 1 --waitForEvent

CORE_PEER_LOCALMSPID=Org2MSP
CORE_PEER_ADDRESS=peer0.org2.example.com:9051
CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/[email protected]/msp
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

peer --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 --orderer=orderer.example.com:7050 lifecycle chaincode approveformyorg --package-id $CC_PACKAGE_ID --channelID mychannel --name fabcar --version 1.0 --signature-policy "AND('Org1MSP.member','Org2MSP.member')" --sequence 1 --waitForEvent

8、将链码定义提交到通道

通过checkcommitreadiness命令可以查看各组织是否批准了相同的链码定义,如果相同,就可以将链码定义提交到通道了(只需要任何组织的某个节点执行一次即可),可以通过querycommitted查询通道上实例化的链码定义。

peer --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 --orderer=orderer.example.com:7050 lifecycle chaincode checkcommitreadiness --channelID mychannel --name fabcar --version 1.0 --signature-policy "AND('Org1MSP.member','Org2MSP.member')" --sequence 1 --output json

peer --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 --orderer=orderer.example.com:7050 lifecycle chaincode commit --channelID mychannel --name fabcar --version 1.0 --signature-policy "AND('Org1MSP.member','Org2MSP.member')" --sequence 1 --waitForEvent --peerAddresses peer0.org1.example.com:7051 --peerAddresses peer0.org2.example.com:9051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt

peer lifecycle chaincode querycommitted --channelID mychannel --name fabcar

9、调用链码初始化函数和query函数

peer --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 --orderer=orderer.example.com:7050 chaincode invoke -C mychannel -n fabcar -c '{"function":"initLedger","Args":[]}' --waitForEvent --waitForEventTimeout 300s --peerAddresses peer0.org1.example.com:7051 --peerAddresses peer0.org2.example.com:9051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt

CORE_PEER_LOCALMSPID=Org1MSP
CORE_PEER_ADDRESS=peer1.org1.example.com:8051
CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/[email protected]/msp
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

peer --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 --orderer=orderer.example.com:7050 chaincode query -C mychannel -n fabcar -c '{"function":"queryAllCars","Args":[]}' --peerAddresses peer1.org1.example.com:8051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt

10、运行成功截图如下

 

你可能感兴趣的:(Hyperledger,Fabric)