Smart contract practice
1. Start the containers on each machine
Enter the scripts directory in 192.168.73.10 and execute Chmod + x up SH adds execution permission to the file, and then/ up.sh,
Then execute the above two commands on 92.168.73.20/30/40/50 respectively, and the fabric network is started. Next, use cli tool instead of client to interact with fabric network
192.168.73.10/20/30/40/50: wang@wang:~/raft-network/scripts$ ./down.sh wang@wang:~/raft-network/scripts$ ./up.sh
2. Create application channel (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 join channel (192.168.73.10)
bash-5.0# peer channel join -b testchannel.block
4. Copy the creation block generated by the channel created above from the container for other nodes to join the channel (192.168.73.10):
wang@wang:~/raft-network$ docker cp cli:/opt/gopath/src/github.com/hyperledger/fabric/peer/testchannel.block ./channel-artifacts
5. Put testchanel Block can be remotely copied to the same directory on 192.168.73.20/30/40, or the whole directory can be copied (192.168.73.10):
wang@wang:~/raft-network$ scp -r channel-artifacts/ wang@192.168.73.20:/home/wang/raft-network wang@wang:~/raft-network$ scp -r channel-artifacts/ wang@192.168.73.30:/home/wang/raft-network wang@wang:~/raft-network$ scp -r channel-artifacts/ wang@192.168.73.40:/home/wang/raft-network wang@wang:~/raft-network$ scp -r channel-artifacts/ wang@192.168.73.50:/home/wang/raft-network
6. Other machines enter the cli container and the creation block file testchnel. In channel artifacts Move block to / opt / gopath / SRC / GitHub COM / hyperledger / fabric / peer directory (I'm not sure whether this step is necessary. It's OK to specify the location of the creation block when adding the channel later, but there will certainly be no problem when adding the channel after moving), the node joins the channel. The machines involved include 192.168.73.20/30/40, 50 which is unnecessary:
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
Next, install the smart contract (which machine to install is customized, here is the full installation)
7. Change the go environment variable (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. Enter the container and change the go environment variable again (there will be problems when only one later packing chain code is changed in 7 and 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. After packaging the smart contract, mycc2 appears in the directory after packaging tar. GZ file (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. Node deployment contract (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. Verify that the contract installation is installed to the node (192.168.73.10):
bash-5.0# peer lifecycle chaincode queryinstalled Installed chaincodes on peer: Package ID: mycc_2:8a1774929663dc91fbcb53e810c04971f878d2da2e7de3547cb67a9cfba40066, Label: mycc_2
12. The current organization agrees to the contract definition (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. Check whether the contract definition meets the policy (192.168.73.10):
fabric2.1. The deployment of smart contracts must meet certain contract definition policies, which are specifically defined in channel configuration. As can be seen from the following, the current contract has not obtained the consent of Org1MSP or Org2MSP. You can wait for peer1org1, peer0org2, and peer1org2 to approve, and then execute this command to check whether the policy is met.
bash-5.0# peer lifecycle chaincode checkcommitreadiness --channelID testchannel --name mycc2 --version 1 --sequence 1 --output json --init-required { "approvals": { "Org1MSP": false, "Org2MSP": false } }
Check the original definition of lifecycle policy, which is to meet more than half of the requirements:
14. If the endorsement policy is not satisfied, the subsequent steps, such as submitting the chain code, will not succeed. Therefore, other peer s will package and install the chain code, and the current organization agrees to the contract definition.
(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 } }
Now that the endorsement strategy has been met (more than half agree), the chain code can be submitted.
15. Submit smart contract (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. View the contract submitted by the node (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. Operation contract
(1) Initialize the contract, execute the init method, and set 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) Query a's balance (192.168.73.10):
bash-5.0# peer chaincode query -C testchannel -n mycc2 -c '{"Args":["query","a"]}' 100
(3) Transfer from a to 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) Query a's balance (192.168.73.10 / 20 / 30 / 40):
bash-5.0# peer chaincode query -C testchannel -n mycc2 -c '{"Args":["query","a"]}' 80