1. 准备
确保本地已经安装好fabric-samples以及环境(可以看我之前的文章进行操作)。接下来我们将利用案例内的test网络进行链码的安装部署。
2. 启动测试网络
进入fabric-samples。
进入test-network文件夹,然后启动测试网络
cd ./test-network
./network.sh down
./network.sh up createChannel
./network.sh down 是为了确保关闭之前可能存在的测试网络。
表示测试网络创建成功,并且设定了一个mychannel的初始通道
3. 打包链码
这里我们选择使用Go语言的链码包作为我们的测试对象。
首先找到链码
cd ../ chaincode/fabcar/go
go.mod 为fabcar.go 提供了API索引,链码的编写可以查看fabcar.go。如果想进一步了解相关内容,可以查看官方文档。
安装链码的依赖
GO111MODULE=on go mod vendor
成功后会产生一个vender文件夹。
之后,我们返回到test-network文件夹
cd ../../../test-network
检查一下peer命令是否被配置到默认环境中
peer version
如果没有,我们进入到bin文件夹中进行配置
cd ../bin
pwd
打开~/.bashrc文件设置环境变量,注意,添加在文件的末尾,并且路径要与当前的位置一致。pwd可以查看当前bin的路径
nano ~/.bashrc
保存退出后
source ~/.bashrc
查看peer。
回到test-network文件夹中
cd ../test-network
接下来设置链码的配置文件
export FABRIC_CFG_PATH=$PWD/../config/
创建链码包
peer lifecycle chaincode package fabcar.tar.gz --path ../chaincode/fabcar/go/ --lang golang --label fabcar_1
查看目录会发现打包好的fabcar.tar.gz
4.安装链码
在我们的测试网络中有如下两个组织:
peer0.org1.example.com
peer0.org2.example.com
接下来我们需要让这两个组织通过我们的安装许可
首先作为peer0.org1.example.com。
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/Admin@org1.example.com/msp export CORE_PEER_ADDRESS=localhost:7051
接下来安装链码包
peer lifecycle chaincode install fabcar.tar.gz
切换成第二个组织
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_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/Admin@org2.example.com/msp export CORE_PEER_ADDRESS=localhost:9051
安装链码
peer lifecycle chaincode install fabcar.tar.gz
5. 通过安装
接下来我们还需要同意链码的安装。
首先我们来查看等待同意的链码序列以及其id(更多相关命令)
peer lifecycle chaincode queryinstalled
设置id(需要跟你自己的链码id匹配)
export CC_PACKAGE_ID=fabcar_1:69de748301770f6ef64b42aa6bb6cb291df20aa39542c3ef94008615704007f3
通过请求(作为peer0.org2.example.com)
peer lifecycle chaincode approveformyorg -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --channelID mychannel --name fabcar --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
成功后会得到如下信息:
切换到第一个组织进行类似的操作
export CORE_PEER_LOCALMSPID="Org1MSP" export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp export CORE_PEER_TLS_ROOTCERT_FILE=${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt export CORE_PEER_ADDRESS=localhost:7051
peer lifecycle chaincode approveformyorg -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --channelID mychannel --name fabcar --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
6. 提交链码在通道上的定义
确实链码已经通过了
peer lifecycle chaincode checkcommitreadiness --channelID mychannel --name fabcar --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
进行提交
peer lifecycle chaincode commit -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --channelID mychannel --name fabcar --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 --channelID mychannel --name fabcar --cafile ${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem
7. 执行链码
进行初始化设定
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 fabcar --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":[]}'
如果成功,我们可以进行链码查询
peer chaincode query -C mychannel -n fabcar -c '{"Args":["queryAllCars"]}'
结果如下
[{"Key":"CAR0","Record":{"make":"Toyota","model":"Prius","colour":"blue","owner":"Tomoko"}}, {"Key":"CAR1","Record":{"make":"Ford","model":"Mustang","colour":"red","owner":"Brad"}}, {"Key":"CAR2","Record":{"make":"Hyundai","model":"Tucson","colour":"green","owner":"Jin Soo"}}, {"Key":"CAR3","Record":{"make":"Volkswagen","model":"Passat","colour":"yellow","owner":"Max"}}, {"Key":"CAR4","Record":{"make":"Tesla","model":"S","colour":"black","owner":"Adriana"}}, {"Key":"CAR5","Record":{"make":"Peugeot","model":"205","colour":"purple","owner":"Michel"}}, {"Key":"CAR6","Record":{"make":"Chery","model":"S22L","colour":"white","owner":"Aarav"}}, {"Key":"CAR7","Record":{"make":"Fiat","model":"Punto","colour":"violet","owner":"Pari"}}, {"Key":"CAR8","Record":{"make":"Tata","model":"Nano","colour":"indigo","owner":"Valeria"}}, {"Key":"CAR9","Record":{"make":"Holden","model":"Barina","colour":"brown","owner":"Shotaro"}}]
有疑问加站长微信联系(非本文作者)