导读
1
说在前面的话
1、说点题外话
2、环境测试
1、区块链环境测试
在今天的内容之前,我们还是要先测试一下我们的环境,方便后续的学习。我们检查测试网络是否存在:
./network.sh up createChannel
./network.sh deployCC -ccn basic -ccp ../asset-transfer-basic/chaincode-go -ccl go
如果没有问题,那就说明我们的环境基本正常,大家也可以看上一篇文章做更加深入的测试:
区块链 | Hyperledger Fabric 03 超详细图解——通过Fabric测试网络深入理解联盟链
确认没问题后,就可以关闭网络了:
./network.sh down
2、网络环境测试
我们要确保系统是联网的,成功连接的前提是有连接的标志,比如:
最简单的方法是打开系统自带的火狐浏览器,然后打开网页测试:
也可以使用命令行(显得更专业吧)!
如果网络连接有问题,就要解决,我们需要考虑我们的Ubuntu系统是独立系统,还是安装在虚拟机中的。更多的问题,可能是出现在虚拟机中,可能的原因有如下:
1、首先确保网络没有问题。网络出现问题,
(1)有可能是网本身连不上;
(2)有可能是电脑的网卡驱动出现问题;
(3)某些网络不支持虚拟机,如锐捷,上网有解决方案;
2、网络没问题,要配置好虚拟机的网络,一般选择NAT
3、Ubuntu的配置问题
(1)检查NetworkManager.state
(2)检查NetworkManager.conf
关于后两点,具体如下:
首先关闭网络管理:
sudo service network-manager stop
然后使用如下命令打开NetworkManager.state:
sudo gedit /var/lib/NetworkManager/NetworkManager.state
将所有的都改为true:
同理,打开NetworkManager.conf:
sudo gedit /etc/NetworkManager/NetworkManager.conf
将其中的managed=false改为managed=true:
然后我们启动:
sudo service network-manager start
或者重启:
sudo service network-manager restart
网络问题可能的原因有很多,我们只能对症下药,但是大多数都是上面的几种情况,有出现其他问题解决不了,我们可以一起交流。
测试完成后,确保都没问题后,我们就可以学习今天的内容啦!
2
启动与配置
1、简述
区块链 | 智能合约
2、启动网络
./network.sh up createChannel
3、设置Logspout
首先要说明的是,这个操作不是必须的,如果大家仅仅是学习简单的开发与应用,可以跳过。
为了监控智能合约的日志,管理员可以使用logspout工具查看来自一组Docker容器的聚合输出。该工具将输出流从不同的Docker容器收集到一个地方,从而轻松查看单个窗口发生了什么。这可以帮助管理员在调用智能合约时安装智能合约或开发人员时调试问题。由于某些容器的创建纯粹是为了启动智能合约,并且仅在短时间内存在,因此从网络中收集所有日志是有帮助的。
我们打开test-network文件夹(不确定路径看下图),会发现有这个文件的存在:
如果这个文件夹下没有,我们可以打开如下路径:
~/HyperledgerFabric2.3/fabric/scripts/fabric-samples/commercial-paper/organization/digibank/configuration/cli
具体的大家跟我会有一定的差别,去fabric-samples文件夹下面按照路径寻找即可。
如果还是没有,可以直接使用如下的命令去查找:
find . -name monitordocker.sh
在根目录打开终端,然后输入上面的命令,就可以找到啦:
monitordocker.sh这个文件是用于安装和配置logSpout的脚本,我们可以直接使用。LogSpout工具会将连续将日志输出到终端,方便起见,我们在该文件夹下创建一个新的终端窗口。
然后再第二个终端(新创建的终端)启动:
./monitordocker.sh fabric_test
这里的网络是我们前面创建的网络:
执行结果如下:
刚开始,不会看到任何日志,后续部署的过程中,会有显示。
3
链码部署
做完了上面的,我们就可以正式开始了。我们的部署包括如下四个阶段:
打包智能合约
安装链码包
批准链码定义
提交链码定义到通道
1、打包智能合约
cd ../chaincode/fabcar/go
cat go.mod
GO111MODULE=on go mod vendor
cd ../../../test-network
export PATH=${PWD}/../bin:$PATH
export FABRIC_CFG_PATH=$PWD/../config/
peer version
peer lifecycle chaincode package fabcar.tar.gz --path ../chaincode/fabcar/go/ --lang golang --label fabcar_1
peer lifecycle chaincode package fabcar.tar.gz
--path ../chaincode/fabcar/go/
--lang golang
--label fabcar_1
2、安装链码包
peer0.org1.example.com
peer0.org2.example.com
我们依次在Org1和Org2安装链码,首先在Org1中安装,设置如下环境变量:
export CORE_PEER_TLS_ENABLED=true
export CORE_PEER_LOCALMSPID="Org1MSP"
export CORE_PEER_TLS_ROOTCERT_FILE=${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt
export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org1.example.com/users/[email protected]/msp
export CORE_PEER_ADDRESS=localhost:7051
其中,第三条命令和和第四条命令表明指向了Org1。
然后我们执行安装过程:
peer lifecycle chaincode install fabcar.tar.gz
同理,使用如下命令将链码安装到Org2:
export CORE_PEER_LOCALMSPID="Org2MSP"
export CORE_PEER_TLS_ROOTCERT_FILE=${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt
export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org2.example.com/users/[email protected]/msp
export CORE_PEER_ADDRESS=localhost:9051
peer lifecycle chaincode install fabcar.tar.gz
特别说明一下,我们前面有安装工具logSpout来监控智能合约日志,我们可以在我们创建的另一个终端查看安装过程:
3、批准链码定义
Application/Channel/lifeycleEndorsement
peer lifecycle chaincode queryinstalled
export CC_PACKAGE_ID=fabcar_1:1146b4b491871bf18b23dd67dd8cc058655b36cc0e2274f165ed06b796a8f27
peer lifecycle chaincode approveformyorg
peer lifecycle chaincode approveformyorg -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --channelID mychannel --name fabcar --version 1.0 --package-id $CC_PACKAGE_ID --sequence 1 --tls --cafile ${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
peer lifecycle chaincode approveformyorg
-o localhost:7050
--ordererTLSHostnameOverride orderer.example.com
--channelID mychannel
--name fabcar
--version 1.0
--package-id $CC_PACKAGE_ID
--sequence 1
--tls
--cafile ${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
export CORE_PEER_LOCALMSPID="Org1MSP"
export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org1.example.com/users/[email protected]/msp
export CORE_PEER_TLS_ROOTCERT_FILE=${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt
export CORE_PEER_ADDRESS=localhost:7051
peer lifecycle chaincode approveformyorg -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --channelID mychannel --name fabcar --version 1.0 --package-id $CC_PACKAGE_ID --sequence 1 --tls --cafile ${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
4、提交链码定义到通道
peer lifecycle chaincode checkcommitreadiness
peer lifecycle chaincode checkcommitreadiness --channelID mychannel --name fabcar --version 1.0 --sequence 1 --tls --cafile ${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem --output json
peer lifecycle chaincode commit
peer lifecycle chaincode commit -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --channelID mychannel --name fabcar --version 1.0 --sequence 1 --tls --cafile ${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem --peerAddresses localhost:7051 --tlsRootCertFiles ${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt --peerAddresses localhost:9051 --tlsRootCertFiles ${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt
peer lifecycle chaincode querycommitted
peer lifecycle chaincode querycommitted --channelID mychannel --name fabcar --cafile ${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
Committed chaincode definition for chaincode 'fabcar' on channel 'mychannel':
Version: 1.0, Sequence: 1, Endorsement Plugin: escc, Validation Plugin: vscc, Approvals: [Org1MSP: true, Org2MSP: true]
4
后续操作
1、调用链码
peer chaincode invoke -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --tls --cafile ${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C mychannel -n fabcar --peerAddresses localhost:7051 --tlsRootCertFiles ${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt --peerAddresses localhost:9051 --tlsRootCertFiles ${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt -c '{"function":"initLedger","Args":[]}'
2022-06-22 22:45:55.177 CST [chaincodeCmd] chaincodeInvokeOrQuery -> INFO 001 Chaincode invoke successful. result: status:200
[{"Key":"CAR0","Record":{"make":"Toyota","model":"Prius","colour":"blue","owner":"Tomoko"}},
{"Key":"CAR1","Record":{"make":"Ford","model":"Mustang","colour":"red","owner":"Brad"}},
{"Key":"CAR2","Record":{"make":"Hyundai","model":"Tucson","colour":"green","owner":"Jin Soo"}},
{"Key":"CAR3","Record":{"make":"Volkswagen","model":"Passat","colour":"yellow","owner":"Max"}},
{"Key":"CAR4","Record":{"make":"Tesla","model":"S","colour":"black","owner":"Adriana"}},
{"Key":"CAR5","Record":{"make":"Peugeot","model":"205","colour":"purple","owner":"Michel"}},
{"Key":"CAR6","Record":{"make":"Chery","model":"S22L","colour":"white","owner":"Aarav"}},
{"Key":"CAR7","Record":{"make":"Fiat","model":"Punto","colour":"violet","owner":"Pari"}},
{"Key":"CAR8","Record":{"make":"Tata","model":"Nano","colour":"indigo","owner":"Valeria"}},
{"Key":"CAR9","Record":{"make":"Holden","model":"Barina","colour":"brown","owner":"Shotaro"}}]
2、升级链码
sudo apt install npm
cd ../chaincode/fabcar/javascript
npm install
cd ../../../test-network
export PATH=${PWD}/../bin:$PATH
export FABRIC_CFG_PATH=$PWD/../config/
export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org1.example.com/users/[email protected]/msp
peer lifecycle chaincode package fabcar_2.tar.gz --path ../chaincode/fabcar/javascript/ --lang node --label fabcar_2
export CORE_PEER_TLS_ENABLED=true
export CORE_PEER_LOCALMSPID="Org1MSP"
export CORE_PEER_TLS_ROOTCERT_FILE=${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt
export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org1.example.com/users/[email protected]/msp
export CORE_PEER_ADDRESS=localhost:7051
peer lifecycle chaincode install fabcar_2.tar.gz
peer lifecycle chaincode queryinstalled
export NEW_CC_PACKAGE_ID=fabcar_2:510df31a69a460b6f45717a1f5674bbc46ffb8241b8bb269266011491d0c0067
peer lifecycle chaincode approveformyorg -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --channelID mychannel --name fabcar --version 2.0 --package-id $NEW_CC_PACKAGE_ID --sequence 2 --tls --cafile ${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
export CORE_PEER_LOCALMSPID="Org2MSP"
export CORE_PEER_TLS_ROOTCERT_FILE=${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt
export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org2.example.com/users/[email protected]/msp
export CORE_PEER_ADDRESS=localhost:9051
peer lifecycle chaincode install fabcar_2.tar.gz
peer lifecycle chaincode approveformyorg -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --channelID mychannel --name fabcar --version 2.0 --package-id $NEW_CC_PACKAGE_ID --sequence 2 --tls --cafile ${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
peer lifecycle chaincode checkcommitreadiness
peer lifecycle chaincode checkcommitreadiness --channelID mychannel --name fabcar --version 2.0 --sequence 2 --tls --cafile ${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem --output json
peer lifecycle chaincode commit -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --channelID mychannel --name fabcar --version 2.0 --sequence 2 --tls --cafile ${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem --peerAddresses localhost:7051 --tlsRootCertFiles ${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt --peerAddresses localhost:9051 --tlsRootCertFiles ${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
69774e4b697c dev-peer0.org2.example.com-fabcar_2-510df31a69a460b6f45717a1f5674bbc46ffb8241b8bb269266011491d0c0067-54e12ab8ea79f61443ac1f1eaf57157b208d217c9b8d01b2a73322f26446789d "docker-entrypoint.s…" 2 minutes ago Up 2 minutes dev-peer0.org2.example.com-fabcar_2-510df31a69a460b6f45717a1f5674bbc46ffb8241b8bb269266011491d0c0067
2ebbaf3e3c7e dev-peer0.org1.example.com-fabcar_2-510df31a69a460b6f45717a1f5674bbc46ffb8241b8bb269266011491d0c0067-48506a0daaa9cd12e0c03b343531a27c82cb0d957d6f0c60256fdcfafb4a81bc "docker-entrypoint.s…" 2 minutes ago Up 2 minutes dev-peer0.org1.example.com-fabcar_2-510df31a69a460b6f45717a1f5674bbc46ffb8241b8bb269266011491d0c0067
e34803048be0 gliderlabs/logspout "/bin/logspout" 5 hours ago Up 5 hours 127.0.0.1:8000->80/tcp logspout
d521514c2f0e hyperledger/fabric-tools:latest "/bin/bash" 5 hours ago Up 5 hours cli
9a9f2b5b95ed hyperledger/fabric-orderer:latest "orderer" 5 hours ago Up 5 hours 0.0.0.0:7050->7050/tcp, :::7050->7050/tcp, 0.0.0.0:7053->7053/tcp, :::7053->7053/tcp, 0.0.0.0:17050->17050/tcp, :::17050->17050/tcp orderer.example.com
f4f5874c1e75 hyperledger/fabric-peer:latest "peer node start" 5 hours ago Up 5 hours 0.0.0.0:9051->9051/tcp, :::9051->9051/tcp, 7051/tcp, 0.0.0.0:19051->19051/tcp, :::19051->19051/tcp peer0.org2.example.com
d89f2f0db832 hyperledger/fabric-peer:latest "peer node start" 5 hours ago Up 5 hours 0.0.0.0:7051->7051/tcp, :::7051->7051/tcp, 0.0.0.0:17051->17051/tcp, :::17051->17051/
peer chaincode invoke -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --tls --cafile ${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C mychannel -n fabcar --peerAddresses localhost:7051 --tlsRootCertFiles ${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt --peerAddresses localhost:9051 --tlsRootCertFiles ${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt -c '{"function":"createCar","Args":["CAR11","Honda","Accord","Black","Tom"]}'
peer chaincode query -C mychannel -n fabcar -c '{"Args":["queryAllCars"]}'
[{"Key":"CAR0","Record":{"make":"Toyota","model":"Prius","colour":"blue","owner":"Tomoko"}},
{"Key":"CAR1","Record":{"make":"Ford","model":"Mustang","colour":"red","owner":"Brad"}},
{"Key":"CAR11","Record":{"color":"Black","docType":"car","make":"Honda","model":"Accord","owner":"Tom"}},
{"Key":"CAR2","Record":{"make":"Hyundai","model":"Tucson","colour":"green","owner":"Jin Soo"}},
{"Key":"CAR3","Record":{"make":"Volkswagen","model":"Passat","colour":"yellow","owner":"Max"}},
{"Key":"CAR4","Record":{"make":"Tesla","model":"S","colour":"black","owner":"Adriana"}},
{"Key":"CAR5","Record":{"make":"Peugeot","model":"205","colour":"purple","owner":"Michel"}},
{"Key":"CAR6","Record":{"make":"Chery","model":"S22L","colour":"white","owner":"Aarav"}},
{"Key":"CAR7","Record":{"make":"Fiat","model":"Punto","colour":"violet","owner":"Pari"}},
{"Key":"CAR8","Record":{"make":"Tata","model":"Nano","colour":"indigo","owner":"Valeria"}},
{"Key":"CAR9","Record":{"make":"Holden","model":"Barina","colour":"brown","owner":"Shotaro"}}]
2、清理链码
docker stop logspout
docker rm logspout
./network.sh down
5
说在后面的话
到这里,我们就了解了如何部署智能合约到通道,虽然只是一件事,但是内容很多,将近两万字,包括了很多重要的操作,需要大家一步一步认认真真去做。遇到困难不可怕,教员的诗词说的好,世上无难事,只要肯登攀。只要我们用科学的方法,认真去做,一定能够达到我们的目的!
一起加油啦!
长按二维码关注
点赞和在看数你最好看
文章评论