这是我搭建的fabric的网络拓扑
3 个 orderer 节点;组织 org1 , org1 下有两个 peer 节点, peer0 和 peer1; 组织 org2 , org2 下有两个 peer 节点, peer0 和 peer1;
以上是我的多机环境的网络拓扑,使用的是docker搭建的。我的网络已经搭建成功,这里主要记录一下多机环境下合约部署与交互的操作,因为是用虚拟机搭建,有时候关机后需要重启dcoek节点,然后创建通道部署链码。关于config和docker-compose的yaml文件不知道怎么部署的可以私信我哦,多机生产网络的搭建有太多的坑了。
1. 启动网络
在两台机器上启动网络
docker-compose -f docker-compose-up.yaml up -d
可以看到我的所有节点都已经起来了,三个orderer,两个组织每个组织包含两个peer节点。
2. 创建通道并加入
2.1 虚拟机1创建通道并加入
进⼊ cli1容器,也就是以 peer0.org1 的⾓⾊与⽹络交互
docker exec -it cli1 bash
创建通道
peer channel create -o orderer0.example.com:7050 -c mychannel -f ./channel-artifacts/channel.tx --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer0.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
组织1 的 peer0 节点加⼊通道 mychannel
peer channel join -b mychannel.block
更新 组织1 的锚节点
peer channel update -o orderer0.example.com:7050 -c mychannel -f ./channel-artifacts/Org1MSPanchors.tx --tls --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer0.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
退出 cli1 容器,从 cli1 容器中拷⻉出 mychannel.block ⽂件到 multiple-deployment ⽂件夹下,并复制到 虚拟机2 中
exit
docker cp cli1:/opt/gopath/src/github.com/hyperledger/fabric/peer/mychannel.block ./
scp -r mychannel.block org2@192.168.5.112:/home/org2/go/src/github.com/hyperledger/fabric-samples/multiple-deployment
查看虚拟机2是否收到:
2.2 虚拟机2加入通道
将 mychannel.block 拷⻉到 虚拟机2 的 cli1 ( 组织2 的 peer0 ⾓⾊)容器中
docker cp mychannel.block cli1:/opt/gopath/src/github.com/hyperledger/fabric/peer/
进⼊ cli1 容器
docker exec -it cli1 bash
ls
加入通道:
peer channel join -b mychannel.block
更新组织 2 的锚节点(主节点)
peer channel update -o orderer0.example.com:7050 -c mychannel -f ./channel-artifacts/Org2MSPanchors.tx --tls --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer0.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
3. 安装调用智能合约
安装的同样是basic链码,在前面对这个链码介绍过,当时在测试网络中进行了部署调用,有兴趣的可以去看看对于合约的讲解。博文
3.1 安装链码
首先是虚拟机1的操作:
# 进入容器
docker exec -it cli1 bash
# 打包链码
peer lifecycle chaincode package mycc.tar.gz --path ../../multiple-deployment/chaincode/go/basic --lang golang --label mycc_1
# 安装链码 安装需要在两个组织都安装,现在只是在虚拟机1
peer lifecycle chaincode install mycc.tar.gz
#控制台信息:
root@b63202ddd2b5:/opt/gopath/src/github.com/hyperledger/fabric/peer# peer lifecycle chaincode install mycc.tar.gz
2023-11-15 02:11:26.585 UTC 0001 INFO [cli.lifecycle.chaincode] submitInstallProposal -> Installed remotely: response:<status:200 payload:"\nGmycc_1:c6ad224cdefc5e53bc261bb6b1fbed12b789225105f59373869b533067c9b009\022\006mycc_1" >
2023-11-15 02:11:26.587 UTC 0002 INFO [cli.lifecycle.chaincode] submitInstallProposal -> Chaincode code package identifier: mycc_1:c6ad224cdefc5e53bc261bb6b1fbed12b789225105f59373869b533067c9b009
注意合约的这个id后面要用
mycc_1:c6ad224cdefc5e53bc261bb6b1fbed12b789225105f59373869b533067c9b009
退出 cli1 容器,将打包的链码 mycc.tar.gz 从 cli1 容器中提取出来,并拷⻉到 虚拟机2的 multiple-deployment ⽬录下
exit
docker cp cli1:/opt/gopath/src/github.com/hyperledger/fabric/peer/mycc.tar.gz ./
scp -r mycc.tar.gz org2@192.168.5.112:/home/org2/go/src/github.com/hyperledger/fabric-samples/multiple-deployment
在虚拟机2查看我们已经收到了发送的打包后的链码
接下来是虚拟机2的操作
退出 cli1 容器,将 multiple-deployment ⽬录下的链码压缩包 mycc.tar.gz 复制到 cli1 ( 组织2 的 peer0 )容器中
exit
docker cp mycc.tar.gz cli1:/opt/gopath/src/github.com/hyperledger/fabric/peer/
# 进入容器查看
docker exec -it cli1 bash
ls
安装链码
peer lifecycle chaincode install mycc.tar.gz
两个组织都已经安装了链码,链码的id应该是一样的:mycc_1:c6ad224cdefc5e53bc261bb6b1fbed12b789225105f59373869b533067c9b009
3.2 授权与提交链码
虚拟机1,再次进⼊ cli1 容器,同意提交链码
docker exec -it cli1 bash
peer lifecycle chaincode approveformyorg --channelID mychannel --name mycc --version 1.0 --init-required --package-id mycc_1:c6ad224cdefc5e53bc261bb6b1fbed12b789225105f59373869b533067c9b009 --sequence 1 --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer0.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
虚拟机2,进⼊ cli1 容器,同意提交链码
peer lifecycle chaincode approveformyorg --channelID mychannel --name mycc --version 1.0 --init-required --package-id mycc_1:c6ad224cdefc5e53bc261bb6b1fbed12b789225105f59373869b533067c9b009 --sequence 1 --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer0.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
虚拟机1:
查看链码状态是否就绪
peer lifecycle chaincode checkcommitreadiness --channelID mychannel --name mycc --version 1.0 --init-required --sequence 1 --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer0.example.com/msp/tlscacerts/tlsca.example.com-cert.pem --output json
提交链码
peer lifecycle chaincode commit -o orderer0.example.com:7050 --channelID mychannel --name mycc --version 1.0 --sequence 1 --init-required --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer0.example.com/msp/tlscacerts/tlsca.example.com-cert.pem --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:7051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt
3.3 与网络进行交互(合约的调用)
3.3.1 初始链码 – 虚拟机1
peer chaincode invoke -o orderer0.example.com:7050 --isInit --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer0.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C mychannel -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:7051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt -c '{"Args":["InitLedger"]}' --waitForEvent
对应的就是完成了下面的代码的功能
我们对账本进行了初始化,并且是在虚拟机1上的操作,现在想要验证一下虚拟机1的操作有没有在网络中进行同步,可以在虚拟机2上查看现在账本上的所有资产,是否是刚才初始化的那些资产。
3.3.2 在虚拟机2上查看一下当前的所有资产
进入容器后
peer chaincode invoke -o orderer0.example.com:7050 --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer0.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C mychannel -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:7051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt -c '{"Args":["GetAllAssets"]}' --waitForEvent
# 控制台输出的资产
{\"AppraisedValue\":300,\"Color\":\"blue\",\"ID\":\"asset1\",\"Owner\":\"Tomoko\",\"Size\":5},{\"AppraisedValue\":400,\"Color\":\"red\",\"ID\":\"asset2\",\"Owner\":\"Brad\",\"Size\":5},{\"AppraisedValue\":500,\"Color\":\"green\",\"ID\":\"asset3\",\"Owner\":\"Jin Soo\",\"Size\":10},{\"AppraisedValue\":600,\"Color\":\"yellow\",\"ID\":\"asset4\",\"Owner\":\"Max\",\"Size\":10},{\"AppraisedValue\":700,\"Color\":\"black\",\"ID\":\"asset5\",\"Owner\":\"Adriana\",\"Size\":15},{\"AppraisedValue\":800,\"Color\":\"white\",\"ID\":\"asset6\",\"Owner\":\"Michel\",\"Size\":15}
通过结果得知,现在我们的操作已经在网络中同步了。
3.3.3 创建一个资产
创建资产CreateAsset(id string, color string, size int, owner string, appraisedValue int)
peer chaincode invoke -o orderer0.example.com:7050 --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer0.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C mychannel -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:7051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt -c '{"Args":["CreateAsset","asset7","red","15","Guandw","1000"]}' --waitForEvent
3.3.4 查看资产
peer chaincode invoke -o orderer0.example.com:7050 --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer0.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C mychannel -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:7051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt -c '{"Args":["GetAllAssets"]}' --waitForEvent
{“AppraisedValue”:300,“Color”:“blue”,“ID”:“asset1”,“Owner”:“Tomoko”,“Size”:5},{“AppraisedValue”:400,“Color”:“red”,“ID”:“asset2”,“Owner”:“Brad”,“Size”:5},
{“AppraisedValue”:500,“Color”:“green”,“ID”:“asset3”,“Owner”:“Jin Soo”,“Size”:10},{“AppraisedValue”:600,“Color”:“yellow”,“ID”:“asset4”,“Owner”:“Max”,“Size”:10},{“AppraisedValue”:700,“Color”:“black”,“ID”:“asset5”,“Owner”:“Adriana”,“Size”:15},{“AppraisedValue”:800,“Color”:“white”,“ID”:“asset6”,“Owner”:“Michel”,“Size”:15},
{\"AppraisedValue\":1000,\"Color\":\"red\",\"ID\":\"asset7\",\"Owner\":\"Guandw\",\"Size\":15}
通过结果我们在虚拟机2创建资产,然后再虚拟机1查看所有资产一样可以看到虚拟机2的操作在网络中同步了。我们的网络搭建也是没有问题的。
4. 关闭网络
如果要结束⽹络,可以在退出 cli1容器 后使⽤以下指令退出
docker-compose -f docker-compose-up.yaml down
如果需要清空容器的volume的话执行,慎重因为清空后无法重启,一般是用来格式化,重新启动容器创建通道
docker system df
docker volume rm $(docker volume ls -qf dangling=true)