1、加载智能合约(以 211 机器为例)
拷贝~/fabric/examples/chaincode/go/
文件夹下的example02
及marbles02
两个目录到~/fabric/aberic/chaincode/go/
目录下。
重启 Peer 服务。
docker-compose -f docker-peer0.org1.yaml down --volumes
docker-compose -f docker-peer0.org1.yaml up
2、安装实例化合约
登入 211 机器的 cli 容器。
docker exec -it cli bash
由于刚刚我们彻底清除了 peer 容器,所以这里需要重新加入通道。
cp channel-artifacts/mychannel.block ./
peer channel join -b mychannel.block
安装实例化合约
peer chaincode install -n mycc -v 1.0 -p github.com/hyperledger/fabric/aberic/chaincode/go/example02/cmd
peer chaincode instantiate -o orderer1.example.com:7050 --tls --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer1.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C mychannel -n mycc -v 1.0 -c '{"Args":["init","a","100","b","200"]}' -P "OR ('Org1MSP.peer','Org2MSP.peer')"
3、查询、交易、验证合约
peer chaincode query -C mychannel -n mycc -c '{"Args":["query","a"]}'
peer chaincode invoke -o orderer1.example.com:7050 --tls --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer1.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 -c '{"Args":["invoke","a","b","10"]}'
peer chaincode query -C mychannel -n mycc -c '{"Args":["query","a"]}'
4、验证 CouchDB
安装、实例化 marbles02 合约,该合约会自动基于 couchdb 创建索引。
peer chaincode install -n marble1 -v 1.0 -p github.com/hyperledger/fabric/aberic/chaincode/go/marbles02
实例化若失败,可稍等几秒钟,可能是安装还没有完成。
peer chaincode instantiate -o orderer1.example.com:7050 --tls --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer1.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C mychannel -n marble1 -c '{"Args":[]}' -P "OR ('Org1MSP.peer', 'Org2MSP.peer')" -v 1.0
验证合约,若成功,也是变相验证了 couchdb。
peer chaincode invoke -C mychannel -n marble1 -c '{"Args":["initMarble","marble1","blue","35","tom"]}' -o orderer1.example.com:7050 --tls --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer1.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
peer chaincode invoke -C mychannel -n marble1 -c '{"Args":["initMarble","marble2","red","50","tom"]}' -o orderer1.example.com:7050 --tls --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer1.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
peer chaincode invoke -C mychannel -n marble1 -c '{"Args":["initMarble","marble3","blue","70","tom"]}' -o orderer1.example.com:7050 --tls --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer1.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
peer chaincode query -C mychannel -n marble1 -c '{"Args":["readMarble","marble1"]}'
peer chaincode query -C mychannel -n marble1 -c '{"Args":["queryMarblesByOwner","tom"]}' # need couchdb
5、验证 Peer 集群其他节点
复制第一步的加载合约,和第二步的安装合约,注意,不必再行实例化,之后直接进行查询。
后面我们尝试动态增加组织。