启动测试网络
#1.拉代码
git clone https://github.com/hyperledger/fabric-samples.git
#2.进入目录
cd fabric-samples/test-network
# 启动网络并创建通道
./network.sh up createChannel
将这些二进制文件添加到您的CLI路径:
export PATH=${PWD}/../bin:$PATH
导入环境变量
设置org1,org2
的环境变量
export FABRIC_CFG_PATH=$PWD/../config/
# Environment variables for Org1
export CORE_PEER_TLS_ENABLED=true
export CORE_PEER_LOCALMSPID="Org1MSP"
export CORE_PEER_TLS_ROOTCERT_FILE=${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt
export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org1.example.com/users/[email protected]/msp
export CORE_PEER_ADDRESS=localhost:7051
# Environment variables for Org2
export FABRIC_CFG_PATH=$PWD/../config/
export CORE_PEER_TLS_ENABLED=true
export CORE_PEER_LOCALMSPID="Org2MSP"
export CORE_PEER_TLS_ROOTCERT_FILE=${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt
export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org2.example.com/users/[email protected]/msp
export CORE_PEER_ADDRESS=localhost:9051
部署链码
如果未来使用在离线环境内,可以运行下面代码,链码如果没有vendor
需要运行以下命令:
GO111MODULE=on
go mod vendor
回到 test-network
目录下进行链码打包
peer lifecycle chaincode package basic.tar.gz --path ../asset-transfer-basic/chaincode-go/ --lang golang --label basic_1.0
安装链码
peer lifecycle chaincode install basic.tar.gz
查看链码安装状态
peer lifecycle chaincode queryinstalled
## 出现下面信息代表安装成功
Installed chaincodes on peer:
Package ID: basic_1.0:56069c8c46fe01a7837a218a6e2ab49a2b6c4a715c95ab2ab321a863b642d021, Label: basic_1.0
approve
设置链码包环境变量 (org1,org2)
我们将在批准链代码时使用CC_PACKAGE_ID
,因此让我们继续并将其保存为环境变量。将 返回的CC_PACKAGE_ID
粘贴到下面的命令中。注意:所有用户的CC_PACKAGE_ID
都不相同,因此您需要使用上一步从命令窗口返回的CC_PACKAGE_ID
完成此步骤。
export CC_PACKAGE_ID=basic_1.0:56069c8c46fe01a7837a218a6e2ab49a2b6c4a715c95ab2ab321a863b642d021
由于环境变量已设置为peer以 Org2 管理员身份运行 CLI,因此我们可以批准 basic
的链码定义为 Org2。Chaincode 在组织级别获得批准,因此该命令只需要针对一个对等方。使用gossip
将批准分发给组织内的其他同行。使用peer lifecycle chaincode approveformyorg命令批准链代码定义:
peer lifecycle chaincode approveformyorg -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --channelID mychannel --name basic --version 1.0 --package-id $CC_PACKAGE_ID --sequence 1 --tls --cafile ${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
将链代码定义提交到通道 commit
在足够数量的组织批准链码定义后,一个组织可以将链码定义提交到通道。如果大多数通道成员批准了定义,则提交交易将成功,并且链代码定义中约定的参数将在通道上实现。
您可以使用peer lifecycle chaincode checkcommitreadiness命令来检查通道成员是否已批准相同的链代码定义。用于该checkcommitreadiness命令的标志与用于为您的组织批准链代码的标志相同。但是,您不需要包括--package-id
标志。
peer lifecycle chaincode checkcommitreadiness --channelID mychannel --name basic --version 1.0 --sequence 1 --tls --cafile ${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem --output json
主要是检查所有组织的授权状态,显示如下返回值:
{
"approvals": {
"Org1MSP": true,
"Org2MSP": true
}
}
由于作为通道成员的两个组织都批准了相同的参数,因此链代码定义已准备好提交给通道。您可以使用peer lifecycle chaincode commit命令将链代码定义提交到通道。提交命令也需要由组织管理员提交。(org1或org2)
peer lifecycle chaincode commit -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --channelID mychannel --name basic --version 1.0 --sequence 1 --tls --cafile ${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem --peerAddresses localhost:7051 --tlsRootCertFiles ${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt --peerAddresses localhost:9051 --tlsRootCertFiles ${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt
使用peer lifecycle chaincode querycommitted命令来确认链码定义已提交到通道。
peer lifecycle chaincode querycommitted --channelID mychannel --name basic --cafile ${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
Committed chaincode definition for chaincode 'basic' on channel 'mychannel':
Version: 1.0, Sequence: 1, Endorsement Plugin: escc, Validation Plugin: vscc, Approvals: [Org1MSP: true, Org2MSP: true]
--output json
可以转换成json格式
{
"sequence": 1,
"version": "1.0",
"endorsement_plugin": "escc",
"validation_plugin": "vscc",
"validation_parameter": "EiAvQ2hhbm5lbC9BcHBsaWNhdGlvbi9FbmRvcnNlbWVudA==",
"collections": {},
"approvals": {
"Org1MSP": true,
"Org2MSP": true
}
}
调用链码
在将链代码定义提交到通道后,链代码将在加入安装链代码的通道的对等节点上启动。basic
链码现在已准备好供客户端应用程序调用。请注意,invoke
命令需要以足够数量的对等点为目标,以满足链代码背书策略。
peer chaincode invoke -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --tls --cafile ${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C mychannel -n basic --peerAddresses localhost:7051 --tlsRootCertFiles ${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt --peerAddresses localhost:9051 --tlsRootCertFiles ${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt -c '{"function":"initLedger","Args":[]}'
如果命令成功,您应该能够收到类似于以下内容的响应:
2022-11-29 11:33:39.636 CST 0001 INFO [chaincodeCmd] chaincodeInvokeOrQuery -> Chaincode invoke successful. result: status:200
查询所有资产
peer chaincode query -C mychannel -n basic -c '{"Args":["getAllAssets"]}'
返回值:
[{"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}]
添加资产
peer chaincode invoke -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --tls --cafile ${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C mychannel -n basic --peerAddresses localhost:7051 --tlsRootCertFiles ${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt --peerAddresses localhost:9051 --tlsRootCertFiles ${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt -c '{"function":"CreateAsset","Args":["asset7", "pink", "80", "Michel", "800"]}'
返回值
2022-11-29 11:51:29.706 CST 0001 INFO [chaincodeCmd] chaincodeInvokeOrQuery -> Chaincode invoke successful. result: status:200
再重新查询一下内容
可以看到新添加的内容就有了
[{"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":800,"Color":"pink","ID":"asset7","Owner":"Michel","Size":80}]
升级链码
重新打包链
peer lifecycle chaincode package basic.2.0.0.tar.gz --path ../asset-transfer-basic/chaincode-go/ --lang golang --label basic_2.0标签:--,chaincode,fabirc,ID,开发,basic,com,example,搭建 From: https://blog.51cto.com/sleep666/5903566