Fabric v2.3 手动模拟(四)- 链码开发及部署

本文依赖于上一篇文章创建通道,基于前面的步骤执行安装及部署步骤。

目录

前提条件

启动测试网络

创建通道

链码安装及部署

模拟参数

设置环境变量

智能合约源码安装依赖(GO语言版本)

 打包链码

安装链码 

在组织1中peer节点安装链码

在组织2中peer节点安装链码

查询组织2中peer节点安装链码的链码包ID

批准智能合约

组织1身份批准智能合约

组织2身份批准智能合约

将链码提交至通道

查询提交的链码

查询组织1提交的链码

查询组织2提交的链码


前提条件

启动测试网络

[root@localhost test-network-myself]# ./network-myself.sh up

创建通道

[root@localhost test-network-myself]# ./network-myself.sh createChannel

链码安装及部署

模拟参数

[root@localhost test-network-myself]# export CHANNEL_NAME="mychannel"
[root@localhost test-network-myself]# export CC_NAME=basic
[root@localhost test-network-myself]# export CC_SRC_PATH=../asset-transfer-basic/chaincode-go
[root@localhost test-network-myself]# export CC_SRC_LANGUAGE=go
[root@localhost test-network-myself]# export CC_VERSION="1.0"
[root@localhost test-network-myself]# export CC_SEQUENCE="1"
[root@localhost test-network-myself]# export CC_INIT_FCN=""
[root@localhost test-network-myself]# export CC_END_POLICY=""
[root@localhost test-network-myself]# export CC_COLL_CONFIG=""
[root@localhost test-network-myself]# export DELAY="3"
[root@localhost test-network-myself]# export MAX_RETRY="5"
[root@localhost test-network-myself]# export VERBOSE="false"
[root@localhost test-network-myself]# export INIT_REQUIRED=""
[root@localhost test-network-myself]# export CC_RUNTIME_LANGUAGE=golang

设置环境变量

[root@localhost test-network-myself]# export FABRIC_CFG_PATH=$PWD/../config/
[root@localhost test-network-myself]# export CORE_PEER_TLS_ENABLED=true
[root@localhost test-network-myself]# export ORDERER_CA=${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
[root@localhost test-network-myself]# export PEER0_ORG1_CA=${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt
[root@localhost test-network-myself]# export PEER0_ORG2_CA=${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt
[root@localhost test-network-myself]# export PEER0_ORG3_CA=${PWD}/organizations/peerOrganizations/org3.example.com/peers/peer0.org3.example.com/tls/ca.crt
[root@localhost test-network-myself]# export ORDERER_ADMIN_TLS_SIGN_CERT=${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/tls/server.crt
[root@localhost test-network-myself]# export ORDERER_ADMIN_TLS_PRIVATE_KEY=${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/tls/server.key

智能合约源码安装依赖(GO语言版本)

[root@localhost test-network-myself]# cd $CC_SRC_PATH
[root@localhost chaincode-go]# GO111MODULE=on go mod vendor
[root@localhost chaincode-go]# cd ../../test-network-myself

安装依赖后将出现vendor目录 ,如下图:

Fabric v2.3 手动模拟(四)- 链码开发及部署_第1张图片

 打包链码

[root@localhost test-network-myself]# peer lifecycle chaincode package ${CC_NAME}.tar.gz --path ${CC_SRC_PATH} --lang ${CC_RUNTIME_LANGUAGE} --label ${CC_NAME}_${CC_VERSION} 

 打包完成将生成打包文件,如下图:

Fabric v2.3 手动模拟(四)- 链码开发及部署_第2张图片

安装链码 

在组织1中peer节点安装链码

[root@localhost test-network-myself]# export CORE_PEER_LOCALMSPID="Org1MSP"
[root@localhost test-network-myself]# export CORE_PEER_TLS_ROOTCERT_FILE=$PEER0_ORG1_CA
[root@localhost test-network-myself]# export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org1.example.com/users/[email protected]/msp
[root@localhost test-network-myself]# export CORE_PEER_ADDRESS=localhost:7051

[root@localhost test-network-myself]# peer lifecycle chaincode install ${CC_NAME}.tar.gz
2021-11-25 15:24:22.291 CST [cli.lifecycle.chaincode] submitInstallProposal -> INFO 001 Installed remotely: response: 
2021-11-25 15:24:22.291 CST [cli.lifecycle.chaincode] submitInstallProposal -> INFO 002 Chaincode code package identifier: basic_1.0:346b35825b660445a5d3da8b8902a4b20bbf10ee50832bdd81bb811bede62cda

在组织2中peer节点安装链码

[root@localhost test-network-myself]# export CORE_PEER_LOCALMSPID="Org2MSP"
[root@localhost test-network-myself]# export CORE_PEER_TLS_ROOTCERT_FILE=$PEER0_ORG2_CA
[root@localhost test-network-myself]# export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org2.example.com/users/[email protected]/msp
[root@localhost test-network-myself]# export CORE_PEER_ADDRESS=localhost:9051

[root@localhost test-network-myself]# peer lifecycle chaincode install ${CC_NAME}.tar.gz 
2021-11-25 15:25:50.317 CST [cli.lifecycle.chaincode] submitInstallProposal -> INFO 001 Installed remotely: response: 
2021-11-25 15:25:50.317 CST [cli.lifecycle.chaincode] submitInstallProposal -> INFO 002 Chaincode code package identifier: basic_1.0:346b35825b660445a5d3da8b8902a4b20bbf10ee50832bdd81bb811bede62cda

查询组织2中peer节点安装链码的链码包ID

在组织1与组织2中安装链码的链码包ID是一样的,因此在一个组织中查看即可

[root@localhost test-network-myself]# peer lifecycle chaincode queryinstalled >&log.txt
[root@localhost test-network-myself]# export PACKAGE_ID=$(sed -n "/${CC_NAME}_${CC_VERSION}/{s/^Package ID: //; s/, Label:.*$//; p;}" log.txt)

[root@localhost test-network-myself]# echo $PACKAGE_ID
basic_1.0:346b35825b660445a5d3da8b8902a4b20bbf10ee50832bdd81bb811bede62cda

批准智能合约

组织1身份批准智能合约

[root@localhost test-network-myself]# export CORE_PEER_LOCALMSPID="Org1MSP"
[root@localhost test-network-myself]# export CORE_PEER_TLS_ROOTCERT_FILE=$PEER0_ORG1_CA
[root@localhost test-network-myself]# export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org1.example.com/users/[email protected]/msp
[root@localhost test-network-myself]# export CORE_PEER_ADDRESS=localhost:7051

[root@localhost test-network-myself]# peer lifecycle chaincode approveformyorg -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --tls --cafile "$ORDERER_CA" --channelID $CHANNEL_NAME --name ${CC_NAME} --version ${CC_VERSION} --package-id ${PACKAGE_ID} --sequence ${CC_SEQUENCE} ${INIT_REQUIRED} ${CC_END_POLICY} ${CC_COLL_CONFIG} 
2021-11-25 15:29:04.476 CST [chaincodeCmd] ClientWait -> INFO 001 txid [b0dbc929665effe24db2d3382aa17d5402ed3d369dfbf440ec14f5d7f25cdd8e] committed with status (VALID) at localhost:7051

组织2身份批准智能合约

[root@localhost test-network-myself]# export CORE_PEER_LOCALMSPID="Org2MSP"
[root@localhost test-network-myself]# export CORE_PEER_TLS_ROOTCERT_FILE=$PEER0_ORG2_CA
[root@localhost test-network-myself]# export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org2.example.com/users/[email protected]/msp
[root@localhost test-network-myself]# export CORE_PEER_ADDRESS=localhost:9051

[root@localhost test-network-myself]# peer lifecycle chaincode approveformyorg -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --tls --cafile "$ORDERER_CA" --channelID $CHANNEL_NAME --name ${CC_NAME} --version ${CC_VERSION} --package-id ${PACKAGE_ID} --sequence ${CC_SEQUENCE} ${INIT_REQUIRED} ${CC_END_POLICY} ${CC_COLL_CONFIG} 
2021-11-25 15:30:05.884 CST [chaincodeCmd] ClientWait -> INFO 001 txid [4f3642be48126e50e700aaf59c84d857fed41f99b86107e3fbdb6e9ad0c9289f] committed with status (VALID) at localhost:9051

将链码提交至通道

[root@localhost test-network-myself]# export PEER_CONN_PARMS=(--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)

[root@localhost test-network-myself]# peer lifecycle chaincode commit -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --tls --cafile "$ORDERER_CA" --channelID $CHANNEL_NAME --name ${CC_NAME} "${PEER_CONN_PARMS[@]}" --version ${CC_VERSION} --sequence ${CC_SEQUENCE} ${INIT_REQUIRED} ${CC_END_POLICY} ${CC_COLL_CONFIG} 
2021-11-25 15:33:22.619 CST [chaincodeCmd] ClientWait -> INFO 001 txid [788ae19b5b7b0cf9c7dc9006cbabbbb747cba7b5a91818f2da27be114c2795f3] committed with status (VALID) at localhost:7051
2021-11-25 15:33:22.624 CST [chaincodeCmd] ClientWait -> INFO 002 txid [788ae19b5b7b0cf9c7dc9006cbabbbb747cba7b5a91818f2da27be114c2795f3] committed with status (VALID) at localhost:9051

查询提交的链码

查询组织1提交的链码

[root@localhost test-network-myself]# export CORE_PEER_LOCALMSPID="Org1MSP"
[root@localhost test-network-myself]# export CORE_PEER_TLS_ROOTCERT_FILE=$PEER0_ORG1_CA
[root@localhost test-network-myself]# export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org1.example.com/users/[email protected]/msp
[root@localhost test-network-myself]# export CORE_PEER_ADDRESS=localhost:7051

[root@localhost test-network-myself]# peer lifecycle chaincode querycommitted --channelID $CHANNEL_NAME --name ${CC_NAME}
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]

查询组织2提交的链码

[root@localhost test-network-myself]# export CORE_PEER_LOCALMSPID="Org2MSP"
[root@localhost test-network-myself]# export CORE_PEER_TLS_ROOTCERT_FILE=$PEER0_ORG2_CA
[root@localhost test-network-myself]# export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org2.example.com/users/[email protected]/msp
[root@localhost test-network-myself]# export CORE_PEER_ADDRESS=localhost:9051
[root@localhost test-network-myself]# 
[root@localhost test-network-myself]# peer lifecycle chaincode querycommitted --channelID $CHANNEL_NAME --name ${CC_NAME}
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]

调用链码函数

当前是调用的是组织2中的peer节点

设置初始资产

[root@localhost test-network-myself]# 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 ${PEER_CONN_PARMS[@]} -c '{"function":"InitLedger","Args":[]}'
2021-11-26 21:12:58.719 CST [chaincodeCmd] chaincodeInvokeOrQuery -> INFO 001 Chaincode invoke successful. result: status:200 

查询所有资产

[root@localhost test-network-myself]# 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}]

调用的是组织1中的peer节点查询所有资产

[root@localhost test-network-myself]# export CORE_PEER_LOCALMSPID="Org1MSP"
[root@localhost test-network-myself]# export CORE_PEER_TLS_ROOTCERT_FILE=$PEER0_ORG1_CA
[root@localhost test-network-myself]# export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org1.example.com/users/[email protected]/msp
[root@localhost test-network-myself]# export CORE_PEER_ADDRESS=localhost:7051
[root@localhost test-network-myself]# 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}]

在组织1与组织2的peer节点中查询的数据是一样的。此链码中还有一些转资产,查询个人资产操作,在这里就不演示啦。

上一篇:Fabric v2.3 测试网络 - 脚本分析(三)- 创建通道 - 设置锚节点setAnchorPeer.sh解析

你可能感兴趣的:(区块链,fabric)