1、启动各台机器上的容器
进入192.168.73.10中scripts目录,执行chmod +x up.sh为文件添加执行权限,再./up.sh,
再在92.168.73.20/30/40/50上分别执行上面的两个命令,fabric的网络就启动完毕了,接下来用cli工具代替客户端去和fabric网络进行交互
192.168.73.10/20/30/40/50:
wang@wang:~/raft-network/scripts$ ./down.sh
wang@wang:~/raft-network/scripts$ ./up.sh
2、创建应用通道(192.168.73.10):
wang@wang:~/raft-network/scripts$ docker exec -it cli bash
bash-5.0# peer channel create -o orderer.example.com:7050 -c testchannel -f ./channel-artifacts/channel.tx --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
3、peer0org1加入通道(192.168.73.10)
bash-5.0# peer channel join -b testchannel.block
4、将上面创建通道生成的创世块从容器中拷贝出来,供其他节点加入通道使用(192.168.73.10):
wang@wang:~/raft-network$ docker cp cli:/opt/gopath/src/github.com/hyperledger/fabric/peer/testchannel.block ./channel-artifacts
5、将testchannel.block远程拷贝到192.168.73.20/30/40上的相同目录下,或者拷贝整个目录也可以(192.168.73.10):
wang@wang:~/raft-network$ scp -r channel-artifacts/ [email protected]:/home/wang/raft-network
wang@wang:~/raft-network$ scp -r channel-artifacts/ [email protected]:/home/wang/raft-network
wang@wang:~/raft-network$ scp -r channel-artifacts/ [email protected]:/home/wang/raft-network
6、其他机器进入到cli容器中,并将channel-artifacts中的创世块文件testchannel.block移动到/opt/gopath/src/github.com/hyperledger/fabric/peer目录下、节点加入通道。涉及的机器包括192.168.73.20/30/40,50就不需要了:
wang@wang:~/raft-network$ docker exec -it cli bash
bash-5.0# mv ./channel-artifacts/testchannel.block /opt/gopath/src/github.com/hyperledger/fabric/peer
bash-5.0# peer channel join -b testchannel.block
接下来安装智能合约(在哪些节点机器上安装自定,这里是全安装了)
7、改一下go环境变量(192.168.73.10):
wang@wang:~/raft-network$ go env -w GO111MODULE=on
wang@wang:~/raft-network$ go env -w GOPROXY=https://goproxy.cn,direct
8、进入容器内再改一下go环境变量(7,8只改动一处在后面打包链码等过程时都会出问题)(192.168.73.10):
wang@wang:~/raft-network$ docker exec -it cli bash
bash-5.0# go env -w GO111MODULE=on
bash-5.0# go env -w GOPROXY=https://goproxy.cn,direct
bash-5.0# go env
9、打包智能合约,打包完目录下出现 mycc2.tar.gz 文件(192.168.73.10):
bash-5.0# peer lifecycle chaincode package mycc2.tar.gz --path /opt/gopath/src/github.com/chaincode/go/ --lang golang --label mycc_2
bash-5.0# ls
channel-artifacts crypto mycc2.tar.gz testchannel.block
10、节点部署合约(192.168.73.10):
bash-5.0# peer lifecycle chaincode install mycc2.tar.gz
2020-08-07 08:01:45.578 UTC [cli.lifecycle.chaincode] submitInstallProposal -> INFO 001 Installed remotely: response:<status:200 payload:"\nGmycc_2:8a1774929663dc91fbcb53e810c04971f878d2da2e7de3547cb67a9cfba40066\022\006mycc_2" >
2020-08-07 08:01:45.601 UTC [cli.lifecycle.chaincode] submitInstallProposal -> INFO 002 Chaincode code package identifier: mycc_2:8a1774929663dc91fbcb53e810c04971f878d2da2e7de3547cb67a9cfba40066
11、验证合约安装是否安装到节点(192.168.73.10):
bash-5.0# peer lifecycle chaincode queryinstalled
Installed chaincodes on peer:
Package ID: mycc_2:8a1774929663dc91fbcb53e810c04971f878d2da2e7de3547cb67a9cfba40066, Label: mycc_2
12、 当前组织同意合约定义(192.168.73.10):
bash-5.0# peer lifecycle chaincode approveformyorg --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 --channelID testchannel --name mycc2 --version 1 init-required --package-id mycc_2:8a1774929663dc91fbcb53e810c04971f878d2da2e7de3547cb67a9cfba40066 --sequence 1 --waitForEvent
13、检查合约定义是否满足策略(192.168.73.10):
fabric2.1的智能合约的部署必须满足一定合约定义策略,策略的定义在通道配置中具体定义。如下可以得知当前合约没有获取Org1的同意,也没得到Org2的同意。可以等peer1org1、peer0org2、peer1org2进行approve之后或中途,再进行此命令查看是否满足策略。
bash-5.0# peer lifecycle chaincode checkcommitreadiness --channelID testchannel --name mycc2 --version 1 --sequence 1 --output json --init-required
{
"approvals": {
"Org1MSP": false,
"Org2MSP": false
}
}
查看原本的lifecycle策略定义是满足“大多数”即可:
14、不满足背书策略的情况下进行后面的步骤提交链码也是不会成功的,所以先其他peer进行链码的打包、安装、当前组织同意合约定义等步骤。
(1)192.168.73.20:
wang@wang:~/scripts$ go env -w GO111MODULE=on
wang@wang:~/scripts$ go env -w GOPROXY=https://goproxy.cn,direct
bash-5.0# go env -w GO111MODULE=on
bash-5.0# go env -w GOPROXY=https://goproxy.cn,direct
bash-5.0# peer lifecycle chaincode package mycc2.tar.gz --path /opt/gopath/src/github.com/chaincode/go/ --lang golang --label mycc_2
bash-5.0# peer lifecycle chaincode install mycc2.tar.gz
bash-5.0# peer lifecycle chaincode queryinstalled
bash-5.0# peer lifecycle chaincode approveformyorg --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 --channelID testchannel --name mycc2 --version 1 --init-required --package-id mycc_2:8a1774929663dc91fbcb53e810c04971f878d2da2e7de3547cb67a9cfba40066 --sequence 1 --waitForEvent
bash-5.0# peer lifecycle chaincode checkcommitreadiness --channelID testchannel --name mycc2 --version 1 --sequence 1 --output json --init-required
{
"approvals": {
"Org1MSP": true,
"Org2MSP": false
}
}
(2)192.168.73.30:
wang@wang:~/scripts$ go env -w GO111MODULE=on
wang@wang:~/scripts$ go env -w GOPROXY=https://goproxy.cn,direct
bash-5.0# go env -w GO111MODULE=on
bash-5.0# go env -w GOPROXY=https://goproxy.cn,direct
bash-5.0# peer lifecycle chaincode package mycc2.tar.gz --path /opt/gopath/src/github.com/chaincode/go/ --lang golang --label mycc_2
bash-5.0# ls
channel-artifacts crypto mycc2.tar.gz testchannel.block
bash-5.0# peer lifecycle chaincode install mycc2.tar.gz
2020-08-07 08:48:15.439 UTC [cli.lifecycle.chaincode] submitInstallProposal -> INFO 001 Installed remotely: response:<status:200 payload:"\nGmycc_2:8a1774929663dc91fbcb53e810c04971f878d2da2e7de3547cb67a9cfba40066\022\006mycc_2" >
2020-08-07 08:48:15.451 UTC [cli.lifecycle.chaincode] submitInstallProposal -> INFO 002 Chaincode code package identifier: mycc_2:8a1774929663dc91fbcb53e810c04971f878d2da2e7de3547cb67a9cfba40066
bash-5.0# peer lifecycle chaincode queryinstalled
Installed chaincodes on peer:
Package ID: mycc_2:8a1774929663dc91fbcb53e810c04971f878d2da2e7de3547cb67a9cfba40066, Label: mycc_2
bash-5.0# peer lifecycle chaincode approveformyorg --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 --channelID testchannel --name mycc2 --version 1 --init-required --package-id mycc_2:8a1774929663dc91fbcb53e810c04971f878d2da2e7de3547cb67a9cfba40066 --sequence 1 --waitForEvent
2020-08-07 08:52:04.431 UTC [cli.lifecycle.chaincode] setOrdererClient -> INFO 001 Retrieved channel (testchannel) orderer endpoint: orderer.example.com:7050
2020-08-07 08:52:07.368 UTC [chaincodeCmd] ClientWait -> INFO 002 txid [55ab44f95633d4a3d568387ff578d96ac203eb9c2a6e1eea6a724f0f933ade47] committed with status (VALID) at
bash-5.0# peer lifecycle chaincode checkcommitreadiness --channelID testchannel --name mycc2 --version 1 --sequence 1 --output json --init-required
{
"approvals": {
"Org1MSP": true,
"Org2MSP": true
}
}
到此已经满足背书策略(过半数同意)可以提交链码了。
15、提交智能合约(192.168.73.10):
bash-5.0# peer lifecycle chaincode commit -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 --channelID testchannel --name mycc2 --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 --version 1 --sequence 1 --init-required
2020-08-07 09:15:05.480 UTC [chaincodeCmd] ClientWait -> INFO 001 txid [a6f1f75f9978bd016036cbcfe8a213334eb4eda48b303b956a0594a6e606a440] committed with status (VALID) at peer0.org1.example.com:7051
2020-08-07 09:15:05.861 UTC [chaincodeCmd] ClientWait -> INFO 002 txid [a6f1f75f9978bd016036cbcfe8a213334eb4eda48b303b956a0594a6e606a440] committed with status (VALID) at peer0.org2.example.com:7051
16、查看节点已提交合约(192.168.73.10):
bash-5.0# peer lifecycle chaincode querycommitted --channelID testchannel --name mycc2
Committed chaincode definition for chaincode 'mycc2' on channel 'testchannel':
Version: 1, Sequence: 1, Endorsement Plugin: escc, Validation Plugin: vscc, Approvals: [Org1MSP: true, Org2MSP: true]
17、操作合约
(1)初始化合约,执行init方法,设置a:100 b:100(192.168.73.10):
bash-5.0# 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 testchannel -n mycc2 --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 --isInit -c '{"Args":["Init","a","100","b","100"]}'
2020-08-07 09:23:47.441 UTC [chaincodeCmd] chaincodeInvokeOrQuery -> INFO 001 Chaincode invoke successful. result: status:200
(2)查询a的余额(192.168.73.10):
bash-5.0# peer chaincode query -C testchannel -n mycc2 -c '{"Args":["query","a"]}'
100
(3)a向b转账20(192.168.73.10):
bash-5.0# 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 testchannel -n mycc2 --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":["invoke","a","b","20"]}'
(4)查询a的余额(192.168.73.10/20/30/40):
bash-5.0# peer chaincode query -C testchannel -n mycc2 -c '{"Args":["query","a"]}'
80