https://blog.csdn.net/qq_28540443/article/details/104318163
在2.1.2 部署合约到节点和2.1.3 当前组织同意合约定义中,智能合约的ID要一致。同时在2.1.5 提交合约中,通道ID要与我们通道配置文件产生的通道名相一致。
1. 验证智能合约是否成功部署到节点
peer lifecycle chaincode queryinstalled
产生如下智能合约的ID,表示部署成功
Installed chaincodes on peer:
Package ID: mycc_2:b81495f08c3450043c81a07c928da5a77c1a4965b3c9a453ef33d5bce791b7cf, Label: mycc_2
其中,智能合约的ID要进行记录,也可以写入环境变量,无需重复复制。
2. 令当前组织同意合约定义
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 channel2 --name mycc2 --version 1 --init-required --package-id mycc_2:b81495f08c3450043c81a07c928da5a77c1a4965b3c9a453ef33d5bce791b7cf --sequence 1 --waitForEvent
这里合约ID要与上述查询到的合约ID相一致
3. 提交合约
我们前述产生的通道名为channel2,且智能合约名为mycc2,在以下代码中要注意保持一致性。
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 channel2 --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:9051 --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
4. 查看节点已提交合约
通道名和合约名前后要保持一致性,cli容器内输入以下命令
peer lifecycle chaincode querycommitted --channelID channel2 --name mycc2
5. 操作合约
fabric智能合约操作主要有invoke跟query,此处与1.x完全一致。
初始化合约,执行init方法,设置a:100 b:100。通道名和合约名要保持一致性。
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 channel2 -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:9051 --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"]}'
查询a的余额
peer chaincode query -C channel2 -n mycc2 -c '{"Args":["query","a"]}'
a向b传输10
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 channel2 -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:9051 --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","10"]}'